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.
141 lines
4.3 KiB
Python
141 lines
4.3 KiB
Python
"""What the old ``_sudo`` left on every host, and taking it back (NetOrk/netork#1090).
|
|
|
|
Until NetOrk/netork#1039, ``_sudo`` typed ``echo '<password>' | sudo -S -p ""
|
|
<command>`` into netmiko's interactive shell: the login user's history kept the
|
|
password once per command. Where only the part before the first ``&&`` ran as
|
|
root, snmpd.conf and apt's proxy file were left to the login user. The driver
|
|
says so through napalm-device-types' ``SudoHistoryMixin``, which counts and
|
|
deletes the lines and gives the files back to root.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import shlex
|
|
from types import SimpleNamespace
|
|
from unittest.mock import MagicMock
|
|
|
|
from napalm_device_types import SudoHistoryMixin
|
|
|
|
from napalm_linux import LinuxDriver
|
|
from napalm_linux.linux import _SNMPD_CONF
|
|
|
|
_PW = "s3cr3t"
|
|
|
|
|
|
class _Channel:
|
|
def __init__(self, transport: _Transport):
|
|
self.transport = transport
|
|
self.command = ""
|
|
self.stdin = b""
|
|
self._out = b""
|
|
|
|
def exec_command(self, command):
|
|
self.command = command
|
|
self.transport.commands.append(command)
|
|
self._out = self.transport.answer(command).encode()
|
|
|
|
def settimeout(self, timeout):
|
|
pass
|
|
|
|
def sendall(self, data):
|
|
self.stdin += data
|
|
|
|
def shutdown_write(self):
|
|
pass
|
|
|
|
def close(self):
|
|
pass
|
|
|
|
def recv_ready(self):
|
|
return bool(self._out)
|
|
|
|
def recv(self, size):
|
|
chunk, self._out = self._out[:size], self._out[size:]
|
|
return chunk
|
|
|
|
def recv_stderr_ready(self):
|
|
return False
|
|
|
|
def recv_stderr(self, size):
|
|
return b""
|
|
|
|
def exit_status_ready(self):
|
|
return not self._out
|
|
|
|
def recv_exit_status(self):
|
|
return 0
|
|
|
|
|
|
class _Transport:
|
|
def __init__(self, answers: dict[str, str]):
|
|
self.answers = answers
|
|
self.commands: list[str] = []
|
|
self.channels: list[_Channel] = []
|
|
|
|
def answer(self, command: str) -> str:
|
|
return next((out for key, out in self.answers.items() if key in command), "")
|
|
|
|
def open_session(self):
|
|
self.channels.append(_Channel(self))
|
|
return self.channels[-1]
|
|
|
|
|
|
def _driver(answers: dict[str, str]) -> tuple[LinuxDriver, _Transport]:
|
|
driver = LinuxDriver("h", "admin", "p", optional_args={"sudo_password": _PW})
|
|
transport = _Transport(answers)
|
|
driver._device = MagicMock()
|
|
driver._device.remote_conn_pre = SimpleNamespace(get_transport=lambda: transport)
|
|
driver._root = False
|
|
return driver, transport
|
|
|
|
|
|
def test_the_marker_is_what_the_old_sudo_typed():
|
|
typed = f'echo {shlex.quote(_PW)} | sudo -S -p "" apt-get update'
|
|
assert isinstance(LinuxDriver.SUDO_HISTORY_MARKER, str)
|
|
assert LinuxDriver.SUDO_HISTORY_MARKER in typed
|
|
|
|
|
|
def test_the_files_the_partial_sudo_left_to_the_login_user():
|
|
assert LinuxDriver.SUDO_ROOT_FILES == (
|
|
_SNMPD_CONF,
|
|
LinuxDriver.APT_PROXY_FILE,
|
|
)
|
|
assert LinuxDriver.APT_PROXY_FILE == "/etc/apt/apt.conf.d/00proxy"
|
|
|
|
|
|
def test_it_is_the_device_types_mixin():
|
|
assert issubclass(LinuxDriver, SudoHistoryMixin)
|
|
|
|
|
|
def test_counting_runs_as_the_login_user_on_the_exec_channel():
|
|
driver, transport = _driver({"grep -c": "121 .bash_history\n0 .zsh_history\n"})
|
|
|
|
assert driver.get_history_sudo_lines() == [{"file": "~/.bash_history", "lines": 121}]
|
|
|
|
# No sudo, no password on stdin, nothing typed into the interactive shell
|
|
# (whose history is where the lines came from).
|
|
assert not transport.channels[-1].command.startswith("sudo")
|
|
assert transport.channels[-1].stdin == b""
|
|
driver._device.send_command.assert_not_called()
|
|
|
|
|
|
def test_the_owner_fix_runs_as_root_without_stdin_but_the_password():
|
|
driver, transport = _driver(
|
|
{
|
|
"sed -i": "done 3 0 .bash_history\n",
|
|
"id -u": "1000\nadmin\n",
|
|
"chown": "fixed /etc/snmp/snmpd.conf\n",
|
|
}
|
|
)
|
|
|
|
result = driver.remove_history_sudo_lines()
|
|
|
|
assert result["success"] is True
|
|
assert result["removed"] == 3
|
|
assert result["owners_restored"] == ["/etc/snmp/snmpd.conf"]
|
|
root = [c for c in transport.channels if c.command.startswith("sudo -S")]
|
|
assert len(root) == 1 and "chown -h 0:0" in root[0].command
|
|
# sudo -S reads the password, and the command itself reads nothing.
|
|
assert root[0].stdin == f"{_PW}\n".encode()
|
|
assert all(c.stdin == b"" for c in transport.channels if c is not root[0])
|