diff --git a/CHANGELOG.md b/CHANGELOG.md index 8008b90..9c2605c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,33 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `get_arp_table()` reports an unknown age as `-1.0` (napalm-device-types' `ARP_AGE_UNKNOWN`) instead of `0.0`, which means "confirmed just now": `ip neigh` has no age, and a STALE entry may be hours old (NetOrk/netork#1327). +- `install_package()` reports success by the package manager's exit status, as + `uninstall_package()` already does (#4). It used to search the output for words like + "failed": an install whose postinst merely mentioned a failure read as failed, and a + failure that used none of the words read as success. +- `get_vpn_tunnels()` makes the public-key prefix in a WireGuard peer's key URL-safe + (`+` becomes `-`, `/` becomes `_`), as napalm-opnsense does (#43). A key with `/` + could not be used as a URL path segment, so such a tunnel could not be edited in + netOrk. The key of a peer whose first eight key characters hold `+` or `/` changes + once; `public_key` is reported unchanged. +- `get_processes()` reports a process's whole start time in `started` + (`Tue Oct 6 08:47:15 2026`) and only the command line in `command` (#21). `started` + held the weekday alone and `command` began with the rest of the date. The start time + is read in the C locale, so its words are English on every host. +- `get_packages()` on apt hosts no longer lists removed packages whose configuration + dpkg still keeps (state `rc`) (#17). They were reported as installed: old kernels, or + an agent that had been removed. A package that is held, unpacked or half-configured + still counts, since its files are on disk. +- `ping()` reads BusyBox's output, the `ping` on Alpine (#15). Its summary says + "2 packets received", which did not match, so every target came back as an error and + a LAN scan through an Alpine host found nothing. Replies to IPv6 targets, BusyBox's + `seq=` replies and replies from a name (`bytes from host (address)`) now appear in + `results`, BusyBox's `round-trip` line fills the RTT fields, and an iputils summary + that counts errors is read as well. +- `get_route_to()` reports a dual-stack host's IPv6 default route as `::/0` (#6). It + came back as a second `0.0.0.0/0` with `family: ipv6`, because `ip -6 route show` + calls its default route `default` as well. An IPv6 host route is now a `/128` + instead of a `/32`. ## [0.15.1] - 2026-10-23 diff --git a/napalm_linux/_dpkg.py b/napalm_linux/_dpkg.py new file mode 100644 index 0000000..ae0bc70 --- /dev/null +++ b/napalm_linux/_dpkg.py @@ -0,0 +1,73 @@ +"""The packages dpkg has on a Debian-family host: the query and its parse.""" + +from __future__ import annotations + +from napalm_device_types.models import PackageDict + +#: One line per package dpkg knows, its state first (``ii``, ``rc``, ``hi``, …): +#: dpkg also lists removed packages whose configuration it still keeps (#17). +DPKG_QUERY_COMMAND = ( + "dpkg-query -W -f='${db:Status-Abbrev}\\t${Package}\\t${Version}\\t${Installed-Size}" + "\\t${source:Package}\\t${source:Version}\\t${binary:Summary}\\n' 2>/dev/null" +) +#: The second letter of ``db:Status-Abbrev`` for a package whose files are gone: +#: ``n`` not installed, ``c`` only its configuration left (``rc``). Every other +#: state -- half-installed, unpacked, half-configured, awaiting triggers -- has +#: its files on disk. +_NOT_ON_DISK = frozenset("nc") + + +class DpkgPackageDict(PackageDict): + """A dpkg package with the source package it was built from.""" + + source_package: str + source_version: str + + +def parse_dpkg_query(out: str) -> list[PackageDict]: + """The packages in ``DPKG_QUERY_COMMAND``'s output whose files are on disk.""" + packages: list[PackageDict] = [] + for line in out.splitlines(): + # Summary stays last and keeps whatever it contains: maxsplit must + # equal the number of tabs the format writes, not the field count. + status, _, line = line.partition("\t") + if status[1:2] in _NOT_ON_DISK: + continue + package = _package(line.split("\t", 5)) + if package is not None: + packages.append(package) + return packages + + +def _package(parts: list[str]) -> DpkgPackageDict | None: + if len(parts) < 2: + return None + name = parts[0].strip() + version = parts[1].strip() + size = int(parts[2].strip()) * 1024 if len(parts) > 2 and parts[2].strip().isdigit() else 0 + # Debian source package (e.g. openssh-server → openssh) for OSV matching. + source_package = parts[3].strip() if len(parts) > 3 and parts[3].strip() else name + # And its version, which is a different number from this package's. + # + # OSV states Debian ranges in *source* versions. A source package + # that ships several binaries gives each its own upstream version: + # libldb2 is 2:2.11.0+samba4.22.11+dfsg-… while its source, samba, + # is 2:4.22.11+dfsg-…. A consumer matching on source_package and + # comparing `version` compares two unrelated numbers — dpkg reads + # ldb's 2.11.0 as older than the 2:4.17.4+dfsg-1 that fixed + # CVE-2022-44640, and a host five releases past the fix was reported + # vulnerable on four packages at once. + # + # dpkg leaves this empty when it equals `Version`; so does an older + # dpkg that does not know the field at all. + source_version = parts[4].strip() if len(parts) > 4 and parts[4].strip() else version + return { + "name": name, + "version": version, + "installed": True, + "description": parts[5].strip() if len(parts) > 5 else "", + "size": size, + "source": "apt", + "source_package": source_package, + "source_version": source_version, + } diff --git a/napalm_linux/_ping.py b/napalm_linux/_ping.py new file mode 100644 index 0000000..82390d6 --- /dev/null +++ b/napalm_linux/_ping.py @@ -0,0 +1,47 @@ +"""NAPALM's ``ping()`` reply from what ``ping(8)`` prints: iputils and BusyBox, IPv4 and IPv6.""" + +from __future__ import annotations + +import re +from typing import Any + +#: iputils: ``5 packets transmitted, 5 received, +2 errors, 0% packet loss, time 4005ms``; +#: BusyBox: ``2 packets transmitted, 2 packets received, 0% packet loss`` (#15). +_SUMMARY = re.compile( + r"(\d+) packets transmitted, (\d+) (?:packets )?received,(?: [^,]*,)*? ([\d.]+)% packet loss" +) +#: iputils: ``rtt min/avg/max/mdev = 0.1/0.2/0.3/0.05 ms``; +#: BusyBox: ``round-trip min/avg/max = 0.1/0.2/0.3 ms``, without a deviation. +_RTT = re.compile( + r"(?:rtt|round-trip) min/avg/max(?:/\w+)? = ([\d.]+)/([\d.]+)/([\d.]+)(?:/([\d.]+))? ms" +) +#: One reply: ``bytes from
:`` or, for a name, ``bytes from (
):``. +#: The address may be IPv6 (``2001:db8::1``, ``fe80::1%eth0``); ``icmp_seq=`` (iputils) +#: or ``seq=`` (BusyBox), and a ``ttl=`` that is not always there, come before the time. +_PROBE = re.compile(r"bytes from (?:\S+ \()?([^\s()]+?)\)?: .*?\btime=([\d.]+) ?ms") + + +def parse_ping(output: str) -> dict[str, Any]: + """``{"success": {...}}`` for *output* with a summary line, else ``{"error": output}``.""" + summary = _SUMMARY.search(output) + if not summary: + return {"error": output} + sent, received = int(summary.group(1)), int(summary.group(2)) + rtt = _RTT.search(output) + rtts = [float(v) if v else 0.0 for v in rtt.groups()] if rtt else [0.0] * 4 + results = [ + {"ip_address": probe.group(1), "rtt": float(probe.group(2))} + for probe in map(_PROBE.search, output.splitlines()) + if probe + ] + return { + "success": { + "probes_sent": sent, + "packet_loss": sent - received, + "rtt_min": rtts[0], + "rtt_avg": rtts[1], + "rtt_max": rtts[2], + "rtt_stddev": rtts[3], + "results": results, + } + } diff --git a/napalm_linux/_routes.py b/napalm_linux/_routes.py new file mode 100644 index 0000000..080585c --- /dev/null +++ b/napalm_linux/_routes.py @@ -0,0 +1,52 @@ +"""Routes from ``ip route show``, one address family at a time.""" + +from __future__ import annotations + +import re +from collections.abc import Iterator +from typing import NamedTuple + +#: The command per family. ``ip -6 route show`` names its default route ``default`` +#: as well, so only the command a line came from says which family it is (#6). +IP_ROUTE_COMMANDS = {"ipv4": "ip -4 route show", "ipv6": "ip -6 route show"} +_DEFAULT = {"ipv4": "0.0.0.0/0", "ipv6": "::/0"} +_HOST_PREFIX = {"ipv4": "/32", "ipv6": "/128"} + +_VIA = re.compile(r"via\s+(\S+)") +_DEV = re.compile(r"dev\s+(\S+)") +_PROTO = re.compile(r"proto\s+(\S+)") +_METRIC = re.compile(r"metric\s+(\d+)") + + +class IpRoute(NamedTuple): + """One line of ``ip route show``.""" + + network: str + proto: str + nexthop: str + iface: str + metric: int + + +def parse_ip_route(out: str, family: str) -> Iterator[IpRoute]: + """The routes in *out*, the output of ``IP_ROUTE_COMMANDS[family]``.""" + for line in out.splitlines(): + line = line.strip() + if not line or line.startswith("#"): + continue + dest = line.split(None, 1)[0] + network = _DEFAULT[family] if dest == "default" else dest + if "/" not in network: + network += _HOST_PREFIX[family] + yield IpRoute( + network, + _first(_PROTO, line) or "kernel", + _first(_VIA, line), + _first(_DEV, line), + int(_first(_METRIC, line) or 0), + ) + + +def _first(pattern: re.Pattern[str], line: str) -> str: + match = pattern.search(line) + return match.group(1) if match else "" diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index c8438ec..9b6269e 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -71,8 +71,11 @@ from napalm_device_types.ssh_host_keys import HostKeyCheck, retry_refused from netmiko import ConnectHandler from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutException +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 +from napalm_linux._routes import IP_ROUTE_COMMANDS, parse_ip_route from napalm_linux._shell import expect_for, prepare_shell from napalm_linux._sudo_history import LinuxSudoHistoryMixin @@ -955,44 +958,9 @@ class LinuxDriver( _make_entry(mapped, nexthop, iface, metric, network) ) - out = self._send("ip -4 route show && ip -6 route show") - for line in out.splitlines(): - line = line.strip() - if not line or line.startswith("#"): - continue - dest_m = re.match(r"^(\S+)", line) - if not dest_m: - continue - raw_dest = dest_m.group(1) - network = ( - "0.0.0.0/0" - if raw_dest == "default" - else ("::/0" if raw_dest == "default6" else raw_dest) - ) - if "/" not in network: - network += "/32" - - nexthop = "" - nh_m = re.search(r"via\s+(\S+)", line) - if nh_m: - nexthop = nh_m.group(1) - - iface = "" - dev_m = re.search(r"dev\s+(\S+)", line) - if dev_m: - iface = dev_m.group(1) - - proto = "kernel" - proto_m = re.search(r"proto\s+(\S+)", line) - if proto_m: - proto = proto_m.group(1) - - metric = 0 - metric_m = re.search(r"metric\s+(\d+)", line) - if metric_m: - metric = int(metric_m.group(1)) - - _add(network, proto, nexthop, iface, metric) + for family, command in IP_ROUTE_COMMANDS.items(): + for route in parse_ip_route(self._send(command), family): + _add(*route) # FRR/Zebra enrichment via vtysh — properly attributes OSPF/BGP/RIP protocols. # FRR installs routes into the kernel as "proto zebra"; vtysh gives the real source. @@ -1121,45 +1089,7 @@ class LinuxDriver( """Execute ping from the remote host.""" src_opt = f"-I {source}" if source else "" cmd = f"ping -c {count} -W {timeout} -s {size} -t {ttl} {src_opt} {destination} 2>&1" - output = self._send(cmd) - - # Parse summary line: "5 packets transmitted, 5 received, 0% packet loss" - m = re.search( - r"(\d+) packets transmitted,\s*(\d+) received,\s*([\d.]+)% packet loss", - output, - ) - if not m: - return {"error": output} - - sent, received = int(m.group(1)), int(m.group(2)) - - # Parse rtt line: "rtt min/avg/max/mdev = 0.123/0.456/0.789/0.100 ms" - rtt_m = re.search(r"rtt .* = ([\d.]+)/([\d.]+)/([\d.]+)/([\d.]+) ms", output) - - results = [] - for line in output.splitlines(): - icmp_m = re.search( - r"bytes from ([\d.]+).*icmp_seq=\d+ ttl=(\d+) time=([\d.]+) ms", line - ) - if icmp_m: - results.append( - { - "ip_address": icmp_m.group(1), - "rtt": float(icmp_m.group(3)), - } - ) - - return { - "success": { - "probes_sent": sent, - "packet_loss": sent - received, - "rtt_min": float(rtt_m.group(1)) if rtt_m else 0.0, - "rtt_avg": float(rtt_m.group(2)) if rtt_m else 0.0, - "rtt_max": float(rtt_m.group(3)) if rtt_m else 0.0, - "rtt_stddev": float(rtt_m.group(4)) if rtt_m else 0.0, - "results": results, - } - } + return parse_ping(self._send(cmd)) # ------------------------------------------------------------------ # KernelFactsMixin – the transport for get_kernel_facts @@ -1237,52 +1167,7 @@ class LinuxDriver( raise NotImplementedError(f"Package manager '{self._pkg_manager}' is not supported") def _get_packages_apt(self) -> list[PackageDict]: - out = self._send( - "dpkg-query -W -f='${Package}\\t${Version}\\t${Installed-Size}" - "\\t${source:Package}\\t${source:Version}\\t${binary:Summary}\\n' 2>/dev/null" - ) - packages: list[PackageDict] = [] - for line in out.splitlines(): - # Summary stays last and keeps whatever it contains: maxsplit must - # equal the number of tabs the format writes, not the field count. - parts = line.split("\t", 5) - if len(parts) < 2: - continue - name = parts[0].strip() - version = parts[1].strip() - size = ( - int(parts[2].strip()) * 1024 if len(parts) > 2 and parts[2].strip().isdigit() else 0 - ) - # Debian source package (e.g. openssh-server → openssh) for OSV matching. - source_package = parts[3].strip() if len(parts) > 3 and parts[3].strip() else name - # And its version, which is a different number from this package's. - # - # OSV states Debian ranges in *source* versions. A source package - # that ships several binaries gives each its own upstream version: - # libldb2 is 2:2.11.0+samba4.22.11+dfsg-… while its source, samba, - # is 2:4.22.11+dfsg-…. A consumer matching on source_package and - # comparing `version` compares two unrelated numbers — dpkg reads - # ldb's 2.11.0 as older than the 2:4.17.4+dfsg-1 that fixed - # CVE-2022-44640, and a host five releases past the fix was reported - # vulnerable on four packages at once. - # - # dpkg leaves this empty when it equals `Version`; so does an older - # dpkg that does not know the field at all. - source_version = parts[4].strip() if len(parts) > 4 and parts[4].strip() else version - description = parts[5].strip() if len(parts) > 5 else "" - packages.append( - { - "name": name, - "version": version, - "installed": True, - "description": description, - "size": size, - "source": "apt", - "source_package": source_package, - "source_version": source_version, - } - ) - return packages + return parse_dpkg_query(self._send(DPKG_QUERY_COMMAND)) def _get_packages_rpm(self) -> list[PackageDict]: out = self._send( @@ -1455,39 +1340,33 @@ class LinuxDriver( return packages def install_package(self, name: str) -> dict[str, Any]: - """Install a package by name. Returns ``{"success": bool, "output": str}``.""" + """Install a package by name. Returns ``{"success": bool, "output": str}``. + + Success is the package manager's exit status, as for ``uninstall_package``.""" from shlex import quote as _q safe = _q(name) pm = self._pkg_manager if pm == "apt": - raw = self._sudo( - f"DEBIAN_FRONTEND=noninteractive apt-get install -y {safe} 2>&1 || true" - ) + cmd = f"DEBIAN_FRONTEND=noninteractive apt-get install -y {safe} 2>&1" elif pm in ("dnf", "yum"): - raw = self._sudo(f"{pm} install -y {safe} 2>&1 || true") + cmd = f"{pm} install -y {safe} 2>&1" elif pm == "apk": - raw = self._sudo(f"apk add {safe} 2>&1 || true") + cmd = f"apk add {safe} 2>&1" elif pm == "pacman": - raw = self._sudo(f"pacman -S --noconfirm {safe} 2>&1 || true") + cmd = f"pacman -S --noconfirm {safe} 2>&1" else: return {"success": False, "output": f"Unsupported package manager: {pm}"} - low = raw.lower() - success = not any( - kw in low - for kw in ( - "error:", - "failed", - "no packages", - "not found", - "unable to locate", - "no match", - ) - ) - return {"success": success, "output": raw.strip()} + raw, rc = self._sudo_status(cmd) + failed = self._package_command_failed(raw, rc, self._INSTALL_FAILED) + return {"success": not failed, "output": raw.strip()} + + #: Words in a package manager's output that mean an install did not happen. + #: Only consulted when the exit status is unknown; see ``_package_command_failed``. + _INSTALL_FAILED = ("error:", "failed", "no packages", "not found", "unable to locate") #: Words in a package manager's output that mean it did not do the job. - #: Only consulted when the exit status is unknown; see ``_uninstall_failed``. + #: Only consulted when the exit status is unknown; see ``_package_command_failed``. _UNINSTALL_FAILED = ("error:", "failed", "not found", "is not installed", "no packages") def uninstall_package(self, name: str, purge: bool = False) -> dict[str, Any]: @@ -1533,16 +1412,17 @@ class LinuxDriver( return {"success": False, "output": f"Unsupported package manager: {pm}"} raw, rc = self._sudo_status(cmd) - failed = self._uninstall_failed(raw, rc) + failed = self._package_command_failed(raw, rc, self._UNINSTALL_FAILED) if failed and pm == "apt": forced, forced_rc = self._sudo_status(f"dpkg --purge --force-all {safe} 2>&1") raw = f"{raw.strip()}\n--- dpkg --purge --force-all ---\n{forced.strip()}" - failed = self._uninstall_failed(forced, forced_rc) + failed = self._package_command_failed(forced, forced_rc, self._UNINSTALL_FAILED) return {"success": not failed, "output": raw.strip()} - def _uninstall_failed(self, output: str, rc: int | None = None) -> bool: + @staticmethod + def _package_command_failed(output: str, rc: int | None, keywords: tuple[str, ...]) -> bool: """Whether the package manager did not do the job. The exit status decides whenever there is one (netork#267): it is the @@ -1566,7 +1446,7 @@ class LinuxDriver( low = output.lower() if any(line.lstrip().startswith("e: ") for line in low.splitlines()): return True - return any(kw in low for kw in self._UNINSTALL_FAILED) + return any(kw in low for kw in keywords) def get_available_updates(self) -> list[UpdateDict]: if self._pkg_manager == "apt": @@ -1857,14 +1737,15 @@ class LinuxDriver( def get_processes(self) -> list[ProcessDict]: """Return running processes via ``ps axo``.""" + # lstart is five words ("Tue Oct 6 08:47:15 2026") -- one word each only in C. out = self._send( - "ps axo pid,ppid,user:20,pcpu,pmem,vsz,rss,tty,stat,lstart,args " + "LC_ALL=C ps axo pid,ppid,user:20,pcpu,pmem,vsz,rss,tty,stat,lstart,args " "--no-headers 2>/dev/null" ) processes: list[ProcessDict] = [] for line in out.splitlines(): - parts = line.split(None, 10) - if len(parts) < 11: + parts = line.split(None, 14) + if len(parts) < 15: continue try: pid = int(parts[0]) @@ -1876,12 +1757,8 @@ class LinuxDriver( rss = int(parts[6]) tty = parts[7] if parts[7] != "?" else "" state = parts[8][0] if parts[8] else "?" - # lstart is 5 tokens: "Mon May 27 12:34:56 2024" → parts[9..13] - # args starts at parts[14] but we merged from 10 onward - # With --no-headers and ps axo, lstart takes 5 parts - # Rebuild: parts[9] is start, args is parts[10] - started = parts[9] - command = parts[10] + started = " ".join(parts[9:14]) + command = parts[14] except (ValueError, IndexError): continue processes.append( @@ -2117,8 +1994,8 @@ class LinuxDriver( Falls back to interface-level data from ``ip link`` + ``/proc/net/dev`` when root access is unavailable. - Full data keyed by ``wireguard--`` (one entry per peer). - Fallback keyed by ``wireguard-`` (one entry per WireGuard interface). + Full data keyed by ``wireguard--``, one per peer, URL-safe + (``+`` → ``-``, ``/`` → ``_``); the fallback by ``wireguard-``, one per interface. """ import time as _time @@ -2195,7 +2072,7 @@ class LinuxDriver( local_port = listen_ports.get(iface, "") local_ep = f":{local_port}" if local_port and local_port != "0" else "" - key = f"wireguard-{iface}-{pubkey[:8]}" + key = f"wireguard-{iface}-{pubkey[:8].replace('+', '-').replace('/', '_')}" tunnels[key] = { "type": "WireGuard", "local_endpoint": local_ep, diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index c558b17..f468186 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -4,7 +4,7 @@ "napalm_linux/linux.py::LinuxDriver._collect_platform_info": 24, "napalm_linux/linux.py::LinuxDriver._parse_cron_line": 12, "napalm_linux/linux.py::LinuxDriver.get_networks": 13, - "napalm_linux/linux.py::LinuxDriver.get_route_to": 23, + "napalm_linux/linux.py::LinuxDriver.get_route_to": 14, "napalm_linux/linux.py::LinuxDriver.get_snmp_config": 13, "napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 28, "napalm_linux/linux.py::LinuxDriver.search_packages": 26 @@ -12,9 +12,8 @@ "function_lines": { "napalm_linux/linux.py::LinuxDriver._collect_platform_info": 106, "napalm_linux/linux.py::LinuxDriver.get_networks": 52, - "napalm_linux/linux.py::LinuxDriver.get_route_to": 138, + "napalm_linux/linux.py::LinuxDriver.get_route_to": 103, "napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 152, - "napalm_linux/linux.py::LinuxDriver.ping": 52, "napalm_linux/linux.py::LinuxDriver.search_packages": 106, "napalm_linux/linux.py::LinuxDriver.uninstall_package": 51 }, @@ -22,7 +21,7 @@ "napalm_linux.linux" ], "python_file_lines": { - "napalm_linux/linux.py": 2265 + "napalm_linux/linux.py": 2142 }, "untyped_public": [] } diff --git a/tests/test_linux.py b/tests/test_linux.py index 97bd1d8..57dcd33 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -259,17 +259,41 @@ def test_parse_uptime_invalid(driver): #: assertion noticed. A fixture simpler than the data cannot fail the way the #: data does. APT_PKG_OUTPUT = ( - "openssh-server\t1:9.2p1-2+deb12u2\t512\topenssh\t1:9.2p1-2+deb12u2" + "ii \topenssh-server\t1:9.2p1-2+deb12u2\t512\topenssh\t1:9.2p1-2+deb12u2" "\tsecure shell server\n" - "curl\t7.88.1-10+deb12u5\t1024\tcurl\t7.88.1-10+deb12u5" + "ii \tcurl\t7.88.1-10+deb12u5\t1024\tcurl\t7.88.1-10+deb12u5" "\tcommand line tool for transferring data\n" # The shape that matters: a binary package whose own upstream version has # nothing to do with its source package's. ldb 2.11.0 is built from samba # 4.22.11, and OSV states Debian ranges in source versions. - "libldb2\t2:2.11.0+samba4.22.11+dfsg-0+deb13u1\t2048\tsamba" + "ii \tlibldb2\t2:2.11.0+samba4.22.11+dfsg-0+deb13u1\t2048\tsamba" "\t2:4.22.11+dfsg-0+deb13u1\tLDB shared library\n" ) +#: Removed packages whose configuration dpkg still keeps (state ``rc``), next to +#: a held one and one whose postinst failed -- both still on disk (#17). +APT_PKG_STATES = ( + "rc \twazuh-agent\t4.14.7-1\t\twazuh-agent\t4.14.7-1\tWazuh agent\n" + "rc \tlinux-image-6.8.0-117-generic\t6.8.0-117.117\t\tlinux-signed\t6.8.0-117.117\tSigned kernel image generic\n" + "hi \tdocker-ce\t5:28.5.1-1~debian.13~trixie\t104858\tdocker-ce\t5:28.5.1-1~debian.13~trixie\tDocker: the open-source application container engine\n" + "iU \tzabbix-agent\t1:7.0.10-1\t1550\tzabbix\t1:7.0.10-1\tZabbix network monitoring solution - agent\n" + "ii \tcurl\t8.14.1-2\t512\tcurl\t8.14.1-2\tcommand line tool for transferring data with URL syntax\n" +) + + +def test_get_packages_apt_reads_the_package_state(driver): + driver._pkg_manager = "apt" + with patch.object(driver, "_send", return_value=APT_PKG_STATES) as send: + driver.get_packages() + assert send.call_args[0][0].startswith("dpkg-query -W -f='${db:Status-Abbrev}\\t${Package}") + + +def test_get_packages_apt_leaves_out_what_only_left_its_configuration(driver): + driver._pkg_manager = "apt" + with patch.object(driver, "_send", return_value=APT_PKG_STATES): + names = [p["name"] for p in driver.get_packages()] + assert names == ["docker-ce", "zabbix-agent", "curl"] + def test_get_packages_apt(driver): driver._pkg_manager = "apt" @@ -309,7 +333,7 @@ def test_get_packages_apt_falls_back_when_dpkg_gives_no_source(driver): """`source:Package` is empty for a package whose source name equals its own. Older dpkg builds leave `source:Version` empty in that case too.""" driver._pkg_manager = "apt" - with patch.object(driver, "_send", return_value="curl\t7.88.1-10\t1024\t\t\ttool\n"): + with patch.object(driver, "_send", return_value="ii \tcurl\t7.88.1-10\t1024\t\t\ttool\n"): (pkg,) = driver.get_packages() assert pkg["source_package"] == "curl" @@ -399,6 +423,99 @@ def test_ping_parses_output(driver): assert result["success"]["rtt_avg"] == pytest.approx(12.1) +#: BusyBox ping (Alpine 3.22), IPv4: "packets received", "seq=", no mdev (#15). +PING_BUSYBOX = """\ +PING 192.0.2.1 (192.0.2.1): 100 data bytes +108 bytes from 192.0.2.1: seq=0 ttl=64 time=0.412 ms +108 bytes from 192.0.2.1: seq=1 ttl=64 time=0.388 ms + +--- 192.0.2.1 ping statistics --- +2 packets transmitted, 2 packets received, 0% packet loss +round-trip min/avg/max = 0.388/0.400/0.412 ms +""" + +#: BusyBox ping, IPv6 link-local. +PING_BUSYBOX_V6 = """\ +PING fe80::1%eth0 (fe80::1%eth0): 100 data bytes +108 bytes from fe80::1%eth0: seq=0 ttl=64 time=0.511 ms + +--- fe80::1%eth0 ping statistics --- +2 packets transmitted, 1 packets received, 50% packet loss +round-trip min/avg/max = 0.511/0.511/0.511 ms +""" + +#: iputils ping (Debian 13), IPv6. +PING_IPUTILS_V6 = """\ +PING 2001:db8::53 (2001:db8::53) 100 data bytes +108 bytes from 2001:db8::53: icmp_seq=1 ttl=58 time=10.5 ms +108 bytes from 2001:db8::53: icmp_seq=2 ttl=58 time=10.9 ms + +--- 2001:db8::53 ping statistics --- +2 packets transmitted, 2 received, 0% packet loss, time 1002ms +rtt min/avg/max/mdev = 10.500/10.700/10.900/0.200 ms +""" + +#: iputils ping to a name: the reply names the host, the address in brackets; +#: unreachable probes add an "errors" field to the summary. +PING_IPUTILS_NAME = """\ +PING gw.example.net (192.0.2.1) 100(128) bytes of data. +108 bytes from gw.example.net (192.0.2.1): icmp_seq=1 ttl=64 time=0.301 ms +From 192.0.2.254 icmp_seq=2 Destination Host Unreachable + +--- gw.example.net ping statistics --- +2 packets transmitted, 1 received, +1 errors, 50% packet loss, time 1001ms +rtt min/avg/max/mdev = 0.301/0.301/0.301/0.000 ms +""" + + +class TestPingFormats: + """``ping`` reads BusyBox's and iputils' output, IPv4 and IPv6 (#15).""" + + def _ping(self, driver, output): + with patch.object(driver, "_send", return_value=output): + return driver.ping("192.0.2.1", count=2)["success"] + + def test_busybox_summary_and_rtt(self, driver): + reply = self._ping(driver, PING_BUSYBOX) + + assert (reply["probes_sent"], reply["packet_loss"]) == (2, 0) + assert (reply["rtt_min"], reply["rtt_avg"], reply["rtt_max"]) == (0.388, 0.4, 0.412) + assert reply["rtt_stddev"] == 0.0 + + def test_busybox_probes(self, driver): + reply = self._ping(driver, PING_BUSYBOX) + + assert reply["results"] == [ + {"ip_address": "192.0.2.1", "rtt": 0.412}, + {"ip_address": "192.0.2.1", "rtt": 0.388}, + ] + + def test_busybox_ipv6(self, driver): + reply = self._ping(driver, PING_BUSYBOX_V6) + + assert (reply["probes_sent"], reply["packet_loss"]) == (2, 1) + assert reply["results"] == [{"ip_address": "fe80::1%eth0", "rtt": 0.511}] + + def test_iputils_ipv6(self, driver): + reply = self._ping(driver, PING_IPUTILS_V6) + + assert reply["results"] == [ + {"ip_address": "2001:db8::53", "rtt": 10.5}, + {"ip_address": "2001:db8::53", "rtt": 10.9}, + ] + assert (reply["rtt_avg"], reply["rtt_stddev"]) == (10.7, 0.2) + + def test_iputils_to_a_name_with_errors(self, driver): + reply = self._ping(driver, PING_IPUTILS_NAME) + + assert (reply["probes_sent"], reply["packet_loss"]) == (2, 1) + assert reply["results"] == [{"ip_address": "192.0.2.1", "rtt": 0.301}] + + def test_no_summary_is_an_error(self, driver): + with patch.object(driver, "_send", return_value="ping: unknown host nosuch"): + assert driver.ping("nosuch") == {"error": "ping: unknown host nosuch"} + + # --------------------------------------------------------------------------- # apply_updates – input validation # --------------------------------------------------------------------------- @@ -1276,6 +1393,51 @@ class TestUninstallExitStatus: assert result["success"] is False +class TestInstallExitStatus: + """``install_package`` decides by the exit status too (#4), as removal does + since netork#267: keywords in the output were guesswork in both directions.""" + + def test_a_non_zero_exit_is_a_failure_whatever_the_output_says(self, driver): + driver._pkg_manager = "dnf" + _mock_send(driver, _with_rc("Last metadata expiration check: 0:01:02 ago.", 1)) + + result = driver.install_package("curl") + + assert result["success"] is False + + def test_a_zero_exit_is_a_success_even_if_the_output_mentions_failure(self, driver): + """A postinst that cannot start a unit in a container says "Failed" and + apt still installs the package.""" + _mock_send( + driver, + _with_rc( + "Setting up nginx (1.26.3-3) ...\n" + "Failed to start nginx.service: System has not been booted with systemd.", + 0, + ), + ) + + result = driver.install_package("nginx") + + assert result["success"] is True + + def test_the_install_command_keeps_its_exit_status(self, driver): + _mock_send(driver, _with_rc("Setting up curl ...", 0)) + + result = driver.install_package("curl") + + sent = driver._device.send_command.call_args[0][0] + assert "|| true" not in sent + assert result["output"] == "Setting up curl ..." + + def test_without_an_exit_status_the_output_is_read(self, driver): + _mock_send(driver, _with_rc("E: Unable to locate package nosuchpkg", -1)) + + result = driver.install_package("nosuchpkg") + + assert result["success"] is False + + # --------------------------------------------------------------------------- # get_kernel_facts -- the command and its parse live in napalm-device-types # --------------------------------------------------------------------------- @@ -1995,3 +2157,131 @@ class TestHardensItsSshServer: with patch.object(driver, "run_command", return_value=CommandResult(out, "", 0)) as run: assert driver.get_ssh_server_algorithms()["mac"] == ["d"] assert run.call_args.kwargs["privileged"] is True + + +class TestWireGuardTunnelKeys: + """A tunnel's key is a URL path segment for its consumer (#43): the public + key's base64 prefix is made URL-safe, as napalm-opnsense does.""" + + _DUMP = ( + "wg0\tPRIVKEY\tSERVERPUBKEY=\t51820\toff\n" + "wg0\tab/c+d9EfGhIjKlMnOpQrStUvWxYz0123456789AB=\t(none)\t" + "198.51.100.7:51820\t10.8.0.2/32\t0\t1024\t2048\toff\n" + ) + + def test_a_slash_or_plus_in_the_key_prefix_is_url_safe(self, driver): + _mock_send(driver, self._DUMP) + + tunnels = driver.get_vpn_tunnels() + + assert list(tunnels) == ["wireguard-wg0-ab_c-d9E"] + + def test_the_public_key_itself_is_reported_unchanged(self, driver): + _mock_send(driver, self._DUMP) + + (tunnel,) = driver.get_vpn_tunnels().values() + + assert tunnel["public_key"] == "ab/c+d9EfGhIjKlMnOpQrStUvWxYz0123456789AB=" + + +class TestGetProcesses: + """``lstart`` is five words; the split has to leave them together (#21).""" + + _PS = ( + " 1 0 root 0.0 0.1 23456 12345 ? Ss Tue Oct 6 08:47:15 2026 /sbin/init splash\n" + " 1234 1 chris 1.5 2.3 456789 98765 pts/0 S+ Tue Oct 6 09:01:02 2026 bash -c echo two spaces\n" + ) + + def test_started_is_the_whole_start_time(self, driver): + _mock_send(driver, self._PS) + + init, shell = driver.get_processes() + + assert init["started"] == "Tue Oct 6 08:47:15 2026" + assert shell["started"] == "Tue Oct 6 09:01:02 2026" + + def test_command_is_the_command_line_alone(self, driver): + _mock_send(driver, self._PS) + + init, shell = driver.get_processes() + + assert init["command"] == "/sbin/init splash" + assert shell["command"] == "bash -c echo two spaces" + + def test_the_other_columns(self, driver): + _mock_send(driver, self._PS) + + shell = driver.get_processes()[1] + + assert (shell["pid"], shell["ppid"], shell["user"], shell["tty"]) == ( + 1234, + 1, + "chris", + "pts/0", + ) + assert (shell["cpu"], shell["memory"], shell["vsz"], shell["rss"]) == ( + 1.5, + 2.3, + 456789, + 98765, + ) + assert shell["state"] == "S" + assert driver.get_processes()[0]["tty"] == "" + + def test_the_start_time_is_in_c_locale_words(self, driver): + """A German host says "Di Okt" -- one word each too, but only C promises that.""" + _mock_send(driver, self._PS) + + driver.get_processes() + + assert driver._device.send_command.call_args[0][0].startswith("LC_ALL=C ps ") + + def test_a_line_cut_short_is_skipped(self, driver): + _mock_send(driver, " 1 0 root 0.0 0.1 23456 12345 ? Ss Tue Oct 6\n") + + assert driver.get_processes() == [] + + +class TestRouteFamilies: + """``ip -6 route show`` calls its default route ``default`` too (#6).""" + + _V4 = ( + "default via 192.0.2.1 dev eth0 proto dhcp src 192.0.2.10 metric 100\n" + "192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10 metric 100\n" + ) + _V6 = ( + "2001:db8:1::/64 dev eth0 proto ra metric 100 expires 86391sec pref medium\n" + "fe80::/64 dev eth0 proto kernel metric 256 pref medium\n" + "default via fe80::1 dev eth0 proto ra metric 100 expires 1791sec pref medium\n" + ) + + def _routes(self, driver, v6=_V6): + def send(command, **_kw): + out = "" + if "ip -4 route" in command: + out += self._V4 + if "ip -6 route" in command: + out += v6 + return out + + with patch.object(driver, "_send", side_effect=send): + return driver.get_route_to() + + def test_the_ipv6_default_route_is_its_own(self, driver): + routes = self._routes(driver) + + assert [r["family"] for r in routes["::/0"]] == ["ipv6"] + assert routes["::/0"][0]["next_hop"] == "fe80::1" + + def test_the_ipv4_default_route_stays_ipv4(self, driver): + routes = self._routes(driver) + + assert [r["family"] for r in routes["0.0.0.0/0"]] == ["ipv4"] + assert routes["0.0.0.0/0"][0]["next_hop"] == "192.0.2.1" + + def test_an_ipv6_host_route_is_a_128(self, driver): + routes = self._routes( + driver, v6="2001:db8:2::7 via fe80::1 dev eth0 proto static metric 1024\n" + ) + + assert "2001:db8:2::7/128" in routes