From 594cc5e48632609ed9ae8f57e8fe2407731733f2 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Fri, 27 Nov 2026 01:45:12 +0100 Subject: [PATCH] refactor(hardening): the SELinux part as three small questions (NetOrk/netork#2706) _selinux_configured, _selinux_running and _selinux_seen, each on its own, so _selinux stays within napalm-standard's complexity limit. The parse is unchanged. --- napalm_device_types/hardening.py | 35 +++++++++++++++++-------- tests/test_hardening.py | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/napalm_device_types/hardening.py b/napalm_device_types/hardening.py index e6b1572..6245bda 100644 --- a/napalm_device_types/hardening.py +++ b/napalm_device_types/hardening.py @@ -158,22 +158,37 @@ def _lsm(sections: dict[str, list[str]]) -> list[str] | None: return [name.strip() for name in line.split(",") if name.strip()] -def _selinux(sections: dict[str, list[str]], lsm: list[str] | None) -> SelinuxDict | None: - said = (_first(sections, "selinux") or "").lower() +def _selinux_configured(sections: dict[str, list[str]]) -> str | None: + """The mode it boots in: ``SELINUX=`` in ``/etc/selinux/config``, the last one.""" configured = None for line in sections.get("selinux_config", []): key, _, value = line.partition("=") if key.strip() == "SELINUX": configured = _unquote(value).lower() or None - mode: str | None = said if said in _SELINUX_MODES else None - if mode is None: - mode = _ENFORCE_FILE.get(_first(sections, "selinux_enforce") or "") - seen = mode is not None or "selinux_config" in sections or "selinux" in (lsm or []) - if not seen: + return configured + + +def _selinux_running(sections: dict[str, list[str]]) -> str | None: + """The mode it runs in: ``getenforce``'s word, else ``/sys/fs/selinux/enforce``.""" + said = (_first(sections, "selinux") or "").lower() + if said in _SELINUX_MODES: + return said + return _ENFORCE_FILE.get(_first(sections, "selinux_enforce") or "") + + +def _selinux_seen(sections: dict[str, list[str]], lsm: list[str] | None, mode: str | None) -> bool: + """Whether the host shows any sign of SELinux: a mode, its configuration, the LSM list.""" + return mode is not None or "selinux_config" in sections or "selinux" in (lsm or []) + + +def _selinux(sections: dict[str, list[str]], lsm: list[str] | None) -> SelinuxDict | None: + mode = _selinux_running(sections) + if not _selinux_seen(sections, lsm, mode): return None - if mode is None and "selinux_config" in sections and lsm is not None and "selinux" not in lsm: - mode = "disabled" - return {"mode": mode, "configured": configured} + if mode is None and "selinux_config" in sections and lsm is not None: + # Installed, but not among the modules the kernel runs: switched off at boot. + mode = None if "selinux" in lsm else "disabled" + return {"mode": mode, "configured": _selinux_configured(sections)} def _profiles(sections: dict[str, list[str]]) -> dict[str, int] | None: diff --git a/tests/test_hardening.py b/tests/test_hardening.py index 03c98d1..847878e 100644 --- a/tests/test_hardening.py +++ b/tests/test_hardening.py @@ -227,6 +227,50 @@ class TestSelinux: assert reading["selinux"] == {"mode": None, "configured": "enforcing"} +class TestSelinuxParts: + """The three questions the SELinux part answers, each on its own (napalm-standard's + complexity limit): the mode it boots in, the mode it runs in, and whether the + host has SELinux at all.""" + + def test_the_configured_mode_is_the_last_selinux_line(self): + from napalm_device_types.hardening import _selinux_configured + + lines = ["SELINUXTYPE=targeted", 'SELINUX="Permissive"'] + assert _selinux_configured({"selinux_config": lines}) == "permissive" + assert _selinux_configured({"selinux_config": ["SELINUX="]}) is None + assert _selinux_configured({}) is None + + @pytest.mark.parametrize( + "sections, mode", + [ + ({"selinux": ["Enforcing"]}, "enforcing"), + ({"selinux": ["something else"], "selinux_enforce": ["0"]}, "permissive"), + ({"selinux_enforce": ["1"]}, "enforcing"), + ({"selinux_enforce": ["2"]}, None), + ({}, None), + ], + ) + def test_the_running_mode_getenforce_first(self, sections, mode): + from napalm_device_types.hardening import _selinux_running + + assert _selinux_running(sections) == mode + + @pytest.mark.parametrize( + "sections, lsm, seen", + [ + ({}, ["capability", "selinux"], True), + ({"selinux_config": []}, None, True), + ({}, ["capability", "apparmor"], False), + ({}, None, False), + ], + ) + def test_whether_the_host_has_it(self, sections, lsm, seen): + from napalm_device_types.hardening import _selinux_seen + + assert _selinux_seen(sections, lsm, None) is seen + assert _selinux_seen(sections, lsm, "enforcing") is True + + class TestAppArmor: def test_in_the_list_of_modules_without_the_parameter_file(self): reading = parse_hardening(_wire([("lsm", "capability,apparmor")]))