diff --git a/CHANGELOG.md b/CHANGELOG.md index e3845bf..690e7b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 always returning an empty list: offset in ms, the poll interval in seconds, the offset's standard deviation as `jitter`; chrony reports no delay, so `delay` is 0.0. On ntpd, a peer without tally mark is no longer dropped (#18). +- The SSH login to the node takes an Ed25519 or ECDSA key in + `ssh_private_key_str`, not only RSA, and uses the key even when a password is + set too (the password stays the fallback). A key that cannot be read is + logged as a warning and the password used (#17). ## [0.8.0] - 2026-10-23 diff --git a/napalm_proxmox/driver.py b/napalm_proxmox/driver.py index 8ca45dc..25305a3 100644 --- a/napalm_proxmox/driver.py +++ b/napalm_proxmox/driver.py @@ -343,17 +343,17 @@ class ProxmoxDriver( import paramiko if self._ssh_client is None: - ssh_pass = self._ssh_password or self.password + # A key is tried first; paramiko falls back to the password. connect_kwargs: _JsonDict = { "username": self._ssh_username or self.username, "timeout": self.timeout, } - if self._ssh_key and not ssh_pass: - from io import StringIO as _StringIO - - connect_kwargs["pkey"] = paramiko.RSAKey.from_private_key(_StringIO(self._ssh_key)) - else: - connect_kwargs["password"] = ssh_pass + password = self._ssh_password or self.password + if password: + connect_kwargs["password"] = password + pkey = _load_private_key(self._ssh_key) if self._ssh_key else None + if pkey is not None: + connect_kwargs["pkey"] = pkey client = paramiko.SSHClient() try: HostKeyCheck(self.optional_args, self.hostname, 22).connect( @@ -511,3 +511,18 @@ class ProxmoxDriver( def cli(self, commands: list[str], encoding: str = "text") -> dict[str, str]: """Execute a list of shell commands and return their outputs.""" return {cmd: self._exec_ssh_command(cmd) for cmd in commands} + + +def _load_private_key(text: str) -> Any: # paramiko.PKey; paramiko is imported lazily + """The SSH private key in *text*, whatever its type, or None when it cannot be read.""" + import io + + import paramiko + + for key_class in (paramiko.Ed25519Key, paramiko.ECDSAKey, paramiko.RSAKey): + try: + return key_class.from_private_key(io.StringIO(text)) + except (paramiko.SSHException, ValueError): + continue + logger.warning("The SSH private key could not be read; logging in with the password") + return None diff --git a/tests/test_ssh_login_key.py b/tests/test_ssh_login_key.py new file mode 100644 index 0000000..d9397d4 --- /dev/null +++ b/tests/test_ssh_login_key.py @@ -0,0 +1,79 @@ +"""The node's SSH login takes any key type, and a key wins over the API password (#17).""" + +from __future__ import annotations + +import io + +import paramiko +import pytest +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 + +from napalm_proxmox.driver import ProxmoxDriver +from tests.test_ssh_host_key import client # noqa: F401 - the fake paramiko client fixture + + +def _ed25519_pem() -> str: + key = ed25519.Ed25519PrivateKey.generate() + return key.private_bytes( + serialization.Encoding.PEM, + serialization.PrivateFormat.OpenSSH, + serialization.NoEncryption(), + ).decode() + + +def _paramiko_pem(key: paramiko.PKey) -> str: + out = io.StringIO() + key.write_private_key(out) + return out.getvalue() + + +def _driver(key: str, ssh_password: str | None = None) -> ProxmoxDriver: + args = {"ssh_private_key_str": key} + if ssh_password is not None: + args["ssh_password"] = ssh_password + return ProxmoxDriver("10.0.0.9", "root", "api-s3cret", optional_args=args) + + +@pytest.mark.parametrize( + ("pem", "kind"), + [ + (_ed25519_pem(), paramiko.Ed25519Key), + (_paramiko_pem(paramiko.ECDSAKey.generate()), paramiko.ECDSAKey), + (_paramiko_pem(paramiko.RSAKey.generate(1024)), paramiko.RSAKey), + ], +) +def test_any_key_type_logs_in_although_an_api_password_is_set(client, pem, kind): # noqa: F811 + _driver(pem)._node_ssh() + + assert isinstance(client.connected_with["pkey"], kind) + + +def test_the_password_stays_as_the_fallback(client): # noqa: F811 + _driver(_ed25519_pem(), ssh_password="ssh-pw")._node_ssh() + + assert client.connected_with["password"] == "ssh-pw" + + +def test_without_key_the_password_logs_in(client): # noqa: F811 + ProxmoxDriver("10.0.0.9", "root", "api-s3cret")._node_ssh() + + assert client.connected_with["password"] == "api-s3cret" + assert "pkey" not in client.connected_with + + +def test_a_key_that_cannot_be_read_leaves_the_password(client, caplog): # noqa: F811 + _driver("not a key")._node_ssh() + + assert "pkey" not in client.connected_with + assert client.connected_with["password"] == "api-s3cret" + assert "not a key" not in caplog.text + assert "could not be read" in caplog.text + + +def test_a_token_only_login_sends_no_empty_password(client): # noqa: F811 + ProxmoxDriver( + "10.0.0.9", "root", "", optional_args={"ssh_private_key_str": _ed25519_pem()} + )._node_ssh() + + assert "password" not in client.connected_with