Repository navigation
fix: honor HostKeyAlias from ssh_config for host key verification - #2113
draarivpatel-ui wants to merge 4 commits into
Conversation
When an ssh_config entry sets HostKeyAlias, pyinfra verified the host key against the resolved HostName instead of the alias, so setups that reuse an internal address across hosts (for example behind ProxyJump) failed with a host key error where ssh connects. ssh stores keys for these setups under the alias in known_hosts, so register them under the hostname being connected to, plus the [hostname]:port form for non-default ports, after loading host keys. Fixes pyinfra-dev#2102.
Fizzadar
left a comment
There was a problem hiding this comment.
Couple of questions inline, the approach looks right otherwise!
| for key_type, key in self._host_keys.lookup(host_key_alias).items(): | ||
| for aliased_hostname in aliased_hostnames: | ||
| self._host_keys.add(aliased_hostname, key_type, key) |
There was a problem hiding this comment.
self._host_keys is the @memoize-d object shared between hosts, so this adds the alias's keys under the real hostname for every other host that connects there too. Could we do this on a copy, or only look up the alias when the hostname lookup misses?
| if port is not None and int(port) != 22: | ||
| aliased_hostnames.append(f"[{hostname}]:{port}") |
There was a problem hiding this comment.
Does paramiko look up the [host]:port form itself already? Wondering if the non-default port branch is needed.
|
Thanks for the review. Fixed in 3a23252. The alias keys are now added to a per-client copy of the host keys instead of mutating the shared memoized object. The alias is also only used when the real lookup name misses, so an explicitly known host key keeps priority. Yes, Paramiko already uses the [host]:port form for non-default ports. The copy now registers the alias under the exact name Paramiko will look up, rather than adding both forms. Validation: 78 passed for tests/test_connectors/test_ssh.py and tests/test_connectors/test_sshuserclient.py; mypy and ruff pass on the changed files. |
wowi42
left a comment
There was a problem hiding this comment.
The alias behavior looks sound in the checks I ran, but the new tests fail with the repository's locked Paramiko version. The new methods also need complete annotations.
| self.server_key = server_key | ||
|
|
||
|
|
||
| class FakeTransport: |
There was a problem hiding this comment.
Paramiko 3.5.1 passes gss_kex and gss_deleg_creds here and expects set_gss_host and gss_kex_used. With the locked version this fake raises TypeError before host-key verification. Green CI installed Paramiko 5.1.0, so it misses the failure. Please make the fake compatible across the supported range and run the focused test with 3.5.1.
| _entries: list[HostKeyEntry] | ||
| _pyinfra_copied_host_key_entry_ids: set[int] | ||
|
|
||
| def __init__(self, host_keys: HostKeys, server_hostkey_name: str, host_key_alias: str): |
There was a problem hiding this comment.
The new HostKeyAliasHostKeys and HostKeyAliasPolicy methods need complete parameter and return type annotations to match the project requirements.
fix: honor HostKeyAlias from ssh_config for host key verification
Fixes #2102.
Problem
When an ssh_config entry sets
HostKeyAlias, pyinfra verified the host key against the resolvedHostNameinstead of the alias. Setups where several hosts share an internal address, for example behindProxyJump, depend on the alias to pick the rightknown_hostsentry.Fix
The alias is treated as the host-key identity only for the duration of the aliased connection. The wrapper copies Paramiko host-key entries and their hostname lists directly rather than rebuilding them through hostname lookup, so a matching hashed entry cannot replace a distinct plain physical key. It maps certificate authority lookups through the alias, preserves physical host-key identities when saving, and restores the original host-key stores and missing-host-key policy after connect. A later aliasless connection on the same client therefore cannot inherit alias state.
Save enumeration now preserves Paramiko's first-match lookup precedence. Repeated stored records for one hostname and key type are scanned once in stored order, and the first record wins instead of a later dict-comprehension overwrite silently changing which key is persisted. Exact and hashed matches are indexed by entry position, avoiding the previous full-entry rescan for every hostname.
Tests cover conflicting physical keys, non-default ports, accept-new persistence with an existing physical key, overlapping hashed and plain entries, mutation isolation, same-client reuse, positive alias certificate identity, rejection of a certificate authority scoped only to the physical host, rejection of a wrong-principal certificate signed by an alias-authorized CA, and a file round trip in which repeated exact physical-host records survive an unrelated accepted alias without changing key precedence.
Verified locally under
taskpolicy -c background nice -n 15:uv run pytest -q tests/test_connectors/test_ssh.py -k 'HostKeyAlias or host_certificate_identity or host_certificate_ca or host_certificate_wrong_principal or repeated_physical_keys'- 13 passed, 45 deselected.uv run ruff check src/pyinfra/connectors/sshuserclient/client.py tests/test_connectors/conftest.py tests/test_connectors/test_ssh.py- passed.uv run ruff format --check src/pyinfra/connectors/sshuserclient/client.py tests/test_connectors/conftest.py tests/test_connectors/test_ssh.py- passed.AI assistance disclosure: this patch was prepared and locally remediated with AI coding assistance. Maintainers should review it as they would any other contribution.