diff --git a/CHANGELOG.md b/CHANGELOG.md index aeb343f..c456b00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `get_host_firewall()` on the PVE driver (napalm-device-types' `HostFirewallMixin`): what the node has loaded -- proxmox-firewall's nftables tables on PVE 9, pve-firewall's iptables chains before -- read as root over SSH, as for any Linux host. +- `get_hardening()` on both drivers (napalm-device-types' `HardeningMixin`, NetOrk/netork#2776): + the security modules, SELinux and AppArmor, the crypto policy and FIPS mode, what the kernel + reports for each CPU vulnerability, and the kernel command line (`mitigations=`, `nosmt`). One + fixed, read-only command over the exec path, which is root's login, so AppArmor's profiles per + mode are read too. A hypervisor's guests share its CPU, so its CPU vulnerabilities matter most. + +### Changed +- Needs napalm-device-types 4.48.0 or later (`HardeningMixin`). ## [0.16.0] - 2026-11-26 diff --git a/README.md b/README.md index 67c53fe..8887b59 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ device = driver( | `get_services_need_restart` | the services that still run code an update replaced on disk, as root over SSH (napalm-device-types' `ServicesRestartMixin`): `needrestart` in list-only mode where it is installed, else the processes whose `/proc//maps` maps deleted code. A running VM's QEMU is a process (`processes`), an LXC container's processes are left out (the container updates itself). Only as root: another SSH login gets no reading, rather than a clean one | | `get_pve_firewall` | the PVE firewall as the operator wrote it, read only (NetOrk/netork#2654): the datacenter's `options`, `rules`, security `groups` (name → rules), `ipsets` (name → `[{cidr, nomatch, comment}]`) and `aliases` (name → cidr), this node's `options` and `rules`, and every guest's on this node (`vmid`, `type`, `name`, `options`, `rules`, and `nics`: `[{name, firewall}]` from `netN … firewall=1`). Each rule keeps PVE's fields and its `origin` (`cluster`, `group`, `node`, `guest`). A part PVE refuses is `null`, never empty | | `get_host_firewall` | what the node has loaded, as for any Linux host (napalm-device-types' `HostFirewallMixin`): `nft list ruleset` (PVE 9's proxmox-firewall) or `iptables-save` (pve-firewall), as root over SSH | +| `get_hardening` | how the node is hardened (napalm-device-types' `HardeningMixin`, NetOrk/netork#2776): security modules, SELinux and AppArmor, crypto policy, FIPS mode, CPU vulnerabilities, kernel command line, as root over SSH. On the backup server too | **Not implemented** (raise `NotImplementedError`): `get_bgp_config`, `get_bgp_neighbors`, `get_bgp_neighbors_detail`, @@ -255,6 +256,7 @@ tests/ ├── test_time_sync.py # get_time_sync / configure_time_sync on both drivers ├── test_certificates.py # get_certificates on both drivers ├── test_firewall.py # get_pve_firewall and get_host_firewall +├── test_hardening.py # get_hardening on both drivers ├── test_utils.py # Unit tests for napalm_proxmox.utils ├── fixtures/pbs_api.json # What a Proxmox Backup Server 4.2 answered (sanitised) ├── pbs_fake.py # A proxmoxer stand-in answering from that recording diff --git a/napalm_proxmox/backup_driver.py b/napalm_proxmox/backup_driver.py index 102035b..e86a7e9 100644 --- a/napalm_proxmox/backup_driver.py +++ b/napalm_proxmox/backup_driver.py @@ -19,6 +19,7 @@ from napalm_device_types import ( BackupServerDriver, EnsurePackageMixin, FingerprintRule, + HardeningMixin, HostStatusMixin, KernelFactsMixin, LicenseMixin, @@ -65,6 +66,7 @@ class ProxmoxBackupDriver( BackupServerDriver, StorageDriver, KernelFactsMixin, + HardeningMixin, ServicePackagesMixin, ListeningSocketsMixin, LogSourcesMixin, diff --git a/napalm_proxmox/driver.py b/napalm_proxmox/driver.py index cc1342b..9b22be2 100644 --- a/napalm_proxmox/driver.py +++ b/napalm_proxmox/driver.py @@ -29,6 +29,7 @@ from napalm_device_types import ( AptProxyMixin, EnsurePackageMixin, FingerprintRule, + HardeningMixin, HostFirewallMixin, HostStatusMixin, HypervisorDriver, @@ -103,6 +104,7 @@ class ProxmoxDriver( ProxmoxFirewallMixin, KernelFactsMixin, HostFirewallMixin, + HardeningMixin, ServicePackagesMixin, ListeningSocketsMixin, LogSourcesMixin, diff --git a/napalm_proxmox/host_mixin.py b/napalm_proxmox/host_mixin.py index 6b8325a..880ea2f 100644 --- a/napalm_proxmox/host_mixin.py +++ b/napalm_proxmox/host_mixin.py @@ -72,6 +72,12 @@ class ProxmoxHostMixin: """The transport for ``KernelFactsMixin.get_kernel_facts``: the exec path.""" return self._exec_ssh_command(command) + def _run_hardening_command(self, command: str, *, privileged: bool) -> str: + """The transport for ``HardeningMixin.get_hardening`` (NetOrk/netork#2776): the exec + path, which is root's login, so AppArmor's profiles per mode are read too. Another + login reads the rest without them, as napalm-device-types' fallback does. No stdin.""" + return str(self._exec_ssh_command(command)) + def _run_host_firewall_command(self, command: str, *, privileged: bool) -> str: """The transport for ``HostFirewallMixin.get_host_firewall``: the exec path, which runs as root either way. What the node has loaded -- proxmox-firewall's nftables diff --git a/pyproject.toml b/pyproject.toml index a51bec1..caa4cf9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ classifiers = [ requires-python = ">=3.12" dependencies = [ "napalm>=5.0.0", - "napalm_device_types>=4.46.0", # ServicesRestartMixin with LXC left out; CredentialExpiryMixin + "napalm_device_types>=4.48.0", # HardeningMixin; ServicesRestartMixin with LXC left out "paramiko>=5.0.0", # CVE-2026-44405; imported directly for the node's SSH session (_base.py) "proxmoxer>=2.0.0", "netaddr>=0.9.0", diff --git a/tests/test_hardening.py b/tests/test_hardening.py new file mode 100644 index 0000000..b268727 --- /dev/null +++ b/tests/test_hardening.py @@ -0,0 +1,71 @@ +"""`get_hardening`: how the node is hardened (NetOrk/netork#2776). + +A hypervisor is where CPU vulnerabilities matter most: its guests share the CPU. +The command and its parse are napalm-device-types' (``HardeningMixin``); both +drivers carry the command over their own exec path, which is root's login, so +AppArmor's profiles per mode are read as well. +""" + +from __future__ import annotations + +from unittest.mock import patch + +import pytest +from napalm_device_types import HardeningMixin + +from napalm_proxmox.backup_driver import ProxmoxBackupDriver +from napalm_proxmox.driver import ProxmoxDriver + +WIRE = "\n".join( + [ + "HARDENING_BEGIN", + "[lsm]", + "lockdown,capability,landlock,yama,apparmor,bpf", + "[cmdline]", + "BOOT_IMAGE=/boot/vmlinuz-6.8.12-4-pve root=/dev/mapper/pve-root ro mitigations=off", + "[fips]", + "0", + "[apparmor_enabled]", + "Y", + "[os]", + "ID=debian", + "[cpu_vulnerabilities]", + "/sys/devices/system/cpu/vulnerabilities/mds:Vulnerable; SMT vulnerable", + "/sys/devices/system/cpu/vulnerabilities/meltdown:Not affected", + "HARDENING_END", + "", + ] +) + + +@pytest.mark.parametrize("cls", [ProxmoxDriver, ProxmoxBackupDriver]) +def test_both_drivers_declare_the_contract(cls): + assert issubclass(cls, HardeningMixin) + assert hasattr(cls, "get_hardening") + + +def test_the_node_runs_the_shared_command_over_its_exec_path(driver): + with patch.object(driver, "_exec_ssh_command", return_value=WIRE) as exec_: + reading = driver.get_hardening() + + exec_.assert_called_once() + assert "HARDENING_" in exec_.call_args.args[0] + assert reading["lsm"] == ["lockdown", "capability", "landlock", "yama", "apparmor", "bpf"] + assert reading["mitigations"] == "off" + assert reading["cpu_vulnerabilities"]["mds"] == "Vulnerable; SMT vulnerable" + assert reading["os_ids"] == ["debian"] + + +def test_the_backup_server_reads_it_the_same_way(): + drv = ProxmoxBackupDriver("192.0.2.80", "root", "", optional_args={"token_name": "a!b"}) + + with patch.object(drv, "_exec_ssh_command", return_value=WIRE): + reading = drv.get_hardening() + + assert reading["cpu_vulnerabilities"]["meltdown"] == "Not affected" + + +def test_output_without_a_report_raises(driver): + with patch.object(driver, "_exec_ssh_command", return_value=""): + with pytest.raises(ValueError): + driver.get_hardening()