Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(bigquery): classify impersonation token-creator denial as non-retryable (#116645)

Tom Owersb17f3e21c29a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/source.py
index b8276e1b4328..cc986cbaf338 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/source.py
@@ -131,6 +131,22 @@ class BigQuerySource(SQLSource[BigQuerySourceConfig]):
# need a change in the customer's Google Cloud project, so retrying can't recover them.
BIGQUERY_SERVICE_ACCOUNT_NOT_FOUND_ERROR: BIGQUERY_SERVICE_ACCOUNT_NOT_FOUND_ERROR,
BIGQUERY_IMPERSONATION_PERMISSION_ERROR: BIGQUERY_IMPERSONATION_PERMISSION_ERROR,
+ # The ownership check above only needs `iam.serviceAccounts.get`, so it can pass while a
+ # later gRPC call (the Storage Read API's `create_read_session`/`read_rows`) still fails
+ # to mint a token, because minting one needs `iam.serviceAccounts.getAccessToken` too.
+ # grpc's auth metadata plugin surfaces that denial as a `ServiceUnavailable` wrapping
+ # Google's raw token-endpoint JSON, e.g. "Getting metadata from plugin failed with
+ # error: ('Unable to acquire impersonated credentials', '{"error": {"code": 403,
+ # "message": "Permission \'iam.serviceAccounts.getAccessToken\' denied on resource ..." ...'".
+ # Same remediation as the ownership check — grant Service Account Token Creator — so reuse
+ # its message. Deterministic IAM config problem; retrying can't grant the permission.
+ # Matched on the stable permission name plus "denied", not the volatile error id or
+ # troubleshooter URL. Mirrors the same check in `classify_bigquery_validation_error`,
+ # which covers this for the connection wizard rather than an in-progress sync. Python's
+ # `repr()` of the JSON body (inside the tuple `str()`) backslash-escapes the quotes
+ # around the permission name, so match both the raw and escaped forms.
+ "iam.serviceAccounts.getAccessToken' denied": BIGQUERY_IMPERSONATION_PERMISSION_ERROR,
+ "iam.serviceAccounts.getAccessToken\\' denied": BIGQUERY_IMPERSONATION_PERMISSION_ERROR,
BIGQUERY_OWNERSHIP_UNVERIFIED_ERROR_PREFIX: None,
# Writing query results into the `__posthog_import_...` temp tables PostHog creates
# (`WRITE_TRUNCATE` in `_run_destination_query_with_job_retry`, on incremental / view /
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py
index 3b42938c68c5..df149aa7f467 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py
@@ -1122,6 +1122,28 @@ def test_non_retryable_errors_match_federated_upstream_permission_denied(observe
assert any(key in observed_error for key in non_retryable_errors)
+ "observed_error",
+ [
+ # The Storage Read API's gRPC auth plugin raises this `ServiceUnavailable` when it can't
+ # mint a fresh token for an impersonated service account — a denial of
+ # `iam.serviceAccounts.getAccessToken`, distinct from the `iam.serviceAccounts.get` the
+ # ownership check already covers via `BIGQUERY_IMPERSONATION_PERMISSION_ERROR`.
+ str(
+ ServiceUnavailable(
+ "Getting metadata from plugin failed with error: ('Unable to acquire impersonated "
+ 'credentials\', \'{"error": {"code": 403, "message": "Permission '
+ "\\'iam.serviceAccounts.getAccessToken\\' denied on resource (or it may not exist).\", "
+ '"status": "PERMISSION_DENIED"}}\')'
+ )
+ ),
+ ],
+)
+def test_non_retryable_errors_match_impersonation_token_denied(observed_error):
+ non_retryable_errors = BigQuerySource().get_non_retryable_errors()
+ assert any(key in observed_error for key in non_retryable_errors)
+
+
@pytest.mark.parametrize(
"observed_error",
[