feat(log-sources): which logs the host writes, through LogSourcesMixin #72

Merged
christianmanivong merged 2 commits from feature/log-sources-2049 into main 2026-11-04 23:53:40 +00:00
6 changed files with 98 additions and 14 deletions

No files matched your search

+4
View File
@@ -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
+37
View File
@@ -0,0 +1,37 @@
"""Readings that need root to see every process: listening sockets and log sources.
Both commands are napalm-device-types' and read-only. Root only widens what they
see -- ``ss`` names every process behind a socket, ``/proc/<pid>/fd`` shows every
process's open files -- so both go through sudo where the driver can, and as the
login user where it cannot; the mixins then say the reading is not ``attributed``.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from napalm_device_types import ListeningSocketsMixin, LogSourcesMixin
#: Seconds either reading may take; a busy host lists thousands of descriptors.
_READ_TIMEOUT = 60
class LinuxProcessReadingsMixin(ListeningSocketsMixin, LogSourcesMixin):
"""The transports for ``get_listening_sockets`` and ``get_log_sources``."""
if TYPE_CHECKING: # pragma: no cover - the driver's own, declared for type checkers
def _send(self, command: str, read_timeout: float = 100) -> str: ...
def _run_privileged(self, command: str, timeout: float = 100) -> str: ...
def _read_as_root_if_possible(self, command: str, privileged: bool) -> str:
if privileged:
return self._run_privileged(command, _READ_TIMEOUT)
return self._send(command, read_timeout=_READ_TIMEOUT)
def _run_listening_sockets_command(self, command: str, *, privileged: bool) -> str:
return self._read_as_root_if_possible(command, privileged)
def _run_log_sources_command(self, command: str, *, privileged: bool) -> str:
return self._read_as_root_if_possible(command, privileged)
+3 -12
View File
@@ -38,7 +38,6 @@ from napalm_device_types import (
HostFirewallMixin,
HostStatusMixin,
KernelFactsMixin,
ListeningSocketsMixin,
LldpNeighborDict,
OpenSshServerMixin,
OSDriver,
@@ -79,6 +78,7 @@ from napalm_linux._dpkg import DPKG_QUERY_COMMAND, parse_dpkg_query
from napalm_linux._hostname import HostnameMixin
from napalm_linux._local_accounts import LinuxLocalAccountsMixin
from napalm_linux._ping import parse_ping, ping_command
from napalm_linux._readings import LinuxProcessReadingsMixin
from napalm_linux._routes import IP_ROUTE_COMMANDS, IpRoute, parse_ip_route, with_length
from napalm_linux._shell import expect_for, prepare_shell, sudo_without_password
from napalm_linux._sudo_history import LinuxSudoHistoryMixin
@@ -303,7 +303,7 @@ class LinuxDriver(
AptProxyMixin,
KernelFactsMixin,
ServicePackagesMixin,
ListeningSocketsMixin,
LinuxProcessReadingsMixin,
HostFirewallMixin,
VirtualIpsMixin,
ServiceClustersMixin,
@@ -1086,18 +1086,9 @@ class LinuxDriver(
return self.run_command(f"sh -c {_shlex_quote(command)}", timeout=120).stdout
# ------------------------------------------------------------------
# ListeningSocketsMixin – the transport for get_listening_sockets
# HostFirewallMixin – the transport for get_host_firewall
# ------------------------------------------------------------------
def _run_listening_sockets_command(self, command: str, *, privileged: bool) -> str:
"""The transport for ``ListeningSocketsMixin.get_listening_sockets``.
Read-only either way; root only so that ``ss`` names every process.
"""
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.
+1 -1
View File
@@ -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
]
+1 -1
View File
@@ -20,7 +20,7 @@
"napalm_linux.linux"
],
"python_file_lines": {
"napalm_linux/linux.py": 2048
"napalm_linux/linux.py": 2039
},
"untyped_public": []
}
+52
View File
@@ -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)
# ---------------------------------------------------------------------------