From b792f6d210f1dd24d02a4f546dae29b6ac78c588 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 26 Nov 2026 18:55:13 +0100 Subject: [PATCH 1/2] feat(services-restart): which services on the node still run replaced code, as root (NetOrk/netork#2750) Both drivers mix in napalm-device-types' ServicesRestartMixin. The command goes over the exec path, root's login as a rule, behind an id -u guard: a login other than root would see only its own processes and read as clean, so it prints nothing and the read raises. A running VM's QEMU is listed as a process; an LXC container's processes are left out by the parse, since a container updates itself. No stdin. --- CHANGELOG.md | 11 +++++ README.md | 3 +- napalm_proxmox/backup_driver.py | 2 + napalm_proxmox/driver.py | 2 + napalm_proxmox/host_mixin.py | 17 +++++--- pyproject.toml | 2 +- tests/test_services_restart.py | 76 +++++++++++++++++++++++++++++++++ 7 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 tests/test_services_restart.py diff --git a/CHANGELOG.md b/CHANGELOG.md index ed7f09d..8ac4de2 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_services_need_restart()` on both drivers (napalm-device-types' `ServicesRestartMixin`, + NetOrk/netork#2750): the services that still run code an update replaced on disk, so they + keep the old library until they restart. One fixed, read-only command over the exec path, + which is root's login: `needs-restarting -s`, else `needrestart` in list-only mode, else the + processes whose `/proc//maps` maps deleted code, each named by its cgroup's service. A + running VM's QEMU (`qemu.slice`) is listed as a process; an LXC container's processes are + left out, since a container updates itself. It runs only when the login is root + (`id -u` is 0): another login gets no report and the read raises, rather than read as + clean what it cannot see. No stdin. + ## [0.13.0] - 2026-11-26 ### Added diff --git a/README.md b/README.md index 6c6a1c3..75bc52b 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ device = driver( | `put_scheduled_job(spec)` / `delete_scheduled_job(key, digest=)` | an application's own job, as a systemd timer | | `get_time_sync` | chrony's sources, the ones DHCP gave apart (`dhcp_ntp_servers`), whether it is synchronised, the zone from `timedatectl`, in one round trip over SSH (napalm-device-types' `PosixTimeSyncMixin`); the zone from `GET /nodes/{node}/time` only where the host names none | | `configure_time_sync(ntp_servers=, timezone=)` | servers to `/etc/chrony/sources.d/netork.sources`, every other configured source commented out (DHCP's `sourcedir` stays), then a reload or a restart of chrony; the zone through `PUT /nodes/{node}/time` (the token needs `Sys.Modify` on `/nodes/{node}`), so `/etc/timezone` and the web UI follow. Only this node: in a cluster each node keeps its own clock | +| `get_services_need_restart` | the services that still run code an update replaced on disk, as root over SSH (napalm-device-types' `ServicesRestartMixin`): `needrestart` in list-only mode where it is installed, else the processes whose `/proc//maps` maps deleted code. A running VM's QEMU is a process (`processes`), an LXC container's processes are left out (the container updates itself). Only as root: another SSH login gets no reading, rather than a clean one | **Not implemented** (raise `NotImplementedError`): `get_bgp_config`, `get_bgp_neighbors`, `get_bgp_neighbors_detail`, @@ -188,7 +189,7 @@ the disks and ZFS pools the server manages itself. | `get_facts`, `get_interfaces`, `get_interfaces_ip`, `get_environment` | the API; host name, DMI and MAC addresses over SSH | | `get_disks`, `get_disk_pools` | `/nodes/localhost/disks/{list,zfs}` | | `get_licenses` | `pbs-subscription` | -| packages, updates, services, kernel, listening sockets, sshd hardening, `reboot_host`, `shutdown_host` | as on Proxmox VE | +| packages, updates, services, kernel, listening sockets, services still running replaced code, sshd hardening, `reboot_host`, `shutdown_host` | as on Proxmox VE | | scheduled jobs | as on Proxmox VE; `proxmox-backup*` units are `platform`. Verify, prune, sync and GC jobs are `get_backup_jobs`, not scheduled jobs | | `get_time_sync`, `configure_time_sync`, `get_ntp_servers` | as on Proxmox VE, the zone through `/nodes/localhost/time` (writing it needs `Sys.Modify` on `/system/time`) | diff --git a/napalm_proxmox/backup_driver.py b/napalm_proxmox/backup_driver.py index 2d0e5d4..8744793 100644 --- a/napalm_proxmox/backup_driver.py +++ b/napalm_proxmox/backup_driver.py @@ -27,6 +27,7 @@ from napalm_device_types import ( OpenSshServerMixin, PortSpec, ServicePackagesMixin, + ServicesRestartMixin, StorageDriver, SystemdServicesMixin, SystemIdentityMixin, @@ -65,6 +66,7 @@ class ProxmoxBackupDriver( ServicePackagesMixin, ListeningSocketsMixin, LogSourcesMixin, + ServicesRestartMixin, SystemIdentityMixin, SystemdServicesMixin, ProxmoxScheduledJobsMixin, diff --git a/napalm_proxmox/driver.py b/napalm_proxmox/driver.py index 1b61ede..bcd9a5e 100644 --- a/napalm_proxmox/driver.py +++ b/napalm_proxmox/driver.py @@ -38,6 +38,7 @@ from napalm_device_types import ( OpenSshServerMixin, PortSpec, ServicePackagesMixin, + ServicesRestartMixin, SnmpAgentMixin, SystemdServicesMixin, SystemIdentityMixin, @@ -98,6 +99,7 @@ class ProxmoxDriver( ServicePackagesMixin, ListeningSocketsMixin, LogSourcesMixin, + ServicesRestartMixin, SystemIdentityMixin, SystemdServicesMixin, ProxmoxScheduledJobsMixin, diff --git a/napalm_proxmox/host_mixin.py b/napalm_proxmox/host_mixin.py index c0466d5..55d4064 100644 --- a/napalm_proxmox/host_mixin.py +++ b/napalm_proxmox/host_mixin.py @@ -1,11 +1,11 @@ """What a Proxmox VE node and a Proxmox Backup Server share as Debian hosts. The transports for napalm-device-types' host readers (kernel facts, service -packages, system identity, listening sockets, systemd services, host status, -apt's proxy), packages and APT updates, the subscription, root file writes, -and restarting or shutting the host down. All of it is the same on both -products: the same ``/nodes/{node}/apt``, ``/subscription`` and ``/status`` -endpoints, and root's shell over the driver's own SSH session. +packages, system identity, listening sockets, the services that still run replaced +code, systemd services, host status, apt's proxy), packages and APT updates, the +subscription, root file writes, and restarting or shutting the host down. All of it +is the same on both products: the same ``/nodes/{node}/apt``, ``/subscription`` and +``/status`` endpoints, and root's shell over the driver's own SSH session. """ from __future__ import annotations @@ -91,6 +91,13 @@ class ProxmoxHostMixin: which runs as root either way.""" return str(self._exec_ssh_command(command)) + def _run_services_restart_command(self, command: str) -> str: + """The transport for ``ServicesRestartMixin.get_services_need_restart``: the exec + path, which is root's login as a rule. Run only as root: a reading of the login's + own processes would read as clean, so another login prints nothing and the parse + raises (NetOrk/netork#2750). No stdin.""" + return str(self._exec_ssh_command(f'[ "$(id -u)" = 0 ] && {command}')) + def get_packages(self) -> list[PackageDict]: """Return installed Debian packages with available-update info. diff --git a/pyproject.toml b/pyproject.toml index ccffe74..006249e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ classifiers = [ requires-python = ">=3.12" dependencies = [ "napalm>=5.0.0", - "napalm_device_types>=4.43.0", # ZabbixAgentMixin, opened_ports + "napalm_device_types>=4.44.0", # ServicesRestartMixin (NAPALM/napalm-device-types#132) "paramiko>=5.0.0", # CVE-2026-44405; imported directly for the node's SSH session (_base.py) "proxmoxer>=2.0.0", "netaddr>=0.9.0", diff --git a/tests/test_services_restart.py b/tests/test_services_restart.py new file mode 100644 index 0000000..b5964ed --- /dev/null +++ b/tests/test_services_restart.py @@ -0,0 +1,76 @@ +"""`get_services_need_restart`: the services on the node that still run replaced code. + +An openssl or glibc update replaces the files on disk; every daemon that had them +loaded keeps the old code until it restarts (NetOrk/netork#2750). On a hypervisor that +is where a restart instead of a reboot matters most. The command and its parse are +napalm-device-types' (``ServicesRestartMixin``); the driver only carries the command +over its exec path, which runs as root. A login that is not root gets no reading: it +would see only its own processes and read as clean. +""" + +from __future__ import annotations + +import subprocess +from unittest.mock import patch + +import pytest +from napalm_device_types import ServicesRestartMixin + +from napalm_proxmox import ProxmoxBackupDriver +from napalm_proxmox.driver import ProxmoxDriver + +#: A PVE node after an openssl update, read by the /proc scan (no needrestart): +#: pveproxy and a running VM's QEMU still map the old library, and so does a +#: container's own nginx, which the container updates itself. +WIRE = ( + "RST_BEGIN\n[maps]\n" + "2101 pveproxy worker\n" + "3100 kvm\n" + "4100 nginx\n" + "[cgroups]\n" + "2101 0::/system.slice/pveproxy.service\n" + "3100 0::/qemu.slice/101.scope\n" + "4100 0::/lxc/200/ns/system.slice/nginx.service\n" + "RST_END\n" +) + + +@pytest.mark.parametrize("driver_class", [ProxmoxDriver, ProxmoxBackupDriver]) +def test_both_drivers_declare_the_contract(driver_class): + assert issubclass(driver_class, ServicesRestartMixin) + + +def test_it_reads_as_root_over_the_exec_path(driver): + with patch.object(driver, "_exec_ssh_command", return_value=WIRE) as exec_: + reading = driver.get_services_need_restart() + + [command] = [c.args[0] for c in exec_.call_args_list] + assert command.startswith('[ "$(id -u)" = 0 ] && sh -c \'') + assert reading == { + "source": "maps", + "units": ["pveproxy"], + "processes": [{"pid": 3100, "name": "kvm"}], + } + + +def test_the_backup_server_reads_it_the_same_way(): + drv = ProxmoxBackupDriver("192.0.2.80", "root", "", optional_args={"token_name": "a!b"}) + wire = "RST_BEGIN\n[needrestart]\nNEEDRESTART-SVC: proxmox-backup-proxy.service\nRST_END\n" + + with patch.object(drv, "_exec_ssh_command", return_value=wire): + reading = drv.get_services_need_restart() + + assert reading["units"] == ["proxmox-backup-proxy"] + + +def test_a_login_other_than_root_gets_no_reading(driver): + """The guard runs nothing but as root, so no report comes back and the parse raises.""" + + def as_this_user(command: str) -> str: + return subprocess.run(["sh", "-c", command], capture_output=True, text=True).stdout + + if subprocess.run(["id", "-u"], capture_output=True, text=True).stdout.strip() == "0": + pytest.skip("runs as root here") + with patch.object(driver, "_exec_ssh_command", side_effect=as_this_user): + with pytest.raises(ValueError): + driver.get_services_need_restart() From 63ffc64050e1a325481089828af11eac3522b2b6 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 26 Nov 2026 18:55:13 +0100 Subject: [PATCH 2/2] chore(release): 0.16.0 --- CHANGELOG.md | 2 ++ pyproject.toml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ac4de2..3cbece5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.16.0] - 2026-11-26 + ### Added - `get_services_need_restart()` on both drivers (napalm-device-types' `ServicesRestartMixin`, NetOrk/netork#2750): the services that still run code an update replaced on disk, so they diff --git a/pyproject.toml b/pyproject.toml index 006249e..d4fe3c1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "napalm-proxmox" -version = "0.13.0" +version = "0.16.0" description = "NAPALM drivers for Proxmox VE (classic Linux networking, SDN, OVS) and Proxmox Backup Server" readme = "README.md" license = "Apache-2.0"