Skip to content

Commit

Permalink
feat: New spec and parser for authselect current (#3490)
Browse files Browse the repository at this point in the history
Add a new spec for authselect current and its parser

Signed-off-by: Xiangce Liu <[email protected]>
  • Loading branch information
xiangce authored Aug 25, 2022
1 parent def453c commit b0076c0
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/shared_parsers_catalog/authselect.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. automodule:: insights.parsers.authselect
:members:
:show-inheritance:
58 changes: 58 additions & 0 deletions insights/parsers/authselect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
AuthSelectCurrent - command ``authselect current``
==================================================
"""
from insights import parser
from insights.core import CommandParser
from insights.parsers import SkipException
from insights.specs import Specs


@parser(Specs.authselect_current)
class AuthSelectCurrent(CommandParser):
"""
Class to parse the output of command "authselect current".
Sample output of command ``authselect current``::
Profile ID: sssd
Enabled features:
- with-sudo
- with-mkhomedir
- with-smartcard
Attributes:
profile_id (str): The enabled profile ID
enabled_features (list): List of enabled features
Examples:
>>> asc.profile_id
'sssd'
>>> len(asc.enabled_features)
3
>>> 'with-sudo' in asc.enabled_features
True
"""
def parse_content(self, content):
feature_flag = False
self.profile_id = None
self.enabled_features = []
for line in content:
if 'No existing configuration detected.' in line:
raise SkipException
line_sp = line.split()
if line.startswith('Profile ID:'):
self.profile_id = line_sp[-1]
elif line.endswith('Enabled features:'):
# Enabled features:
# - ...
feature_flag = True
elif line.startswith('Enabled features:') and len(line_sp) > 2:
# Enabled features: None
feature_flag = True
if line_sp[-1] != 'None':
self.enabled_features.append(line_sp[-1])
elif line.startswith('-') and feature_flag:
self.enabled_features.append(line_sp[-1])
if self.profile_id is None:
raise SkipException
1 change: 1 addition & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Specs(SpecSet):
auditctl_status = RegistryPoint()
auditd_conf = RegistryPoint()
audit_log = RegistryPoint(filterable=True)
authselect_current = RegistryPoint()
autofs_conf = RegistryPoint()
avc_hash_stats = RegistryPoint()
avc_cache_threshold = 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 @@ -66,6 +66,7 @@ class DefaultSpecs(Specs):
abrt_status_bare = simple_command("/usr/bin/abrt status --bare=True")
alternatives_display_python = simple_command("/usr/sbin/alternatives --display python")
amq_broker = glob_file("/var/opt/amq-broker/*/etc/broker.xml")
authselect_current = simple_command("/usr/bin/authselect current")
dse_ldif = glob_file("/etc/dirsrv/*/dse.ldif")
auditctl_rules = simple_command("/sbin/auditctl -l")
auditctl_status = simple_command("/sbin/auditctl -s")
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 @@ -16,6 +16,7 @@ class InsightsArchiveSpecs(Specs):
ansible_host = simple_file("ansible_host")
auditctl_rules = simple_file("insights_commands/auditctl_-l")
auditctl_status = simple_file("insights_commands/auditctl_-s")
authselect_current = simple_file("insights_commands/authselect_current")
aws_instance_id_doc = simple_file("insights_commands/python_-m_insights.tools.cat_--no-header_aws_instance_id_doc")
aws_instance_id_pkcs7 = simple_file("insights_commands/python_-m_insights.tools.cat_--no-header_aws_instance_id_pkcs7")
awx_manage_check_license = simple_file("insights_commands/awx-manage_check_license")
Expand Down
50 changes: 50 additions & 0 deletions insights/tests/parsers/test_authselect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
import doctest
from insights.parsers.authselect import AuthSelectCurrent
from insights.parsers import SkipException, authselect
from insights.tests import context_wrap

AUTHSELECT_CURRENT_1 = """
Profile ID: sssd
Enabled features:
- with-sudo
- with-mkhomedir
- with-smartcard
""".strip()

AUTHSELECT_CURRENT_2 = """
Profile ID: custom/password-policy
Enabled features: None
""".strip()

AUTHSELECT_CURRENT_EMPTY = ""

AUTHSELECT_CURRENT_NG = """
No existing configuration detected.
""".strip()


def test_authselect_current():
asc = AuthSelectCurrent(context_wrap(AUTHSELECT_CURRENT_1))
assert asc.profile_id == 'sssd'
assert asc.enabled_features == ['with-sudo', 'with-mkhomedir', 'with-smartcard']

asc = AuthSelectCurrent(context_wrap(AUTHSELECT_CURRENT_2))
assert asc.profile_id == 'custom/password-policy'
assert asc.enabled_features == []


def test_authselect_current_exp():
with pytest.raises(SkipException):
AuthSelectCurrent(context_wrap(AUTHSELECT_CURRENT_EMPTY))

with pytest.raises(SkipException):
AuthSelectCurrent(context_wrap(AUTHSELECT_CURRENT_NG))


def test_doc_examples():
env = {
'asc': AuthSelectCurrent(context_wrap(AUTHSELECT_CURRENT_1))
}
failed, total = doctest.testmod(authselect, globs=env)
assert failed == 0

0 comments on commit b0076c0

Please sign in to comment.