Repository navigation
fix(pubsub): apply GCP service account impersonation when creating Pub/Sub clients (#32517) - #32808
Conversation
…b/Sub clients (closes open-metadata#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.
✅ PR checks passedThe linked issue has a description and all required Shipping project fields set. Thanks! |
|
Hi there 👋 Thanks for your contribution! The OpenMetadata team will review the PR shortly! Once it has been labeled as Let us know if you need any help! |
|
Hey @zerafachris, thanks for the contribution!
Worth deciding the scope list explicitly while you are in there: the shared helper defaults to Two smaller things: the credential block is gated on Cheers |
|
Thanks @mohittilala! Good catch. I've pushed a follow-up commit that delegates credential building to the shared |
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.
|
Addressed — replaced the inline |
|
Hi there 👋 Thanks for your contribution! The OpenMetadata team will review the PR shortly! Once it has been labeled as Let us know if you need any help! |
| @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.
This test replaces the credential helper and all three Pub/Sub client constructors, then only checks calls between those mocks. It therefore cannot exercise credential construction or show that an operation uses the target identity. The repository requires tests to assert observable behavior instead of mocked internal wiring, so this requirement must be satisfied before merging. Please retain real credential construction and mock only the external Google authentication or API boundary.
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!
|
@zerafachris helpers look right. One thing the refactor changed: a whitespace-only service account now ends up with no credentials at all, where before it fell back to the default. Nothing covers that branch in the tests. The emulator guard asymmetry from earlier is still there too, and worth a look at greptile's comment on the impersonation test. |
|
Thanks @mohittilala — all three points addressed in the latest commit:
|
|
@zerafachris nothing new landed here, still on 95291f6 from the 16th. sounds like the commit didn't get pushed? |
… 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]>
|
@mohittilala — apologies for the delay. The commit is now pushed (47e68df). Three issues addressed: 1. Whitespace-only SA falls back to ADC 2. Emulator guard asymmetry fixed 3. Two new behavioural tests
All 10 |
|
Hi there 👋 Thanks for your contribution! The OpenMetadata team will review the PR shortly! Once it has been labeled as Let us know if you need any help! |
|
Hi there 👋 Thanks for your contribution! The OpenMetadata team will review the PR shortly! Once it has been labeled as Let us know if you need any help! |
|
Hi there 👋 Thanks for your contribution! The OpenMetadata team will review the PR shortly! Once it has been labeled as Let us know if you need any help! |
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.
|
Hi there 👋 Thanks for your contribution! The OpenMetadata team will review the PR shortly! Once it has been labeled as Let us know if you need any help! |
Code Review ✅ Approved 1 closed / 1 findings🔴 High risk · Pub/Sub clients now use impersonated GCP identities for API authorization. Applies GCP service account impersonation to Pub/Sub clients by materializing credentials after ✅ 1 closed✅ Quality: Reimplements existing shared GCP impersonation helper
OptionsDisplay: compact → Counting what did not apply, without listing it. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source |
|
🚦 Removed from the merge queue —
|
c05a291
…b/Sub clients (#32517) (#32808) (#35831) * 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. * 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. --------- (cherry picked from commit c05a291) Co-authored-by: zerafachris <[email protected]> Co-authored-by: Claude Sonnet 4.6 <[email protected]> Co-authored-by: Mohit Tilala <[email protected]> Co-authored-by: Mohit Tilala <[email protected]>



Summary
Fixes #32517
Root Cause
When
gcpImpersonateServiceAccountis configured,_get_client()calledset_google_credentials()to set up the base identity and then constructedPublisherClient(),SubscriberClient(), andSchemaServiceClient()with no credentials argument. All three clients then fell back to Application Default Credentials (ADC) — the source service account — rather than the configured target SA. Every GCP API call (GetTopics,GetSubscriptions, schema registry) ran under the wrong identity, causing403 IAM_PERMISSION_DENIEDeven though the target SA hadroles/pubsub.viewer.What Changed
After
set_google_credentials()configures the base identity, the fix:google.auth.default()to materialise the source credentials.connection.gcpConfig.gcpImpersonateServiceAccount; if a non-empty target SA is configured, wraps the source credentials ingoogle.auth.impersonated_credentials.Credentials.PublisherClient,SubscriberClient,SchemaServiceClient).Without impersonation
gcp_credentialsstays as the plain ADC/JSON-key credential, so the emulator and JSON-key paths are unchanged.The pattern is identical to the existing BigQuery and Google Drive connectors.
Test Evidence
test_get_connection_with_project_id,test_get_connection_without_schema_registry, andtest_get_connection_missing_project_id_raisesto mockauth_default(newly called in the non-emulator path) and setgcpImpersonateServiceAccount = Noneto exercise the non-impersonation path.test_get_connection_with_impersonationasserting thatimpersonated_credentials.Credentialsis built with the correctsource_credentialsandtarget_principal, and that every client receives the impersonated credential.Prepared with AI assistance (Claude Code, Anthropic), reviewed for correctness before submission.
The credential propagation appears correct, but the explicit repository testing requirement remains unsatisfied and must be addressed before merging.
Findings
Summary
This PR propagates configured GCP credentials, including service-account impersonation, to all Pub/Sub clients and validates emulator configuration more explicitly.
The previous testing-rule finding remains outstanding: the added tests still replace credential helpers and client constructors and assert calls between mocks rather than observable authentication behavior.
Diagram
%%{init: {'theme': 'neutral'}}%% flowchart LR C[Pub/Sub connection configuration] --> E{Emulator enabled?} E -->|Yes| H[Validate emulator host] H --> P[Publisher and subscriber emulator clients] E -->|No| B[Configure base GCP identity] B --> I{Nonblank impersonation target?} I -->|Yes| IC[Create impersonated credentials] I -->|No| DC[Load default credentials] IC --> PC[Create Pub/Sub clients] DC --> PC PC --> PUB[Publisher client] PC --> SUB[Subscriber client] PC --> SCH[Optional schema client]Reviews (2) · Last reviewed commit: "fix(pubsub): address maintainer feedback..."