fix(ssh): a refused RSA/ECDSA key is an authentication failure (NetOrk/netork#2512)
driver / lint (pull_request) Successful in 14s
driver / standard (pull_request) Successful in 17s
driver / types (pull_request) Successful in 1m0s
driver / test (3.13) (pull_request) Successful in 59s
driver / test (3.12) (pull_request) Successful in 1m6s
driver / lowest (pull_request) Successful in 32s
driver / build (pull_request) Successful in 31s
driver / security (pull_request) Successful in 1m2s
CI / driver (pull_request) Successful in 2m18s
driver / publish (pull_request) Skipped

open() handed the key file netOrk passes in key_file to paramiko as
key_filename. For a key file, SSHClient._auth tries the key as every key
type and keeps only the last error, so a host turning down an RSA or ECDSA
key raised "SSH to <host> failed: encountered RSA key, expected OPENSSH
key" instead of "Authentication failed.", which pointed at the key rather
than at the host that refused it. Ed25519 keys were not affected: they are
the last type tried.

The key is now loaded once with its own type (PKey.from_path) and passed
as pkey=, as netOrk's own SSH sessions do since NetOrk/netork#2480. A key
file that cannot be read raises ConnectionException before anything is
dialled.

Tested against an in-process SSH server on loopback, with RSA, ECDSA and
Ed25519 keys refused and accepted.
This commit is contained in:
Christian Manivong committed 2026-11-14 00:04:32 +01:00
1 parent 11c9d02b3e
commit fd1d90c88c
3 files changed
+160 -1

No files matched your search

+17 -1
View File
@@ -89,6 +89,21 @@ def render_snmpd_conf(settings: SnmpAgentSettingsDict) -> str:
return "\n".join(lines) + "\n"
def _load_key(key_file: str | None) -> paramiko.PKey | None:
"""The private key in *key_file*, loaded as the type it is; None without one.
Handed a key file, paramiko tries it as every key type and keeps the last
error, so a host turning down an RSA or ECDSA key read "encountered RSA key,
expected OPENSSH key" instead of the refusal it was (NetOrk/netork#2512).
"""
if not key_file:
return None
try:
return paramiko.PKey.from_path(key_file)
except (OSError, ValueError, TypeError, paramiko.UnknownKeyType) as exc:
raise ConnectionException(f"SSH key {key_file} could not be read: {exc}") from exc
class BsdDriver(
BsdScheduledJobsMixin,
SnmpAgentMixin,
@@ -150,13 +165,14 @@ class BsdDriver(
# -- connection --------------------------------------------------------------
def open(self) -> None:
pkey = _load_key(self._key_file)
client = paramiko.SSHClient()
try:
self._host_key.connect(
client,
username=self.username,
password=self.password or None,
key_filename=self._key_file,
pkey=pkey,
allow_agent=self._allow_agent,
look_for_keys=self._look_for_keys,
timeout=self.timeout,