diff --git a/plugins/become/doas.py b/plugins/become/doas.py index b93c5d2c47..08b6361b4d 100644 --- a/plugins/become/doas.py +++ b/plugins/become/doas.py @@ -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) diff --git a/plugins/become/dzdo.py b/plugins/become/dzdo.py index 6b60b6d865..5badde818e 100644 --- a/plugins/become/dzdo.py +++ b/plugins/become/dzdo.py @@ -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) diff --git a/plugins/become/ksu.py b/plugins/become/ksu.py index c741b61ba1..dc8d9bb52f 100644 --- a/plugins/become/ksu.py +++ b/plugins/become/ksu.py @@ -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)) diff --git a/plugins/become/machinectl.py b/plugins/become/machinectl.py index 6515badf4b..6751f9b46b 100644 --- a/plugins/become/machinectl.py +++ b/plugins/become/machinectl.py @@ -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) diff --git a/plugins/become/pbrun.py b/plugins/become/pbrun.py index 1ac4a9b6c4..34673fabc0 100644 --- a/plugins/become/pbrun.py +++ b/plugins/become/pbrun.py @@ -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') diff --git a/plugins/become/pfexec.py b/plugins/become/pfexec.py index 994bbaa17d..d20e860601 100644 --- a/plugins/become/pfexec.py +++ b/plugins/become/pfexec.py @@ -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)) diff --git a/plugins/become/pmrun.py b/plugins/become/pmrun.py index 8d5ddfd001..52fc336029 100644 --- a/plugins/become/pmrun.py +++ b/plugins/become/pmrun.py @@ -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))) diff --git a/plugins/become/sesu.py b/plugins/become/sesu.py index 5773b74b23..8b56f025f4 100644 --- a/plugins/become/sesu.py +++ b/plugins/become/sesu.py @@ -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)) diff --git a/tests/unit/plugins/become/helper.py b/tests/unit/plugins/become/helper.py new file mode 100644 index 0000000000..69e3ea6cd0 --- /dev/null +++ b/tests/unit/plugins/become/helper.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- + +# (c) 2012-2014, Michael DeHaan +# +# 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) diff --git a/tests/unit/plugins/become/test_doas.py b/tests/unit/plugins/become/test_doas.py index 00b1b1b7fb..1c55a73350 100644 --- a/tests/unit/plugins/become/test_doas.py +++ b/tests/unit/plugins/become/test_doas.py @@ -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) diff --git a/tests/unit/plugins/become/test_dzdo.py b/tests/unit/plugins/become/test_dzdo.py index 2110a7dcbe..ae6715af02 100644 --- a/tests/unit/plugins/become/test_dzdo.py +++ b/tests/unit/plugins/become/test_dzdo.py @@ -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 diff --git a/tests/unit/plugins/become/test_ksu.py b/tests/unit/plugins/become/test_ksu.py index 7906fe1dda..696e6e150e 100644 --- a/tests/unit/plugins/become/test_ksu.py +++ b/tests/unit/plugins/become/test_ksu.py @@ -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) diff --git a/tests/unit/plugins/become/test_pbrun.py b/tests/unit/plugins/become/test_pbrun.py index a008b8ae75..e3653e3f69 100644 --- a/tests/unit/plugins/become/test_pbrun.py +++ b/tests/unit/plugins/become/test_pbrun.py @@ -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 diff --git a/tests/unit/plugins/become/test_pfexec.py b/tests/unit/plugins/become/test_pfexec.py index e1c54e78b5..4cbbf636d4 100644 --- a/tests/unit/plugins/become/test_pfexec.py +++ b/tests/unit/plugins/become/test_pfexec.py @@ -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