Skip to content

fix: honor HostKeyAlias from ssh_config for host key verification - #2113

Open
draarivpatel-ui wants to merge 2 commits into
pyinfra-dev:3.xfrom
draarivpatel-ui:ssh-hostkeyalias
Open

draarivpatel-ui wants to merge 2 commits into
pyinfra-dev:3.xfrom
draarivpatel-ui:ssh-hostkeyalias

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Fixes #2102.

Problem

When an ssh_config entry sets HostKeyAlias, pyinfra verifies the host key against the resolved HostName instead of the alias. Setups where several hosts share an internal address (for example behind ProxyJump) depend on the alias to pick the right known_hosts entry, so ssh connects while pyinfra fails with a host key error:

Host rhost-a
  HostName 10.0.0.2
  HostKeyAlias rhost-a
  ProxyJump some.example.net

Fix

OpenSSH stores host keys for these setups under the alias in known_hosts. After loading the host keys in SSHClient.connect, the keys registered under the alias are now also registered under the hostname being connected to, plus the [hostname]:port form for non-default ports. The alias is threaded out of parse_config through a private config key that is popped before anything reaches paramiko.

Tests

New tests in tests/test_connectors/test_ssh.py cover the alias registration under the connect hostname, the [hostname]:port form for a non-default port, and that no port entry is written for the default port. The full tests/test_connectors/ suite passes (268 passed) along with ruff check, ruff format --check and mypy on the changed files.

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.
Copilot AI balanced review requested due to automatic review settings November 25, 2026 04:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@wowi42 wowi42 added the connectors Connector issues - builtin integrations with other tools. label Nov 25, 2026
@draarivpatel-ui draarivpatel-ui changed the title Honor HostKeyAlias from ssh_config for host key verification fix: Honor HostKeyAlias from ssh_config for host key verification Nov 25, 2026
@draarivpatel-ui draarivpatel-ui changed the title fix: Honor HostKeyAlias from ssh_config for host key verification fix: honor HostKeyAlias from ssh_config for host key verification Nov 25, 2026

@Fizzadar Fizzadar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of questions inline, the approach looks right otherwise!

Comment on lines +427 to +429
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment on lines +425 to +426
if port is not None and int(port) != 22:
aliased_hostnames.append(f"[{hostname}]:{port}")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does paramiko look up the [host]:port form itself already? Wondering if the non-default port branch is needed.

@draarivpatel-ui

Copy link
Copy Markdown
Author

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

connectors Connector issues - builtin integrations with other tools.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ssh connector: support ssh_config HostKeyAlias

4 participants