From 08f10d57588c79ab6b5c8deec45cc3323043b9fe Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Wed, 29 Jul 2020 20:27:16 +0200 Subject: [PATCH] Fix more become plugins (#708) * Fix more become plugins. * Don't re-use var. * Other way around. --- .../704-doas-set-correct-default-values.yml | 2 +- .../708-set-correct-default-values.yml | 3 +++ plugins/become/dzdo.py | 5 ++--- plugins/become/ksu.py | 1 - plugins/become/pbrun.py | 5 ++--- tests/unit/plugins/become/test_doas.py | 21 ++++++++++++++++++ tests/unit/plugins/become/test_dzdo.py | 21 ++++++++++++++++++ tests/unit/plugins/become/test_ksu.py | 22 +++++++++++++++++++ tests/unit/plugins/become/test_pbrun.py | 21 ++++++++++++++++++ tests/unit/plugins/become/test_pfexec.py | 20 +++++++++++++++++ 10 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 changelogs/fragments/708-set-correct-default-values.yml diff --git a/changelogs/fragments/704-doas-set-correct-default-values.yml b/changelogs/fragments/704-doas-set-correct-default-values.yml index db1b4d3c4a..911a1146ef 100644 --- a/changelogs/fragments/704-doas-set-correct-default-values.yml +++ b/changelogs/fragments/704-doas-set-correct-default-values.yml @@ -1,2 +1,2 @@ bugfixes: -- doas become plugin - address a bug with the parameters handling that was breaking the plugin in community.general when `become_flags` and `become_user` were not explicitly specified (https://github.com/ansible-collections/community.general/pull/704). +- doas become plugin - address a bug with the parameters handling that was breaking the plugin in community.general when ``become_flags`` and ``become_user`` were not explicitly specified (https://github.com/ansible-collections/community.general/pull/704). diff --git a/changelogs/fragments/708-set-correct-default-values.yml b/changelogs/fragments/708-set-correct-default-values.yml new file mode 100644 index 0000000000..27630da19e --- /dev/null +++ b/changelogs/fragments/708-set-correct-default-values.yml @@ -0,0 +1,3 @@ +bugfixes: +- dzdo become plugin - address a bug with the parameters handling that was breaking the plugin in community.general when ``become_user`` was not explicitly specified (https://github.com/ansible-collections/community.general/pull/708). +- pbrun become plugin - address a bug with the parameters handling that was breaking the plugin in community.general when ``become_user`` was not explicitly specified (https://github.com/ansible-collections/community.general/pull/708). diff --git a/plugins/become/dzdo.py b/plugins/become/dzdo.py index 5badde818e..a0ff4c0538 100644 --- a/plugins/become/dzdo.py +++ b/plugins/become/dzdo.py @@ -89,8 +89,7 @@ class BecomeModule(BecomeBase): 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') - if user: - user = '-u %s' % (user) + become_user = self.get_option('become_user') + user = '-u %s' % (become_user) if become_user else '' return ' '.join([becomecmd, flags, user, self._build_success_command(cmd, shell)]) diff --git a/plugins/become/ksu.py b/plugins/become/ksu.py index dc8d9bb52f..d81b7a119d 100644 --- a/plugins/become/ksu.py +++ b/plugins/become/ksu.py @@ -13,7 +13,6 @@ DOCUMENTATION = ''' options: become_user: description: User you 'become' to execute the task - default: '' ini: - section: privilege_escalation key: become_user diff --git a/plugins/become/pbrun.py b/plugins/become/pbrun.py index 34673fabc0..9d64ff6a2d 100644 --- a/plugins/become/pbrun.py +++ b/plugins/become/pbrun.py @@ -97,9 +97,8 @@ class BecomeModule(BecomeBase): 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) + become_user = self.get_option('become_user') + user = '-u %s' % (become_user) if become_user else '' noexe = not self.get_option('wrap_exe') return ' '.join([become_exe, flags, user, self._build_success_command(cmd, shell, noexe=noexe)]) diff --git a/tests/unit/plugins/become/test_doas.py b/tests/unit/plugins/become/test_doas.py index 1c55a73350..0474b3b615 100644 --- a/tests/unit/plugins/become/test_doas.py +++ b/tests/unit/plugins/become/test_doas.py @@ -14,6 +14,27 @@ from ansible import context from .helper import call_become_plugin +def test_doas_basic(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-.+?' + + task = { + 'become_method': 'community.general.doas', + } + var_options = {} + cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe) + print(cmd) + assert (re.match("""%s %s %s -c 'echo %s; %s'""" % (doas_exe, doas_flags, default_exe, success, + default_cmd), cmd) is not None) + + def test_doas(mocker, parser, reset_cli_args): options = parser.parse_args([]) context._init_global_context(options) diff --git a/tests/unit/plugins/become/test_dzdo.py b/tests/unit/plugins/become/test_dzdo.py index ae6715af02..eb5932cd10 100644 --- a/tests/unit/plugins/become/test_dzdo.py +++ b/tests/unit/plugins/become/test_dzdo.py @@ -14,6 +14,27 @@ from ansible import context from .helper import call_become_plugin +def test_dzdo_basic(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 = '-H -S -n' + + success = 'BECOME-SUCCESS-.+?' + + task = { + 'become_method': 'community.general.dzdo', + } + var_options = {} + cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe) + print(cmd) + assert re.match("""%s %s %s -c 'echo %s; %s'""" % (dzdo_exe, dzdo_flags, default_exe, + success, default_cmd), cmd) is not None + + def test_dzdo(mocker, parser, reset_cli_args): options = parser.parse_args([]) context._init_global_context(options) diff --git a/tests/unit/plugins/become/test_ksu.py b/tests/unit/plugins/become/test_ksu.py index 696e6e150e..87c337064a 100644 --- a/tests/unit/plugins/become/test_ksu.py +++ b/tests/unit/plugins/become/test_ksu.py @@ -14,6 +14,28 @@ from ansible import context from .helper import call_become_plugin +def test_ksu_basic(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-.+?' + + task = { + 'become_user': 'foo', + 'become_method': 'community.general.ksu', + } + 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(mocker, parser, reset_cli_args): options = parser.parse_args([]) context._init_global_context(options) diff --git a/tests/unit/plugins/become/test_pbrun.py b/tests/unit/plugins/become/test_pbrun.py index e3653e3f69..b8e369d274 100644 --- a/tests/unit/plugins/become/test_pbrun.py +++ b/tests/unit/plugins/become/test_pbrun.py @@ -14,6 +14,27 @@ from ansible import context from .helper import call_become_plugin +def test_pbrun_basic(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-.+?' + + task = { + 'become_method': 'community.general.pbrun', + } + 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'""" % (pbrun_exe, pbrun_flags, + success, default_cmd), cmd) is not None + + def test_pbrun(mocker, parser, reset_cli_args): options = parser.parse_args([]) context._init_global_context(options) diff --git a/tests/unit/plugins/become/test_pfexec.py b/tests/unit/plugins/become/test_pfexec.py index 4cbbf636d4..3adc2ed51d 100644 --- a/tests/unit/plugins/become/test_pfexec.py +++ b/tests/unit/plugins/become/test_pfexec.py @@ -14,6 +14,26 @@ from ansible import context from .helper import call_become_plugin +def test_pfexec_basic(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 = '-H -S -n' + + success = 'BECOME-SUCCESS-.+?' + + task = { + 'become_method': 'community.general.pfexec', + } + 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(mocker, parser, reset_cli_args): options = parser.parse_args([]) context._init_global_context(options)