refactor(hardening): the SELinux part as three small questions (NetOrk/netork#2706)
driver / lint (pull_request) Successful in 17s
driver / standard (pull_request) Successful in 19s
driver / types (pull_request) Successful in 1m1s
driver / test (3.13) (pull_request) Successful in 1m12s
driver / test (3.12) (pull_request) Successful in 1m19s
driver / lowest (pull_request) Successful in 39s
driver / build (pull_request) Successful in 29s
driver / security (pull_request) Successful in 1m0s
CI / driver (pull_request) Successful in 2m31s
driver / publish (pull_request) Skipped

_selinux_configured, _selinux_running and _selinux_seen, each on its own, so
_selinux stays within napalm-standard's complexity limit. The parse is
unchanged.
This commit is contained in:
christianmanivong committed 2026-11-27 01:45:12 +01:00
1 parent e440bee9e1
commit 594cc5e486
2 files changed
+69 -10

No files matched your search

+25 -10
View File
@@ -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:
+44
View File
@@ -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")]))