Skip to content

[2.0] fix(pubsub): apply GCP service account impersonation when creating Pub/Sub clients - #35831

Merged
ulixius9 merged 1 commit into
2.0from
backport/2.0-pubsub-impersonation
Oct 21, 2026
Merged

ulixius9 merged 1 commit into
2.0from
backport/2.0-pubsub-impersonation

Conversation

@ulixius9

@ulixius9 ulixius9 commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Fixes #35830 (Pub/Sub part, 2.0 line)

Backport of #32808 to 2.0 (clean cherry-pick -x of c05a291).

Fixes the Pub/Sub half of #35830 (originally #32517) for 2.0.x: PublisherClient, SubscriberClient and SchemaServiceClient were built without credentials, so they fell back to ADC and ignored gcpImpersonateServiceAccount → GetTopics 403 IAM_PERMISSION_DENIED. 2.0.5 and 2.0.6 ship without this fix.

The GCS half of #35830 is not fixed on main either and will be a separate PR.

Testing

  • tests/unit/source/messaging/test_pubsub.py on this branch: 41 passed (includes the new impersonation / whitespace-SA / emulator-without-hostPort tests)
  • ruff format --check + ruff check clean on changed files

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 4/5

The credential change appears sound, but the new tests must satisfy the repository’s explicit testing and comments requirements before merging.

Findings

  1. P2 Tests skip real credentials ▶
  2. P2 Comments repeat the assertions ▶

Summary

The PR passes configured credentials to all three Pub/Sub clients.

  • Pub/Sub clients use the configured GCP identity for their requests.
  • Pub/Sub emulator connections need an address to start.

Reviews (1) · Last reviewed commit: "fix(pubsub): apply GCP service account i..." · Reviewed by Greptile

…b/Sub clients (#32517) (#32808)

* fix(pubsub): apply GCP service account impersonation when creating Pub/Sub clients (closes #32517)

When gcpImpersonateServiceAccount is configured, PublisherClient,
SubscriberClient, and SchemaServiceClient were constructed without
credentials, causing them to fall back to Application Default
Credentials (ADC) and authenticate as the source service account
rather than the configured target.  Every GCP API call on the
connector (GetTopics, GetSubscriptions, schema registry) then ran
under the wrong identity, resulting in 403 IAM_PERMISSION_DENIED
even though the target SA had the necessary Pub/Sub Viewer permissions.

Fix: after set_google_credentials() sets up the base identity, call
auth.default() to obtain the source credentials, then wrap them in
google.auth.impersonated_credentials.Credentials when the target SA
is configured.  Pass the resulting credential object explicitly to
every Pub/Sub client constructor.  Without impersonation the
gcp_credentials value stays as the plain ADC credential, keeping the
existing emulator and JSON-key paths unchanged.

The pattern mirrors the existing BigQuery and Google Drive connectors.

Add test_get_connection_with_impersonation to assert that:
- impersonated_credentials.Credentials is built with the correct
  source_credentials and target_principal;
- every client (publisher, subscriber, schema) receives the
  impersonated credential — not None or ADC.

Prepared with AI assistance (Claude Code, Anthropic), reviewed for correctness before submission.

* refactor(pubsub): delegate GCP credential building to shared helpers

Replace inline `auth.default()` + `impersonated_credentials.Credentials()`
with the project's existing `get_gcp_default_credentials()` and
`get_gcp_impersonate_credentials()` helpers from
`metadata.utils.credentials`, matching the pattern used by the BigQuery
connector (`utils/bigquery_utils.py`).

- Removes redundant `from google.auth import default as auth_default` and
  `from google.auth import impersonated_credentials` imports.
- `get_gcp_default_credentials()` calls `auth.default(scopes=...)` with
  `GOOGLE_CLOUD_SCOPES`, keeping credential scoping consistent with the
  rest of the codebase.
- `get_gcp_impersonate_credentials()` encapsulates the same
  source-credential fetch + impersonation wrap that was previously done
  inline.
- Tests updated to patch the shared helpers at their new call sites.

Prepared with AI assistance (Claude Code, Anthropic), reviewed for correctness before submission.

* fix(pubsub): address maintainer feedback — whitespace SA fallback and emulator guard

Three issues from @mohittilala's review:

1. Whitespace-only impersonation SA left all Pub/Sub clients with
   credentials=None (no API call would succeed). Added an inner else
   so a blank target falls back to get_gcp_default_credentials(), matching
   the behaviour of a missing impersonation config.

2. useEmulator=True without hostPort silently fell through to the GCP-
   credentials branch, raising a confusing "gcpConfig is required" error
   instead of a clear "hostPort is required" message. Restructured the
   outer guard so emulator mode is handled first, with an early ValueError
   when hostPort is absent.

3. Added two behavioural tests:
   - test_get_connection_whitespace_sa_falls_back_to_adc: verifies all
     three clients receive get_gcp_default_credentials() output when the
     SA is whitespace.
   - test_get_connection_emulator_without_hostport_raises: verifies a
     clear ValueError fires before any credential or client code runs.

All 10 TestPubSubConnection tests pass; ruff clean.

Prepared with AI assistance (Claude Code, Anthropic), reviewed for correctness before submission.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* Apply ruff format to pubsub connection and tests

* Accept credentials in the pubsub client stubs

The stubs declared no-arg constructors, matching how the connector built its clients when they were added. The real clients take a credentials argument, and passing it is how impersonation reaches Pub/Sub, so basedpyright was rejecting valid calls.

---------

Co-authored-by: Claude Sonnet 4.6 <[email protected]>
Co-authored-by: Mohit Tilala <[email protected]>
Co-authored-by: Mohit Tilala <[email protected]>
(cherry picked from commit c05a291)
@ulixius9
ulixius9 requested a review from a team as a code owner October 21, 2026 06:36
@ulixius9
ulixius9 requested review from mohittilala and removed request for a team October 21, 2026 06:36
@github-actions

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

  • No GitHub issue is linked. Link an issue in the Development section of the PR (or add Fixes #12345 to the description). For a same-org cross-repo issue, add Fixes open-metadata/<repo>#123 to the description.

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

@github-actions github-actions Bot added Ingestion safe to test Add this label to run secure Github workflows on PRs labels Oct 21, 2026
@gitar-bot

gitar-bot Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Code Review ✅ Approved

🔴 High risk · Passing configured impersonated credentials to Pub/Sub clients changes cloud authorization identity; misconfiguration could grant or deny topic and subscription access.

Backports the GCP service account impersonation fix for Pub/Sub clients to 2.0, so PublisherClient, SubscriberClient and SchemaServiceClient now receive impersonated credentials instead of falling back to ADC. No issues found.

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

Comment on lines +235 to +239
@patch("metadata.ingestion.source.messaging.pubsub.connection.get_gcp_impersonate_credentials")
@patch("metadata.ingestion.source.messaging.pubsub.connection.set_google_credentials")
@patch("metadata.ingestion.source.messaging.pubsub.connection.pubsub_v1.PublisherClient")
@patch("metadata.ingestion.source.messaging.pubsub.connection.pubsub_v1.SubscriberClient")
@patch("metadata.ingestion.source.messaging.pubsub.connection.SchemaServiceClient")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Tests skip real credentials

The new impersonation tests replace both credential helpers and all three clients, then only check calls between mocks. This violates the repository requirement to “Test real behavior, not mock wiring.” The tests can pass even if the real helpers fail with a real configuration.

Keep the real credential helpers and configuration models, mock Google authentication at the boundary, and check that the resulting credentials carry the requested account and lifetime. This repository requirement must be satisfied before merging.

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


PubSubConnection(mock_connection)._get_client()

# Shared helper must have been called with the correct target SA and lifetime.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Comments repeat the assertions

This comment and the one above the client assertions restate the code immediately below them. The repository explicitly says, “NEVER add single-line comments that describe what the code obviously does.” Remove both comments; the assertions already explain the checks. This repository requirement must be satisfied before merging.

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@sonarqubecloud

Copy link
Copy Markdown

@ulixius9
ulixius9 merged commit 78175fb into 2.0 Oct 21, 2026
61 of 66 checks passed
@ulixius9
ulixius9 deleted the backport/2.0-pubsub-impersonation branch October 21, 2026 15:38

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ingestion safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants