From f6cc50d9d71c6fbcade821348ee6ddff101029c3 Mon Sep 17 00:00:00 2001 From: xintli <986222045@qq.com> Date: Thu, 25 Aug 2022 14:47:32 +0800 Subject: [PATCH] Feat: New parser for 'ls -lanL /etc/ssh' command (#3499) * Feat: New parser for 'ls -lanL /etc/ssh' command New parser for 'ls -lanL /etc/ssh' command Signed-off-by: Xinting Li --- insights/parsers/ls_etc.py | 164 ++++++++++++++++---------- insights/specs/__init__.py | 1 + insights/specs/default.py | 1 + insights/specs/insights_archive.py | 1 + insights/tests/parsers/test_ls_etc.py | 28 ++++- 5 files changed, 132 insertions(+), 63 deletions(-) diff --git a/insights/parsers/ls_etc.py b/insights/parsers/ls_etc.py index cf987b9ef8..b34389eff7 100644 --- a/insights/parsers/ls_etc.py +++ b/insights/parsers/ls_etc.py @@ -1,67 +1,14 @@ """ +Ls Etc Directory +================ + +Parsers provided in this module includes: + LsEtc - command ``ls -lan /etc `` -================================================ - -The ``ls -lan /etc `` command provides information for -the listing of the ``/etc`` directory and specified sub-directories. -See ``FileListing`` class for additional information. - -Sample ``ls -lan /etc/sysconfig /etc/rc.d/rc3.d`` output:: - - /etc/sysconfig: - total 96 - drwxr-xr-x. 7 0 0 4096 Jul 6 23:41 . - drwxr-xr-x. 77 0 0 8192 Jul 13 03:55 .. - drwxr-xr-x. 2 0 0 41 Jul 6 23:32 cbq - drwxr-xr-x. 2 0 0 6 Sep 16 2015 console - -rw-------. 1 0 0 1390 Mar 4 2014 ebtables-config - -rw-r--r--. 1 0 0 72 Sep 15 2015 firewalld - lrwxrwxrwx. 1 0 0 17 Jul 6 23:32 grub -> /etc/default/grub - - /etc/rc.d/rc3.d: - total 4 - drwxr-xr-x. 2 0 0 58 Jul 6 23:32 . - drwxr-xr-x. 10 0 0 4096 Sep 16 2015 .. - lrwxrwxrwx. 1 0 0 20 Jul 6 23:32 K50netconsole -> ../init.d/netconsole - lrwxrwxrwx. 1 0 0 17 Jul 6 23:32 S10network -> ../init.d/network - lrwxrwxrwx. 1 0 0 15 Jul 6 23:32 S97rhnsd -> ../init.d/rhnsd - -Examples: - - >>> "sysconfig" in ls_etc - False - >>> "/etc/sysconfig" in ls_etc - True - >>> len(ls_etc.files_of('/etc/sysconfig')) - 3 - >>> ls_etc.files_of("/etc/sysconfig") - ['ebtables-config', 'firewalld', 'grub'] - >>> ls_etc.dirs_of("/etc/sysconfig") - ['.', '..', 'cbq', 'console'] - >>> ls_etc.specials_of("/etc/sysconfig") - [] - >>> ls_etc.total_of("/etc/sysconfig") - 96 - >>> ls_etc.dir_entry('/etc/sysconfig', 'grub') == {'group': '0', 'name': 'grub', 'links': 1, 'perms': 'rwxrwxrwx.', 'raw_entry': 'lrwxrwxrwx. 1 0 0 17 Jul 6 23:32 grub -> /etc/default/grub', 'owner': '0', 'link': '/etc/default/grub', 'date': 'Jul 6 23:32', 'type': 'l', 'dir': '/etc/sysconfig', 'size': 17} - True - >>> ls_etc.files_of('/etc/rc.d/rc3.d') - ['K50netconsole', 'S10network', 'S97rhnsd'] - >>> sorted(ls_etc.listing_of("/etc/sysconfig").keys()) == sorted(['console', 'grub', '..', 'firewalld', '.', 'cbq', 'ebtables-config']) - True - >>> sorted(ls_etc.listing_of("/etc/sysconfig")['console'].keys()) == sorted(['group', 'name', 'links', 'perms', 'raw_entry', 'owner', 'date', 'type', 'dir', 'size']) - True - >>> ls_etc.listing_of("/etc/sysconfig")['console']['type'] - 'd' - >>> ls_etc.listing_of("/etc/sysconfig")['console']['perms'] - 'rwxr-xr-x.' - >>> ls_etc.dir_contains("/etc/sysconfig", "console") - True - >>> ls_etc.dir_entry("/etc/sysconfig", "console") == {'group': '0', 'name': 'console', 'links': 2, 'perms': 'rwxr-xr-x.', 'raw_entry': 'drwxr-xr-x. 2 0 0 6 Sep 16 2015 console', 'owner': '0', 'date': 'Sep 16 2015', 'type': 'd', 'dir': '/etc/sysconfig', 'size': 6} - True - >>> ls_etc.dir_entry("/etc/sysconfig", "grub")['type'] - 'l' - >>> ls_etc.dir_entry("/etc/sysconfig", "grub")['link'] - '/etc/default/grub' +------------------------------------------------ + +LsEtcSsh - command ``/bin/ls -lanL /etc/ssh`` +--------------------------------------------- """ from .. import parser, CommandParser from .. import FileListing @@ -72,5 +19,98 @@ class LsEtc(CommandParser, FileListing): """ Parses output of ``ls -lan /etc `` command. + + The ``ls -lan /etc `` command provides information for + the listing of the ``/etc`` directory and specified sub-directories. + See ``FileListing`` class for additional information. + + Sample ``ls -lan /etc/sysconfig /etc/rc.d/rc3.d`` output:: + + /etc/sysconfig: + total 96 + drwxr-xr-x. 7 0 0 4096 Jul 6 23:41 . + drwxr-xr-x. 77 0 0 8192 Jul 13 03:55 .. + drwxr-xr-x. 2 0 0 41 Jul 6 23:32 cbq + drwxr-xr-x. 2 0 0 6 Sep 16 2015 console + -rw-------. 1 0 0 1390 Mar 4 2014 ebtables-config + -rw-r--r--. 1 0 0 72 Sep 15 2015 firewalld + lrwxrwxrwx. 1 0 0 17 Jul 6 23:32 grub -> /etc/default/grub + + /etc/rc.d/rc3.d: + total 4 + drwxr-xr-x. 2 0 0 58 Jul 6 23:32 . + drwxr-xr-x. 10 0 0 4096 Sep 16 2015 .. + lrwxrwxrwx. 1 0 0 20 Jul 6 23:32 K50netconsole -> ../init.d/netconsole + lrwxrwxrwx. 1 0 0 17 Jul 6 23:32 S10network -> ../init.d/network + lrwxrwxrwx. 1 0 0 15 Jul 6 23:32 S97rhnsd -> ../init.d/rhnsd + + Examples: + + >>> "sysconfig" in ls_etc + False + >>> "/etc/sysconfig" in ls_etc + True + >>> len(ls_etc.files_of('/etc/sysconfig')) + 3 + >>> ls_etc.files_of("/etc/sysconfig") + ['ebtables-config', 'firewalld', 'grub'] + >>> ls_etc.dirs_of("/etc/sysconfig") + ['.', '..', 'cbq', 'console'] + >>> ls_etc.specials_of("/etc/sysconfig") + [] + >>> ls_etc.total_of("/etc/sysconfig") + 96 + >>> ls_etc.dir_entry('/etc/sysconfig', 'grub') == {'group': '0', 'name': 'grub', 'links': 1, 'perms': 'rwxrwxrwx.', 'raw_entry': 'lrwxrwxrwx. 1 0 0 17 Jul 6 23:32 grub -> /etc/default/grub', 'owner': '0', 'link': '/etc/default/grub', 'date': 'Jul 6 23:32', 'type': 'l', 'dir': '/etc/sysconfig', 'size': 17} + True + >>> ls_etc.files_of('/etc/rc.d/rc3.d') + ['K50netconsole', 'S10network', 'S97rhnsd'] + >>> sorted(ls_etc.listing_of("/etc/sysconfig").keys()) == sorted(['console', 'grub', '..', 'firewalld', '.', 'cbq', 'ebtables-config']) + True + >>> sorted(ls_etc.listing_of("/etc/sysconfig")['console'].keys()) == sorted(['group', 'name', 'links', 'perms', 'raw_entry', 'owner', 'date', 'type', 'dir', 'size']) + True + >>> ls_etc.listing_of("/etc/sysconfig")['console']['type'] + 'd' + >>> ls_etc.listing_of("/etc/sysconfig")['console']['perms'] + 'rwxr-xr-x.' + >>> ls_etc.dir_contains("/etc/sysconfig", "console") + True + >>> ls_etc.dir_entry("/etc/sysconfig", "console") == {'group': '0', 'name': 'console', 'links': 2, 'perms': 'rwxr-xr-x.', 'raw_entry': 'drwxr-xr-x. 2 0 0 6 Sep 16 2015 console', 'owner': '0', 'date': 'Sep 16 2015', 'type': 'd', 'dir': '/etc/sysconfig', 'size': 6} + True + >>> ls_etc.dir_entry("/etc/sysconfig", "grub")['type'] + 'l' + >>> ls_etc.dir_entry("/etc/sysconfig", "grub")['link'] + '/etc/default/grub' + """ + pass + + +@parser(Specs.ls_etc_ssh) +class LsEtcSsh(CommandParser, FileListing): + """ + Parses output of ``ls -lanL /etc/ssh`` command. + + Sample ``ls -lanL /etc/ssh`` output:: + + total 612 + drwxr-xr-x. 3 0 0 245 Aug 11 14:19 . + drwxr-xr-x. 138 0 0 8192 Jul 29 19:11 .. + -rw-r--r--. 1 0 0 577388 Mar 27 2020 moduli + -rw-r--r--. 1 0 0 1770 Mar 27 2020 ssh_config + drwxr-xr-x. 2 0 0 28 May 12 17:10 ssh_config.d + -rw-r-----. 1 0 994 480 May 13 09:58 ssh_host_ecdsa_key + -rw-r--r--. 1 0 0 162 May 13 09:58 ssh_host_ecdsa_key.pub + -rw-r-----. 1 0 994 387 May 13 09:58 ssh_host_ed25519_key + -rw-r--r--. 1 0 0 82 May 13 09:58 ssh_host_ed25519_key.pub + -rw-r-----. 1 0 994 2578 May 13 09:58 ssh_host_rsa_key + -rw-r--r--. 1 0 0 554 May 13 09:58 ssh_host_rsa_key.pub + -rw-------. 1 0 0 4260 Aug 11 14:19 sshd_config + + Examples: + >>> type(ls_etc_ssh) + + >>> ls_etc_ssh.files_of("/etc/ssh") + ['moduli', 'ssh_config', 'ssh_host_ecdsa_key', 'ssh_host_ecdsa_key.pub', 'ssh_host_ed25519_key', 'ssh_host_ed25519_key.pub', 'ssh_host_rsa_key', 'ssh_host_rsa_key.pub', 'sshd_config'] + >>> ls_etc_ssh.dir_entry('/etc/ssh', 'ssh_host_rsa_key')['perms'] + 'rw-r-----.' """ pass diff --git a/insights/specs/__init__.py b/insights/specs/__init__.py index 3a6263869c..1a947f625a 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -324,6 +324,7 @@ class Specs(SpecSet): ls_docker_volumes = RegistryPoint() ls_edac_mc = RegistryPoint() ls_etc = RegistryPoint() + ls_etc_ssh = RegistryPoint() ls_ipa_idoverride_memberof = RegistryPoint() ls_lib_firmware = RegistryPoint() ls_ocp_cni_openshift_sdn = RegistryPoint() diff --git a/insights/specs/default.py b/insights/specs/default.py index 89cc51cf2f..88fd3f3341 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -326,6 +326,7 @@ class DefaultSpecs(Specs): "/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_ssh = simple_command("/bin/ls -lanL /etc/ssh") 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") diff --git a/insights/specs/insights_archive.py b/insights/specs/insights_archive.py index 785e14a58c..b0bf131545 100644 --- a/insights/specs/insights_archive.py +++ b/insights/specs/insights_archive.py @@ -119,6 +119,7 @@ class InsightsArchiveSpecs(Specs): ls_disk = simple_file("insights_commands/ls_-lanR_.dev.disk") ls_edac_mc = simple_file("insights_commands/ls_-lan_.sys.devices.system.edac.mc") ls_etc = simple_file("insights_commands/ls_-lan_.etc_.etc.cloud.cloud.cfg.d_.etc.nova.migration_.etc.pki.ovirt-vmconsole_.etc.pki.tls.certs_.etc.pki.tls.private_.etc.rc.d.init.d_.etc.sysconfig") + ls_etc_ssh = simple_file("insights_commands/ls_-lanL_.etc.ssh") ls_ipa_idoverride_memberof = simple_file("insights_commands/ls_-lan_.usr.share.ipa.ui.js.plugins.idoverride-memberof") ls_lib_firmware = simple_file("insights_commands/ls_-lanR_.lib.firmware") ls_ocp_cni_openshift_sdn = simple_file("insights_commands/ls_-l_.var.lib.cni.networks.openshift-sdn") diff --git a/insights/tests/parsers/test_ls_etc.py b/insights/tests/parsers/test_ls_etc.py index 0dd8854b35..cfd1f771c8 100644 --- a/insights/tests/parsers/test_ls_etc.py +++ b/insights/tests/parsers/test_ls_etc.py @@ -201,6 +201,22 @@ lrwxrwxrwx. 1 0 0 15 Jul 6 23:32 S97rhnsd -> ../init.d/rhnsd """ +LS_ETC_SSH = """ +total 612 +drwxr-xr-x. 3 0 0 245 Aug 11 14:19 . +drwxr-xr-x. 138 0 0 8192 Jul 29 19:11 .. +-rw-r--r--. 1 0 0 577388 Mar 27 2020 moduli +-rw-r--r--. 1 0 0 1770 Mar 27 2020 ssh_config +drwxr-xr-x. 2 0 0 28 May 12 17:10 ssh_config.d +-rw-r-----. 1 0 994 480 May 13 09:58 ssh_host_ecdsa_key +-rw-r--r--. 1 0 0 162 May 13 09:58 ssh_host_ecdsa_key.pub +-rw-r-----. 1 0 994 387 May 13 09:58 ssh_host_ed25519_key +-rw-r--r--. 1 0 0 82 May 13 09:58 ssh_host_ed25519_key.pub +-rw-r-----. 1 0 994 2578 May 13 09:58 ssh_host_rsa_key +-rw-r--r--. 1 0 0 554 May 13 09:58 ssh_host_rsa_key.pub +-rw-------. 1 0 0 4260 Aug 11 14:19 sshd_config +""" + def test_ls_etc(): list_etc = ls_etc.LsEtc(context_wrap(LS_ETC)) @@ -234,6 +250,16 @@ def test_ls_etc(): def test_ls_etc_documentation(): failed_count, tests = doctest.testmod( ls_etc, - globs={'ls_etc': ls_etc.LsEtc(context_wrap(LS_ETC))} + globs={'ls_etc': ls_etc.LsEtc(context_wrap(LS_ETC)), + 'ls_etc_ssh': ls_etc.LsEtcSsh(context_wrap(LS_ETC_SSH, path='ls_-lanL_.etc.ssh'))} ) assert failed_count == 0 + + +def test_ls_etc_ssh(): + ls_etc_ssh = ls_etc.LsEtcSsh(context_wrap(LS_ETC_SSH, path='ls_-lanL_.etc.ssh')) + assert '/etc/ssh' in ls_etc_ssh + assert len(ls_etc_ssh.files_of("/etc/ssh")) == 9 + assert ls_etc_ssh.files_of("/etc/ssh") == ['moduli', 'ssh_config', 'ssh_host_ecdsa_key', 'ssh_host_ecdsa_key.pub', 'ssh_host_ed25519_key', 'ssh_host_ed25519_key.pub', 'ssh_host_rsa_key', 'ssh_host_rsa_key.pub', 'sshd_config'] + assert ls_etc_ssh.dirs_of("/etc/ssh") == ['.', '..', 'ssh_config.d'] + assert ls_etc_ssh.listing_of("/etc/ssh")['ssh_host_rsa_key'] == {'group': '994', 'name': 'ssh_host_rsa_key', 'links': 1, 'perms': 'rw-r-----.', 'raw_entry': '-rw-r-----. 1 0 994 2578 May 13 09:58 ssh_host_rsa_key', 'owner': '0', 'date': 'May 13 09:58', 'type': '-', 'dir': '/etc/ssh', 'size': 2578}