Merge pull request 'feat(packages): each rpm names the source rpm it was built from' (#80) from fix/rpm-source-package into main
driver / standard (push) Successful in 17s
driver / lint (push) Successful in 14s
driver / types (push) Successful in 1m8s
driver / test (3.12) (push) Successful in 1m18s
driver / test (3.13) (push) Successful in 1m18s
driver / lowest (push) Successful in 35s
driver / build (push) Successful in 34s
driver / security (push) Successful in 1m10s
CI / driver (push) Successful in 2m59s
driver / publish (push) Skipped

This commit was merged in pull request #80.
This commit is contained in:
christianmanivong committed 2026-11-12 18:07:58 +00:00
commit 5f6595d160
3 files changed
+100 -17

No files matched your search

+6
View File
@@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added ### Added
- On an rpm host each package names the source rpm it was built from, as `source_rpm`
(`perf` → `kernel`); absent where rpm names none (`gpg-pubkey`). Red Hat lists `perf`,
`python3-perf` and `bpftool` in every kernel CVE, and the source is what tells them apart
from user space (NetOrk/netork#2394). Not dpkg's `source_package`: netOrk reads that as
the name a distribution files its verdicts under, and Oracle's and Red Hat's name the
binary.
- `get_installed_advisory_cves()` through napalm-device-types' `UpdateMixin` (4.37.0): the - `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 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 the cache. netOrk reads it where its knowledge base holds no records of the host's
+46 -13
View File
@@ -2,35 +2,68 @@
from __future__ import annotations from __future__ import annotations
from typing import NotRequired
from napalm_device_types.models import PackageDict from napalm_device_types.models import PackageDict
#: One line per package, the version as ``[epoch:]version-release`` the way dpkg #: 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 #: 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 #: 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). #: than every installed build (NetOrk/netork#2394). The source rpm before the
#: summary, which stays last and keeps whatever it contains.
RPM_QUERY_COMMAND = ( RPM_QUERY_COMMAND = (
"rpm -qa --queryformat " "rpm -qa --queryformat "
"'%{NAME}\\t%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\t%{SIZE}\\t%{SUMMARY}\\n'" "'%{NAME}\\t%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\t%{SIZE}\\t%{SOURCERPM}"
"\\t%{SUMMARY}\\n'"
" 2>/dev/null" " 2>/dev/null"
) )
class RpmPackageDict(PackageDict):
"""An rpm package with the name of the source rpm it was built from.
``perf``, ``python3-perf`` and ``bpftool`` come from ``kernel``, and Red Hat
lists them in every kernel CVE; their source is what tells them apart from
user space (NetOrk/netork#2394). Deliberately not dpkg's ``source_package``:
netOrk reads that as the name a distribution files its verdicts under, which
holds for Debian and not for Oracle or Red Hat, whose records name the binary.
Absent where rpm names none (``gpg-pubkey``).
"""
source_rpm: NotRequired[str]
def source_rpm_name(sourcerpm: str) -> str | None:
"""``kernel-5.14.0-687.66.1.el9_8.src.rpm`` → ``kernel``; ``None`` for ``(none)``."""
stem = sourcerpm.strip()
for suffix in (".src.rpm", ".nosrc.rpm"):
if stem.endswith(suffix):
stem = stem.removesuffix(suffix)
break
else:
return None
name, *version_release = stem.rsplit("-", 2)
return name if len(version_release) == 2 and name else None
def parse_rpm_query(out: str, source: str) -> list[PackageDict]: def parse_rpm_query(out: str, source: str) -> list[PackageDict]:
"""The packages in ``RPM_QUERY_COMMAND``'s output, each marked as *source*'s.""" """The packages in ``RPM_QUERY_COMMAND``'s output, each marked as *source*'s."""
packages: list[PackageDict] = [] packages: list[PackageDict] = []
for line in out.splitlines(): for line in out.splitlines():
parts = line.split("\t", 3) parts = line.split("\t", 4)
if len(parts) < 2: if len(parts) < 2:
continue continue
size = parts[2].strip() if len(parts) > 2 else "" size = parts[2].strip() if len(parts) > 2 else ""
packages.append( package: RpmPackageDict = {
{ "name": parts[0].strip(),
"name": parts[0].strip(), "version": parts[1].strip(),
"version": parts[1].strip(), "installed": True,
"installed": True, "description": parts[4].strip() if len(parts) > 4 else "",
"description": parts[3].strip() if len(parts) > 3 else "", "size": int(size) if size.isdigit() else 0,
"size": int(size) if size.isdigit() else 0, "source": source,
"source": source, }
} built_from = source_rpm_name(parts[3]) if len(parts) > 3 else None
) if built_from:
package["source_rpm"] = built_from
packages.append(package)
return packages return packages
+48 -4
View File
@@ -7,6 +7,7 @@ import pytest
from napalm_device_types import DNF_INSTALLED_CVES_COMMAND from napalm_device_types import DNF_INSTALLED_CVES_COMMAND
from napalm_linux._routes import IpRoute, parse_ip_route from napalm_linux._routes import IpRoute, parse_ip_route
from napalm_linux._rpm import source_rpm_name
from napalm_linux.linux import ( from napalm_linux.linux import (
_YUM_SECURITY_COMMAND, _YUM_SECURITY_COMMAND,
LinuxDriver, LinuxDriver,
@@ -2598,11 +2599,17 @@ class TestIpv6NextHops:
# get_packages (rpm) and the installed advisories (NetOrk/netork#2394) # 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 on Oracle Linux 9.8 (aarch64): gmp and openssl carry epoch 1; perf is
# built from the kernel's source rpm; gpg-pubkey has none.
RPM_QA = ( RPM_QA = (
"glibc-common\t2.34-284.0.1.el9_8.4\t1210460\tCommon binaries and locale data for glibc\n" "glibc-common\t2.34-284.0.1.el9_8.4\t1210460\tglibc-2.34-284.0.1.el9_8.4.src.rpm"
"gmp\t1:6.2.0-13.el9\t805146\tA GNU arbitrary precision library\n" "\tCommon binaries and locale data for glibc\n"
"openssl\t1:3.5.8-2.0.1.el9_8\t2106925\tUtilities from the general purpose cryptography library\n" "gmp\t1:6.2.0-13.el9\t805146\tgmp-6.2.0-13.el9.src.rpm\tA GNU arbitrary precision library\n"
"openssl\t1:3.5.8-2.0.1.el9_8\t2106925\topenssl-3.5.8-2.0.1.el9_8.src.rpm"
"\tUtilities from the general purpose cryptography library\n"
"perf\t5.14.0-687.66.1.el9_8\t5401292\tkernel-5.14.0-687.66.1.el9_8.src.rpm"
"\tPerformance monitoring for the Linux kernel\n"
"gpg-pubkey\t8d8b756f-629e59ec\t0\t(none)\tOracle OSS group (Open Source Software group)\n"
) )
@@ -2620,6 +2627,26 @@ class TestRpmPackages:
assert pkgs["glibc-common"]["version"] == "2.34-284.0.1.el9_8.4" assert pkgs["glibc-common"]["version"] == "2.34-284.0.1.el9_8.4"
assert pkgs["glibc-common"]["source"] == "dnf" assert pkgs["glibc-common"]["source"] == "dnf"
assert pkgs["glibc-common"]["size"] == 1210460 assert pkgs["glibc-common"]["size"] == 1210460
assert pkgs["openssl"]["description"] == (
"Utilities from the general purpose cryptography library"
)
def test_each_package_names_the_source_rpm_it_was_built_from(self, driver):
"""perf, python3-perf and bpftool are built from the kernel's source rpm,
and Red Hat lists them in every kernel CVE: netOrk tells them apart from
user space by that name (NetOrk/netork#2394)."""
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 "%{SOURCERPM}" in send.call_args.args[0]
assert pkgs["perf"]["source_rpm"] == "kernel"
assert pkgs["glibc-common"]["source_rpm"] == "glibc"
assert pkgs["openssl"]["source_rpm"] == "openssl"
assert "source_rpm" not in pkgs["gpg-pubkey"]
# Not `source_package`: netOrk reads that as the name a distribution
# files its verdicts under, and Oracle's and Red Hat's name the binary.
assert all("source_package" not in p for p in pkgs.values())
class TestInstalledAdvisoryCves: class TestInstalledAdvisoryCves:
@@ -2654,3 +2681,20 @@ class TestInstalledAdvisoryCves:
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
driver.get_installed_advisory_cves() driver.get_installed_advisory_cves()
class TestSourceRpmName:
@pytest.mark.parametrize(
("sourcerpm", "name"),
[
("kernel-5.14.0-687.66.1.el9_8.src.rpm", "kernel"),
("kernel-uek-6.12.0-104.43.4.3.el9uek.src.rpm", "kernel-uek"),
("python-dateutil-2.8.1-7.el9.src.rpm", "python-dateutil"),
("dummy-1.0-1.nosrc.rpm", "dummy"),
("(none)", None),
("", None),
("broken.src.rpm", None),
],
)
def test_the_name_is_what_precedes_version_and_release(self, sourcerpm, name):
assert source_rpm_name(sourcerpm) == name