paramiko's look_for_keys and allow_agent stayed at their default True, so the node login also tried every key in the running process's own ~/.ssh and any SSH agent it could reach. The driver standard lets a driver authenticate only with what it was handed; both are now False on every connect. Closes #48
93 lines
3.0 KiB
Python
93 lines
3.0 KiB
Python
"""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
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"args",
|
|
[{"ssh_private_key_str": _ed25519_pem()}, {"ssh_password": "ssh-pw"}, {}],
|
|
ids=["key", "ssh-password", "api-password"],
|
|
)
|
|
def test_only_the_given_credentials_log_in(client, args): # noqa: F811
|
|
"""Neither the keys in the worker's own ~/.ssh nor an SSH agent it reaches (#48)."""
|
|
ProxmoxDriver("10.0.0.9", "root", "api-s3cret", optional_args=args)._node_ssh()
|
|
|
|
assert client.connected_with["look_for_keys"] is False
|
|
assert client.connected_with["allow_agent"] is False
|