Files
napalm-proxmox/tests/test_hardening.py
Christian Manivong b6bb13009f
driver / lint (pull_request) Successful in 14s
driver / standard (pull_request) Successful in 17s
driver / types (pull_request) Successful in 1m1s
driver / test (3.13) (pull_request) Successful in 1m7s
driver / test (3.12) (pull_request) Successful in 1m11s
driver / lowest (pull_request) Successful in 37s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Successful in 1m4s
CI / driver (pull_request) Successful in 2m29s
driver / publish (pull_request) Skipped
feat(hardening): get_hardening on both drivers over the exec path; needs device-types 4.48.0 (NetOrk/netork#2776)
2026-11-28 08:01:27 +01:00

72 lines
2.4 KiB
Python

"""`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()