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