No files matched your search
@@ -45,6 +45,33 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6).
|
||||
|
||||
### Fixed
|
||||
- `get_route_to()` reads unreachable, blackhole, prohibit and throw routes and
|
||||
multipath routes (#57). Their destinations came back as `unreachable/32`,
|
||||
`blackhole/32` or `nexthop/32`, and a multipath route as one entry without a next
|
||||
hop. A typed route now has its real prefix and its type in
|
||||
`protocol_attributes` (`{"type": "blackhole"}`), with no next hop and no outgoing
|
||||
interface, since it sends nothing out. A multipath route has one entry per next hop,
|
||||
each with its gateway and interface. A host route that FRR reports without a prefix
|
||||
length is a `/128` for IPv6 instead of a `/32` (#64), so it meets the kernel's route
|
||||
for the same host and gets FRR's protocol.
|
||||
- `get_route_to()` reads the next hop of an IPv4 route through an IPv6 gateway (RFC
|
||||
8950, as BGP unnumbered sets up), which `ip route` prints as `via inet6 fe80::1`
|
||||
(#65). The next hop came back as `inet6`; it is now the address, for multipath
|
||||
routes too. Such a route stays `family: ipv4`: the family is the destination's,
|
||||
never the next hop's.
|
||||
- `get_available_updates()` works on dnf and yum hosts whose login user has
|
||||
passwordless sudo (NOPASSWD), as netOrk's own account does, and marks the security
|
||||
fixes there (#56). The security-advisory query did not parse under `sudo -n`, so no
|
||||
answer came back and the read ran into its 120 s timeout. Without a sudo password a
|
||||
privileged command now runs as root as a whole, in one `sh -c`, as it already did
|
||||
with a password; before, only its first command did. A refused sudo still reports
|
||||
its exit status at once.
|
||||
- `ping()` passes `destination` and `source` to the host's shell as one quoted word
|
||||
each (#55). A destination such as `192.0.2.1; id` ran the command after the `;` as
|
||||
the login user. `count`, `timeout`, `size` and `ttl` must be numbers; anything else
|
||||
raises `ValueError` before a command is sent. A destination or source that starts
|
||||
with `-` is refused the same way (#63): `ping` would read `-f` as its flood option,
|
||||
and no address or hostname starts with `-`.
|
||||
- dnf and yum hosts read pending updates and security advisories from the cached
|
||||
metadata only (`--cacheonly`), as apt reads its local index (#59). With an expired
|
||||
cache `check-update` downloaded the repositories' metadata first, and on an Oracle
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from shlex import quote
|
||||
from typing import Any
|
||||
|
||||
#: iputils: ``5 packets transmitted, 5 received, +2 errors, 0% packet loss, time 4005ms``;
|
||||
@@ -21,6 +22,23 @@ _RTT = re.compile(
|
||||
_PROBE = re.compile(r"bytes from (?:\S+ \()?([^\s()]+?)\)?: .*?\btime=([\d.]+) ?ms")
|
||||
|
||||
|
||||
def ping_command(
|
||||
destination: str, source: str, ttl: int, timeout: int, size: int, count: int
|
||||
) -> str:
|
||||
"""The shell line that pings *destination*: the names quoted, the numbers numbers (#55).
|
||||
|
||||
:raises ValueError: for a count, timeout, size or ttl that is no number, and for a
|
||||
destination or source that ping would read as an option (#63): no address or
|
||||
hostname starts with ``-``.
|
||||
"""
|
||||
for name in (destination, source):
|
||||
if name.startswith("-"):
|
||||
raise ValueError(f"{name!r} would be read as a ping option, not an address")
|
||||
numbers = f"-c {int(count)} -W {int(timeout)} -s {int(size)} -t {int(ttl)}"
|
||||
src_opt = f" -I {quote(source)}" if source else ""
|
||||
return f"ping {numbers}{src_opt} {quote(destination)} 2>&1"
|
||||
|
||||
|
||||
def parse_ping(output: str) -> dict[str, Any]:
|
||||
"""``{"success": {...}}`` for *output* with a summary line, else ``{"error": output}``."""
|
||||
summary = _SUMMARY.search(output)
|
||||
|
||||
+67
-13
@@ -12,39 +12,93 @@ 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+)")
|
||||
#: The route types ``ip route`` prints in front of the destination; a unicast route
|
||||
#: carries none (#57).
|
||||
_ROUTE_TYPES = frozenset(
|
||||
{
|
||||
"unicast",
|
||||
"local",
|
||||
"broadcast",
|
||||
"anycast",
|
||||
"multicast",
|
||||
"blackhole",
|
||||
"unreachable",
|
||||
"prohibit",
|
||||
"throw",
|
||||
"nat",
|
||||
"xresolve",
|
||||
}
|
||||
)
|
||||
#: Route types that send nothing out of an interface: no next hop, and no outgoing
|
||||
#: interface either, though IPv6 names ``lo`` for them.
|
||||
_NO_FORWARDING = frozenset({"blackhole", "unreachable", "prohibit", "throw"})
|
||||
#: The line a multipath route's next hops start with, one each, after the route.
|
||||
_NEXTHOP = "nexthop"
|
||||
|
||||
#: ``via 192.0.2.1``, or with the gateway's family in front: ``via inet6 fe80::1`` for an
|
||||
#: IPv4 route with an IPv6 next hop (RFC 8950, #65).
|
||||
_VIA = re.compile(r"via\s+(?:inet6?\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``."""
|
||||
"""One route of ``ip route show``; a multipath route is one per next hop."""
|
||||
|
||||
network: str
|
||||
proto: str
|
||||
nexthop: str
|
||||
iface: str
|
||||
metric: int
|
||||
route_type: str = "unicast"
|
||||
|
||||
@property
|
||||
def attributes(self) -> dict[str, str]:
|
||||
"""NAPALM's ``protocol_attributes`` for this route: its type, unless unicast."""
|
||||
return {} if self.route_type == "unicast" else {"type": self.route_type}
|
||||
|
||||
|
||||
def parse_ip_route(out: str, family: str) -> Iterator[IpRoute]:
|
||||
"""The routes in *out*, the output of ``IP_ROUTE_COMMANDS[family]``."""
|
||||
route = ""
|
||||
hops: list[str] = []
|
||||
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),
|
||||
)
|
||||
if line.split(None, 1)[0] == _NEXTHOP:
|
||||
hops.append(line)
|
||||
continue
|
||||
if route:
|
||||
yield from _routes(route, hops, family)
|
||||
route, hops = line, []
|
||||
if route:
|
||||
yield from _routes(route, hops, family)
|
||||
|
||||
|
||||
def with_length(prefix: str) -> str:
|
||||
"""*prefix* with a prefix length: a host route without one is ``/32`` or ``/128``
|
||||
by the address's family (#64)."""
|
||||
if "/" in prefix:
|
||||
return prefix
|
||||
return prefix + _HOST_PREFIX["ipv6" if ":" in prefix else "ipv4"]
|
||||
|
||||
|
||||
def _routes(line: str, hops: list[str], family: str) -> Iterator[IpRoute]:
|
||||
"""The route *line* with its multipath next-hop lines *hops*: one per next hop."""
|
||||
words = line.split()
|
||||
route_type = words.pop(0) if words[0] in _ROUTE_TYPES and len(words) > 1 else "unicast"
|
||||
network = _DEFAULT[family] if words[0] == "default" else words[0]
|
||||
if "/" not in network:
|
||||
network += _HOST_PREFIX[family]
|
||||
proto = _first(_PROTO, line) or "kernel"
|
||||
metric = int(_first(_METRIC, line) or 0)
|
||||
if route_type in _NO_FORWARDING:
|
||||
yield IpRoute(network, proto, "", "", metric, route_type)
|
||||
return
|
||||
for hop in hops or [line]:
|
||||
yield IpRoute(network, proto, _first(_VIA, hop), _first(_DEV, hop), metric, route_type)
|
||||
|
||||
|
||||
def _first(pattern: re.Pattern[str], line: str) -> str:
|
||||
|
||||
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
from shlex import quote
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -17,6 +18,10 @@ PROMPT_RE = r"[#$\>]\s*$"
|
||||
#: napalm-device-types.
|
||||
STATUS_ECHO_RE = re.compile(r"echo\s+(__[A-Z_]+=)\$\?")
|
||||
|
||||
#: The same echo as the last command of a line: ``sudo_without_password`` keeps it
|
||||
#: outside sudo.
|
||||
_TRAILING_STATUS_ECHO_RE = re.compile(r";\s*echo\s+__[A-Z_]+=\$\?\s*$")
|
||||
|
||||
|
||||
def expect_for(command: str) -> str:
|
||||
"""The pattern that ends *command*'s output.
|
||||
@@ -35,6 +40,20 @@ def expect_for(command: str) -> str:
|
||||
return re.escape(markers[-1]) + r"\d+\s*\n.*" + PROMPT_RE
|
||||
|
||||
|
||||
def sudo_without_password(command: str) -> str:
|
||||
"""The line that runs all of *command* as root through ``sudo -n`` (#56).
|
||||
|
||||
The command goes to ``sh -c`` as one argument: ``sudo -n <command>`` covered only
|
||||
its first simple command, and one that starts with a brace group did not parse.
|
||||
A status echo at its end stays outside and reports sh's status, the command's:
|
||||
it arrives when sudo refuses as well, where netmiko would otherwise wait for it
|
||||
until the read timeout.
|
||||
"""
|
||||
echo = _TRAILING_STATUS_ECHO_RE.search(command)
|
||||
body, tail = (command[: echo.start()], echo.group()) if echo else (command, "")
|
||||
return f"sudo -n sh -c {quote(body)}{tail}"
|
||||
|
||||
|
||||
#: Sent before anything else in the interactive session (NetOrk/netork#1039):
|
||||
#: the shell writes no history file and records nothing more. The leading space
|
||||
#: keeps this line itself out of a history under ``HISTCONTROL=ignorespace``;
|
||||
|
||||
+18
-22
@@ -74,9 +74,9 @@ from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutExc
|
||||
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._ping import parse_ping, ping_command
|
||||
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
|
||||
|
||||
logger = logging.getLogger("napalm_linux")
|
||||
@@ -510,14 +510,14 @@ class LinuxDriver(
|
||||
return self._run_privileged(command, timeout)
|
||||
|
||||
def _run_privileged(self, command: str, timeout: float = 100) -> str:
|
||||
"""Run *command* as root: directly for a root login, through ``_sudo``
|
||||
with a sudo password, and through ``sudo -n`` without one -- which fails at
|
||||
once where a password prompt would hang the session until the timeout."""
|
||||
"""Run all of *command* as root: directly for a root login, through ``_sudo``
|
||||
with a sudo password, and through ``sudo -n sh -c`` without one -- which fails
|
||||
at once where a password prompt would hang the session until the timeout."""
|
||||
if self._is_root():
|
||||
return self._send(command, read_timeout=timeout)
|
||||
if self._sudo_password:
|
||||
return self._sudo(command, read_timeout=timeout) # never typed into the shell
|
||||
return self._send(f"sudo -n {command}", read_timeout=timeout)
|
||||
return self._send(sudo_without_password(command), read_timeout=timeout)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Command channel (napalm-device-types CommandChannelMixin)
|
||||
@@ -933,7 +933,7 @@ class LinuxDriver(
|
||||
def _make_entry(
|
||||
proto: str, nexthop: str, iface: str, metric: int, network: str
|
||||
) -> dict[str, Any]:
|
||||
family = "ipv6" if (":" in network or (nexthop and ":" in nexthop)) else "ipv4"
|
||||
family = "ipv6" if ":" in network else "ipv4" # not the next hop's (#65)
|
||||
return {
|
||||
"protocol": proto,
|
||||
"family": family,
|
||||
@@ -948,19 +948,19 @@ class LinuxDriver(
|
||||
"protocol_attributes": {},
|
||||
}
|
||||
|
||||
def _add(network: str, proto: str, nexthop: str, iface: str, metric: int) -> None:
|
||||
if destination and network != destination:
|
||||
def _add(route: IpRoute) -> None:
|
||||
if destination and route.network != destination:
|
||||
return
|
||||
mapped = proto_map.get(proto, proto)
|
||||
mapped = proto_map.get(route.proto, route.proto)
|
||||
if protocol and mapped != protocol.lower():
|
||||
return
|
||||
routes.setdefault(network, []).append(
|
||||
_make_entry(mapped, nexthop, iface, metric, network)
|
||||
)
|
||||
entry = _make_entry(mapped, route.nexthop, route.iface, route.metric, route.network)
|
||||
entry["protocol_attributes"] = route.attributes
|
||||
routes.setdefault(route.network, []).append(entry)
|
||||
|
||||
for family, command in IP_ROUTE_COMMANDS.items():
|
||||
for route in parse_ip_route(self._send(command), family):
|
||||
_add(*route)
|
||||
_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.
|
||||
@@ -989,12 +989,10 @@ class LinuxDriver(
|
||||
if not vm:
|
||||
continue
|
||||
code = vm.group(1)
|
||||
prefix = vm.group(2)
|
||||
prefix = with_length(vm.group(2))
|
||||
metric = int(vm.group(4))
|
||||
nexthop = vm.group(5) or ""
|
||||
iface = (vm.group(6) or "").rstrip(",")
|
||||
if "/" not in prefix:
|
||||
prefix += "/32"
|
||||
mapped_proto = _frr_code.get(code, code.lower())
|
||||
if prefix in routes:
|
||||
for entry in routes[prefix]:
|
||||
@@ -1086,10 +1084,8 @@ class LinuxDriver(
|
||||
count: int = 5,
|
||||
vrf: str = "",
|
||||
) -> dict[str, Any]:
|
||||
"""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"
|
||||
return parse_ping(self._send(cmd))
|
||||
"""Ping from the remote host; ValueError for a bad number or a name starting with -."""
|
||||
return parse_ping(self._send(ping_command(destination, source, ttl, timeout, size, count)))
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# KernelFactsMixin – the transport for get_kernel_facts
|
||||
|
||||
@@ -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": 14,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_route_to": 13,
|
||||
"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,7 +12,7 @@
|
||||
"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": 103,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_route_to": 101,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 152,
|
||||
"napalm_linux/linux.py::LinuxDriver.search_packages": 106,
|
||||
"napalm_linux/linux.py::LinuxDriver.uninstall_package": 51
|
||||
@@ -21,7 +21,7 @@
|
||||
"napalm_linux.linux"
|
||||
],
|
||||
"python_file_lines": {
|
||||
"napalm_linux/linux.py": 2142
|
||||
"napalm_linux/linux.py": 2138
|
||||
},
|
||||
"untyped_public": []
|
||||
}
|
||||
+263
-2
@@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from napalm_linux._routes import IpRoute, parse_ip_route
|
||||
from napalm_linux.linux import (
|
||||
_YUM_SECURITY_COMMAND,
|
||||
LinuxDriver,
|
||||
@@ -516,6 +517,48 @@ class TestPingFormats:
|
||||
assert driver.ping("nosuch") == {"error": "ping: unknown host nosuch"}
|
||||
|
||||
|
||||
class TestPingArguments:
|
||||
"""``ping`` types its arguments into a shell: each one stays one word (#55)."""
|
||||
|
||||
def _sent(self, driver, *args, **kwargs):
|
||||
with patch.object(driver, "_send", return_value=PING_OUTPUT) as send:
|
||||
driver.ping(*args, **kwargs)
|
||||
return send.call_args[0][0]
|
||||
|
||||
def test_the_destination_is_one_quoted_word(self, driver):
|
||||
sent = self._sent(driver, "1.2.3.4; id")
|
||||
|
||||
assert sent == "ping -c 5 -W 2 -s 100 -t 255 '1.2.3.4; id' 2>&1"
|
||||
|
||||
def test_the_source_is_one_quoted_word(self, driver):
|
||||
sent = self._sent(driver, "192.0.2.1", source="$(id)")
|
||||
|
||||
assert sent == "ping -c 5 -W 2 -s 100 -t 255 -I '$(id)' 192.0.2.1 2>&1"
|
||||
|
||||
def test_the_numbers_are_numbers(self, driver):
|
||||
sent = self._sent(driver, "192.0.2.1", count="3", timeout=1.0, size="56", ttl=64)
|
||||
|
||||
assert sent == "ping -c 3 -W 1 -s 56 -t 64 192.0.2.1 2>&1"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("destination", "source"), [("-f", ""), ("192.0.2.1", "-f"), ("-p ff", "")]
|
||||
)
|
||||
def test_an_option_in_place_of_a_name_is_refused_before_anything_is_sent(
|
||||
self, driver, destination, source
|
||||
):
|
||||
"""Quoting keeps a word one word, and ping still reads ``-f`` as its option (#63)."""
|
||||
with patch.object(driver, "_send") as send, pytest.raises(ValueError, match="option"):
|
||||
driver.ping(destination, source=source)
|
||||
|
||||
send.assert_not_called()
|
||||
|
||||
def test_a_number_that_is_not_one_is_refused_before_anything_is_sent(self, driver):
|
||||
with patch.object(driver, "_send") as send, pytest.raises(ValueError):
|
||||
driver.ping("192.0.2.1", count="5; id")
|
||||
|
||||
send.assert_not_called()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# apply_updates – input validation
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -1571,7 +1614,7 @@ class TestManageService:
|
||||
|
||||
driver.manage_service("cron", "enable")
|
||||
|
||||
assert self._sent(driver)[1].startswith("sudo -n timeout 45 systemctl")
|
||||
assert self._sent(driver)[1].startswith("sudo -n sh -c 'timeout 45 systemctl")
|
||||
|
||||
def test_a_missing_sudo_password_is_explained(self, driver):
|
||||
driver._device.send_command.side_effect = [
|
||||
@@ -1802,7 +1845,7 @@ class TestRefreshAvailableUpdates:
|
||||
result = driver.refresh_available_updates()
|
||||
|
||||
assert result["success"] is False
|
||||
assert self._sent(driver)[1].startswith("sudo -n apt-get update")
|
||||
assert self._sent(driver)[1].startswith("sudo -n sh -c 'apt-get update")
|
||||
|
||||
def test_dnf_refreshes_its_metadata(self, driver):
|
||||
driver._pkg_manager = "dnf"
|
||||
@@ -2311,3 +2354,221 @@ class TestRouteFamilies:
|
||||
)
|
||||
|
||||
assert "2001:db8:2::7/128" in routes
|
||||
|
||||
|
||||
#: ``ip -4 route show`` / ``ip -6 route show``, iproute2 6.1.0 on Linux 7.0 (Linux Mint
|
||||
#: 22.3), recorded in a network namespace set up for it: a multipath default per
|
||||
#: family, and unreachable, prohibit, throw and blackhole routes (#57). Documentation
|
||||
#: addresses only. The trailing blanks and the tabs are ip's.
|
||||
_IP4_ROUTE_TYPES = (
|
||||
"default proto static metric 100 \n"
|
||||
"\tnexthop via 192.0.2.1 dev eth0 weight 1 \n"
|
||||
"\tnexthop via 198.51.100.1 dev eth1 weight 1 \n"
|
||||
"unreachable 10.0.0.0/8 metric 1024 \n"
|
||||
"prohibit 172.16.0.0/12 \n"
|
||||
"192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10 \n"
|
||||
"throw 192.168.0.0/16 \n"
|
||||
"198.51.100.0/24 dev eth1 proto kernel scope link src 198.51.100.10 \n"
|
||||
"blackhole 203.0.113.0/24 proto static \n"
|
||||
"203.0.113.7 via 192.0.2.1 dev eth0 \n"
|
||||
)
|
||||
_IP6_ROUTE_TYPES = (
|
||||
"2001:db8:1::/64 dev eth0 proto kernel metric 256 pref medium\n"
|
||||
"2001:db8:2::/64 dev eth1 proto kernel metric 256 pref medium\n"
|
||||
"blackhole 2001:db8:beef::/48 dev lo metric 1024 pref medium\n"
|
||||
"unreachable 2001:db8:dead::/48 dev lo metric 1024 pref medium\n"
|
||||
"fe80::/64 dev eth0 proto kernel metric 256 pref medium\n"
|
||||
"fe80::/64 dev eth1 proto kernel metric 256 pref medium\n"
|
||||
"default proto static metric 1024 pref medium\n"
|
||||
"\tnexthop via 2001:db8:1::1 dev eth0 weight 1 \n"
|
||||
"\tnexthop via 2001:db8:2::1 dev eth1 weight 1 \n"
|
||||
)
|
||||
|
||||
|
||||
class TestRouteTypesAndMultipath:
|
||||
"""A route type in front of the destination is the type, and a multipath
|
||||
route's ``nexthop`` lines are its next hops (#57)."""
|
||||
|
||||
@pytest.fixture()
|
||||
def routes(self, driver):
|
||||
def send(command, **_kw):
|
||||
if "ip -4 route" in command:
|
||||
return _IP4_ROUTE_TYPES
|
||||
if "ip -6 route" in command:
|
||||
return _IP6_ROUTE_TYPES
|
||||
return ""
|
||||
|
||||
with patch.object(driver, "_send", side_effect=send):
|
||||
return driver.get_route_to()
|
||||
|
||||
def test_every_destination_is_an_address(self, routes):
|
||||
assert sorted(routes) == sorted(
|
||||
[
|
||||
"0.0.0.0/0",
|
||||
"10.0.0.0/8",
|
||||
"172.16.0.0/12",
|
||||
"192.0.2.0/24",
|
||||
"192.168.0.0/16",
|
||||
"198.51.100.0/24",
|
||||
"203.0.113.0/24",
|
||||
"203.0.113.7/32",
|
||||
"2001:db8:1::/64",
|
||||
"2001:db8:2::/64",
|
||||
"2001:db8:beef::/48",
|
||||
"2001:db8:dead::/48",
|
||||
"fe80::/64",
|
||||
"::/0",
|
||||
]
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("network", "route_type", "metric"),
|
||||
[
|
||||
("10.0.0.0/8", "unreachable", 1024),
|
||||
("172.16.0.0/12", "prohibit", 0),
|
||||
("192.168.0.0/16", "throw", 0),
|
||||
("203.0.113.0/24", "blackhole", 0),
|
||||
("2001:db8:beef::/48", "blackhole", 1024),
|
||||
("2001:db8:dead::/48", "unreachable", 1024),
|
||||
],
|
||||
)
|
||||
def test_a_route_that_forwards_nothing_says_its_type(self, routes, network, route_type, metric):
|
||||
(entry,) = routes[network]
|
||||
|
||||
assert entry["protocol_attributes"] == {"type": route_type}
|
||||
assert entry["preference"] == metric
|
||||
# Nothing leaves through an interface; IPv6 names lo for it.
|
||||
assert (entry["next_hop"], entry["outgoing_interface"]) == ("", "")
|
||||
|
||||
def test_the_rest_of_a_typed_line_is_read_as_before(self, routes):
|
||||
assert routes["203.0.113.0/24"][0]["protocol"] == "static"
|
||||
|
||||
def test_a_unicast_route_has_no_type(self, routes):
|
||||
assert routes["192.0.2.0/24"][0]["protocol_attributes"] == {}
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("network", "hops"),
|
||||
[
|
||||
("0.0.0.0/0", [("192.0.2.1", "eth0"), ("198.51.100.1", "eth1")]),
|
||||
("::/0", [("2001:db8:1::1", "eth0"), ("2001:db8:2::1", "eth1")]),
|
||||
],
|
||||
)
|
||||
def test_a_multipath_route_has_one_entry_per_next_hop(self, routes, network, hops):
|
||||
entries = routes[network]
|
||||
|
||||
assert [(e["next_hop"], e["outgoing_interface"]) for e in entries] == hops
|
||||
assert {(e["protocol"], e["preference"]) for e in entries} == {
|
||||
("static", 100 if network == "0.0.0.0/0" else 1024)
|
||||
}
|
||||
|
||||
def test_no_next_hop_line_is_a_route_of_its_own(self, routes):
|
||||
assert not any(network.startswith("nexthop") for network in routes)
|
||||
|
||||
|
||||
class TestParseIpRoute:
|
||||
"""``parse_ip_route`` line by line (#57)."""
|
||||
|
||||
def test_a_typed_route_that_forwards_keeps_its_interface(self):
|
||||
# ``ip route show table local``: a local route names the interface it is on.
|
||||
line = "local 192.0.2.10 dev eth0 proto kernel scope host src 192.0.2.10\n"
|
||||
|
||||
assert list(parse_ip_route(line, "ipv4")) == [
|
||||
IpRoute("192.0.2.10/32", "kernel", "", "eth0", 0, "local")
|
||||
]
|
||||
|
||||
def test_a_next_hop_line_without_a_route_is_dropped(self):
|
||||
out = "\tnexthop via 192.0.2.1 dev eth0 weight 1\n192.0.2.0/24 dev eth0 proto kernel\n"
|
||||
|
||||
assert [r.network for r in parse_ip_route(out, "ipv4")] == ["192.0.2.0/24"]
|
||||
|
||||
def test_a_route_without_next_hop_lines_is_one_route(self):
|
||||
out = "default via 192.0.2.1 dev eth0 proto dhcp metric 100\n"
|
||||
|
||||
assert list(parse_ip_route(out, "ipv4")) == [
|
||||
IpRoute("0.0.0.0/0", "dhcp", "192.0.2.1", "eth0", 100)
|
||||
]
|
||||
|
||||
|
||||
class TestFrrHostRoutes:
|
||||
"""The FRR enrichment completes a prefix without a length by its family (#64)."""
|
||||
|
||||
# FRR installs its routes as "proto zebra"; vtysh names the real source.
|
||||
_IP6 = "2001:db8:2::7 via fe80::1 dev eth0 proto zebra metric 20 pref medium\n"
|
||||
_VTYSH = (
|
||||
"O>* 2001:db8:2::7 [110/20] via fe80::1, eth0, weight 1, 00:01:02\n"
|
||||
"O>* 2001:db8:3::9 [110/30] via fe80::1, eth0, weight 1, 00:01:02\n"
|
||||
"O>* 10.10.0.7 [110/20] via 10.255.255.2, wg0, weight 1, 00:01:02\n"
|
||||
"O>* 10.10.1.0/24 [110/20] via 10.255.255.2, wg0, weight 1, 00:01:02\n"
|
||||
)
|
||||
|
||||
@pytest.fixture()
|
||||
def routes(self, driver):
|
||||
def send(command, **_kw):
|
||||
if command.startswith("command -v vtysh"):
|
||||
return "/usr/bin/vtysh"
|
||||
if command.startswith("vtysh"):
|
||||
return self._VTYSH
|
||||
if "ip -6 route" in command:
|
||||
return self._IP6
|
||||
return ""
|
||||
|
||||
with patch.object(driver, "_send", side_effect=send):
|
||||
return driver.get_route_to()
|
||||
|
||||
def test_an_ipv6_host_route_is_a_128(self, routes):
|
||||
assert "2001:db8:3::9/128" in routes
|
||||
assert not any(
|
||||
network.endswith("::7/32") or network.endswith("::9/32") for network in routes
|
||||
)
|
||||
|
||||
def test_it_meets_the_kernel_route_of_the_same_host(self, routes):
|
||||
assert [e["protocol"] for e in routes["2001:db8:2::7/128"]] == ["ospf"]
|
||||
|
||||
def test_an_ipv4_host_route_stays_a_32(self, routes):
|
||||
assert "10.10.0.7/32" in routes
|
||||
|
||||
def test_a_prefix_with_a_length_keeps_it(self, routes):
|
||||
assert "10.10.1.0/24" in routes
|
||||
|
||||
|
||||
#: ``ip -4 route show``, iproute2 6.1.0 on Linux 7.0 (Linux Mint 22.3), recorded in a
|
||||
#: network namespace set up for it: IPv4 routes with IPv6 next hops (RFC 8950), single
|
||||
#: and multipath, as BGP unnumbered installs them (#65). Documentation addresses only.
|
||||
_IP4_VIA_INET6 = (
|
||||
"default via inet6 2001:db8:1::1 dev eth0 \n"
|
||||
"10.20.0.0/16 proto bgp metric 20 \n"
|
||||
"\tnexthop via inet6 2001:db8:1::1 dev eth0 weight 1 \n"
|
||||
"\tnexthop via inet6 2001:db8:2::1 dev eth1 weight 1 \n"
|
||||
"10.40.0.0/16 via inet6 2001:db8:1::1 dev eth0 proto bgp metric 20 \n"
|
||||
"192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10 \n"
|
||||
)
|
||||
|
||||
|
||||
class TestIpv6NextHops:
|
||||
"""``via inet6 <address>``: the next hop is the address (#65)."""
|
||||
|
||||
@pytest.fixture()
|
||||
def routes(self, driver):
|
||||
def send(command, **_kw):
|
||||
return _IP4_VIA_INET6 if "ip -4 route" in command else ""
|
||||
|
||||
with patch.object(driver, "_send", side_effect=send):
|
||||
return driver.get_route_to()
|
||||
|
||||
@pytest.mark.parametrize("network", ["0.0.0.0/0", "10.40.0.0/16"])
|
||||
def test_the_next_hop_is_the_address(self, routes, network):
|
||||
assert [e["next_hop"] for e in routes[network]] == ["2001:db8:1::1"]
|
||||
|
||||
def test_a_multipath_route_has_each_address(self, routes):
|
||||
hops = [(e["next_hop"], e["outgoing_interface"]) for e in routes["10.20.0.0/16"]]
|
||||
|
||||
assert hops == [("2001:db8:1::1", "eth0"), ("2001:db8:2::1", "eth1")]
|
||||
|
||||
def test_the_route_stays_ipv4(self, routes):
|
||||
"""The family is the destination's; an IPv6 next hop does not change it."""
|
||||
assert {e["family"] for entries in routes.values() for e in entries} == {"ipv4"}
|
||||
|
||||
def test_a_family_word_for_ipv4_is_skipped_too(self):
|
||||
out = "10.50.0.0/16 via inet 192.0.2.1 dev eth0\n"
|
||||
|
||||
assert [r.nexthop for r in parse_ip_route(out, "ipv4")] == ["192.0.2.1"]
|
||||
@@ -0,0 +1,199 @@
|
||||
"""Without a sudo password the whole command runs as root, in one ``sh -c`` (#56).
|
||||
|
||||
A login that is not root and has no sudo password (NOPASSWD sudo, e.g. netOrk's
|
||||
own account from NetOrk/netork#1817) types its privileged commands into the
|
||||
interactive shell. ``sudo -n <command>`` covered only the first simple command of
|
||||
the line, and a command that starts with a brace group did not even parse:
|
||||
``sudo -n { LC_ALL=C dnf …; echo __DNF_RC=$?; } | cat`` is a syntax error, so dnf's
|
||||
and yum's security advisories were always unknown.
|
||||
|
||||
The command now goes to ``sudo -n sh -c`` as one argument, as ``_as_root`` does.
|
||||
A status echo at its end (``; echo __NETORK_RC=$?``) stays outside: it reports
|
||||
sh's status, which is the command's, and still arrives when sudo refuses.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from shlex import quote
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from napalm_device_types import DNF_SECURITY_COMMAND
|
||||
|
||||
from napalm_linux import LinuxDriver
|
||||
from napalm_linux.linux import _YUM_SECURITY_COMMAND
|
||||
|
||||
BASH = shutil.which("bash")
|
||||
needs_bash = pytest.mark.skipif(BASH is None, reason="needs bash")
|
||||
|
||||
#: dnf's answer on a Fedora 40 host with one pending security advisory.
|
||||
_ADVISORY = "FEDORA-2024-1a2b3c4d5e Important/Sec. openssl-libs-1:3.1.4-2.fc40.x86_64"
|
||||
|
||||
|
||||
def _driver(pkg_manager: str = "dnf", answers: list[str] | None = None) -> LinuxDriver:
|
||||
"""A driver logged in as a user that is not root, without a sudo password."""
|
||||
d = LinuxDriver.__new__(LinuxDriver)
|
||||
d.hostname = "testhost"
|
||||
d._sudo_password = None
|
||||
d._pkg_manager = pkg_manager
|
||||
d._root = False
|
||||
d._device = MagicMock()
|
||||
d._device.send_command.side_effect = answers or []
|
||||
return d
|
||||
|
||||
|
||||
def _sent(driver: LinuxDriver) -> list[str]:
|
||||
return [c[0][0] for c in driver._device.send_command.call_args_list]
|
||||
|
||||
|
||||
def _bash_parses(line: str) -> str:
|
||||
"""bash's complaint about *line*, empty when it parses."""
|
||||
assert BASH is not None
|
||||
checked = subprocess.run( # noqa: S603 - a fixed interpreter, the line only parsed
|
||||
[BASH, "-n", "-c", line], capture_output=True, text=True, check=False
|
||||
)
|
||||
return checked.stderr
|
||||
|
||||
|
||||
class TestTheSecurityAdvisories:
|
||||
@needs_bash
|
||||
@pytest.mark.parametrize("manager", ["dnf", "yum"])
|
||||
def test_the_line_parses(self, manager):
|
||||
driver = _driver(manager, [f"{_ADVISORY}\n__DNF_RC=0\n__NETORK_RC=0"])
|
||||
|
||||
driver._rpm_security_names()
|
||||
|
||||
assert _bash_parses(_sent(driver)[0]) == ""
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("manager", "command"), [("dnf", DNF_SECURITY_COMMAND), ("yum", _YUM_SECURITY_COMMAND)]
|
||||
)
|
||||
def test_all_of_it_runs_as_root_and_the_status_stays_outside(self, manager, command):
|
||||
driver = _driver(manager, [f"{_ADVISORY}\n__DNF_RC=0\n__NETORK_RC=0"])
|
||||
|
||||
driver._rpm_security_names()
|
||||
|
||||
assert _sent(driver) == [f"sudo -n sh -c {quote(command)}; echo __NETORK_RC=$?"]
|
||||
|
||||
def test_the_advisories_are_read(self):
|
||||
driver = _driver("dnf", [f"{_ADVISORY}\n__DNF_RC=0\n__NETORK_RC=0"])
|
||||
|
||||
assert driver._rpm_security_names() == {"openssl-libs"}
|
||||
|
||||
|
||||
#: Every caller of ``_run_privileged``, with an answer that satisfies it.
|
||||
_CALLERS = {
|
||||
"reboot_host": (lambda d: d.reboot_host(), ["__NETORK_RC=0"]),
|
||||
"refresh apt": (lambda d: d.refresh_available_updates(), ["__NETORK_RC=0"]),
|
||||
"check-update": (
|
||||
lambda d: d.get_available_updates(),
|
||||
["__NETORK_RC=0", "__DNF_RC=0\n__NETORK_RC=0"],
|
||||
),
|
||||
"manage_service": (lambda d: d.manage_service("wg-quick@wg0", "restart"), ["__SVC_RC=0"]),
|
||||
"listening sockets": (
|
||||
lambda d: d._run_listening_sockets_command("sh -c 'ss -H -ltn'", privileged=True),
|
||||
[""],
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
@needs_bash
|
||||
@pytest.mark.parametrize("caller", sorted(_CALLERS))
|
||||
def test_every_privileged_line_parses_and_runs_whole_as_root(caller):
|
||||
call, answers = _CALLERS[caller]
|
||||
driver = _driver("apt" if caller == "refresh apt" else "dnf", answers)
|
||||
|
||||
call(driver)
|
||||
|
||||
for line in _sent(driver):
|
||||
assert line.startswith("sudo -n sh -c '")
|
||||
assert _bash_parses(line) == ""
|
||||
|
||||
|
||||
def test_the_service_status_stays_outside():
|
||||
driver = _driver(answers=["__SVC_RC=0"])
|
||||
|
||||
driver.manage_service("cron", "restart")
|
||||
|
||||
assert _sent(driver) == [
|
||||
"sudo -n sh -c 'timeout 45 systemctl --no-ask-password restart -- cron.service 2>&1';"
|
||||
" echo __SVC_RC=$?"
|
||||
]
|
||||
|
||||
|
||||
def test_a_command_without_a_status_echo_is_wrapped_whole():
|
||||
driver = _driver(answers=[""])
|
||||
|
||||
driver._run_privileged("ss -H -ltnp | sort")
|
||||
|
||||
assert _sent(driver) == ["sudo -n sh -c 'ss -H -ltnp | sort'"]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# The lines run in a real bash, with a stand-in sudo and package manager
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_SUDO_OK = '#!/bin/sh\n[ "$1" = -n ] && shift\nexec "$@"\n'
|
||||
_SUDO_REFUSES = "#!/bin/sh\necho 'sudo: a password is required' >&2\nexit 1\n"
|
||||
_DNF = f"#!/bin/sh\necho '{_ADVISORY}'\n"
|
||||
|
||||
|
||||
def _shell_host(tmp_path: Path, sudo: str) -> LinuxDriver:
|
||||
"""A driver whose interactive shell is a real bash with stand-ins on its PATH.
|
||||
|
||||
Like netmiko, it reads until the command's expect pattern matches and fails
|
||||
when it never does.
|
||||
"""
|
||||
for name, script in (("sudo", sudo), ("dnf", _DNF)):
|
||||
stub = tmp_path / name
|
||||
stub.write_text(script)
|
||||
stub.chmod(0o755)
|
||||
env = {"PATH": f"{tmp_path}:/usr/bin:/bin", "LC_ALL": "C"}
|
||||
|
||||
def send_command(command, *, expect_string, **_kwargs):
|
||||
assert BASH is not None
|
||||
ran = subprocess.run( # noqa: S603 - a fixed interpreter and stand-ins
|
||||
[BASH, "-c", command],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT, # one stream, in order, as on the PTY
|
||||
text=True,
|
||||
env=env,
|
||||
check=False,
|
||||
)
|
||||
output = f"{ran.stdout}user@host:~$ "
|
||||
if not re.search(expect_string, output):
|
||||
raise TimeoutError(f"pattern not detected: {expect_string!r} in {output!r}")
|
||||
return output
|
||||
|
||||
driver = _driver("dnf")
|
||||
driver._device.send_command.side_effect = send_command
|
||||
return driver
|
||||
|
||||
|
||||
@needs_bash
|
||||
def test_a_real_shell_reads_the_advisories(tmp_path):
|
||||
driver = _shell_host(tmp_path, _SUDO_OK)
|
||||
|
||||
assert driver._rpm_security_names() == {"openssl-libs"}
|
||||
|
||||
|
||||
@needs_bash
|
||||
def test_a_refused_sudo_still_reports_its_status(tmp_path):
|
||||
driver = _shell_host(tmp_path, _SUDO_REFUSES)
|
||||
|
||||
result = driver.refresh_available_updates()
|
||||
|
||||
assert result["success"] is False
|
||||
assert "a password is required" in result["output"]
|
||||
assert "NOPASSWD" in result["output"] # the hint
|
||||
|
||||
|
||||
@needs_bash
|
||||
def test_a_refused_sudo_leaves_the_advisories_unknown(tmp_path):
|
||||
driver = _shell_host(tmp_path, _SUDO_REFUSES)
|
||||
|
||||
assert driver._rpm_security_names() is None
|
||||
Reference in new issue
Block a user