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 ProcKeys for '/proc/keys' file #3417

Merged
merged 6 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions docs/shared_parsers_catalog/proc_keys.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. automodule:: insights.parsers.proc_keys
:members:
:show-inheritance:
91 changes: 91 additions & 0 deletions insights/parsers/proc_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""
ProcKeys - File ``/proc/keys``
==============================

This parser reads the content of ``/proc/keys``.

"""

from insights import Parser, parser
from insights.parsers import SkipException, get_active_lines
from insights.specs import Specs


@parser(Specs.proc_keys)
class ProcKeys(Parser):
xiangce marked this conversation as resolved.
Show resolved Hide resolved
"""
Class ``ProcKeys`` parses the content of the ``/proc/keys``.

Attributes:
ID (string): The ID of the key, expressed in hexadecimal.
xiangce marked this conversation as resolved.
Show resolved Hide resolved
Flags (string): A set of flags describing the state of the key
Usage (string): The count of the number of kernel credential structures
that are pinning the key.
Timeout (string): The amount of time until the key will expire (weeks, days,
hours, minutes, and seconds). The string perm here means
that the key is permanent (no timeout). The string expd
means that the key has already expired, but has not yet
been garbage collected.
Permissions (string): The key permissions, expressed as four hexadecimal bytes
containing, from left to right, the possessor, user, group,
and other permissions.
UID (string): The user ID of the key owner.
GID (string): The group ID of the key.
Type (string) : The key type.
Description (string) The key description (name). For most key types, it has the
form name[: extra-info]. (The name subfield is the key's
description (name). The optional extra-info field provides
some further information about the key.)

Sample content of '/proc/keys' file looks like::

009a2028 I--Q--- 1 perm 3f010000 1000 1000 user krb_ccache:primary: 12
1806c4ba I--Q--- 1 perm 3f010000 1000 1000 keyring _pid: 2
25d3a08f I--Q--- 1 perm 1f3f0000 1000 65534 keyring _uid_ses.1000: 1
28576bd8 I--Q--- 3 perm 3f010000 1000 1000 keyring _krb: 1
2c546d21 I--Q--- 190 perm 3f030000 1000 1000 keyring _ses: 2
30a4e0be I------ 4 2d 1f030000 1000 65534 keyring _persistent.1000: 1
32100fab I--Q--- 4 perm 1f3f0000 1000 65534 keyring _uid.1000: 2
32a387ea I--Q--- 1 perm 3f010000 1000 1000 keyring _pid: 2
3ce56aea I--Q--- 5 perm 3f030000 1000 1000 keyring _ses: 1


Examples:
>>> type(proc_keys)
<class 'insights.parsers.proc_keys.ProcKeys'>
>>> proc_keys.data[0]['id']
'009a2028'
>>> proc_keys.data[0]['flags']
'I--Q---'
>>> proc_keys.data[0]['usage']
'1'
>>> proc_keys.data[0]['timeout']
'perm'
>>> proc_keys.data[0]['permissions']
'3f010000'
>>> proc_keys.data[0]['uid']
'1000'
>>> proc_keys.data[0]['gid']
'1000'
>>> proc_keys.data[0]['type']
'user'
>>> proc_keys.data[0]['description']
'krb_ccache:primary: 12'
"""

def parse_content(self, content):
content = get_active_lines(content, comment_char="COMMAND>")
xiangce marked this conversation as resolved.
Show resolved Hide resolved

if not content:
raise SkipException("No Contents")

self.data = []
xiangce marked this conversation as resolved.
Show resolved Hide resolved
column = ['id', 'flags', 'usage', 'timeout', 'permissions', 'uid', 'gid', 'type', 'description']

for line in content:
line = line.split(None, 8)
row = [obj for obj in line]
if column and row and len(column) == len(row):
self.data.append(dict(zip(column, row)))
else:
raise SkipException("Invalid Contents")
xiangce marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ class Specs(SpecSet):
postgresql_conf = RegistryPoint()
postgresql_log = RegistryPoint(multi_output=True, filterable=True)
prev_uploader_log = RegistryPoint()
proc_keys = RegistryPoint()
proc_netstat = RegistryPoint()
proc_slabinfo = RegistryPoint()
proc_snmp_ipv4 = 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 @@ -106,6 +106,7 @@ class DefaultSpecs(Specs):
candlepin_broker = candlepin_broker.candlepin_broker
candlepin_log = simple_file("/var/log/candlepin/candlepin.log")
cgroups = simple_file("/proc/cgroups")
proc_keys = simple_file("/proc/keys")
ps_alxwww = simple_command("/bin/ps alxwww")
ps_aux = simple_command("/bin/ps aux")
ps_auxcww = simple_command("/bin/ps auxcww")
Expand Down
37 changes: 37 additions & 0 deletions insights/tests/parsers/test_proc_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from insights.parsers.proc_keys import ProcKeys
from insights.parsers import proc_keys
from insights.tests import context_wrap
import doctest

PROC_KEYS = """
009a2028 I--Q--- 1 perm 3f010000 1000 1000 user krb_ccache:primary: 12
1806c4ba I--Q--- 1 perm 3f010000 1000 1000 keyring _pid: 2
25d3a08f I--Q--- 1 perm 1f3f0000 1000 65534 keyring _uid_ses.1000: 1
28576bd8 I--Q--- 3 perm 3f010000 1000 1000 keyring _krb: 1
2c546d21 I--Q--- 190 perm 3f030000 1000 1000 keyring _ses: 2
30a4e0be I------ 4 2d 1f030000 1000 65534 keyring _persistent.1000: 1
32100fab I--Q--- 4 perm 1f3f0000 1000 65534 keyring _uid.1000: 2
32a387ea I--Q--- 1 perm 3f010000 1000 1000 keyring _pid: 2
3ce56aea I--Q--- 5 perm 3f030000 1000 1000 keyring _ses: 1
""".strip()


def test_etc_systemd():
proc_keys_content = ProcKeys(context_wrap(PROC_KEYS))
assert proc_keys_content.data[1]['id'] == '1806c4ba'
assert proc_keys_content.data[1]['flags'] == 'I--Q---'
assert proc_keys_content.data[1]['usage'] == '1'
assert proc_keys_content.data[1]['timeout'] == 'perm'
assert proc_keys_content.data[2]['permissions'] == '1f3f0000'
assert proc_keys_content.data[2]['uid'] == '1000'
assert proc_keys_content.data[2]['gid'] == '65534'
assert proc_keys_content.data[2]['type'] == 'keyring'
assert proc_keys_content.data[2]['description'] == '_uid_ses.1000: 1'


def test_systemd_examples():
env = {
'proc_keys': ProcKeys(context_wrap(PROC_KEYS))
}
failed, total = doctest.testmod(proc_keys, globs=env)
assert failed == 0