Compare commits

...
7 Commits
Author SHA1 Message Date
christianmanivong 5258a43dc9 Merge pull request 'feat(updates): the CVEs a host's installed advisories close (4.37.0)' (#111) from feature/installed-advisories into main
driver / types (push) Successful in 59s
driver / standard (push) Successful in 15s
driver / lint (push) Successful in 13s
driver / test (3.12) (push) Successful in 1m12s
driver / test (3.13) (push) Successful in 1m16s
driver / lowest (push) Successful in 39s
driver / build (push) Successful in 28s
driver / security (push) Successful in 1m6s
driver / publish (push) Successful in 28s
CI / driver (push) Successful in 6m6s
2026-11-12 11:12:53 +00:00
christianmanivong f3cb3601a8 feat(updates): the CVEs a host's installed advisories close (4.37.0)
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m2s
driver / test (3.13) (pull_request) Successful in 1m11s
driver / test (3.12) (pull_request) Successful in 1m24s
driver / lowest (pull_request) Successful in 40s
driver / build (pull_request) Successful in 28s
driver / security (pull_request) Successful in 1m0s
CI / driver (pull_request) Successful in 2m29s
driver / publish (pull_request) Skipped
UpdateMixin declares get_installed_advisory_cves(): per package, the CVEs
the installed security advisories close -- the distribution's word that a
backport is in although the upstream version did not move. The command
(DNF_INSTALLED_CVES_COMMAND, dnf 4, from the cache) and its parser
(parse_dnf_installed_cves) live here for every dnf driver, as the
security read's do. netOrk reads it where its knowledge base holds no
records of the host's release (NetOrk/netork#2394).
2026-11-12 12:09:37 +01:00
christianmanivong a8fedf299a Merge pull request 'fix(host-status): kernel releases compare the way rpm does (4.36.2)' (#110) from fix/rpm-kernel-version-order into main
driver / types (push) Successful in 1m4s
driver / standard (push) Successful in 15s
driver / lint (push) Successful in 14s
driver / test (3.12) (push) Successful in 1m20s
driver / test (3.13) (push) Successful in 1m13s
driver / lowest (push) Successful in 46s
driver / build (push) Successful in 40s
driver / security (push) Successful in 1m16s
driver / publish (push) Successful in 30s
CI / driver (push) Successful in 3m36s
2026-11-12 10:08:48 +00:00
christianmanivong c736fb30d0 refactor(host-status): compare one version segment in a helper of its own
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 56s
driver / test (3.12) (pull_request) Successful in 1m8s
driver / test (3.13) (pull_request) Successful in 1m8s
driver / lowest (pull_request) Successful in 40s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 57s
CI / driver (pull_request) Successful in 2m30s
driver / publish (pull_request) Skipped
The quality ratchet refused _version_cmp at complexity 12. Comparing one
segment moves to _segment_cmp, which also takes 01 and 1 as equal, as
rpm does.
2026-11-12 11:05:49 +01:00
christianmanivong cf555d006d fix(host-status): kernel releases compare the way rpm does (4.36.2)
driver / lint (pull_request) Successful in 13s
driver / standard (pull_request) Failing after 15s
driver / types (pull_request) Successful in 1m5s
driver / test (3.13) (pull_request) Successful in 1m14s
driver / test (3.12) (pull_request) Successful in 1m19s
driver / lowest (pull_request) Successful in 42s
driver / build (pull_request) Successful in 28s
driver / security (pull_request) Successful in 1m3s
CI / driver (pull_request) Failing after 2m32s
driver / publish (pull_request) Skipped
_version_key compared the text between numbers as plain strings, so
'.el' outranked '.' and 6.12.0-208.112.5.el9uek counted as newer than
the running 6.12.0-208.112.5.1.el9uek: an Oracle Linux host asked to
reboot into an older kernel, and a reboot never cleared it. The
releases now compare like rpmvercmp: separators only separate, a
number is newer than letters in its place, and '~' comes before
anything. Checked against rpm.labelCompare on Oracle Linux 9.

Closes #109
2026-11-12 11:03:46 +01:00
christianmanivong 4e79cd1fc8 Merge pull request 'fix(host-status): an Oracle Linux host's UEK kernels are not an interrupted dnf (4.36.1)' (#108) from fix/dnf-installonly-defaults into main
driver / types (push) Successful in 1m0s
driver / standard (push) Successful in 16s
driver / lint (push) Successful in 13s
driver / test (3.12) (push) Successful in 1m17s
driver / test (3.13) (push) Successful in 1m12s
driver / lowest (push) Successful in 41s
driver / build (push) Successful in 30s
driver / security (push) Successful in 1m3s
driver / publish (push) Successful in 30s
CI / driver (push) Successful in 3m21s
2026-11-12 09:37:34 +00:00
christianmanivong 304f404567 fix(host-status): an Oracle Linux host's UEK kernels are not an interrupted dnf (4.36.1)
driver / lint (pull_request) Successful in 16s
driver / standard (pull_request) Successful in 18s
driver / types (pull_request) Successful in 59s
driver / test (3.13) (pull_request) Successful in 1m12s
driver / test (3.12) (pull_request) Successful in 1m17s
driver / lowest (pull_request) Successful in 41s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 56s
CI / driver (pull_request) Successful in 2m26s
driver / publish (pull_request) Skipped
The installonly packages were dnf's upstream defaults plus dnf.conf's.
Oracle builds kernel-uek and its parts into libdnf's defaults, and the
UEK packages provide nothing of upstream's list, so every host with more
than one UEK kernel was reported as left halfway. The defaults are now
also asked of the host's libdnf through its Python binding; upstream's
list stays the floor for dnf 5 and yum.

Closes #107
2026-11-12 10:30:48 +01:00
9 changed files with 273 additions and 14 deletions

No files matched your search

+25
View File
@@ -7,6 +7,31 @@ recorded here; their history is the git log.
## [Unreleased]
## [4.37.0] - 2026-11-12
### Added
- `UpdateMixin.get_installed_advisory_cves()` (declared): the CVEs a host's installed
security advisories close, per package. With `DNF_INSTALLED_CVES_COMMAND` (dnf 4,
read from the cache) and `parse_dnf_installed_cves()`, the parser every dnf driver
shares. netOrk reads it where its knowledge base holds no records of the host's
release, as the distribution's word that a backport is in (netOrk #2394).
## [4.36.2] - 2026-11-12
### Fixed
- `kernel_reboot_pending` compares kernel releases the way rpm does (`rpmvercmp`). A
number is newer than letters in the same place, so an Oracle Linux host running
`6.12.0-208.112.5.1.el9uek` no longer asks to reboot into the older
`6.12.0-208.112.5.el9uek`. A `~` (release candidate) comes before its release (#109).
## [4.36.1] - 2026-11-12
### Fixed
- The host status no longer reports an Oracle Linux host's UEK kernels as an interrupted
dnf transaction. The installonly packages are now also the ones the host's libdnf was
built with, read from its Python binding: Oracle adds `kernel-uek` and its parts there,
and their packages provide nothing of upstream's defaults (#107).
## [4.36.0] - 2026-11-11
### Added
+4
View File
@@ -217,8 +217,10 @@ from napalm_device_types.os import OSDriver
from napalm_device_types.ospf import OspfMixin
from napalm_device_types.package_updates import (
APT_UPGRADABLE_COMMAND,
DNF_INSTALLED_CVES_COMMAND,
DNF_SECURITY_COMMAND,
parse_apt_upgradable,
parse_dnf_installed_cves,
parse_dnf_security,
)
from napalm_device_types.packages import PackageManagementMixin
@@ -432,8 +434,10 @@ __all__ = [
"OSDriver",
"PackageManagementMixin",
"APT_UPGRADABLE_COMMAND",
"DNF_INSTALLED_CVES_COMMAND",
"DNF_SECURITY_COMMAND",
"parse_apt_upgradable",
"parse_dnf_installed_cves",
"parse_dnf_security",
"parse_host_status",
"strip_terminal_codes",
+37 -5
View File
@@ -60,6 +60,7 @@ too full is not decided here.
from __future__ import annotations
import re
from functools import cmp_to_key
from typing import TYPE_CHECKING
from napalm_device_types.models import (
@@ -171,9 +172,39 @@ def _sections(output: str) -> dict[str, list[str]]:
return sections
def _version_key(version: str) -> tuple[object, ...]:
"""Natural order: 6.8.0-142 after 6.8.0-87, 7.0.14 after 7.0.2."""
return tuple(int(part) if part.isdigit() else part for part in re.split(r"(\d+)", version))
#: A version's segments as rpm compares them: runs of digits, runs of letters, and
#: ``~``; whatever else stands between them only separates.
_VERSION_SEGMENT = re.compile(r"~|\d+|[A-Za-z]+")
def _segment_cmp(x: str, y: str) -> int:
"""One segment against another, as :func:`_version_cmp` takes them; 0 when equal."""
if "~" in (x, y):
return (y == "~") - (x == "~")
if x.isdigit() and y.isdigit():
return (int(x) > int(y)) - (int(x) < int(y))
if x.isdigit() or y.isdigit():
return 1 if x.isdigit() else -1
return (x > y) - (x < y)
def _version_cmp(a: str, b: str) -> int:
"""rpm's ``rpmvercmp``: below 0 when ``a`` is older, above when newer.
Numbers compare as numbers (6.8.0-142 after 6.8.0-87), letters as text, and a
number is newer than letters in its place: ``112.5.1.el9uek`` after
``112.5.el9uek`` (#109). A version that has more segments where the other
ends is the newer one, unless its next is ``~``, which comes before anything:
``6.13~rc5`` before ``6.13``, as dpkg sorts it too.
"""
left, right = _VERSION_SEGMENT.findall(a), _VERSION_SEGMENT.findall(b)
for x, y in zip(left, right, strict=False):
if order := _segment_cmp(x, y):
return order
if len(left) == len(right):
return 0
longer, sign = (left, 1) if len(left) > len(right) else (right, -1)
return -sign if longer[min(len(left), len(right))] == "~" else sign
def kernel_reboot_pending(running: str, installed: list[str]) -> str | None:
@@ -188,8 +219,9 @@ def kernel_reboot_pending(running: str, installed: list[str]) -> str | None:
same = [k for k in installed if _kernel_flavour(k)[1] == flavour]
if not same:
return None
newest = max(same, key=lambda k: _version_key(_kernel_flavour(k)[0]))
if _version_key(_kernel_flavour(newest)[0]) > _version_key(version):
order = cmp_to_key(_version_cmp)
newest = max(same, key=lambda k: order(_kernel_flavour(k)[0]))
if order(_kernel_flavour(newest)[0]) > order(version):
return newest
return None
+19 -6
View File
@@ -13,9 +13,13 @@ what ``dnf check --duplicates`` and ``package-cleanup --dupes`` report. Packages
dnf installs side by side on purpose do not count: its default ``installonlypkgs``
(``kernel``, ``installonlypkg(kernel)`` and the rest), plus any the host adds in
``dnf.conf``, asked of rpm by what they provide; and ``gpg-pubkey``, one entry
per imported key. Also interrupted is a transaction that never ended: dnf 4's
history (libdnf's ``trans`` table) holds its row with ``state`` 0 until
``finish()`` writes 1 (done) or 2 (error), so the newest row at 0 is one cut off;
per imported key. The defaults are the ones the host's libdnf was built with, asked
of its Python binding, as a distribution may add its own: Oracle Linux adds
``kernel-uek`` and its parts, whose packages provide nothing of upstream's list.
Upstream's list is the floor where libdnf cannot be asked (dnf 5, yum). Also
interrupted is a transaction that never ended: dnf 4's history (libdnf's ``trans``
table) holds its row with ``state`` 0 until ``finish()`` writes 1 (done) or 2
(error), so the newest row at 0 is one cut off;
yum leaves a ``transaction-all.*`` journal in ``/var/lib/yum``, which
``yum-complete-transaction`` reads (one renamed ``.disabled`` is given up). The
history is read with Python's sqlite3 module in read-only mode, as a stock EL host
@@ -81,6 +85,13 @@ _DNF_HISTORY_PY = (
"r = c.execute('SELECT id, state FROM trans ORDER BY id DESC LIMIT 1').fetchone(); "
"r and print(*r)"
)
#: The ``installonlypkgs`` the host's libdnf (dnf 4) was built with, space-separated;
#: nothing where there is no libdnf. ``ConfigMain`` is held in a name: an option
#: taken from a temporary one outlives it, and reading it then crashes Python.
_LIBDNF_INSTALLONLY_PY = (
"import libdnf.conf; c = libdnf.conf.ConfigMain(); "
"print(c.installonlypkgs().getValueString().replace(',', ' '))"
)
_RUNNING_PART = (
"echo '[pm-running]'; cat /proc/[0-9]*/comm 2>/dev/null | grep -xE '"
@@ -90,7 +101,10 @@ _RUNNING_PART = (
_RPM_PART = (
"if command -v rpm >/dev/null 2>&1 && { command -v dnf || command -v yum; } >/dev/null 2>&1; "
"then echo '[rpm-frontend]'; { command -v dnf || command -v yum; } | sed 's#.*/##'; "
"rpmpy=; for p in /usr/libexec/platform-python python3; do "
"if command -v $p >/dev/null 2>&1; then rpmpy=$p; break; fi; done; "
f"echo '[rpm-installonly]'; io=\"{' '.join(_INSTALLONLY)} "
f'$([ -n "$rpmpy" ] && $rpmpy -c "{_LIBDNF_INSTALLONLY_PY}" 2>/dev/null) '
f"$(sed -n 's/^[[:space:]]*installonlypkgs[[:space:]]*=//p' {_DNF_CONF} 2>/dev/null "
"| tr ',' ' ')\"; rpm -q --whatprovides $io --qf '%{NAME}\\n' 2>/dev/null "
"| grep -v ' ' | sort -u; "
@@ -98,9 +112,8 @@ _RPM_PART = (
"| grep -v '^gpg-pubkey ' | sort | uniq -d | head -n 50; "
f"echo '[yum-journal]'; ls -1 {YUM_DIR} 2>/dev/null | grep '^transaction-all\\.' "
"| head -n 20; "
"echo '[dnf-history]'; for py in /usr/libexec/platform-python python3; do "
f'if command -v $py >/dev/null 2>&1; then $py -c "{_DNF_HISTORY_PY}" 2>/dev/null; '
"break; fi; done; fi; "
"echo '[dnf-history]'; "
f'[ -n "$rpmpy" ] && $rpmpy -c "{_DNF_HISTORY_PY}" 2>/dev/null; fi; '
)
_APK_PART = (
f"if command -v apk >/dev/null 2>&1 && [ -r {APK_INSTALLED} ]; then echo '[apk-audit]'; "
+31
View File
@@ -36,11 +36,24 @@ DNF_SECURITY_COMMAND = (
" echo __DNF_RC=$?; } | cat"
)
#: Read-only. Every advisory whose fix the installed packages carry, one line per
#: CVE it names (``CVE SEVERITY/Sec. NEVRA``): the distribution's own word that a
#: backport is in, where netOrk's knowledge base holds no records of the release
#: (netOrk #2394). dnf 4 only: dnf 5 reads ``--with-cve`` as a filter, and
#: Fedora's advisories reference bugs rather than CVEs. From the cache, as
#: :data:`DNF_SECURITY_COMMAND` reads it; the output is long (90 000 lines on an
#: Oracle Linux 9 host), so a caller reads it seldom.
DNF_INSTALLED_CVES_COMMAND = (
"{ LC_ALL=C dnf --cacheonly updateinfo list --installed --with-cve --quiet 2>/dev/null;"
" echo __DNF_RC=$?; } | cat"
)
# openssl/noble-updates,noble-security 3.0.13-0ubuntu3.6 amd64 [upgradable from: 3.0.13-0ubuntu3.5]
_APT_LINE = re.compile(r"^(\S+)/(\S+)\s+(\S+)\s+\S+\s+\[upgradable from:\s+(\S+)\]")
_SECURITY_SUITE = "-security"
_APT_STATUS = re.compile(r"^__APT_RC=(\d+)\s*$", re.MULTILINE)
_DNF_STATUS = re.compile(r"^__DNF_RC=(\d+)\s*$", re.MULTILINE)
_CVE_ID = re.compile(r"^CVE-\d{4}-\d{4,}$")
def _joined_lines(output: str) -> list[str]:
@@ -118,3 +131,21 @@ def parse_dnf_security(output: str) -> set[str]:
if len(parts) >= 3 and parts[1].endswith("/Sec."):
names.add(nevra_name(parts[-1]))
return names
def parse_dnf_installed_cves(output: str) -> dict[str, list[str]]:
"""The CVEs the installed advisories close, per package name, each once and sorted.
Parses :data:`DNF_INSTALLED_CVES_COMMAND`'s ``CVE SEVERITY/Sec. NEVRA`` lines;
a line that names no CVE (a key prompt, an advisory id) is no statement.
:raises ValueError: when dnf failed or its exit status never arrived -- "could
not read" must not look like "no advisory installed".
"""
by_name: dict[str, set[str]] = {}
listing = _listing(output, _DNF_STATUS, "dnf updateinfo list --installed --with-cve")
for line in listing.splitlines():
parts = line.split()
if len(parts) >= 3 and _CVE_ID.match(parts[0]):
by_name.setdefault(nevra_name(parts[-1]), set()).add(parts[0])
return {name: sorted(cves) for name, cves in by_name.items()}
+22
View File
@@ -50,6 +50,28 @@ class UpdateMixin:
"""
...
def get_installed_advisory_cves(self) -> dict[str, list[str]]:
"""
The CVEs the host's installed security advisories close, per package
name: the distribution's own word that a fix is in the installed build,
although its upstream version did not move (a backport).
netOrk asks for it only where its knowledge base holds no records of
the host's release (netOrk #2394), since the answer can be long. A
package manager whose advisories name no CVEs has nothing to say here.
**An empty dict means no installed advisory names a CVE.** A reader
that cannot read raises, as :meth:`get_available_updates` does.
``NotImplementedError`` where the host's package manager keeps no such
advisories (:data:`~napalm_device_types.package_updates.DNF_INSTALLED_CVES_COMMAND`
is dnf 4's).
Example::
{"glibc-common": ["CVE-2023-4911"], "curl": ["CVE-2023-38545"]}
"""
...
def refresh_available_updates(self) -> dict[str, Any]:
"""
Refreshes the host's package index, so that :meth:`get_available_updates`
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "napalm-device-types"
version = "4.36.0"
version = "4.37.0"
description = "Abstract device-type base classes for NAPALM drivers"
readme = "README.md"
requires-python = ">=3.12"
+60 -2
View File
@@ -154,6 +154,24 @@ class TestKernelRebootPending:
"5.14.0-503.11.1.el9_5.x86_64",
),
("6.9.7-200.fc40.x86_64", ["6.9.7-200.fc40.x86_64"], None),
# Oracle UEK: 112.5.1 is newer than 112.5, as rpm counts -- a number after
# 5 beats the letters ``el`` (#109, build-arm-01).
(
"6.12.0-208.112.5.1.el9uek.aarch64",
[
"6.12.0-207.111.5.6.el9uek.aarch64",
"6.12.0-208.112.5.el9uek.aarch64",
"6.12.0-208.112.5.1.el9uek.aarch64",
],
None,
),
(
"6.12.0-208.112.5.el9uek.aarch64",
["6.12.0-208.112.5.el9uek.aarch64", "6.12.0-208.112.5.1.el9uek.aarch64"],
"6.12.0-208.112.5.1.el9uek.aarch64",
),
# A release candidate (``~``) comes before its release, in rpm and in dpkg.
("6.13~rc5-1-amd64", ["6.13~rc5-1-amd64", "6.13-1-amd64"], "6.13-1-amd64"),
],
)
def test_the_newer_kernel_of_the_running_flavour(self, running, installed, newer):
@@ -598,8 +616,8 @@ class TestInterruptedRpm:
The outputs are shaped after the documented formats -- ``rpm -qa --qf``,
libdnf's ``trans`` table (``state`` 0 = UNKNOWN until ``finish()`` sets DONE
1 or ERROR 2), yum's ``transaction-all.*`` journal that
``yum-complete-transaction`` reads -- and still have to be checked against a
real Oracle Linux 9 host.
``yum-complete-transaction`` reads. Checked against Oracle Linux 9 (build-arm-01,
2026-11-12): its UEK kernels are installonly only by libdnf's built-in defaults.
"""
INSTALLONLY = ("kernel-core", "kernel-modules", "kernel-uek-core")
@@ -804,6 +822,29 @@ case "$*" in
esac
"""
#: An rpm on an Oracle Linux host: its UEK kernel ``vendor-kernel`` provides nothing
#: of dnf's upstream defaults and sits in the database three times, like glibc twice.
_RPM_VENDOR_KERNEL_STUB = """#!/bin/sh
case "$*" in
*--whatprovides*) for a; do [ "$a" = vendor-kernel ] && echo vendor-kernel; done
echo 'no package provides kernel-PAE' ;;
-qa*) printf '%s\\n' 'vendor-kernel aarch64' 'vendor-kernel aarch64' 'vendor-kernel aarch64' \\
'glibc aarch64' 'glibc aarch64' ;;
esac
"""
#: libdnf's Python binding, as far as the command asks it: the installonlypkgs a
#: distribution built into its libdnf -- here one more than upstream's.
_LIBDNF_CONF = """class _Option:
def getValueString(self):
return "kernel, installonlypkg(kernel), vendor-kernel"
class ConfigMain:
def installonlypkgs(self):
return _Option()
"""
#: /lib/apk/db/installed shaped after apk-tools' database.c: busybox's script failed.
_APK_DB = """C:Q1abc=
P:alpine-baselayout
@@ -886,6 +927,23 @@ class TestThePackageManagerCommandOnARealShell:
assert status["package_manager"] == "dnf"
assert status["interrupted_packages"] == [{"name": "glibc", "state": "duplicate"}]
def test_the_installonly_packages_libdnf_was_built_with(self, tmp_path, shell):
"""Oracle builds kernel-uek into libdnf's defaults; its UEK packages provide
nothing of upstream's, so only libdnf itself tells (#107)."""
_skip_busybox_rpm(shell)
env = _stubs(tmp_path, "rpm", "dnf")
(tmp_path / "bin" / "rpm").write_text(_RPM_VENDOR_KERNEL_STUB)
libdnf = tmp_path / "py" / "libdnf"
libdnf.mkdir(parents=True)
(libdnf / "__init__.py").write_text("")
(libdnf / "conf.py").write_text(_LIBDNF_CONF)
env["PYTHONPATH"] = str(tmp_path / "py")
out = _run(PACKAGE_MANAGERS_COMMAND, env, shell)
status = parse_host_status(out)
assert status["interrupted_packages"] == [{"name": "glibc", "state": "duplicate"}]
def test_dnfs_last_transaction_that_never_ended(self, tmp_path, shell):
_skip_busybox_rpm(shell)
history = tmp_path / "history.sqlite"
+74
View File
@@ -18,9 +18,11 @@ import pytest
from napalm_device_types.package_updates import (
APT_UPGRADABLE_COMMAND,
DNF_INSTALLED_CVES_COMMAND,
DNF_SECURITY_COMMAND,
nevra_name,
parse_apt_upgradable,
parse_dnf_installed_cves,
parse_dnf_security,
)
@@ -198,3 +200,75 @@ __DNF_RC=0
)
def test_the_name_of_a_nevra(self, nevra, name):
assert nevra_name(nevra) == name
class TestDnfInstalledCves:
"""The CVEs the host's installed advisories close, per package (netOrk #2394).
Where netOrk's knowledge base holds no records of a release, the host's own
advisories are the distribution's word that a backport is in. Lines from real
hosts: Oracle Linux 9.8 (dnf 4.14, aarch64) and Amazon Linux 2023 (dnf 4.14).
"""
ORACLE = """\
CVE-2024-6501 Low/Sec. NetworkManager-1:1.48.10-2.0.1.el9_5.aarch64
CVE-2024-3661 Moderate/Sec. NetworkManager-1:1.48.10-5.0.1.el9_5.aarch64
CVE-2023-38545 Important/Sec. curl-7.76.1-23.el9_2.4.aarch64
CVE-2023-38545 Important/Sec. curl-7.76.1-26.el9_3.2.aarch64
CVE-2023-4911 Important/Sec. glibc-common-2.34-60.0.3.el9.aarch64
CVE-2023-4911 Important/Sec. glibc-common-2.34-60.0.3.el9_2.7.aarch64
__DNF_RC=0
"""
AMAZON = """\
CVE-2022-3715 Medium/Sec. bash-5.2.15-1.amzn2023.0.2.x86_64
CVE-2022-23491 Important/Sec. ca-certificates-2021.2.50-1.0.amzn2023.0.6.noarch
CVE-2023-32803 Important/Sec. ca-certificates-2023.2.60-1.0.amzn2023.0.2.noarch
__DNF_RC=0
"""
def test_cves_per_package_once_each_and_sorted(self):
assert parse_dnf_installed_cves(self.ORACLE) == {
"NetworkManager": ["CVE-2024-3661", "CVE-2024-6501"],
"curl": ["CVE-2023-38545"],
"glibc-common": ["CVE-2023-4911"],
}
def test_amazon_linux(self):
assert parse_dnf_installed_cves(self.AMAZON) == {
"bash": ["CVE-2022-3715"],
"ca-certificates": ["CVE-2022-23491", "CVE-2023-32803"],
}
def test_lines_that_name_no_cve_are_left_out(self):
"""A GPG prompt a repository without its key prints, an advisory id."""
out = (
"Importing GPG key 0x6E93CD0C:\n"
' Userid : "https://packagecloud.io/crowdsec/crowdsec"\n'
"ELSA-2023-5453 Important/Sec. glibc-2.34-60.0.3.el9_2.7.aarch64\n"
"__DNF_RC=0\n"
)
assert parse_dnf_installed_cves(out) == {}
def test_a_failed_dnf_raises_instead_of_reading_as_no_advisories(self):
with pytest.raises(ValueError, match="exit status 1"):
parse_dnf_installed_cves("__DNF_RC=1\n")
def test_output_cut_short_raises(self):
with pytest.raises(ValueError, match="no exit status"):
parse_dnf_installed_cves(self.ORACLE.replace("__DNF_RC=0\n", ""))
def test_the_command_lists_installed_advisories_with_their_cves_from_the_cache(self, tmp_path):
args = tmp_path / "args"
dnf = tmp_path / "dnf"
dnf.write_text(f"#!/bin/sh\nprintf '%s\\n' \"$@\" > {args}\n")
dnf.chmod(0o755)
env = {"PATH": f"{tmp_path}:{os.environ.get('PATH', '/usr/bin:/bin')}"}
out = subprocess.run(
["sh", "-c", DNF_INSTALLED_CVES_COMMAND], capture_output=True, text=True, env=env
).stdout
assert {"--cacheonly", "updateinfo", "list", "--installed", "--with-cve"} <= set(
args.read_text().split()
)
assert out == "__DNF_RC=0\n"