Skip to content

Commit

Permalink
Feat: New parser for 'ls -lanL /etc/ssh' command (#3499)
Browse files Browse the repository at this point in the history
* Feat: New parser for 'ls -lanL /etc/ssh' command

New parser for 'ls -lanL /etc/ssh' command

Signed-off-by: Xinting Li <[email protected]>
  • Loading branch information
TZ3070 authored Aug 25, 2022
1 parent b0076c0 commit f6cc50d
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 63 deletions.
164 changes: 102 additions & 62 deletions insights/parsers/ls_etc.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,14 @@
"""
Ls Etc Directory
================
Parsers provided in this module includes:
LsEtc - command ``ls -lan /etc </etc/sub-dirs>``
================================================
The ``ls -lan /etc </etc/sub-directories>`` 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
Expand All @@ -72,5 +19,98 @@
class LsEtc(CommandParser, FileListing):
"""
Parses output of ``ls -lan /etc </etc/sub-directories>`` command.
The ``ls -lan /etc </etc/sub-directories>`` 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)
<class 'insights.parsers.ls_etc.LsEtcSsh'>
>>> 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
1 change: 1 addition & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions insights/specs/insights_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
28 changes: 27 additions & 1 deletion insights/tests/parsers/test_ls_etc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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}

0 comments on commit f6cc50d

Please sign in to comment.