Skip to content

Commit

Permalink
Feat: enhance combiners sudoers (#4088)
Browse files Browse the repository at this point in the history
* Feat: enhance combiners sudoers

Signed-off-by: Xinting Li <xintli@Xintings-MacBook-Pro.local>
  • Loading branch information
TZ3070 committed Apr 25, 2024
1 parent 7069995 commit d8d8ab5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions insights/combiners/sudoers.py
Expand Up @@ -19,6 +19,9 @@ class Sudoers(SudoersBase):
``/etc/sudoers.d/*`` files. The order of lines keeps their original
order them in files. And the files are read with `"filename`"
alphabetical order.
data(dict): The dict of RAW lines of ``/etc/sudoers`` and each file under
``/etc/sudoers.d/``, The keys are the filename and values are RAW lines
of each file.
.. note::
1. If there is not `"#includedir /etc/sudoers.d"` line in the entry
Expand All @@ -41,8 +44,10 @@ class Sudoers(SudoersBase):
"""
def __init__(self, sudoers):
self.lines = []
self.data = {}
first = False
for sdr in sorted(sudoers, key=lambda x: x.file_path):
self.data[sdr.file_path] = sdr.lines
self.lines.extend(sdr.lines)
if not first:
first = True
Expand Down
4 changes: 4 additions & 0 deletions insights/tests/combiners/test_sudoers.py
Expand Up @@ -47,6 +47,9 @@ def test_sudoers():
assert wheel[1] == '%wheel ALL=(ALL) ABC'
assert sudo.last("Defaults") == 'Defaults:wheel !requiretty'

assert sudo.data['/etc/sudoers'] == ['%wheel ALL=(ALL) ALL', '#includedir /etc/sudoers.d']
assert sudo.data['/etc/sudoers.d/test'] == ['%wheel ALL=(ALL) ABC', 'Defaults:wheel !requiretty']

with pytest.raises(TypeError):
sudo.get({})

Expand All @@ -56,6 +59,7 @@ def test_sudoers_no_includedir():
sudo2 = EtcSudoers(context_wrap(SUDOERS_FM, path=SUDOERS_PATH2))
sudo3 = EtcSudoers(context_wrap(SUDOERS_WH, path=SUDOERS_PATH3))
sudo = Sudoers([sudo1, sudo2, sudo3])
assert sudo.data['/etc/sudoers'] == ['%wheel ALL=(ALL) ALL']
assert len(sudo.lines) == 1


Expand Down

0 comments on commit d8d8ab5

Please sign in to comment.