Repository navigation
fix: honor HostKeyAlias from ssh_config for host key verification - #2113
draarivpatel-ui wants to merge 2 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. |
Fixes #2102.
Problem
When an ssh_config entry sets
HostKeyAlias, pyinfra verifies 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, sosshconnects while pyinfra fails with a host key error:Fix
OpenSSH stores host keys for these setups under the alias in
known_hosts. After loading the host keys inSSHClient.connect, the keys registered under the alias are now also registered under the hostname being connected to, plus the[hostname]:portform for non-default ports. The alias is threaded out ofparse_configthrough a private config key that is popped before anything reaches paramiko.Tests
New tests in
tests/test_connectors/test_ssh.pycover the alias registration under the connect hostname, the[hostname]:portform for a non-default port, and that no port entry is written for the default port. The fulltests/test_connectors/suite passes (268 passed) along withruff check,ruff format --checkandmypyon the changed files.