refactor(channel): the sudo probe of a privileged stream lives in _shell (NetOrk/netork#818)
driver / lint (pull_request) Successful in 14s
driver / standard (pull_request) Successful in 19s
driver / types (pull_request) Successful in 1m5s
driver / test (3.13) (pull_request) Failing after 1m1s
driver / test (3.12) (pull_request) Failing after 1m7s
driver / lowest (pull_request) Failing after 29s
driver / build (pull_request) Successful in 38s
driver / security (pull_request) Successful in 1m19s
CI / driver (pull_request) Failing after 2m39s
driver / publish (pull_request) Skipped

linux.py stays at the size its quality ratchet records.
This commit is contained in:
Christian Manivong committed 2026-11-17 00:16:33 +01:00
1 parent 553b4d5153
commit 69c43d3288
3 files changed
+33 -21

No files matched your search

+26
View File
@@ -7,6 +7,8 @@ import re
from shlex import quote
from typing import Any
from napalm_device_types.channel import run_on_transport
logger = logging.getLogger(__name__)
#: The end of a command's output when nothing better is known: a line that looks
@@ -54,6 +56,30 @@ def sudo_without_password(command: str) -> str:
return f"sudo -n sh -c {quote(body)}{tail}"
#: Whether sudo runs a command for the login user without a password now: in
#: the shape every privileged command takes (``sh -c``), so a sudoers rule that
#: covers one covers the other.
SUDO_ASKS_NOTHING = "sudo -n sh -c true"
def unprompted_stream(
transport: Any, command: str, line: str, prefix: bytes | None
) -> tuple[str, bytes | None]:
"""The line and stdin prefix a privileged stream of *command* starts with.
With a sudo password (*prefix*) sudo is asked first whether it wants it: a
sudo that does not prompt -- NOPASSWD, a cached timestamp -- would leave the
password on stdin, where the stream's own data follows it, the Podman API
over ``sudo podman system dial-stdio`` say (NetOrk/netork#818). Such a sudo
gets *command* through ``sudo -n`` and no password; one that asks gets it.
"""
if not prefix:
return line, prefix
if run_on_transport(transport, SUDO_ASKS_NOTHING, timeout=15).exit_code == 0:
return f"sudo -n sh -c {quote(command)}", None
return line, prefix
#: 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``;
+5 -20
View File
@@ -83,7 +83,7 @@ from napalm_linux._posix import LinuxPosixMixin
from napalm_linux._readings import LinuxProcessReadingsMixin
from napalm_linux._routes import IP_ROUTE_COMMANDS, IpRoute, parse_ip_route, with_length
from napalm_linux._rpm import RPM_QUERY_COMMAND, parse_rpm_query
from napalm_linux._shell import expect_for, prepare_shell, sudo_without_password
from napalm_linux._shell import expect_for, prepare_shell, sudo_without_password, unprompted_stream
from napalm_linux._snmp_agent import (
_SNMP_INSTALL,
_SNMPD_CONF,
@@ -158,11 +158,6 @@ _PRIVILEGED_STDIN = (
"mktemp, cat) and have root install that file."
)
#: Whether sudo runs a command for the login user without a password now: in
#: the shape every privileged command takes (``sh -c``), so a sudoers rule that
#: covers one covers the other (``open_stream``).
_SUDO_ASKS_NOTHING = "sudo -n sh -c true"
#: First in every script ``_sudo`` runs: a sudo that does not prompt (NOPASSWD,
#: a cached timestamp) leaves the password on stdin, and nothing may read it.
_NO_STDIN = "exec </dev/null; "
@@ -563,24 +558,14 @@ class LinuxDriver(
def open_stream(self, command: str, *, privileged: bool = False) -> ByteStream:
"""Start *command* on an exec channel and return a stream to it.
A privileged stream with a sudo password asks sudo first whether it
wants the password (``sudo -n sh -c true``): a sudo that does not prompt
-- NOPASSWD, a cached timestamp -- would leave it on stdin, and the
stream's own data follows it there, the Podman API over ``sudo podman
system dial-stdio`` say (NetOrk/netork#818). Such a sudo gets the
command through ``sudo -n``; one that asks gets the password first, for
``sudo -S``.
A privileged stream with a sudo password gets it first only from a sudo
that asks for it; one that does not would hand it to *command* as the
stream's first bytes (``unprompted_stream``, NetOrk/netork#818).
"""
line, prefix = self._privileged(command, privileged)
if prefix and self._sudo_asks_nothing():
line, prefix = f"sudo -n sh -c {_shlex_quote(command)}", None
line, prefix = unprompted_stream(self._transport(), command, line, prefix)
return open_stream_on_transport(self._transport(), line, stdin_prefix=prefix)
def _sudo_asks_nothing(self) -> bool:
"""Whether sudo runs a command for the login user without its password now."""
probe = run_on_transport(self._transport(), _SUDO_ASKS_NOTHING, timeout=15)
return probe.exit_code == 0
def reboot_host(self) -> None:
"""Restart the host (``HostRebootMixin``); returns once the restart is under way.