diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cd3e0c..67d4533 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- `get_hardening()` through napalm-device-types' `HardeningMixin`: how the host is hardened + -- its security modules, SELinux's and AppArmor's state, the crypto policy and FIPS mode, + the kernel's answer for each CPU vulnerability, and `mitigations=`/`nosmt` from the + kernel command line (NetOrk/netork#2706). One read-only command on an exec channel + (`aa-status --json` prints one long line), as root first for AppArmor's profiles and as + the login user where root is refused. OpenMediaVault and QNAP have it through + `LinuxDriver`. + ## [0.19.0] - 2026-11-26 ### Added diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index fe67862..afdc9fc 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -36,6 +36,7 @@ from napalm_device_types import ( ContainerEngineMixin, EnsurePackageMixin, FingerprintRule, + HardeningMixin, HostFirewallMixin, HostStatusMixin, KernelFactsMixin, @@ -277,6 +278,7 @@ class LinuxDriver( EnsurePackageMixin, AptProxyMixin, KernelFactsMixin, + HardeningMixin, ServicePackagesMixin, LinuxProcessReadingsMixin, HostFirewallMixin, @@ -1051,6 +1053,12 @@ class LinuxDriver( """The transport for ``KernelFactsMixin.get_kernel_facts``: read-only, no sudo.""" return self._send(command, read_timeout=60) + def _run_hardening_command(self, command: str, *, privileged: bool) -> str: + """The transport for ``HardeningMixin.get_hardening`` (NetOrk/netork#2706): an exec + channel, since ``aa-status --json`` prints one line longer than the PTY netmiko + holds reliably takes. Read-only either way; root only for AppArmor's profiles.""" + return self.run_command(command, privileged=privileged, timeout=60).stdout + def _run_service_packages_command(self, command: str) -> str: """The transport for ``ServicePackagesMixin.get_service_packages``. diff --git a/tests/test_linux.py b/tests/test_linux.py index 944cbcf..4f3de81 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -2085,6 +2085,53 @@ class TestGetSystemIdentity: driver._device.send_command.assert_not_called() +class TestGetHardening: + """How the host is hardened (NetOrk/netork#2706): napalm-device-types' command on an + exec channel, as root first for AppArmor's profiles, as the login user where root is + refused. ``aa-status --json`` prints one long line, past what the PTY holds reliably.""" + + _REPORT = ( + "HARDENING_BEGIN\n[lsm]\nlockdown,capability,yama,selinux,bpf\n" + "[selinux]\nEnforcing\n[crypto_policy]\nDEFAULT\nHARDENING_END\n" + ) + + def test_it_reads_the_shared_command_as_root_on_an_exec_channel(self, driver): + from napalm_device_types import CommandResult, HardeningMixin + + assert isinstance(driver, HardeningMixin) + calls = [] + + def run_command(command, *, privileged=False, timeout=60, stdin=None): + calls.append((command, privileged)) + return CommandResult(self._REPORT, "", 0) + + driver.run_command = run_command + + reading = driver.get_hardening() + + assert reading["selinux"] == {"mode": "enforcing", "configured": None} + assert reading["crypto_policy"] == "DEFAULT" + assert [privileged for _, privileged in calls] == [True] + assert calls[0][0].startswith("sh -c ") + driver._device.send_command.assert_not_called() + + def test_root_refused_reads_as_the_login_user(self, driver): + from napalm_device_types import CommandResult + + calls = [] + + def run_command(command, *, privileged=False, timeout=60, stdin=None): + calls.append(privileged) + if privileged: + return CommandResult("", "sudo: a password is required", 1) + return CommandResult(self._REPORT, "", 0) + + driver.run_command = run_command + + assert driver.get_hardening()["lsm"][3] == "selinux" + assert calls == [True, False] + + # --------------------------------------------------------------------------- # get_host_firewall (napalm-device-types HostFirewallMixin, NetOrk/netork#1344) # ---------------------------------------------------------------------------