Skip to content

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

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

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

Conversation

@draarivpatel-ui

@draarivpatel-ui draarivpatel-ui commented Nov 25, 2026 •

Copy link
Copy Markdown

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 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.

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.

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.

@wowi42 wowi42 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The new HostKeyAliasHostKeys and HostKeyAliasPolicy methods need complete parameter and return type annotations to match the project requirements.

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