Only RSA keys were read, and a key was used only when no password was set; with the API password always present it never was. The key is now read as Ed25519, ECDSA or RSA and offered first, the password second. The rest of the issue (the API exec branch, errors read as empty output) belongs to the shared transport of NAPALM/napalm-device-types#35. Refs #17
80 lines
2.5 KiB
Python
80 lines
2.5 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
|