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 the impersonated token permission error (#108064)

Tom Owers62d59aee8da6
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py
index b11c28adbfc5..3ab44fff72ec 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py
@@ -839,6 +839,17 @@ def classify_bigquery_validation_error(e: Exception) -> str:
return BIGQUERY_INVALID_KEY_FILE_ERROR
if "invalid_grant" in message:
return BIGQUERY_CREDENTIALS_REJECTED_ERROR
+ if "iam.serviceAccounts.getAccessToken" in message and "denied" in message:
+ # Raised as a `RefreshError` when Google rejects the impersonated-credentials token
+ # refresh: the ownership check only needs `iam.serviceAccounts.get`, so this can fail
+ # even though that check passed. Same remediation (grant Token Creator) as
+ # `BIGQUERY_IMPERSONATION_PERMISSION_ERROR`, so reuse its wording. Doesn't match the
+ # generic "Access Denied"/"PermissionDenied"/"permission denied" wording below (this
+ # message reads "Permission '...' denied on resource"), so it would otherwise fall
+ # through to the generic message and get captured as unexpected noise. Requiring
+ # "denied" alongside the permission name keeps this from matching an opaque
+ # `RefreshError` that merely mentions the permission without actually denying it.
+ return BIGQUERY_IMPERSONATION_PERMISSION_ERROR
if (
"Invalid project ID" in message
or "Invalid dataset ID" in message
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 9a05090b2d9e..440e9fb08fe2 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
@@ -29,6 +29,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.b
BIGQUERY_CREATE_READ_SESSION_RETRY,
BIGQUERY_CREDENTIALS_REJECTED_ERROR,
BIGQUERY_DATASET_NOT_FOUND_ERROR,
+ BIGQUERY_IMPERSONATION_PERMISSION_ERROR,
BIGQUERY_INTEGRATION_NOT_FOUND_ERROR,
BIGQUERY_INVALID_IDENTIFIER_ERROR,
BIGQUERY_INVALID_KEY_FILE_ERROR,
@@ -1319,6 +1320,22 @@ def test_bigquery_rejects_non_google_token_uri_before_building_credentials(token
False,
),
(RefreshError("('invalid_grant: Invalid JWT Signature.', {})"), BIGQUERY_CREDENTIALS_REJECTED_ERROR, False),
+ (
+ RefreshError(
+ "('Unable to acquire impersonated credentials', "
+ '\'{"error": {"code": 403, "message": "Permission \\\'iam.serviceAccounts.getAccessToken\\\' '
+ 'denied on resource (or it may not exist).", "status": "PERMISSION_DENIED"}}\')'
+ ),
+ BIGQUERY_IMPERSONATION_PERMISSION_ERROR,
+ False,
+ ),
+ (
+ # Names the permission without denying it, so it must not match the check above and
+ # should fall through to the generic (captured) branch.
+ RefreshError("('Unable to acquire impersonated credentials', 'iam.serviceAccounts.getAccessToken')"),
+ BIGQUERY_VALIDATION_GENERIC_ERROR,
+ True,
+ ),
(BadRequest('Invalid dataset ID "(default)"'), BIGQUERY_INVALID_IDENTIFIER_ERROR, False),
(BadRequest("400 ProjectId must be non-empty"), BIGQUERY_INVALID_IDENTIFIER_ERROR, False),
(