Skip to content

Add host_key_policy option to ComputeEngineSSHHook#66746

Merged
potiuk merged 2 commits into
apache:mainfrom
potiuk:add-host-key-policy-option-to-compute-engine-ssh-hook
May 13, 2026
Merged

Add host_key_policy option to ComputeEngineSSHHook#66746
potiuk merged 2 commits into
apache:mainfrom
potiuk:add-host-key-policy-option-to-compute-engine-ssh-hook

Conversation

@potiuk
Copy link
Copy Markdown
Member

@potiuk potiuk commented May 12, 2026

Summary

Expose paramiko's MissingHostKeyPolicy choice as a constructor argument on ComputeEngineSSHHook, so callers can opt into strict host-key verification on the SSH transport. The hook previously hard-coded paramiko.AutoAddPolicy, which means callers who wanted the remote host authenticated had no way to ask for it.

The new host_key_policy argument accepts:

  • the string aliases "auto_add", "reject" and "warning" — mapped to the matching paramiko policy classes;
  • any custom paramiko.MissingHostKeyPolicy instance — so a caller that wants to pin the remote host's key from GCE guest attributes / instance metadata can plug in a policy that loads it on the fly.

The default is "auto_add", preserving the historical behaviour of the hook; no migration is required for existing callers.

The previous inline comment that claimed the missing host-key check was unrelated to the local private key is removed — it conflated two different concerns — and replaced with a pointer to the new constructor argument.

Files changed

  • providers/google/src/airflow/providers/google/cloud/hooks/compute_ssh.py — new host_key_policy parameter, helper resolver, applied in _connect_to_instance. Misleading comment removed.
  • providers/google/tests/unit/google/cloud/hooks/test_compute_ssh.py — new TestHostKeyPolicyResolution class (4 cases: default, string aliases, custom instance, unknown-string error).

Test plan

  • uv run --project providers/google pytest providers/google/tests/unit/google/cloud/hooks/test_compute_ssh.py — 22 / 22 pass
  • prek run --from-ref upstream/main --stage pre-commit — clean

Migration

None. Callers that don't pass host_key_policy get the same paramiko AutoAddPolicy behaviour as before.

Was generative AI tooling used to co-author this PR?
  • Yes — Claude Opus 4.7 (1M context)

Generated-by: Claude Opus 4.7 (1M context) following the guidelines at https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions

Exposes paramiko's `MissingHostKeyPolicy` choice as a constructor
argument so callers can opt into strict host-key verification on the
SSH transport. The argument accepts the string aliases `"auto_add"`,
`"reject"` and `"warning"` (which map to the matching `paramiko`
policy classes) and also passes through any custom
`paramiko.MissingHostKeyPolicy` instance — so a caller that wants to
pin the remote host's key from GCE guest attributes / instance
metadata can plug in a policy that loads it on the fly.

The default is `"auto_add"`, preserving the historical behaviour of
this hook; no migration is required for existing callers. The
previous inline comment claiming the missing host-key check was
unrelated to the local private key is removed — it conflated two
different concerns and is replaced with a pointer to the new
constructor argument.

Generated-by: Claude Opus 4.7 (1M context) following the guidelines at
https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions
@potiuk potiuk requested a review from shahar1 as a code owner May 12, 2026 00:50
@boring-cyborg boring-cyborg Bot added area:providers provider:google Google (including GCP) related issues labels May 12, 2026
Comment thread providers/google/src/airflow/providers/google/cloud/hooks/compute_ssh.py Outdated
Comment thread providers/google/tests/unit/google/cloud/hooks/test_compute_ssh.py Outdated
Comment thread providers/google/src/airflow/providers/google/cloud/hooks/compute_ssh.py Outdated
…port

@shahar1 review on PR apache#66746:

* Add :param host_key_policy: to the ComputeEngineSSHHook docstring so
  users see the option without reading the source.
* Fix the _resolve_host_key_policy() docstring to say ValueError (matches
  what is actually raised) instead of AirflowException.
* Re-raise the unknown-policy ValueError with from None so the KeyError
  implementation detail doesn't leak into the chained traceback.
* Move 'import paramiko' to module top in test_compute_ssh.py; the
  function-local imports were unnecessary now that the test class is
  permanent.
@potiuk potiuk merged commit 120dbed into apache:main May 13, 2026
94 checks passed
@potiuk potiuk deleted the add-host-key-policy-option-to-compute-engine-ssh-hook branch May 13, 2026 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:google Google (including GCP) related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants