From 2b153cd61c71881d0a4226aef6e25299a727296a Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 5 Nov 2026 00:24:32 +0100 Subject: [PATCH] feat(log-sources): which logs the host writes, through LogSourcesMixin The driver mixes in napalm-device-types' LogSourcesMixin (4.30.0) and carries its command: as root through sudo where it can, else what the login user may see. OpenMediaVault and QNAP inherit it. Refs NetOrk/netork#2049 --- CHANGELOG.md | 4 ++++ napalm_linux/linux.py | 11 +++++++++ pyproject.toml | 2 +- tests/test_linux.py | 52 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2454173..7fe28a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- `get_log_sources()` through napalm-device-types' `LogSourcesMixin` (4.30.0): the units + journald holds entries for and the log files running processes hold open, with the service + or container writing each, as root, and without root what the login user may see + (NetOrk/netork#2049). OpenMediaVault and QNAP inherit it. - A deleted key can take the password with it (NetOrk/netork#2033): through napalm-device-types' `remove_local_account_keys(..., clear_password=True)` the keys go as the person, the password goes (`usermod -p '*'`), and so do the sudo rule and the diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index 633613a..2cd4556 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -40,6 +40,7 @@ from napalm_device_types import ( KernelFactsMixin, ListeningSocketsMixin, LldpNeighborDict, + LogSourcesMixin, OpenSshServerMixin, OSDriver, PosixScheduledJobsMixin, @@ -304,6 +305,7 @@ class LinuxDriver( KernelFactsMixin, ServicePackagesMixin, ListeningSocketsMixin, + LogSourcesMixin, HostFirewallMixin, VirtualIpsMixin, ServiceClustersMixin, @@ -1098,6 +1100,15 @@ class LinuxDriver( return self._run_privileged(command, 60) return self._send(command, read_timeout=60) + def _run_log_sources_command(self, command: str, *, privileged: bool) -> str: + """The transport for ``LogSourcesMixin.get_log_sources``. + + Read-only either way; root only so that every process shows its open files. + """ + if privileged: + return self._run_privileged(command, 60) + return self._send(command, read_timeout=60) + def _run_host_firewall_command(self, command: str, *, privileged: bool) -> str: """The transport for ``HostFirewallMixin.get_host_firewall``: an exec channel. diff --git a/pyproject.toml b/pyproject.toml index 6185536..2599dfb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,7 @@ classifiers = [ ] dependencies = [ "napalm>=4.0", - "napalm-device-types>=4.29.0", + "napalm-device-types>=4.30.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 7e288ca..f375777 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -1955,6 +1955,58 @@ class TestGetListeningSockets: assert plain.startswith("sh -c '") +# --------------------------------------------------------------------------- +# Log sources: journald units and the log files processes hold open (#2049) +# --------------------------------------------------------------------------- + +_LOGS = ( + "LOGS_BEGIN\n[journald]\nnginx.service\n" + "[open]\n/proc/900/fd/5 /var/log/nginx/access.log\n" + "[stat]\n/proc/900/fd/5 2048 1762250200\n" + "[cgroups]\n900 0::/system.slice/nginx.service\n" + "[comm]\n900 nginx\n" + "LOGS_END\n" +) + + +class TestGetLogSources: + def _sent(self, driver) -> list[str]: + return [c[0][0] for c in driver._device.send_command.call_args_list] + + def test_it_reads_with_the_shared_command_as_root(self, driver): + from napalm_device_types import LogSourcesMixin + + assert isinstance(driver, LogSourcesMixin) + driver._root = True + driver._device.send_command.return_value = _LOGS + + reading = driver.get_log_sources() + + assert (reading["attributed"], reading["journald"]) == (True, True) + access = [s for s in reading["sources"] if s["kind"] == "file"] + assert [(s["path"], s["unit"]) for s in access] == [("/var/log/nginx/access.log", "nginx")] + assert self._sent(driver)[0].startswith("sh -c '") + + def test_without_a_sudo_password_the_whole_script_runs_under_sudo_n(self, driver): + driver._root = False + driver._device.send_command.return_value = _LOGS + + driver.get_log_sources() + + assert self._sent(driver)[0].startswith("sudo -n sh -c '") + + def test_when_sudo_refuses_it_reads_what_the_user_may_see(self, driver): + driver._root = False + driver._device.send_command.side_effect = ["sudo: a password is required", _LOGS] + + reading = driver.get_log_sources() + + assert reading["attributed"] is False + refused, plain = self._sent(driver) + assert refused.startswith("sudo -n sh -c '") + assert plain.startswith("sh -c '") + + # --------------------------------------------------------------------------- # get_system_identity (napalm-device-types SystemIdentityMixin) # ---------------------------------------------------------------------------