Repository navigation
[2.0] fix(pubsub): apply GCP service account impersonation when creating Pub/Sub clients - #35831
Conversation
…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)
❌ PR checklist incompleteThis PR cannot be merged until the following are addressed on its linked issue:
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 |
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 VerificationRules No rules evaluated Functional validation Not enabledAuto-approval Not enabledFor more verification details, comment OptionsDisplay: compact → Showing compact verification results. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source |
| @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") |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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!
|



Fixes #35830 (Pub/Sub part, 2.0 line)
Backport of #32808 to
2.0(cleancherry-pick -xof c05a291).Fixes the Pub/Sub half of #35830 (originally #32517) for 2.0.x:
PublisherClient,SubscriberClientandSchemaServiceClientwere built without credentials, so they fell back to ADC and ignoredgcpImpersonateServiceAccount→GetTopics403IAM_PERMISSION_DENIED. 2.0.5 and 2.0.6 ship without this fix.The GCS half of #35830 is not fixed on
maineither and will be a separate PR.Testing
tests/unit/source/messaging/test_pubsub.pyon this branch: 41 passed (includes the new impersonation / whitespace-SA / emulator-without-hostPort tests)ruff format --check+ruff checkclean on changed files🤖 Generated with Claude Code
The credential change appears sound, but the new tests must satisfy the repository’s explicit testing and comments requirements before merging.
Findings
Summary
The PR passes configured credentials to all three Pub/Sub clients.
Reviews (1) · Last reviewed commit: "fix(pubsub): apply GCP service account i..." · Reviewed by Greptile