From e7754a0070546f7af2fd6b802d96d609fa826069 Mon Sep 17 00:00:00 2001 From: Sheng Lao <39508521+shlao@users.noreply.github.com> Date: Thu, 28 Apr 2022 13:28:08 +0800 Subject: [PATCH] Feat: Add spec and parser for 'containers_policy' (#3394) * Feat: Add spec and parser for 'containers_policy' Signed-off-by: shlao * Updated the class docstring Signed-off-by: shlao (cherry picked from commit 6c473116ce5efb955c3273b05c001bbf9b483a6a) --- .../containers_policy.rst | 3 + insights/parsers/containers_policy.py | 59 +++++++++++++++++ .../parsers/tests/test_containers_policy.py | 64 +++++++++++++++++++ insights/specs/__init__.py | 1 + insights/specs/sos_archive.py | 1 + 5 files changed, 128 insertions(+) create mode 100644 docs/shared_parsers_catalog/containers_policy.rst create mode 100644 insights/parsers/containers_policy.py create mode 100644 insights/parsers/tests/test_containers_policy.py diff --git a/docs/shared_parsers_catalog/containers_policy.rst b/docs/shared_parsers_catalog/containers_policy.rst new file mode 100644 index 0000000000..3628bd63c5 --- /dev/null +++ b/docs/shared_parsers_catalog/containers_policy.rst @@ -0,0 +1,3 @@ +.. automodule:: insights.parsers.containers_policy + :members: + :show-inheritance: diff --git a/insights/parsers/containers_policy.py b/insights/parsers/containers_policy.py new file mode 100644 index 0000000000..7954b2d53e --- /dev/null +++ b/insights/parsers/containers_policy.py @@ -0,0 +1,59 @@ +""" +ContainersPolicy - file ``/etc/containers/policy.json`` +======================================================= +""" +from insights import JSONParser, parser +from insights.specs import Specs + + +@parser(Specs.containers_policy) +class ContainersPolicy(JSONParser): + """ + Class for converting file ``/etc/containers/policy.json`` + into a dictionary that matches the JSON string in the file. + + Sample file content:: + + { + "default": [ + { + "type": "insecureAcceptAnything" + } + ], + "transports": { + "docker": { + "registry.access.redhat.com": [ + { + "type": "signedBy", + "keyType": "GPGKeys", + "keyPath": "/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" + } + ], + "registry.redhat.io/redhat/redhat-operator-index": [ + { + "type": "insecureAcceptAnything" + } + ], + "registry.redhat.io": [ + { + "type": "signedBy", + "keyType": "GPGKeys", + "keyPath": "/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" + } + ] + }, + "docker-daemon": { + "": [ + { + "type": "insecureAcceptAnything" + } + ] + } + } + } + + Examples: + >>> len(containers_policy["default"]) + 1 + """ + pass diff --git a/insights/parsers/tests/test_containers_policy.py b/insights/parsers/tests/test_containers_policy.py new file mode 100644 index 0000000000..68c66deec6 --- /dev/null +++ b/insights/parsers/tests/test_containers_policy.py @@ -0,0 +1,64 @@ +import doctest + +from insights.parsers import containers_policy +from insights.parsers.containers_policy import ContainersPolicy +from insights.parsers.tests import skip_exception_check +from insights.tests import context_wrap + +CONTAINERS_POLICY_FILE = ''' +{ + "default": [ + { + "type": "insecureAcceptAnything" + } + ], + "transports": { + "docker": { + "registry.access.redhat.com": [ + { + "type": "signedBy", + "keyType": "GPGKeys", + "keyPath": "/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" + } + ], + "registry.redhat.io/redhat/redhat-operator-index": [ + { + "type": "insecureAcceptAnything" + } + ], + "registry.redhat.io": [ + { + "type": "signedBy", + "keyType": "GPGKeys", + "keyPath": "/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" + } + ] + }, + "docker-daemon": { + "": [ + { + "type": "insecureAcceptAnything" + } + ] + } + } +} +'''.strip() + + +def test_doc_examples(): + env = { + 'containers_policy': ContainersPolicy(context_wrap(CONTAINERS_POLICY_FILE)), + } + failed, total = doctest.testmod(containers_policy, globs=env) + assert failed == 0 + + +def test_containers_policy(): + conf = ContainersPolicy(context_wrap(CONTAINERS_POLICY_FILE)) + assert len(conf["default"]) == 1 + assert conf["default"][0]["type"] == "insecureAcceptAnything" + + +def test_containers_policy_empty(): + assert 'Empty output.' in skip_exception_check(ContainersPolicy) diff --git a/insights/specs/__init__.py b/insights/specs/__init__.py index 50373545e4..633933b2cb 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -73,6 +73,7 @@ class Specs(SpecSet): cni_podman_bridge_conf = RegistryPoint() cobbler_modules_conf = RegistryPoint() cobbler_settings = RegistryPoint() + containers_policy = RegistryPoint() corosync = RegistryPoint() corosync_cmapctl = RegistryPoint(multi_output=True) corosync_conf = RegistryPoint() diff --git a/insights/specs/sos_archive.py b/insights/specs/sos_archive.py index 842835ccb9..7b7a0bb2fd 100644 --- a/insights/specs/sos_archive.py +++ b/insights/specs/sos_archive.py @@ -43,6 +43,7 @@ class SosSpecs(Specs): cni_podman_bridge_conf = simple_file("/etc/cni/net.d/87-podman-bridge.conflist") cobbler_settings = first_file(["/etc/cobbler/settings", "/conf/cobbler/settings"]) cobbler_modules_conf = first_file(["/etc/cobbler/modules.conf", "/conf/cobbler/modules.conf"]) + containers_policy = simple_file("/etc/containers/policy.json") corosync_cmapctl = glob_file("sos_commands/corosync/corosync-cmapctl*") cpe = simple_file("/etc/system-release-cpe") cpu_smt_control = simple_file("sys/devices/system/cpu/smt/control")