| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 3 |
# (c) 2016 Toshio Kuratomi <[email protected]> |
| 4 |
# (c) 2017 Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import errno |
| 10 |
import pytest |
| 11 |
|
| 12 |
from unittest.mock import mock_open, patch |
| 13 |
|
| 14 |
from ansible.module_utils import basic |
| 15 |
import builtins |
| 16 |
|
| 17 |
from ansible.module_utils.testing import patch_module_args |
| 18 |
|
| 19 |
|
| 20 |
def no_args_module(selinux_enabled=None, selinux_mls_enabled=None): |
| 21 |
am = basic.AnsibleModule(argument_spec={}) |
| 22 |
# just dirty-patch the wrappers on the object instance since it's short-lived |
| 23 |
if isinstance(selinux_enabled, bool): |
| 24 |
patch.object(am, 'selinux_enabled', return_value=selinux_enabled).start() |
| 25 |
if isinstance(selinux_mls_enabled, bool): |
| 26 |
patch.object(am, 'selinux_mls_enabled', return_value=selinux_mls_enabled).start() |
| 27 |
return am |
| 28 |
|
| 29 |
|
| 30 |
# test AnsibleModule selinux wrapper methods |
| 31 |
@pytest.mark.usefixtures('stdin') |
| 32 |
class TestSELinuxMU: |
| 33 |
def test_selinux_enabled(self): |
| 34 |
# test selinux unavailable |
| 35 |
# selinux unavailable, should return false |
| 36 |
with patch.object(basic, 'HAVE_SELINUX', False): |
| 37 |
assert no_args_module().selinux_enabled() is False |
| 38 |
|
| 39 |
# test selinux present/not-enabled |
| 40 |
disabled_mod = no_args_module() |
| 41 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 42 |
selinux.is_selinux_enabled.return_value = 0 |
| 43 |
assert disabled_mod.selinux_enabled() is False |
| 44 |
|
| 45 |
# ensure value is cached (same answer after unpatching) |
| 46 |
assert disabled_mod.selinux_enabled() is False |
| 47 |
|
| 48 |
# and present / enabled |
| 49 |
with patch.object(basic, 'HAVE_SELINUX', True): |
| 50 |
enabled_mod = no_args_module() |
| 51 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 52 |
selinux.is_selinux_enabled.return_value = 1 |
| 53 |
assert enabled_mod.selinux_enabled() is True |
| 54 |
# ensure value is cached (same answer after unpatching) |
| 55 |
assert enabled_mod.selinux_enabled() is True |
| 56 |
|
| 57 |
def test_selinux_mls_enabled(self): |
| 58 |
# selinux unavailable, should return false |
| 59 |
with patch.object(basic, 'HAVE_SELINUX', False): |
| 60 |
assert no_args_module().selinux_mls_enabled() is False |
| 61 |
# selinux disabled, should return false |
| 62 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 63 |
selinux.is_selinux_mls_enabled.return_value = 0 |
| 64 |
assert no_args_module(selinux_enabled=False).selinux_mls_enabled() is False |
| 65 |
|
| 66 |
with patch.object(basic, 'HAVE_SELINUX', True): |
| 67 |
# selinux enabled, should pass through the value of is_selinux_mls_enabled |
| 68 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 69 |
selinux.is_selinux_mls_enabled.return_value = 1 |
| 70 |
assert no_args_module(selinux_enabled=True).selinux_mls_enabled() is True |
| 71 |
|
| 72 |
def test_selinux_initial_context(self): |
| 73 |
# selinux missing/disabled/enabled sans MLS is 3-element None |
| 74 |
assert no_args_module(selinux_enabled=False, selinux_mls_enabled=False).selinux_initial_context() == [None, None, None] |
| 75 |
assert no_args_module(selinux_enabled=True, selinux_mls_enabled=False).selinux_initial_context() == [None, None, None] |
| 76 |
# selinux enabled with MLS is 4-element None |
| 77 |
assert no_args_module(selinux_enabled=True, selinux_mls_enabled=True).selinux_initial_context() == [None, None, None, None] |
| 78 |
|
| 79 |
def test_selinux_default_context(self): |
| 80 |
# selinux unavailable |
| 81 |
with patch.object(basic, 'HAVE_SELINUX', False): |
| 82 |
assert no_args_module().selinux_default_context(path='/foo/bar') == [None, None, None] |
| 83 |
|
| 84 |
am = no_args_module(selinux_enabled=True, selinux_mls_enabled=True) |
| 85 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 86 |
# matchpathcon success |
| 87 |
selinux.matchpathcon.return_value = [0, 'unconfined_u:object_r:default_t:s0'] |
| 88 |
assert am.selinux_default_context(path='/foo/bar') == ['unconfined_u', 'object_r', 'default_t', 's0'] |
| 89 |
|
| 90 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 91 |
# matchpathcon fail (return initial context value) |
| 92 |
selinux.matchpathcon.return_value = [-1, ''] |
| 93 |
assert am.selinux_default_context(path='/foo/bar') == [None, None, None, None] |
| 94 |
|
| 95 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 96 |
# matchpathcon OSError |
| 97 |
selinux.matchpathcon.side_effect = OSError |
| 98 |
assert am.selinux_default_context(path='/foo/bar') == [None, None, None, None] |
| 99 |
|
| 100 |
def test_selinux_context(self): |
| 101 |
# selinux unavailable |
| 102 |
with patch.object(basic, 'HAVE_SELINUX', False): |
| 103 |
assert no_args_module().selinux_context(path='/foo/bar') == [None, None, None] |
| 104 |
|
| 105 |
am = no_args_module(selinux_enabled=True, selinux_mls_enabled=True) |
| 106 |
# lgetfilecon_raw passthru |
| 107 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 108 |
selinux.lgetfilecon_raw.return_value = [0, 'unconfined_u:object_r:default_t:s0'] |
| 109 |
assert am.selinux_context(path='/foo/bar') == ['unconfined_u', 'object_r', 'default_t', 's0'] |
| 110 |
|
| 111 |
# lgetfilecon_raw returned a failure |
| 112 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 113 |
selinux.lgetfilecon_raw.return_value = [-1, ''] |
| 114 |
assert am.selinux_context(path='/foo/bar') == [None, None, None, None] |
| 115 |
|
| 116 |
# lgetfilecon_raw OSError (should bomb the module) |
| 117 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 118 |
selinux.lgetfilecon_raw.side_effect = OSError(errno.ENOENT, 'NotFound') |
| 119 |
with pytest.raises(SystemExit): |
| 120 |
am.selinux_context(path='/foo/bar') |
| 121 |
|
| 122 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 123 |
selinux.lgetfilecon_raw.side_effect = OSError() |
| 124 |
with pytest.raises(SystemExit): |
| 125 |
am.selinux_context(path='/foo/bar') |
| 126 |
|
| 127 |
def test_is_special_selinux_path(self): |
| 128 |
args = dict( |
| 129 |
_ansible_selinux_special_fs="nfs,nfsd,foos", |
| 130 |
_ansible_remote_tmp="/tmp", |
| 131 |
_ansible_keep_remote_files=False, |
| 132 |
) |
| 133 |
|
| 134 |
with patch_module_args(args): |
| 135 |
am = basic.AnsibleModule( |
| 136 |
argument_spec=dict(), |
| 137 |
) |
| 138 |
|
| 139 |
def _mock_find_mount_point(path): |
| 140 |
if path.startswith('/some/path'): |
| 141 |
return '/some/path' |
| 142 |
elif path.startswith('/weird/random/fstype'): |
| 143 |
return '/weird/random/fstype' |
| 144 |
return '/' |
| 145 |
|
| 146 |
am.find_mount_point = _mock_find_mount_point |
| 147 |
am.selinux_context = lambda path: ['foo_u', 'foo_r', 'foo_t', 's0'] |
| 148 |
|
| 149 |
m = mock_open() |
| 150 |
m.side_effect = OSError |
| 151 |
|
| 152 |
with patch.object(builtins, 'open', m, create=True): |
| 153 |
assert am.is_special_selinux_path('/some/path/that/should/be/nfs') == (False, None) |
| 154 |
|
| 155 |
mount_data = [ |
| 156 |
'/dev/disk1 / ext4 rw,seclabel,relatime,data=ordered 0 0\n', |
| 157 |
'10.1.1.1:/path/to/nfs /some/path nfs ro 0 0\n', |
| 158 |
'whatever /weird/random/fstype foos rw 0 0\n', |
| 159 |
] |
| 160 |
|
| 161 |
# mock_open has a broken readlines() implementation apparently... |
| 162 |
# this should work by default but doesn't, so we fix it |
| 163 |
m = mock_open(read_data=''.join(mount_data)) |
| 164 |
m.return_value.readlines.return_value = mount_data |
| 165 |
|
| 166 |
with patch.object(builtins, 'open', m, create=True): |
| 167 |
assert am.is_special_selinux_path('/some/random/path') == (False, None) |
| 168 |
assert am.is_special_selinux_path('/some/path/that/should/be/nfs') == (True, ['foo_u', 'foo_r', 'foo_t', 's0']) |
| 169 |
assert am.is_special_selinux_path('/weird/random/fstype/path') == (True, ['foo_u', 'foo_r', 'foo_t', 's0']) |
| 170 |
|
| 171 |
def test_set_context_if_different(self): |
| 172 |
am = no_args_module(selinux_enabled=False) |
| 173 |
assert am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], True) is True |
| 174 |
assert am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False) is False |
| 175 |
|
| 176 |
am = no_args_module(selinux_enabled=True, selinux_mls_enabled=True) |
| 177 |
am.selinux_context = lambda path: ['bar_u', 'bar_r', None, None] |
| 178 |
am.is_special_selinux_path = lambda path: (False, None) |
| 179 |
|
| 180 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 181 |
selinux.lsetfilecon.return_value = 0 |
| 182 |
assert am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False) is True |
| 183 |
selinux.lsetfilecon.assert_called_with('/path/to/file', 'foo_u:foo_r:foo_t:s0') |
| 184 |
selinux.lsetfilecon.reset_mock() |
| 185 |
am.check_mode = True |
| 186 |
assert am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False) is True |
| 187 |
assert not selinux.lsetfilecon.called |
| 188 |
am.check_mode = False |
| 189 |
|
| 190 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 191 |
selinux.lsetfilecon.return_value = 1 |
| 192 |
with pytest.raises(SystemExit): |
| 193 |
am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], True) |
| 194 |
|
| 195 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 196 |
selinux.lsetfilecon.side_effect = OSError |
| 197 |
with pytest.raises(SystemExit): |
| 198 |
am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], True) |
| 199 |
|
| 200 |
am.is_special_selinux_path = lambda path: (True, ['sp_u', 'sp_r', 'sp_t', 's0']) |
| 201 |
|
| 202 |
with patch.object(basic, 'selinux', create=True) as selinux: |
| 203 |
selinux.lsetfilecon.return_value = 0 |
| 204 |
assert am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False) is True |
| 205 |
selinux.lsetfilecon.assert_called_with('/path/to/file', 'sp_u:sp_r:sp_t:s0') |