feat(hardening): get_hardening through napalm-device-types' HardeningMixin (NetOrk/netork#2706)
The transport runs the shared, read-only command on an exec channel, as root first for AppArmor's profiles and as the login user where root is refused.
This commit is contained in:
1 parent
670e62e0f0
commit
a9ed615b9b
3 files changed
+64
No files matched your search
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [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
|
## [0.19.0] - 2026-11-26
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ from napalm_device_types import (
|
|||||||
ContainerEngineMixin,
|
ContainerEngineMixin,
|
||||||
EnsurePackageMixin,
|
EnsurePackageMixin,
|
||||||
FingerprintRule,
|
FingerprintRule,
|
||||||
|
HardeningMixin,
|
||||||
HostFirewallMixin,
|
HostFirewallMixin,
|
||||||
HostStatusMixin,
|
HostStatusMixin,
|
||||||
KernelFactsMixin,
|
KernelFactsMixin,
|
||||||
@@ -277,6 +278,7 @@ class LinuxDriver(
|
|||||||
EnsurePackageMixin,
|
EnsurePackageMixin,
|
||||||
AptProxyMixin,
|
AptProxyMixin,
|
||||||
KernelFactsMixin,
|
KernelFactsMixin,
|
||||||
|
HardeningMixin,
|
||||||
ServicePackagesMixin,
|
ServicePackagesMixin,
|
||||||
LinuxProcessReadingsMixin,
|
LinuxProcessReadingsMixin,
|
||||||
HostFirewallMixin,
|
HostFirewallMixin,
|
||||||
@@ -1051,6 +1053,12 @@ class LinuxDriver(
|
|||||||
"""The transport for ``KernelFactsMixin.get_kernel_facts``: read-only, no sudo."""
|
"""The transport for ``KernelFactsMixin.get_kernel_facts``: read-only, no sudo."""
|
||||||
return self._send(command, read_timeout=60)
|
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:
|
def _run_service_packages_command(self, command: str) -> str:
|
||||||
"""The transport for ``ServicePackagesMixin.get_service_packages``.
|
"""The transport for ``ServicePackagesMixin.get_service_packages``.
|
||||||
|
|
||||||
|
|||||||
@@ -2085,6 +2085,53 @@ class TestGetSystemIdentity:
|
|||||||
driver._device.send_command.assert_not_called()
|
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)
|
# get_host_firewall (napalm-device-types HostFirewallMixin, NetOrk/netork#1344)
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in new issue
Block a user