mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 21:32:49 +00:00
become mixin is no more (#54002)
* become mixin is no more since sudo/su keywords are removed in 2.9 .. no need to keep this code around * also don't need test for code that is removed * made preprocess_data on base noop its not used by anything anymore, but kept for backwards compat since other methods of same name are used
This commit is contained in:
@@ -367,12 +367,6 @@ class BaseSubClass(base.Base):
|
||||
_test_attr_method_missing = FieldAttribute(isa='string', default='some attr with a missing getter',
|
||||
always_post_validate=True)
|
||||
|
||||
def _preprocess_data_basesubclass(self, ds):
|
||||
return ds
|
||||
|
||||
def preprocess_data(self, ds):
|
||||
return super(BaseSubClass, self).preprocess_data(ds)
|
||||
|
||||
def _get_attr_test_attr_method(self):
|
||||
return 'foo bar'
|
||||
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# (c) 2018 Matt Martz <matt@sivel.net>
|
||||
# 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
|
||||
|
||||
import re
|
||||
|
||||
from ansible.errors import AnsibleParserError
|
||||
from ansible.playbook.become import Become
|
||||
from ansible.module_utils._text import to_native
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class InString(str):
|
||||
def __eq__(self, other):
|
||||
return self in other
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ds", [
|
||||
{},
|
||||
{'become': True},
|
||||
{'become_user': 'root'},
|
||||
{'sudo': True},
|
||||
{'sudo_user': 'root'},
|
||||
{'su': True},
|
||||
{'su_user': 'root'}
|
||||
])
|
||||
def test_detect_privilege_escalation_conflict_valid(ds):
|
||||
become = Become()
|
||||
become._detect_privilege_escalation_conflict(ds)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ds,message", [
|
||||
({'become': True, 'sudo': True}, re.compile('"become".*"sudo"')),
|
||||
({'become': True, 'su': True}, re.compile('"become".*"su"')),
|
||||
({'sudo': True, 'su': True}, re.compile('"sudo".*"su"')),
|
||||
({'become_user': 'root', 'sudo': True}, re.compile('"become".*"sudo"')),
|
||||
({'sudo_user': 'root', 'su': True}, re.compile('"sudo".*"su"')),
|
||||
])
|
||||
def test_detect_privilege_escalation_conflict_invalid(ds, message):
|
||||
become = Become()
|
||||
with pytest.raises(AnsibleParserError) as excinfo:
|
||||
become._detect_privilege_escalation_conflict(ds)
|
||||
assert message.search(excinfo.value.message) is not None
|
||||
|
||||
|
||||
def test_preprocess_data_become(mocker):
|
||||
display_mock = mocker.patch('ansible.playbook.become.display')
|
||||
|
||||
become = Become()
|
||||
ds = {}
|
||||
assert become._preprocess_data_become(ds) == {}
|
||||
|
||||
display_mock.reset_mock()
|
||||
ds = {'sudo': True}
|
||||
out = become._preprocess_data_become(ds)
|
||||
assert 'sudo' not in out
|
||||
assert out.get('become_method') == 'sudo'
|
||||
display_mock.deprecated.assert_called_once_with(
|
||||
"Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default)",
|
||||
'2.9'
|
||||
)
|
||||
|
||||
ds = {'sudo_user': 'root'}
|
||||
out = become._preprocess_data_become(ds)
|
||||
assert 'sudo_user' not in out
|
||||
assert out.get('become_user') == 'root'
|
||||
|
||||
ds = {'sudo': True, 'sudo_user': 'root'}
|
||||
out = become._preprocess_data_become(ds)
|
||||
assert 'sudo' not in out
|
||||
assert 'sudo_user' not in out
|
||||
assert out.get('become_method') == 'sudo'
|
||||
assert out.get('become_user') == 'root'
|
||||
|
||||
display_mock.reset_mock()
|
||||
ds = {'su': True}
|
||||
out = become._preprocess_data_become(ds)
|
||||
assert 'su' not in out
|
||||
assert out.get('become_method') == 'su'
|
||||
display_mock.deprecated.assert_called_once_with(
|
||||
"Instead of su/su_user, use become/become_user and set become_method to 'su' (default is sudo)",
|
||||
'2.9'
|
||||
)
|
||||
display_mock.reset_mock()
|
||||
|
||||
ds = {'su_user': 'root'}
|
||||
out = become._preprocess_data_become(ds)
|
||||
assert 'su_user' not in out
|
||||
assert out.get('become_user') == 'root'
|
||||
|
||||
ds = {'su': True, 'su_user': 'root'}
|
||||
out = become._preprocess_data_become(ds)
|
||||
assert 'su' not in out
|
||||
assert 'su_user' not in out
|
||||
assert out.get('become_method') == 'su'
|
||||
assert out.get('become_user') == 'root'
|
||||
@@ -140,6 +140,7 @@ def test_play_context_make_become_cmd(mocker, parser, reset_cli_args):
|
||||
default_exe, success, default_cmd), cmd) is not None)
|
||||
|
||||
play_context.become_pass = None
|
||||
play_context.become_method = 'su'
|
||||
play_context.set_become_plugin(become_loader.get('su'))
|
||||
play_context.become_flags = su_flags
|
||||
cmd = play_context.make_become_cmd(cmd=default_cmd, executable="/bin/bash")
|
||||
@@ -147,33 +148,39 @@ def test_play_context_make_become_cmd(mocker, parser, reset_cli_args):
|
||||
success, default_cmd), cmd) is not None)
|
||||
|
||||
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="/bin/bash")
|
||||
assert re.match("""%s %s -u %s 'echo %s; %s'""" % (pbrun_exe, pbrun_flags, play_context.become_user,
|
||||
success, default_cmd), cmd) is not None
|
||||
|
||||
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="/bin/bash")
|
||||
assert re.match('''%s %s "'echo %s; %s'"''' % (pfexec_exe, pfexec_flags, success, default_cmd), cmd) is not None
|
||||
|
||||
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="/bin/bash")
|
||||
assert (re.match("""%s %s -u %s %s -c 'echo %s; %s'""" % (doas_exe, doas_flags, play_context.become_user, default_exe, success,
|
||||
default_cmd), cmd) is not None)
|
||||
|
||||
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="/bin/bash")
|
||||
assert (re.match("""%s %s %s -e %s -c 'echo %s; %s'""" % (ksu_exe, play_context.become_user, ksu_flags,
|
||||
default_exe, success, default_cmd), cmd) is not None)
|
||||
|
||||
play_context.set_become_plugin(become_loader.get('bad'))
|
||||
play_context.become_method = 'bad'
|
||||
with pytest.raises(AnsibleError):
|
||||
play_context.make_become_cmd(cmd=default_cmd, executable="/bin/bash")
|
||||
|
||||
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="/bin/bash")
|
||||
assert re.match("""%s %s -u %s %s -c 'echo %s; %s'""" % (dzdo_exe, dzdo_flags, play_context.become_user, default_exe,
|
||||
|
||||
Reference in New Issue
Block a user