diff --git a/insights/parsers/cups_confs.py b/insights/parsers/cups_confs.py index 830346aa3..8330c011d 100644 --- a/insights/parsers/cups_confs.py +++ b/insights/parsers/cups_confs.py @@ -6,6 +6,8 @@ CupsdConf - file ``/etc/cups/cupsd.conf`` ----------------------------------------- +CupsBrowsedConf - file ``/etc/cups/cups-browsed.conf`` +------------------------------------------------------ CupsFilesConf - file ``/etc/cups/cups-files.conf`` -------------------------------------------------- """ @@ -90,6 +92,40 @@ def parse_doc(self, content): return Entry(children=result, src=self) +@parser(Specs.cups_browsed_conf) +class CupsBrowsedConf(Parser, dict): + """ + Class for parsing the file ``/etc/cups/cups-browsed.conf`` + + Sample file content:: + + BrowseRemoteProtocols dnssd cups + BrowseAllow cups.example.com + + Examples: + >>> type(cups_browsed_conf) + + >>> 'dnssd' in cups_browsed_conf['BrowseRemoteProtocols'] + True + >>> 'cups.example.com' in cups_browsed_conf['BrowseAllow'] + True + """ + def parse_content(self, content): + if not content: + raise ParseException('Empty Content') + + for line in get_active_lines(content): + k, v = [i.strip() for i in line.split(None, 1)] + if k not in self: + self[k] = v if len(v.split()) == 1 else v.split() + else: + _v = self[k] + _v = [_v] if not isinstance(_v, list) else _v + if v not in _v: + _v.append(v) + self[k] = _v + + @parser(Specs.cups_files_conf) class CupsFilesConf(Parser, dict): """ diff --git a/insights/specs/__init__.py b/insights/specs/__init__.py index f45a4e2c7..a339a1bd1 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -124,6 +124,7 @@ class Specs(SpecSet): crypto_policies_state_current = RegistryPoint(no_obfuscate=['hostname', 'ip']) cryptsetup_luksDump = RegistryPoint(multi_output=True, no_obfuscate=['hostname', 'ip']) cupsd_conf = RegistryPoint() + cups_browsed_conf = RegistryPoint(filterable=True) cups_files_conf = RegistryPoint() cups_ppd = RegistryPoint(multi_output=True, no_obfuscate=['hostname', 'ip']) current_clocksource = RegistryPoint(no_obfuscate=['hostname', 'ip']) diff --git a/insights/specs/default.py b/insights/specs/default.py index 94cd24508..c2460f561 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -176,6 +176,7 @@ class DefaultSpecs(Specs): crypto_policies_state_current = simple_file("/etc/crypto-policies/state/current") cryptsetup_luksDump = luks_devices.luks_data_sources cupsd_conf = simple_file("/etc/cups/cupsd.conf") + cups_browsed_conf = simple_file("/etc/cups/cups-browsed.conf") cups_files_conf = simple_file("/etc/cups/cups-files.conf") current_clocksource = simple_file("/sys/devices/system/clocksource/clocksource0/current_clocksource") date = simple_command("/bin/date") diff --git a/insights/tests/parsers/test_cups_confs.py b/insights/tests/parsers/test_cups_confs.py index 0adedeb24..91f984f78 100644 --- a/insights/tests/parsers/test_cups_confs.py +++ b/insights/tests/parsers/test_cups_confs.py @@ -3,7 +3,7 @@ from insights.core.exceptions import ParseException from insights.parsers import cups_confs -from insights.parsers.cups_confs import CupsdConf, CupsFilesConf +from insights.parsers.cups_confs import CupsdConf, CupsFilesConf, CupsBrowsedConf from insights.parsr.query import first, last from insights.tests import context_wrap @@ -127,6 +127,21 @@ """.strip() +CUPS_BROWSED_CONF = """ +# Which protocols will we use to discover printers on the network? +# Can use DNSSD and/or CUPS and/or LDAP, or 'none' for neither. + +BrowseRemoteProtocols dnssd cups +BrowseAllow 192.168.0.1 +BrowseAllow 192.168.0.255 +BrowseAllow cups.example.com +BrowseAllow 192.168.0.255 +""".strip() + +CUPS_BROWSED_CONF_CONF_EMPTY = """ +""".strip() + + def test_cupsd_conf(): context = context_wrap(CUPSD_CONF) result = CupsdConf(context) @@ -151,16 +166,31 @@ def test_cups_files_conf(): assert result['AccessLog'] == 'syslog' -def test_cups_files_conf_empyt(): +def test_cups_files_conf_empty(): with pytest.raises(ParseException) as exc: CupsFilesConf(context_wrap(CUPS_FILES_CONF_EMPTY)) assert str(exc.value) == "Empty Content" +def test_cups_browsed_files_conf(): + result = CupsBrowsedConf(context_wrap(CUPS_BROWSED_CONF)) + assert len(result) == 2 + assert 'BrowseRemoteProtocols' in result + assert result['BrowseRemoteProtocols'] == ['dnssd', 'cups'] + assert sorted(result['BrowseAllow']) == sorted(['192.168.0.1', '192.168.0.255', 'cups.example.com']) + + +def test_cups_browsed_conf_empty(): + with pytest.raises(ParseException) as exc: + CupsBrowsedConf(context_wrap(CUPS_BROWSED_CONF_CONF_EMPTY)) + assert str(exc.value) == "Empty Content" + + def test_doc(): env = { 'cupsd_conf': CupsdConf(context_wrap(CUPSD_CONF_EXAMPLE)), - 'cups_files_conf': CupsFilesConf(context_wrap(CUPS_FILES_CONF)) + 'cups_files_conf': CupsFilesConf(context_wrap(CUPS_FILES_CONF)), + 'cups_browsed_conf': CupsBrowsedConf(context_wrap(CUPS_BROWSED_CONF)) } failed, total = doctest.testmod(cups_confs, globs=env) assert failed == 0