feat(hardening): install_cpu_microcode through CpuMicrocodeMixin, 0.24.0 (NetOrk/netork#2838)
driver / lint (pull_request) Successful in 16s
driver / standard (pull_request) Successful in 22s
driver / lowest (pull_request) Successful in 36s
driver / types (pull_request) Successful in 59s
driver / test (3.12) (pull_request) Successful in 58s
driver / build (pull_request) Successful in 25s
driver / test (3.13) (pull_request) Successful in 1m11s
driver / security (pull_request) Successful in 1m22s
CI / driver (pull_request) Successful in 2m20s
driver / publish (pull_request) Skipped

LinuxHardeningMixin carries napalm-device-types' CpuMicrocodeMixin (4.53.0):
the CPU vendor's microcode on a host that runs on its own CPU, Debian's
firmware component added to Debian's sources first. OpenMediaVault and QNAP
inherit it; QNAP's os IDs name no package, so it refuses there.
This commit is contained in:
Christian Manivong committed 2026-11-29 00:07:28 +01:00
1 parent 8b09d5a713
commit 3e41122dfe
5 files changed
+82 -5

No files matched your search

+60
View File
@@ -414,3 +414,63 @@ class TestFactsNotWarnings:
def test_the_applications_proxy_url_is_no_argument_any_more(self):
d = LinuxDriver("h", "admin", "p", optional_args={"apt_proxy_url": "http://c:3142"})
assert not hasattr(d, "_apt_proxy_url")
class TestCpuMicrocode:
"""``install_cpu_microcode`` (napalm-device-types ``CpuMicrocodeMixin``) on this
driver's transports: the apt report and the file come over the exec channel, the
file goes back through ``_install_file_as_root`` (NetOrk/netork#2838)."""
SOURCES = "/etc/apt/sources.list.d/debian.sources"
TEXT = "Types: deb\nURIs: http://deb.debian.org/debian\nSuites: trixie\nComponents: main\n"
def _debian_host(self, driver, *, virtualization="none"):
import base64
driver.get_hardening = lambda: {
"os_ids": ["debian"],
"virtualization": virtualization,
"cpu_vendor": "GenuineIntel",
}
driver.get_packages = lambda: []
driver._sudo_status = MagicMock(return_value=("Setting up intel-microcode", 0))
driver.refresh_available_updates = MagicMock(return_value={"success": True, "output": ""})
driver._install_file_as_root = MagicMock(
return_value=MagicMock(exit_code=0, stdout="", stderr="")
)
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 run_command(command, *, privileged=False, timeout=60, stdin=None):
if command.startswith("base64 "):
out = base64.b64encode(self.TEXT.encode()).decode()
else:
out = report
return MagicMock(exit_code=0, stdout=out, stderr="")
driver.run_command = run_command
def test_a_debian_host_gets_the_component_then_the_package(self, driver):
self._debian_host(driver)
result = driver.install_cpu_microcode()
assert result["success"] is True, result["output"]
assert result["package"] == "intel-microcode"
path, content = driver._install_file_as_root.call_args.args
assert path == self.SOURCES
assert "Components: main non-free-firmware" in content
driver.refresh_available_updates.assert_called_once()
assert "apt-get install -y intel-microcode" in driver._sudo_status.call_args.args[0]
def test_a_guest_installs_nothing(self, driver):
self._debian_host(driver, virtualization="kvm")
result = driver.install_cpu_microcode()
assert result["success"] is False
driver._sudo_status.assert_not_called()
driver._install_file_as_root.assert_not_called()