From 66b6068ebf755603a4a62e987954961896846cd2 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 26 Nov 2026 23:05:44 +0100 Subject: [PATCH] feat(updates): the update history of an apt host from apt's own log (NetOrk/netork#2728) get_update_history through napalm-device-types' AptHistoryMixin, over run_command as the login user. Another package manager raises NotImplementedError. Requires napalm-device-types 4.47.0. --- CHANGELOG.md | 11 +++++ napalm_linux/_posix.py | 13 +++--- napalm_linux/_update_history.py | 28 ++++++++++++ pyproject.toml | 2 +- tests/test_update_history.py | 78 +++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 napalm_linux/_update_history.py create mode 100644 tests/test_update_history.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cd3e0c..ad0b27f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- `get_update_history(since)` on apt hosts through napalm-device-types' `AptHistoryMixin` + (4.47.0): every transaction in apt's history log and its rotations, newest first, with + what it changed, when, who asked and the error it ended with (NetOrk/netork#2728). Read + over `run_command` as the login user, as root only for a file that user may not read. A + host with another package manager (dnf, yum, apk, pacman) raises `NotImplementedError`: + their histories are not read yet. OpenMediaVault has it through `LinuxDriver`. + +### Changed +- Requires napalm-device-types 4.47.0, where `AptHistoryMixin` is released. + ## [0.19.0] - 2026-11-26 ### Added diff --git a/napalm_linux/_posix.py b/napalm_linux/_posix.py index b311619..0ed2106 100644 --- a/napalm_linux/_posix.py +++ b/napalm_linux/_posix.py @@ -1,8 +1,9 @@ -"""What napalm-device-types implements for any POSIX host over ``run_command``. +"""What napalm-device-types implements for a Linux host over ``run_command``. The driver supplies ``run_command``; the readings, their parses and the writes are -the mixins': scheduled jobs (cron, anacron, systemd timers) and time sync (chrony, -systemd-timesyncd, ``timedatectl``). Their default paths are Debian's, which a +the mixins': scheduled jobs (cron, anacron, systemd timers), time sync (chrony, +systemd-timesyncd, ``timedatectl``), and on an apt host the update history from +apt's own log (``_update_history``). Their default paths are Debian's, which a Linux host shares. """ @@ -10,6 +11,8 @@ from __future__ import annotations from napalm_device_types import PosixScheduledJobsMixin, PosixTimeSyncMixin +from napalm_linux._update_history import LinuxUpdateHistoryMixin -class LinuxPosixMixin(PosixScheduledJobsMixin, PosixTimeSyncMixin): - """Scheduled jobs and time sync of the host, over the driver's ``run_command``.""" + +class LinuxPosixMixin(PosixScheduledJobsMixin, PosixTimeSyncMixin, LinuxUpdateHistoryMixin): + """Scheduled jobs, time sync and the update history, over the driver's ``run_command``.""" diff --git a/napalm_linux/_update_history.py b/napalm_linux/_update_history.py new file mode 100644 index 0000000..8a8a94f --- /dev/null +++ b/napalm_linux/_update_history.py @@ -0,0 +1,28 @@ +"""The host's past package transactions (``UpdateMixin.get_update_history``, NetOrk/netork#2728). + +On an apt host, napalm-device-types' ``AptHistoryMixin``: apt's history log over +``run_command``, as the login user, and as root only for a file the login user +may not read. dnf, yum, apk and pacman keep histories of their own that this +driver does not read yet: ``NotImplementedError``, never an empty list. +""" + +from __future__ import annotations + +from napalm_device_types import AptHistoryMixin, UpdateHistoryDict + + +class LinuxUpdateHistoryMixin(AptHistoryMixin): + """``get_update_history`` on the hosts whose package manager the driver detected as apt.""" + + _pkg_manager: str | None + + def get_update_history(self, since: int | None = None) -> list[UpdateHistoryDict]: + """apt's transactions on the host, newest first, from *since* (unix seconds) on. + + :raises NotImplementedError: on a host whose package manager is not apt. + """ + if self._pkg_manager != "apt": + raise NotImplementedError( + f"No update history is read for package manager {self._pkg_manager!r}" + ) + return super().get_update_history(since) diff --git a/pyproject.toml b/pyproject.toml index f32e43a..bf4955c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,7 @@ classifiers = [ ] dependencies = [ "napalm>=4.0", - "napalm-device-types>=4.45.0", # ServicesRestartMixin + "napalm-device-types>=4.47.0", # AptHistoryMixin (the release after 4.46.0) "netmiko>=4.0.0", "paramiko>=5.0.0", # CVE-2026-44405 ] diff --git a/tests/test_update_history.py b/tests/test_update_history.py new file mode 100644 index 0000000..a03c983 --- /dev/null +++ b/tests/test_update_history.py @@ -0,0 +1,78 @@ +"""The host's past package transactions (``UpdateMixin.get_update_history``, NetOrk/netork#2728). + +On an apt host the reading and its parse are napalm-device-types' ``AptHistoryMixin``: +apt's history log over ``run_command``, as the login user, root only for a file +the login user may not read. Another package manager has no history this driver +reads yet: ``NotImplementedError``, so a caller knows not to ask, not an empty list. +""" + +from __future__ import annotations + +import pytest +from napalm_device_types import APT_HISTORY_COMMAND, CommandResult + +from napalm_linux import LinuxDriver + +REPORT = """\ +### apt-history zone /usr/share/zoneinfo/Etc/UTC +### apt-history offset +0000 +### apt-history file /var/log/apt/history.log + +Start-Date: 2026-11-26 09:00:00 +Commandline: apt-get -y upgrade +Requested-By: admin (1000) +Upgrade: openssh-server:amd64 (1:10.0p1-7, 1:10.0p1-7+deb13u1) +End-Date: 2026-11-26 09:01:00 + +### apt-history status 0 +### apt-history end +""" + + +def _driver(pkg_manager: str | None) -> tuple[LinuxDriver, list[tuple[str, bool]]]: + driver = LinuxDriver("host", "user", "pw", optional_args={"sudo_password": "pw"}) + driver._pkg_manager = pkg_manager + calls: list[tuple[str, bool]] = [] + + def run_command(command, *, privileged=False, timeout=60, stdin=None): # type: ignore[no-untyped-def] + calls.append((command, privileged)) + return CommandResult(REPORT, "", 0) + + driver.run_command = run_command # type: ignore[method-assign] + return driver, calls + + +def test_an_apt_host_reads_apts_log_as_the_login_user(): + driver, calls = _driver("apt") + assert driver.get_update_history() == [ + { + "id": "1795683600-0", + "start": 1795683600, + "end": 1795683660, + "packages": [ + { + "name": "openssh-server", + "action": "upgrade", + "old_version": "1:10.0p1-7", + "new_version": "1:10.0p1-7+deb13u1", + } + ], + "requested_by": "admin", + "command": "apt-get -y upgrade", + "error": None, + } + ] + assert calls == [(APT_HISTORY_COMMAND, False)] + + +def test_since_is_passed_on(): + driver, _ = _driver("apt") + assert driver.get_update_history(since=1795683601) == [] + + +@pytest.mark.parametrize("pkg_manager", ["dnf", "yum", "apk", "pacman", None]) +def test_another_package_manager_has_no_history_here(pkg_manager): + driver, calls = _driver(pkg_manager) + with pytest.raises(NotImplementedError): + driver.get_update_history() + assert calls == []