From 257efdef8e1cb33a16ed65a632b628de79759797 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 12 Nov 2026 12:10:05 +0100 Subject: [PATCH 1/3] feat(packages): the rpm epoch in the version, and the installed advisories On an rpm host a package's version now carries its epoch where it has one (1:6.2.0-13.el9), as on a dpkg host: Oracle's and Red Hat's fixes name it, and without it an epoch-1 fix read as newer than every installed build. get_installed_advisory_cves() reads, as root and from the cache, the CVEs the installed security advisories close, through napalm-device-types 4.37.0; dnf only (NetOrk/netork#2394). --- CHANGELOG.md | 12 +++++++++ napalm_linux/linux.py | 33 ++++++++++++++++++++++- pyproject.toml | 2 +- tests/test_linux.py | 63 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b018ee..f319cc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- `get_installed_advisory_cves()` through napalm-device-types' `UpdateMixin` (4.37.0): the + CVEs a dnf host's installed security advisories close, per package, read as root from + the cache. netOrk reads it where its knowledge base holds no records of the host's + release (NetOrk/netork#2394). dnf only; other package managers raise + `NotImplementedError`. + +### Changed +- On an rpm host a package's version carries its epoch where it has one + (`1:6.2.0-13.el9`), as on a dpkg host. Oracle's and Red Hat's fixes name it, and without + it an epoch-1 fix read as newer than every installed build (NetOrk/netork#2394). + ### Added - `get_time_sync()` and `configure_time_sync()` through napalm-device-types' `PosixTimeSyncMixin` (4.35.0): the host's NTP servers in its client's order, its time zone, diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index 5e551d5..666a109 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -27,6 +27,7 @@ from napalm.base.exceptions import ConnectionClosedException, ConnectionExceptio from napalm.base.netmiko_helpers import netmiko_args from napalm_device_types import ( APT_UPGRADABLE_COMMAND, + DNF_INSTALLED_CVES_COMMAND, DNF_SECURITY_COMMAND, LLDPCTL_KEYVALUE_COMMAND, AptProxyMixin, @@ -51,6 +52,7 @@ from napalm_device_types import ( lldp_neighbors, open_stream_on_transport, parse_apt_upgradable, + parse_dnf_installed_cves, parse_dnf_security, parse_lldpctl_keyvalue, run_fix_steps, @@ -1138,9 +1140,16 @@ class LinuxDriver( return parse_dpkg_query(self._send(DPKG_QUERY_COMMAND)) def _get_packages_rpm(self) -> list[PackageDict]: + """``rpm -qa``, the version as ``[epoch:]version-release``, as dpkg writes it. + + The epoch is there only where the package has one: Oracle's and Red Hat's + fixes name it (``1:6.2.0-13.el9``), and without it an epoch-1 fix reads as + newer than every installed build (NetOrk/netork#2394). + """ out = self._send( "rpm -qa --queryformat " - "'%{NAME}\\t%{VERSION}-%{RELEASE}\\t%{SIZE}\\t%{SUMMARY}\\n' 2>/dev/null" + "'%{NAME}\\t%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\t%{SIZE}\\t%{SUMMARY}\\n'" + " 2>/dev/null" ) packages: list[PackageDict] = [] for line in out.splitlines(): @@ -1477,6 +1486,28 @@ class LinuxDriver( except ValueError: return None + def get_installed_advisory_cves(self) -> dict[str, list[str]]: + """The CVEs the installed security advisories close, per package (dnf 4). + + As root, from the cache, as the security read does: root's cache is the one + the host's own updates keep. The output is long (90 000 lines on an Oracle + Linux 9 host), so it gets a longer budget; netOrk reads it only where its + knowledge base holds no records of the release (NetOrk/netork#2394). + + :raises NotImplementedError: on any package manager but dnf. + :raises ValueError: when dnf failed or its output was cut short. + """ + if self._pkg_manager != "dnf": + raise NotImplementedError( + f"Installed advisories are not read for {self._pkg_manager!r}" + ) + cmd = DNF_INSTALLED_CVES_COMMAND + output, status = _split_status(self._run_privileged(f"{cmd}; echo {_RC_MARKER}$?", 300)) + if status != 0: + hint = f"\n{_SUDO_PASSWORD_HINT}" if "password is required" in output else "" + raise RuntimeError(f"dnf updateinfo failed (exit {status}): {output[-200:]}{hint}") + return parse_dnf_installed_cves(output) + def refresh_available_updates(self) -> dict[str, Any]: """Refresh the package index (apt-get update, dnf makecache, apk update).""" cmd = _REFRESH.get(self._pkg_manager or "") diff --git a/pyproject.toml b/pyproject.toml index b5becd2..8e987bf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,7 @@ classifiers = [ ] dependencies = [ "napalm>=4.0", - "napalm-device-types>=4.35.0", + "napalm-device-types>=4.37.0", "netmiko>=4.0.0", "paramiko>=5.0.0", # CVE-2026-44405 ] diff --git a/tests/test_linux.py b/tests/test_linux.py index f375777..41ba0cc 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -4,6 +4,7 @@ import re from unittest.mock import MagicMock, patch import pytest +from napalm_device_types import DNF_INSTALLED_CVES_COMMAND from napalm_linux._routes import IpRoute, parse_ip_route from napalm_linux.linux import ( @@ -2591,3 +2592,65 @@ class TestIpv6NextHops: out = "10.50.0.0/16 via inet 192.0.2.1 dev eth0\n" assert [r.nexthop for r in parse_ip_route(out, "ipv4")] == ["192.0.2.1"] + + +# --------------------------------------------------------------------------- +# get_packages (rpm) and the installed advisories (NetOrk/netork#2394) +# --------------------------------------------------------------------------- + +# rpm -qa on Oracle Linux 9.8 (aarch64): gmp and openssl carry epoch 1. +RPM_QA = ( + "glibc-common\t2.34-284.0.1.el9_8.4\t1210460\tCommon binaries and locale data for glibc\n" + "gmp\t1:6.2.0-13.el9\t805146\tA GNU arbitrary precision library\n" + "openssl\t1:3.5.8-2.0.1.el9_8\t2106925\tUtilities from the general purpose cryptography library\n" +) + + +class TestRpmPackages: + def test_the_version_carries_the_epoch_where_the_package_has_one(self, driver): + """Oracle's and Red Hat's fixes name it (`1:6.2.0-13.el9`); without it an + epoch-1 fix reads as newer than every installed build.""" + driver._pkg_manager = "dnf" + with patch.object(driver, "_send", return_value=RPM_QA) as send: + pkgs = {p["name"]: p for p in driver.get_packages()} + + assert "%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}" in send.call_args.args[0] + assert pkgs["gmp"]["version"] == "1:6.2.0-13.el9" + assert pkgs["openssl"]["version"] == "1:3.5.8-2.0.1.el9_8" + assert pkgs["glibc-common"]["version"] == "2.34-284.0.1.el9_8.4" + assert pkgs["glibc-common"]["source"] == "dnf" + assert pkgs["glibc-common"]["size"] == 1210460 + + +class TestInstalledAdvisoryCves: + def test_dnf_reads_the_installed_advisories_as_root(self, driver): + driver._pkg_manager = "dnf" + driver._device.send_command.side_effect = [ + "0", # id -u + ( + "CVE-2023-4911 Important/Sec. glibc-common-2.34-60.0.3.el9_2.7.aarch64\n" + "CVE-2023-38545 Important/Sec. curl-7.76.1-26.el9_3.2.aarch64\n" + "__DNF_RC=0\n__NETORK_RC=0" + ), + ] + + assert driver.get_installed_advisory_cves() == { + "glibc-common": ["CVE-2023-4911"], + "curl": ["CVE-2023-38545"], + } + sent = driver._device.send_command.call_args.args[0] + assert DNF_INSTALLED_CVES_COMMAND in sent + + def test_a_failed_dnf_raises_instead_of_reading_as_nothing_installed(self, driver): + driver._pkg_manager = "dnf" + driver._device.send_command.side_effect = ["0", "__DNF_RC=1\n__NETORK_RC=0"] + + with pytest.raises(ValueError, match="exit status 1"): + driver.get_installed_advisory_cves() + + @pytest.mark.parametrize("manager", ["yum", "apt", "apk", "pacman"]) + def test_other_package_managers_keep_no_such_advisories(self, driver, manager): + driver._pkg_manager = manager + + with pytest.raises(NotImplementedError): + driver.get_installed_advisory_cves() From 1d377acc5f943ab4df60ccc7b49073136e3e6566 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 12 Nov 2026 12:13:48 +0100 Subject: [PATCH 2/3] test(time-sync): the reading carries dhcp_ntp_servers since device-types 4.36 (#75) --- tests/test_time_sync.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_time_sync.py b/tests/test_time_sync.py index fb6a7c5..efc639d 100644 --- a/tests/test_time_sync.py +++ b/tests/test_time_sync.py @@ -74,6 +74,8 @@ def test_the_reading_is_one_round_trip_as_the_login_user(): "timezone": "Europe/Berlin", "synchronized": True, "daemon": "chrony", + # napalm-device-types 4.36.0: the servers it got by DHCP, none here (#75). + "dhcp_ntp_servers": [], } assert [privileged for _, privileged, _ in channel.calls] == [False] From bc7162affd3b22fb188cda64a3f6872e13ffb111 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 12 Nov 2026 12:24:43 +0100 Subject: [PATCH 3/3] refactor(packages): the rpm query and its parse in _rpm.py, as dpkg's in _dpkg.py linux.py stays under its recorded size (2039 -> 2034 lines). --- napalm_linux/_rpm.py | 36 ++++++++++++++++++++++++++++ napalm_linux/linux.py | 48 +++++-------------------------------- tests/quality_baseline.json | 2 +- 3 files changed, 43 insertions(+), 43 deletions(-) create mode 100644 napalm_linux/_rpm.py diff --git a/napalm_linux/_rpm.py b/napalm_linux/_rpm.py new file mode 100644 index 0000000..1a55543 --- /dev/null +++ b/napalm_linux/_rpm.py @@ -0,0 +1,36 @@ +"""The packages rpm has on a dnf or yum host: the query and its parse.""" + +from __future__ import annotations + +from napalm_device_types.models import PackageDict + +#: One line per package, the version as ``[epoch:]version-release`` the way dpkg +#: writes it: the epoch only where the package has one. Oracle's and Red Hat's +#: fixes name it (``1:6.2.0-13.el9``), and without it an epoch-1 fix reads as newer +#: than every installed build (NetOrk/netork#2394). +RPM_QUERY_COMMAND = ( + "rpm -qa --queryformat " + "'%{NAME}\\t%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\t%{SIZE}\\t%{SUMMARY}\\n'" + " 2>/dev/null" +) + + +def parse_rpm_query(out: str, source: str) -> list[PackageDict]: + """The packages in ``RPM_QUERY_COMMAND``'s output, each marked as *source*'s.""" + packages: list[PackageDict] = [] + for line in out.splitlines(): + parts = line.split("\t", 3) + if len(parts) < 2: + continue + size = parts[2].strip() if len(parts) > 2 else "" + packages.append( + { + "name": parts[0].strip(), + "version": parts[1].strip(), + "installed": True, + "description": parts[3].strip() if len(parts) > 3 else "", + "size": int(size) if size.isdigit() else 0, + "source": source, + } + ) + return packages diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index 666a109..90f89f1 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -82,6 +82,7 @@ from napalm_linux._ping import parse_ping, ping_command from napalm_linux._posix import LinuxPosixMixin from napalm_linux._readings import LinuxProcessReadingsMixin from napalm_linux._routes import IP_ROUTE_COMMANDS, IpRoute, parse_ip_route, with_length +from napalm_linux._rpm import RPM_QUERY_COMMAND, parse_rpm_query from napalm_linux._shell import expect_for, prepare_shell, sudo_without_password from napalm_linux._sudo_history import LinuxSudoHistoryMixin @@ -1140,35 +1141,7 @@ class LinuxDriver( return parse_dpkg_query(self._send(DPKG_QUERY_COMMAND)) def _get_packages_rpm(self) -> list[PackageDict]: - """``rpm -qa``, the version as ``[epoch:]version-release``, as dpkg writes it. - - The epoch is there only where the package has one: Oracle's and Red Hat's - fixes name it (``1:6.2.0-13.el9``), and without it an epoch-1 fix reads as - newer than every installed build (NetOrk/netork#2394). - """ - out = self._send( - "rpm -qa --queryformat " - "'%{NAME}\\t%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\t%{SIZE}\\t%{SUMMARY}\\n'" - " 2>/dev/null" - ) - packages: list[PackageDict] = [] - for line in out.splitlines(): - parts = line.split("\t", 3) - if len(parts) < 2: - continue - packages.append( - { - "name": parts[0].strip(), - "version": parts[1].strip(), - "installed": True, - "description": parts[3].strip() if len(parts) > 3 else "", - "size": int(parts[2].strip()) - if len(parts) > 2 and parts[2].strip().isdigit() - else 0, - "source": self._pkg_manager or "rpm", - } - ) - return packages + return parse_rpm_query(self._send(RPM_QUERY_COMMAND), self._pkg_manager or "rpm") def _get_packages_apk(self) -> list[PackageDict]: out = self._send("apk info -v 2>/dev/null") @@ -1487,20 +1460,11 @@ class LinuxDriver( return None def get_installed_advisory_cves(self) -> dict[str, list[str]]: - """The CVEs the installed security advisories close, per package (dnf 4). - - As root, from the cache, as the security read does: root's cache is the one - the host's own updates keep. The output is long (90 000 lines on an Oracle - Linux 9 host), so it gets a longer budget; netOrk reads it only where its - knowledge base holds no records of the release (NetOrk/netork#2394). - - :raises NotImplementedError: on any package manager but dnf. - :raises ValueError: when dnf failed or its output was cut short. - """ + """The CVEs the installed advisories close, per package: dnf only, as root and + from the cache like the security read, with a longer budget for its 90 000 + lines on Oracle Linux 9 (NetOrk/netork#2394).""" if self._pkg_manager != "dnf": - raise NotImplementedError( - f"Installed advisories are not read for {self._pkg_manager!r}" - ) + raise NotImplementedError(f"No installed advisories for {self._pkg_manager!r}") cmd = DNF_INSTALLED_CVES_COMMAND output, status = _split_status(self._run_privileged(f"{cmd}; echo {_RC_MARKER}$?", 300)) if status != 0: diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index dcab948..e80fa03 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -20,7 +20,7 @@ "napalm_linux.linux" ], "python_file_lines": { - "napalm_linux/linux.py": 2039 + "napalm_linux/linux.py": 2034 }, "untyped_public": [] }