1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Fix become plugins (#50)

* Fix become plugins.

* Fix become unit tests to avoid play_context.make_become_cmd.

* Remove hack.

* Remove explicit defaults. Adjust tests to be more like Ansible itself.

* Forgot two lines.

* Rewrite tests (again).

* Rename play_context -> task, add possibility to pass var_options.

* Add var_options variants.

* Properly test overwriting.
This commit is contained in:
Felix Fontein 2020-03-30 19:09:45 +02:00 committed by GitHub
parent 7d8ca8bdbb
commit 0026c9f5b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 238 additions and 84 deletions

View file

@ -111,13 +111,13 @@ class BecomeModule(BecomeBase):
self.prompt = True
become_exe = self.get_option('become_exe') or self.name
become_exe = self.get_option('become_exe')
flags = self.get_option('become_flags') or ''
flags = self.get_option('become_flags')
if not self.get_option('become_pass') and '-n' not in flags:
flags += ' -n'
user = self.get_option('become_user') or ''
user = self.get_option('become_user')
if user:
user = '-u %s' % (user)

View file

@ -82,14 +82,14 @@ class BecomeModule(BecomeBase):
if not cmd:
return cmd
becomecmd = self.get_option('become_exe') or self.name
becomecmd = self.get_option('become_exe')
flags = self.get_option('become_flags') or ''
flags = self.get_option('become_flags')
if self.get_option('become_pass'):
self.prompt = '[dzdo via ansible, key=%s] password:' % self._id
flags = '%s -p "%s"' % (flags.replace('-n', ''), self.prompt)
user = self.get_option('become_user') or ''
user = self.get_option('become_user')
if user:
user = '-u %s' % (user)

View file

@ -13,6 +13,7 @@ DOCUMENTATION = '''
options:
become_user:
description: User you 'become' to execute the task
default: ''
ini:
- section: privilege_escalation
key: become_user
@ -113,7 +114,8 @@ class BecomeModule(BecomeBase):
if not cmd:
return cmd
exe = self.get_option('become_exe') or self.name
flags = self.get_option('become_flags') or ''
user = self.get_option('become_user') or ''
exe = self.get_option('become_exe')
flags = self.get_option('become_flags')
user = self.get_option('become_user')
return '%s %s %s -e %s ' % (exe, user, flags, self._build_success_command(cmd, shell))

View file

@ -13,6 +13,7 @@ DOCUMENTATION = '''
options:
become_user:
description: User you 'become' to execute the task
default: ''
ini:
- section: privilege_escalation
key: become_user
@ -80,7 +81,8 @@ class BecomeModule(BecomeBase):
if not cmd:
return cmd
become = self.get_option('become_exe') or self.name
flags = self.get_option('become_flags') or ''
user = self.get_option('become_user') or ''
become = self.get_option('become_exe')
flags = self.get_option('become_flags')
user = self.get_option('become_user')
return '%s -q shell %s %s@ %s' % (become, flags, user, cmd)

View file

@ -41,6 +41,7 @@ DOCUMENTATION = '''
- name: ANSIBLE_PBRUN_EXE
become_flags:
description: Options to pass to pbrun
default: ''
ini:
- section: privilege_escalation
key: become_flags
@ -93,9 +94,10 @@ class BecomeModule(BecomeBase):
if not cmd:
return cmd
become_exe = self.get_option('become_exe') or self.name
flags = self.get_option('become_flags') or ''
user = self.get_option('become_user') or ''
become_exe = self.get_option('become_exe')
flags = self.get_option('become_flags')
user = self.get_option('become_user')
if user:
user = '-u %s' % (user)
noexe = not self.get_option('wrap_exe')

View file

@ -97,7 +97,8 @@ class BecomeModule(BecomeBase):
if not cmd:
return cmd
exe = self.get_option('become_exe') or self.name
exe = self.get_option('become_exe')
flags = self.get_option('become_flags')
noexe = not self.get_option('wrap_exe')
return '%s %s "%s"' % (exe, flags, self._build_success_command(cmd, shell, noexe=noexe))

View file

@ -27,6 +27,7 @@ DOCUMENTATION = '''
- name: ANSIBLE_PMRUN_EXE
become_flags:
description: Options to pass to pmrun
default: ''
ini:
- section: privilege_escalation
key: become_flags
@ -70,6 +71,7 @@ class BecomeModule(BecomeBase):
if not cmd:
return cmd
become = self.get_option('become_exe') or self.name
flags = self.get_option('become_flags') or ''
become = self.get_option('become_exe')
flags = self.get_option('become_flags')
return '%s %s %s' % (become, flags, shlex_quote(self._build_success_command(cmd, shell)))

View file

@ -13,6 +13,7 @@ DOCUMENTATION = '''
options:
become_user:
description: User you 'become' to execute the task
default: ''
ini:
- section: privilege_escalation
key: become_user
@ -83,7 +84,8 @@ class BecomeModule(BecomeBase):
if not cmd:
return cmd
become = self.get_option('become_exe') or self.name
flags = self.get_option('become_flags') or ''
user = self.get_option('become_user') or ''
become = self.get_option('become_exe')
flags = self.get_option('become_flags')
user = self.get_option('become_user')
return '%s %s %s -c %s' % (become, flags, user, self._build_success_command(cmd, shell))

View file

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.errors import AnsibleError
from ansible.plugins.loader import become_loader, get_shell_plugin
def call_become_plugin(task, var_options, cmd, executable=None):
"""Helper function to call become plugin simiarly on how Ansible itself handles this."""
plugin = become_loader.get(task['become_method'])
plugin.set_options(task_keys=task, var_options=var_options)
shell = get_shell_plugin(executable=executable)
return plugin.build_become_command(cmd, shell)

View file

@ -10,30 +10,54 @@ __metaclass__ = type
import re
from ansible import context
from ansible.playbook.play_context import PlayContext
from ansible.plugins.loader import become_loader
from .helper import call_become_plugin
def test_doas(mocker, parser, reset_cli_args):
options = parser.parse_args([])
context._init_global_context(options)
play_context = PlayContext()
default_cmd = "/bin/foo"
default_exe = "/bin/bash"
doas_exe = 'doas'
doas_flags = '-n'
cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
assert cmd == default_cmd
success = 'BECOME-SUCCESS-.+?'
task = {
'become_user': 'foo',
'become_method': 'community.general.doas',
'become_flags': doas_flags,
}
var_options = {}
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
print(cmd)
assert (re.match("""%s %s -u %s %s -c 'echo %s; %s'""" % (doas_exe, doas_flags, task['become_user'], default_exe, success,
default_cmd), cmd) is not None)
def test_doas_varoptions(mocker, parser, reset_cli_args):
options = parser.parse_args([])
context._init_global_context(options)
default_cmd = "/bin/foo"
default_exe = "/bin/bash"
doas_exe = 'doas'
doas_flags = '-n'
success = 'BECOME-SUCCESS-.+?'
play_context.become = True
play_context.become_user = 'foo'
play_context.set_become_plugin(become_loader.get('doas'))
play_context.become_method = 'doas'
play_context.become_flags = doas_flags
cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
assert (re.match("""%s %s -u %s %s -c 'echo %s; %s'""" % (doas_exe, doas_flags, play_context.become_user, default_exe, success,
task = {
'become_user': 'foo',
'become_method': 'community.general.doas',
'become_flags': 'xxx',
}
var_options = {
'ansible_become_user': 'bar',
'ansible_become_flags': doas_flags,
}
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
print(cmd)
assert (re.match("""%s %s -u %s %s -c 'echo %s; %s'""" % (doas_exe, doas_flags, var_options['ansible_become_user'], default_exe, success,
default_cmd), cmd) is not None)

View file

@ -10,35 +10,64 @@ __metaclass__ = type
import re
from ansible import context
from ansible.playbook.play_context import PlayContext
from ansible.plugins.loader import become_loader
from .helper import call_become_plugin
def test_dzdo(mocker, parser, reset_cli_args):
options = parser.parse_args([])
context._init_global_context(options)
play_context = PlayContext()
default_cmd = "/bin/foo"
default_exe = "/bin/bash"
dzdo_exe = 'dzdo'
dzdo_flags = ''
cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
assert cmd == default_cmd
success = 'BECOME-SUCCESS-.+?'
task = {
'become_user': 'foo',
'become_method': 'community.general.dzdo',
'become_flags': dzdo_flags,
}
var_options = {}
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
print(cmd)
assert re.match("""%s %s -u %s %s -c 'echo %s; %s'""" % (dzdo_exe, dzdo_flags, task['become_user'], default_exe,
success, default_cmd), cmd) is not None
task['become_pass'] = 'testpass'
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
print(cmd)
assert re.match("""%s %s -p %s -u %s %s -c 'echo %s; %s'""" % (dzdo_exe, dzdo_flags, r'\"\[dzdo via ansible, key=.+?\] password:\"',
task['become_user'], default_exe, success, default_cmd), cmd) is not None
def test_dzdo_varoptions(mocker, parser, reset_cli_args):
options = parser.parse_args([])
context._init_global_context(options)
default_cmd = "/bin/foo"
default_exe = "/bin/bash"
dzdo_exe = 'dzdo'
dzdo_flags = ''
success = 'BECOME-SUCCESS-.+?'
play_context.become = True
play_context.become_user = 'foo'
play_context.set_become_plugin(become_loader.get('dzdo'))
play_context.become_method = 'dzdo'
play_context.become_flags = dzdo_flags
cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
assert re.match("""%s %s -u %s %s -c 'echo %s; %s'""" % (dzdo_exe, dzdo_flags, play_context.become_user, default_exe,
task = {
'become_user': 'foo',
'become_method': 'community.general.dzdo',
'become_flags': 'xxx',
}
var_options = {
'ansible_become_user': 'bar',
'ansible_become_flags': dzdo_flags,
}
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
print(cmd)
assert re.match("""%s %s -u %s %s -c 'echo %s; %s'""" % (dzdo_exe, dzdo_flags, var_options['ansible_become_user'], default_exe,
success, default_cmd), cmd) is not None
play_context.become_pass = 'testpass'
play_context.set_become_plugin(become_loader.get('dzdo'))
cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
var_options['ansible_become_pass'] = 'testpass'
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
print(cmd)
assert re.match("""%s %s -p %s -u %s %s -c 'echo %s; %s'""" % (dzdo_exe, dzdo_flags, r'\"\[dzdo via ansible, key=.+?\] password:\"',
play_context.become_user, default_exe, success, default_cmd), cmd) is not None
var_options['ansible_become_user'], default_exe, success, default_cmd), cmd) is not None

View file

@ -10,30 +10,54 @@ __metaclass__ = type
import re
from ansible import context
from ansible.playbook.play_context import PlayContext
from ansible.plugins.loader import become_loader
from .helper import call_become_plugin
def test_ksu(mocker, parser, reset_cli_args):
options = parser.parse_args([])
context._init_global_context(options)
play_context = PlayContext()
default_cmd = "/bin/foo"
default_exe = "/bin/bash"
ksu_exe = 'ksu'
ksu_flags = ''
cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
assert cmd == default_cmd
success = 'BECOME-SUCCESS-.+?'
task = {
'become_user': 'foo',
'become_method': 'community.general.ksu',
'become_flags': ksu_flags,
}
var_options = {}
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
print(cmd)
assert (re.match("""%s %s %s -e %s -c 'echo %s; %s'""" % (ksu_exe, task['become_user'], ksu_flags,
default_exe, success, default_cmd), cmd) is not None)
def test_ksu_varoptions(mocker, parser, reset_cli_args):
options = parser.parse_args([])
context._init_global_context(options)
default_cmd = "/bin/foo"
default_exe = "/bin/bash"
ksu_exe = 'ksu'
ksu_flags = ''
success = 'BECOME-SUCCESS-.+?'
play_context.become = True
play_context.become_user = 'foo'
play_context.set_become_plugin(become_loader.get('ksu'))
play_context.become_method = 'ksu'
play_context.become_flags = ksu_flags
cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
assert (re.match("""%s %s %s -e %s -c 'echo %s; %s'""" % (ksu_exe, play_context.become_user, ksu_flags,
task = {
'become_user': 'foo',
'become_method': 'community.general.ksu',
'become_flags': 'xxx',
}
var_options = {
'ansible_become_user': 'bar',
'ansible_become_flags': ksu_flags,
}
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
print(cmd)
assert (re.match("""%s %s %s -e %s -c 'echo %s; %s'""" % (ksu_exe, var_options['ansible_become_user'], ksu_flags,
default_exe, success, default_cmd), cmd) is not None)

View file

@ -10,30 +10,54 @@ __metaclass__ = type
import re
from ansible import context
from ansible.playbook.play_context import PlayContext
from ansible.plugins.loader import become_loader
from .helper import call_become_plugin
def test_pbrun(mocker, parser, reset_cli_args):
options = parser.parse_args([])
context._init_global_context(options)
play_context = PlayContext()
default_cmd = "/bin/foo"
default_exe = "/bin/bash"
pbrun_exe = 'pbrun'
pbrun_flags = ''
cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
assert cmd == default_cmd
success = 'BECOME-SUCCESS-.+?'
task = {
'become_user': 'foo',
'become_method': 'community.general.pbrun',
'become_flags': pbrun_flags,
}
var_options = {}
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
print(cmd)
assert re.match("""%s %s -u %s 'echo %s; %s'""" % (pbrun_exe, pbrun_flags, task['become_user'],
success, default_cmd), cmd) is not None
def test_pbrun_var_varoptions(mocker, parser, reset_cli_args):
options = parser.parse_args([])
context._init_global_context(options)
default_cmd = "/bin/foo"
default_exe = "/bin/bash"
pbrun_exe = 'pbrun'
pbrun_flags = ''
success = 'BECOME-SUCCESS-.+?'
play_context.become = True
play_context.become_user = 'foo'
play_context.set_become_plugin(become_loader.get('pbrun'))
play_context.become_method = 'pbrun'
play_context.become_flags = pbrun_flags
cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
assert re.match("""%s %s -u %s 'echo %s; %s'""" % (pbrun_exe, pbrun_flags, play_context.become_user,
task = {
'become_user': 'foo',
'become_method': 'community.general.pbrun',
'become_flags': 'xxx',
}
var_options = {
'ansible_become_user': 'bar',
'ansible_become_flags': pbrun_flags,
}
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
print(cmd)
assert re.match("""%s %s -u %s 'echo %s; %s'""" % (pbrun_exe, pbrun_flags, var_options['ansible_become_user'],
success, default_cmd), cmd) is not None

View file

@ -10,29 +10,52 @@ __metaclass__ = type
import re
from ansible import context
from ansible.playbook.play_context import PlayContext
from ansible.plugins.loader import become_loader
from .helper import call_become_plugin
def test_pfexec(mocker, parser, reset_cli_args):
options = parser.parse_args([])
context._init_global_context(options)
play_context = PlayContext()
default_cmd = "/bin/foo"
default_exe = "/bin/bash"
pfexec_exe = 'pfexec'
pfexec_flags = ''
cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
assert cmd == default_cmd
success = 'BECOME-SUCCESS-.+?'
task = {
'become_user': 'foo',
'become_method': 'community.general.pfexec',
'become_flags': pfexec_flags,
}
var_options = {}
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
print(cmd)
assert re.match('''%s %s "'echo %s; %s'"''' % (pfexec_exe, pfexec_flags, success, default_cmd), cmd) is not None
def test_pfexec_varoptions(mocker, parser, reset_cli_args):
options = parser.parse_args([])
context._init_global_context(options)
default_cmd = "/bin/foo"
default_exe = "/bin/bash"
pfexec_exe = 'pfexec'
pfexec_flags = ''
success = 'BECOME-SUCCESS-.+?'
play_context.become = True
play_context.become_user = 'foo'
play_context.set_become_plugin(become_loader.get('pfexec'))
play_context.become_method = 'pfexec'
play_context.become_flags = pfexec_flags
cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
task = {
'become_user': 'foo',
'become_method': 'community.general.pfexec',
'become_flags': 'xxx',
}
var_options = {
'ansible_become_user': 'bar',
'ansible_become_flags': pfexec_flags,
}
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
print(cmd)
assert re.match('''%s %s "'echo %s; %s'"''' % (pfexec_exe, pfexec_flags, success, default_cmd), cmd) is not None