feat: count and remove the sudo password the old _sudo left in histories (NetOrk/netork#1090)
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m3s
driver / test (3.12) (pull_request) Successful in 1m4s
driver / test (3.13) (pull_request) Successful in 1m10s
driver / lowest (pull_request) Successful in 59s
driver / build (pull_request) Successful in 38s
driver / security (pull_request) Successful in 1m30s
CI / driver (pull_request) Successful in 3m6s
driver / publish (pull_request) Skipped

Until NetOrk/netork#1039, _sudo typed `echo '<password>' | sudo -S -p ""
<command>` into netmiko's interactive shell, so the login user's history
kept the password once per command, and the part after the first `&&` ran
without root, leaving snmpd.conf and apt's 00proxy to the login user.

LinuxDriver mixes in napalm-device-types' SudoHistoryMixin and names what
to look for: the marker "| sudo -S -p " and those two files.
get_history_sudo_lines() and remove_history_sudo_lines() run on the exec
channel; OpenMediaVault and QNAP inherit both.

The _privileged docstring points at _as_root instead of restating it, so
linux.py does not grow.

Requires the napalm-device-types release with SudoHistoryMixin.
This commit is contained in:
Christian Manivong committed 2026-10-30 21:09:33 +01:00
1 parent 4cd1710e19
commit fbaded5913
5 files changed
+181 -5

No files matched your search

+5 -5
View File
@@ -74,6 +74,7 @@ from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutExc
from napalm_linux._hostname import HostnameMixin
from napalm_linux._shell import PROMPT_RE as _PROMPT_RE
from napalm_linux._shell import prepare_shell
from napalm_linux._sudo_history import LinuxSudoHistoryMixin
logger = logging.getLogger("napalm_linux")
@@ -305,6 +306,7 @@ def render_snmpd_conf(settings: SnmpAgentSettingsDict) -> str:
class LinuxDriver(
LinuxSudoHistoryMixin,
SnmpAgentMixin,
EnsurePackageMixin,
AptProxyMixin,
@@ -547,12 +549,10 @@ class LinuxDriver(
def _privileged(self, command: str, privileged: bool) -> tuple[str, bytes | None]:
"""The command line that runs *command* with the privileges asked for,
and what has to reach its stdin first.
and what has to reach its stdin first: root's as ``_as_root`` says.
Root runs it directly. With a sudo password, ``sudo -S`` reads it from
stdin, so it never appears in a process list. Without one, ``sudo -n``
fails at once where a prompt would hang. The command goes to ``sh -c``
as one argument, so the privilege covers every part of it.
The command goes to ``sh -c`` as one argument, so the privilege covers
every part of it.
"""
if not privileged or self._is_root():
return command, None