Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: New parser for systemd_perms #3339

Merged
merged 8 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions insights/parsers/ls_systemd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"""
Systemd File Permissions parsers
================================

Parsers included in this module are:

SystemdPerms - command ``/bin/ls -lanRL /etc/systemd``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be modified too.

--------------------------------------------------------------

UsrLibSystemdPerms - command ``/bin/ls -lanRL /usr/lib/systemd``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same comment as above

---------------------------------------------------------------


"""
from insights.core import CommandParser, FileListing
from insights.core.plugins import parser
from insights.specs import Specs


@parser(Specs.ls_etc_systemd)
class LsEtcSystemdPermsParser(CommandParser, FileListing):
Copy link
Contributor

@xiangce xiangce Feb 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How above removing the part of PermsParser?

"""
Class for parsing ``ls -lanRL /etc/systemd`` command.

The ``ls -lanRL /etc/systemd`` command provides information for the listing of the ``/etc/systemd`` directory.

See the ``FileListing`` class for a more complete description of the available features of the class.

Sample output of ``ls -lanRL /etc/systemd`` command is::

/etc/systemd:
total 48
drwxr-xr-x 10 501 20 320 Feb 23 2021 .
drwxr-xr-x 129 501 20 4128 Nov 7 15:01 ..
-rw-r--r-- 1 501 20 720 Jan 16 2021 bootchart.conf
-rw-r--r-- 1 501 20 615 Jan 16 2021 coredump.conf
drwxr-xr-x 2 501 20 64 Jan 16 2021 user
-rw-r--r-- 1 501 20 1127 Jan 16 2021 user.conf

/etc/systemd/user:
total 0
drwxr-xr-x 2 501 20 64 Jan 16 2021 .
drwxr-xr-x 10 501 20 320 Feb 23 2021 ..

Examples:

>>> type(etc_systemd)
<class 'insights.parsers.ls_systemd.LsEtcSystemdPermsParser'>
>>> '/etc/systemd' in etc_systemd
True
>>> '/etc/systemd/system' in etc_systemd
False
>>> etc_systemd.files_of('/etc/systemd')
['bootchart.conf', 'coredump.conf', 'user.conf']
>>> etc_systemd.dirs_of('/etc/systemd')
['.', '..', 'user']
>>> etc_systemd.dir_contains('/etc/systemd', 'coredump.conf')
True
>>> etc_systemd_dicts = etc_systemd.listing_of('/etc/systemd')
>>> etc_systemd_dicts['coredump.conf']['perms']
'rw-r--r--'
>>> etc_systemd_dicts['user']['perms']
'rwxr-xr-x'
"""
pass


@parser(Specs.ls_usr_lib_systemd)
class LsUsrLibSystemdPermsParser(CommandParser, FileListing):
xiangce marked this conversation as resolved.
Show resolved Hide resolved
"""
Class for parsing ``ls -lanRL /usr/lib/systemd`` command.

The ``ls -lanRL /usr/lib/systemd`` command provides information for the listing of the ``/usr/lib/systemd`` directory.

See the ``FileListing`` class for a more complete description of the available features of the class.

Sample output of ``ls -lanRL /usr/lib/systemd`` command is::

/usr/lib/systemd:
total 0
drwxr-xr-x 4 501 20 128 Feb 23 2021 .
dr-xr-xr-x 8 501 20 256 Oct 13 01:34 ..
drwxr-xr-x 404 501 20 12928 Oct 13 02:50 system
drwxr-xr-x 15 501 20 480 Feb 23 2021 user

/usr/lib/systemd/user:
total 104
drwxr-xr-x 15 501 20 480 Feb 23 2021 .
drwxr-xr-x 4 501 20 128 Feb 23 2021 ..
-rw-r--r-- 1 501 20 457 Jan 16 2021 basic.target
-rw-r--r-- 1 501 20 379 Jan 16 2021 bluetooth.target
-rw-r--r-- 1 501 20 414 Jan 16 2021 default.target
-rw-r--r-- 1 501 20 499 Jan 16 2021 exit.target
-rw-r--r-- 1 501 20 147 May 31 2018 glib-pacrunner.service
-rw-r--r-- 1 501 20 354 Jan 16 2021 paths.target
-rw-r--r-- 1 501 20 377 Jan 16 2021 printer.target
-rw-r--r-- 1 501 20 402 Jan 16 2021 shutdown.target
-rw-r--r-- 1 501 20 380 Jan 16 2021 smartcard.target
-rw-r--r-- 1 501 20 356 Jan 16 2021 sockets.target
-rw-r--r-- 1 501 20 380 Jan 16 2021 sound.target
-rw-r--r-- 1 501 20 501 Jan 16 2021 systemd-exit.service
-rw-r--r-- 1 501 20 405 Jan 16 2021 timers.target

Examples:

>>> type(usr_lib_systemd)
<class 'insights.parsers.ls_systemd.LsUsrLibSystemdPermsParser'>
>>> '/usr/lib/systemd' in usr_lib_systemd
True
>>> '/usr/lib/systemd/system' in usr_lib_systemd
False
>>> usr_lib_systemd.files_of('/usr/lib/systemd/user')
['basic.target', 'bluetooth.target', 'default.target', 'exit.target', 'glib-pacrunner.service', 'paths.target', 'printer.target', 'shutdown.target', 'smartcard.target', 'sockets.target', 'sound.target', 'systemd-exit.service', 'timers.target']
>>> usr_lib_systemd.dirs_of('/usr/lib/systemd')
['.', '..', 'system', 'user']
>>> usr_lib_systemd.dir_contains('/usr/lib/systemd/user', 'basic.target')
True
>>> usr_lib_systemd_dicts = usr_lib_systemd.listing_of('/usr/lib/systemd/user')
>>> usr_lib_systemd_dicts['basic.target']['perms']
'rw-r--r--'

"""
pass
134 changes: 134 additions & 0 deletions insights/parsers/tests/test_ls_systemd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
from insights.parsers.ls_systemd import LsEtcSystemdPermsParser, LsUsrLibSystemdPermsParser
from insights.parsers import ls_systemd
from insights.tests import context_wrap
import doctest

ETC_SYSTEMD = """
/etc/systemd:
total 48
drwxr-xr-x 10 501 20 320 Feb 23 2021 .
drwxr-xr-x 129 501 20 4128 Nov 7 15:01 ..
-rw-r--r-- 1 501 20 720 Jan 16 2021 bootchart.conf
-rw-r--r-- 1 501 20 615 Jan 16 2021 coredump.conf
-rw-r--r-- 1 501 20 983 Jan 16 2021 journald.conf
-rw-r--r-- 1 501 20 957 Jan 16 2021 logind.conf
drwxr-xr-x 39 501 20 1248 Oct 14 04:49 system
-rw-r--r-- 1 501 20 1552 Jan 16 2021 system.conf
drwxr-xr-x 2 501 20 64 Jan 16 2021 user
-rw-r--r-- 1 501 20 1127 Jan 16 2021 user.conf

/etc/systemd/system:
total 104
drwxr-xr-x 39 501 20 1248 Oct 14 04:49 .
drwxr-xr-x 10 501 20 320 Feb 23 2021 ..
drwxr-xr-x 5 501 20 160 Jun 19 2018 basic.target.wants
-rw-r--r-- 1 501 20 657 Apr 16 2021 dbus-org.fedoraproject.FirewallD1.service
-rw-r--r-- 1 501 20 1445 Sep 30 2020 dbus-org.freedesktop.NetworkManager.service
-rw-r--r-- 1 501 20 353 Sep 30 2020 dbus-org.freedesktop.nm-dispatcher.service
-rw-r--r-- 1 501 20 492 Jan 16 2021 default.target
-rw-r--r-- 1 501 20 149 Oct 13 02:51 postgresql.service
-r--r--r-- 1 501 20 946 Oct 13 03:02 pulpcore-api.service
-r--r--r-- 1 501 20 157 Oct 13 03:02 pulpcore-api.socket
-r--r--r-- 1 501 20 832 Oct 14 04:49 pulpcore-content.service
-r--r--r-- 1 501 20 162 Oct 13 03:02 pulpcore-content.socket
-r--r--r-- 1 501 20 813 Oct 13 03:02 pulpcore-resource-manager.service
-r--r--r-- 1 501 20 861 Oct 13 03:02 [email protected]
-rw-r--r-- 1 501 20 164 May 13 2020 [email protected]
-rw-r--r-- 1 501 20 602 Jan 16 2020 rh-mongodb34-mongod.service

/etc/systemd/system/basic.target.wants:
total 24
drwxr-xr-x 5 501 20 160 Jun 19 2018 .
drwxr-xr-x 39 501 20 1248 Oct 14 04:49 ..
-rw-r--r-- 1 501 20 657 Apr 16 2021 firewalld.service
-rw-r--r-- 1 501 20 284 Jul 28 2021 microcode.service
-rw-r--r-- 1 501 20 217 May 22 2020 rhel-dmesg.service

/etc/systemd/user:
total 0
drwxr-xr-x 2 501 20 64 Jan 16 2021 .
drwxr-xr-x 10 501 20 320 Feb 23 2021 ..
"""

ETC_SYSTEMD_EXAMPLE = """
/etc/systemd:
total 48
drwxr-xr-x 10 501 20 320 Feb 23 2021 .
drwxr-xr-x 129 501 20 4128 Nov 7 15:01 ..
-rw-r--r-- 1 501 20 720 Jan 16 2021 bootchart.conf
-rw-r--r-- 1 501 20 615 Jan 16 2021 coredump.conf
drwxr-xr-x 2 501 20 64 Jan 16 2021 user
-rw-r--r-- 1 501 20 1127 Jan 16 2021 user.conf

/etc/systemd/user:
total 0
drwxr-xr-x 2 501 20 64 Jan 16 2021 .
drwxr-xr-x 10 501 20 320 Feb 23 2021 ..
"""

USR_LIB_SYSTEMD = """
/usr/lib/systemd:
total 0
drwxr-xr-x 4 501 20 128 Feb 23 2021 .
dr-xr-xr-x 8 501 20 256 Oct 13 01:34 ..
drwxr-xr-x 404 501 20 12928 Oct 13 02:50 system
drwxr-xr-x 15 501 20 480 Feb 23 2021 user

/usr/lib/systemd/user:
total 104
drwxr-xr-x 15 501 20 480 Feb 23 2021 .
drwxr-xr-x 4 501 20 128 Feb 23 2021 ..
-rw-r--r-- 1 501 20 457 Jan 16 2021 basic.target
-rw-r--r-- 1 501 20 379 Jan 16 2021 bluetooth.target
-rw-r--r-- 1 501 20 414 Jan 16 2021 default.target
-rw-r--r-- 1 501 20 499 Jan 16 2021 exit.target
-rw-r--r-- 1 501 20 147 May 31 2018 glib-pacrunner.service
-rw-r--r-- 1 501 20 354 Jan 16 2021 paths.target
-rw-r--r-- 1 501 20 377 Jan 16 2021 printer.target
-rw-r--r-- 1 501 20 402 Jan 16 2021 shutdown.target
-rw-r--r-- 1 501 20 380 Jan 16 2021 smartcard.target
-rw-r--r-- 1 501 20 356 Jan 16 2021 sockets.target
-rw-r--r-- 1 501 20 380 Jan 16 2021 sound.target
-rw-r--r-- 1 501 20 501 Jan 16 2021 systemd-exit.service
-rw-r--r-- 1 501 20 405 Jan 16 2021 timers.target
"""


def test_etc_systemd():
etc_systemd_perm = LsEtcSystemdPermsParser(context_wrap(ETC_SYSTEMD))
assert '/etc/systemd' in etc_systemd_perm
assert '/etc/systemd/system' in etc_systemd_perm
assert '/etc/systemd/system/basic.target.wants' in etc_systemd_perm
assert '/etc/systemd/user' in etc_systemd_perm
assert etc_systemd_perm.dirs_of('/etc/systemd') == ['.', '..', 'system', 'user']

basic_target_wants = etc_systemd_perm.files_of('/etc/systemd/system/basic.target.wants')
assert 'firewalld.service' in basic_target_wants
assert 'microcode.service' in basic_target_wants
assert 'rhel-dmesg.service' in basic_target_wants
assert len(basic_target_wants) == 3

assert etc_systemd_perm.listing_of('/etc/systemd')['user.conf'] == {'type': '-', 'perms': 'rw-r--r--', 'links': 1, 'owner': '501', 'group': '20', 'size': 1127, 'date': 'Jan 16 2021', 'name': 'user.conf', 'raw_entry': '-rw-r--r-- 1 501 20 1127 Jan 16 2021 user.conf', 'dir': '/etc/systemd'}


def test_usr_lib_systemd():
usr_lib_systemd_perm = LsUsrLibSystemdPermsParser(context_wrap(USR_LIB_SYSTEMD))
assert '/usr/lib/systemd' in usr_lib_systemd_perm
assert '/usr/lib/systemd/user' in usr_lib_systemd_perm
assert usr_lib_systemd_perm.dirs_of('/usr/lib/systemd') == ['.', '..', 'system', 'user']

usr_lib_systemd_use = usr_lib_systemd_perm.files_of('/usr/lib/systemd/user')
assert 'basic.target' in usr_lib_systemd_use
assert 'bluetooth.target' in usr_lib_systemd_use
assert len(usr_lib_systemd_use) == 13

assert usr_lib_systemd_perm.listing_of('/usr/lib/systemd/user')['timers.target'] == {'type': '-', 'perms': 'rw-r--r--', 'links': 1, 'owner': '501', 'group': '20', 'size': 405, 'date': 'Jan 16 2021', 'name': 'timers.target', 'raw_entry': '-rw-r--r-- 1 501 20 405 Jan 16 2021 timers.target', 'dir': '/usr/lib/systemd/user'}


def test_systemd_examples():
env = {
'etc_systemd': LsEtcSystemdPermsParser(context_wrap(ETC_SYSTEMD_EXAMPLE)),
'usr_lib_systemd': LsUsrLibSystemdPermsParser(context_wrap(USR_LIB_SYSTEMD))
}
failed, total = doctest.testmod(ls_systemd, globs=env)
assert failed == 0
2 changes: 2 additions & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ class Specs(SpecSet):
ls_docker_volumes = RegistryPoint()
ls_edac_mc = RegistryPoint()
ls_etc = RegistryPoint()
ls_etc_systemd = RegistryPoint()
ls_ipa_idoverride_memberof = RegistryPoint()
ls_lib_firmware = RegistryPoint()
ls_ocp_cni_openshift_sdn = RegistryPoint()
Expand All @@ -323,6 +324,7 @@ class Specs(SpecSet):
ls_sys_firmware = RegistryPoint()
ls_usr_bin = RegistryPoint(filterable=True)
ls_usr_lib64 = RegistryPoint(filterable=True)
ls_usr_lib_systemd = RegistryPoint()
ls_usr_sbin = RegistryPoint(filterable=True)
ls_var_cache_pulp = RegistryPoint()
ls_var_lib_mongodb = RegistryPoint()
Expand Down
2 changes: 2 additions & 0 deletions insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ def httpd_cmd(broker):
"/etc/pki/ovirt-vmconsole", "/etc/nova/migration", "/etc/sysconfig",
"/etc/cloud/cloud.cfg.d", "/etc/rc.d/init.d"])
ls_etc = simple_command("/bin/ls -lan {0}".format(' '.join(etc_and_sub_dirs)), keep_rc=True)
ls_etc_systemd = simple_command("/bin/ls -lanRL /etc/systemd")
xiangce marked this conversation as resolved.
Show resolved Hide resolved
ls_ipa_idoverride_memberof = simple_command("/bin/ls -lan /usr/share/ipa/ui/js/plugins/idoverride-memberof")
ls_lib_firmware = simple_command("/bin/ls -lanR /lib/firmware")
ls_ocp_cni_openshift_sdn = simple_command("/bin/ls -l /var/lib/cni/networks/openshift-sdn")
Expand All @@ -382,6 +383,7 @@ def httpd_cmd(broker):
ls_tmp = simple_command("/bin/ls -la /tmp")
ls_usr_bin = simple_command("/bin/ls -lan /usr/bin")
ls_usr_lib64 = simple_command("/bin/ls -lan /usr/lib64")
ls_usr_lib_systemd = simple_command("/bin/ls -lanRL /usr/lib/systemd")
xiangce marked this conversation as resolved.
Show resolved Hide resolved
ls_var_cache_pulp = simple_command("/bin/ls -lan /var/cache/pulp")
ls_var_lib_mongodb = simple_command("/bin/ls -la /var/lib/mongodb")
ls_var_lib_nova_instances = simple_command("/bin/ls -laRZ /var/lib/nova/instances")
Expand Down