feat(ssh): harden the node's sshd through a run_command channel (#50)
driver / standard (pull_request) Successful in 19s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m13s
driver / test (3.12) (pull_request) Successful in 1m23s
driver / lowest (pull_request) Successful in 44s
driver / test (3.13) (pull_request) Successful in 1m23s
driver / build (pull_request) Successful in 34s
driver / security (pull_request) Successful in 1m13s
CI / driver (pull_request) Successful in 2m55s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 19s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m13s
driver / test (3.12) (pull_request) Successful in 1m23s
driver / lowest (pull_request) Successful in 44s
driver / test (3.13) (pull_request) Successful in 1m23s
driver / build (pull_request) Successful in 34s
driver / security (pull_request) Successful in 1m13s
CI / driver (pull_request) Successful in 2m55s
driver / publish (pull_request) Skipped
A Proxmox node runs Debian's stock OpenSSH, so OpenSshServerMixin works there as on a plain Linux host. What it needed was run_command: an exec channel on the node's SSH session, via napalm-device-types' run_on_transport. The USES_SSH comment said there was no SSH session; it now says what the flag means to netOrk.
This commit is contained in:
1 parent
b368ad9856
commit
ef1900c32a
3 files changed
+168
-1
No files matched your search
@@ -0,0 +1,125 @@
|
||||
"""The node's sshd can be hardened: a node runs Debian's stock OpenSSH (NetOrk/netork#1027).
|
||||
|
||||
Proxmox VE leaves sshd_config to Debian, with ``Include /etc/ssh/sshd_config.d/*.conf``,
|
||||
so napalm-device-types' ``OpenSshServerMixin`` works as it does on a plain Linux host.
|
||||
What it needs from the driver is ``run_command``: an exec channel on the node's SSH
|
||||
session, with stdin, stderr and the exit status kept apart.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import paramiko
|
||||
import pytest
|
||||
from napalm.base.exceptions import ConnectionException
|
||||
from napalm_device_types import CommandResult
|
||||
|
||||
from napalm_proxmox.driver import ProxmoxDriver
|
||||
|
||||
|
||||
class _Channel:
|
||||
"""An exec channel that prints *out* and *err* and exits with *status*."""
|
||||
|
||||
def __init__(self, out: bytes = b"", err: bytes = b"", status: int = 0) -> None:
|
||||
self._out, self._err, self._status = out, err, status
|
||||
self.command: str | None = None
|
||||
self.stdin = b""
|
||||
self.write_shut = False
|
||||
self.closed = False
|
||||
|
||||
def settimeout(self, timeout: float | None) -> None:
|
||||
pass
|
||||
|
||||
def exec_command(self, command: str) -> None:
|
||||
self.command = command
|
||||
|
||||
def sendall(self, data: bytes) -> None:
|
||||
self.stdin += data
|
||||
|
||||
def shutdown_write(self) -> None:
|
||||
self.write_shut = True
|
||||
|
||||
def recv_ready(self) -> bool:
|
||||
return bool(self._out)
|
||||
|
||||
def recv_stderr_ready(self) -> bool:
|
||||
return bool(self._err)
|
||||
|
||||
def exit_status_ready(self) -> bool:
|
||||
return True
|
||||
|
||||
def recv(self, size: int) -> bytes:
|
||||
chunk, self._out = self._out[:size], self._out[size:]
|
||||
return chunk
|
||||
|
||||
def recv_stderr(self, size: int) -> bytes:
|
||||
chunk, self._err = self._err[:size], self._err[size:]
|
||||
return chunk
|
||||
|
||||
def recv_exit_status(self) -> int:
|
||||
return self._status
|
||||
|
||||
def close(self) -> None:
|
||||
self.closed = True
|
||||
|
||||
|
||||
def _driver(channel: _Channel) -> ProxmoxDriver:
|
||||
driver = ProxmoxDriver("10.0.0.9", "root", "s3cret")
|
||||
driver._node_name = "pve1"
|
||||
driver._api = MagicMock()
|
||||
session = MagicMock()
|
||||
session.get_transport.return_value.open_session.return_value = channel
|
||||
driver._node_ssh = MagicMock(return_value=session) # type: ignore[method-assign]
|
||||
return driver
|
||||
|
||||
|
||||
def test_the_driver_offers_the_ssh_hardening():
|
||||
assert callable(getattr(ProxmoxDriver, "harden_ssh_server", None))
|
||||
assert callable(getattr(ProxmoxDriver, "exempt_from_ssh_penalties", None))
|
||||
|
||||
|
||||
def test_a_command_reports_stdout_stderr_and_its_exit_status():
|
||||
channel = _Channel(b"root\n", b"a warning\n", 3)
|
||||
|
||||
result = _driver(channel).run_command("id -un")
|
||||
|
||||
assert result == CommandResult("root\n", "a warning\n", 3)
|
||||
assert channel.command == "id -un"
|
||||
assert channel.closed
|
||||
|
||||
|
||||
def test_stdin_reaches_the_command_and_is_closed():
|
||||
channel = _Channel()
|
||||
|
||||
_driver(channel).run_command("cat > /tmp/x", stdin=b"PasswordAuthentication no\n")
|
||||
|
||||
assert channel.stdin == b"PasswordAuthentication no\n"
|
||||
assert channel.write_shut
|
||||
|
||||
|
||||
def test_privileged_needs_nothing_more_as_the_node_is_logged_in_as_root():
|
||||
channel = _Channel()
|
||||
|
||||
_driver(channel).run_command("sshd -T", privileged=True)
|
||||
|
||||
assert channel.command == "sshd -T"
|
||||
|
||||
|
||||
def test_a_command_that_cannot_run_raises():
|
||||
driver = _driver(_Channel())
|
||||
driver._node_ssh = MagicMock( # type: ignore[method-assign]
|
||||
side_effect=paramiko.AuthenticationException("Authentication failed.")
|
||||
)
|
||||
|
||||
with pytest.raises(ConnectionException, match="Authentication failed"):
|
||||
driver.run_command("true")
|
||||
|
||||
|
||||
def test_a_command_that_outlasts_its_timeout_raises():
|
||||
class _Hangs(_Channel):
|
||||
def exit_status_ready(self) -> bool:
|
||||
return False
|
||||
|
||||
with pytest.raises(ConnectionException, match="did not finish"):
|
||||
_driver(_Hangs()).run_command("sleep 60", timeout=0.05)
|
||||
Reference in new issue
Block a user