Files
napalm-proxmox/tests/test_hardening.py
T
Christian Manivong 7e300c740e
driver / lint (pull_request) Successful in 13s
driver / standard (pull_request) Successful in 18s
driver / test (3.12) (pull_request) Failing after 15s
driver / types (pull_request) Failing after 23s
driver / test (3.13) (pull_request) Failing after 13s
driver / lowest (pull_request) Failing after 16s
driver / build (pull_request) Successful in 27s
driver / security (pull_request) Failing after 34s
CI / driver (pull_request) Failing after 56s
driver / publish (pull_request) Skipped
feat(hardening): install_cpu_microcode on PVE and PBS, 0.19.0 (NetOrk/netork#2838)
Both drivers mix in napalm-device-types' CpuMicrocodeMixin (4.53.0). A Proxmox
install enables no non-free-firmware, where intel-microcode and
amd64-microcode live: it is added to Debian's own sources, never Proxmox's.
_reload_package_index runs apt-get update over the exec path and waits for
it, as the API's apt/update only starts a task the install would race.
2026-11-29 00:07:29 +01:00

138 lines
4.9 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()
class TestCpuMicrocode:
"""``install_cpu_microcode`` (napalm-device-types ``CpuMicrocodeMixin``, NetOrk/netork#2838):
a node runs on its own CPU, and Proxmox's own install enables no ``non-free-firmware``."""
SOURCES = "/etc/apt/sources.list.d/debian.sources"
TEXT = (
"Types: deb\nURIs: http://deb.debian.org/debian/\nSuites: trixie trixie-updates\n"
"Components: main contrib\n"
)
def _node(self, drv, *, virtualization="none"):
import base64
ran: list[str] = []
file_lines = "".join(f"|{line}\n" for line in self.TEXT.split("\n"))
report = (
"ARELEASE_BEGIN\n@@os\n|ID=debian\n|VERSION_CODENAME=trixie\n"
f"@@file {self.SOURCES}\n{file_lines}ARELEASE_END\n"
)
def exec_(command):
ran.append(command)
if command.startswith("base64 "):
return base64.b64encode(self.TEXT.encode()).decode()
if "ARELEASE_" in command:
return report
return "done\n__PVE_RC=0"
drv.get_hardening = lambda: {
"os_ids": ["debian"],
"virtualization": virtualization,
"cpu_vendor": "GenuineIntel",
}
drv.get_packages = lambda: []
drv.refresh_available_updates = lambda: pytest.fail("the API refresh does not wait")
drv._exec_ssh_command = exec_
return ran
@pytest.mark.parametrize("cls", [ProxmoxDriver, ProxmoxBackupDriver])
def test_both_drivers_carry_it(self, cls):
assert callable(getattr(cls, "install_cpu_microcode", None))
def test_the_component_then_apt_get_update_then_the_package(self, driver):
ran = self._node(driver)
result = driver.install_cpu_microcode()
assert result["success"] is True, result["output"]
assert result["sources_changed"] is True
written = next(c for c in ran if "install -m 644" in c)
assert self.SOURCES in written
update = next(i for i, c in enumerate(ran) if "apt-get update" in c)
install = next(i for i, c in enumerate(ran) if "apt-get install -y intel-microcode" in c)
assert ran.index(written) < update < install
def test_a_virtual_backup_server_installs_nothing(self):
drv = ProxmoxBackupDriver("192.0.2.80", "root", "", optional_args={"token_name": "a!b"})
ran = self._node(drv, virtualization="kvm")
result = drv.install_cpu_microcode()
assert result["success"] is False
assert "its host does" in result["output"]
assert ran == []