: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(bigquery): treat rejected service account credentials as non-retryable (#63695)
Tom Owersaf7da42b084f
diff --git a/posthog/temporal/data_imports/sources/bigquery/source.py b/posthog/temporal/data_imports/sources/bigquery/source.pyindex fd27edfbce4a..cb4530e54275 100644--- a/posthog/temporal/data_imports/sources/bigquery/source.py+++ b/posthog/temporal/data_imports/sources/bigquery/source.py@@ -42,6 +42,14 @@ class BigQuerySource(SQLSource[BigQuerySourceConfig]):return {"PermissionDenied: 403 request failed": "BigQuery permission denied. Please check that your service account has the necessary permissions.","NotFound: 404": "BigQuery dataset or table not found. Please verify your project, dataset, and table names.",+ # OAuth2 error code returned by Google's token endpoint when the service account grant+ # is rejected — a rotated/revoked private key ("Invalid JWT Signature") or a deleted+ # service account ("account not found"). Raised as a `RefreshError` while refreshing the+ # token, so it surfaces on every BigQuery call rather than at a single site. Retrying+ # can't recover invalid credentials; the user must upload a new key file. Matched on the+ # stable `invalid_grant` code rather than `RefreshError`, which can also wrap transient+ # token-endpoint failures that should stay retryable.+ "invalid_grant": "Your BigQuery service account credentials were rejected by Google. The key may have been rotated or revoked, or the service account deleted. Please upload a new Google Cloud JSON key file.",# Raised from the shared `evolve_pyarrow_schema` in `pipelines/pipeline/utils.py`# when an integer column's source type was widened (e.g. `INT64` widened from a# narrower numeric type) after the destination table was created with the narrowerdiff --git a/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py b/posthog/temporal/data_imports/sources/bigquery/tests/test_source.pyindex 050dba0c17e5..5a8820713b6e 100644--- a/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py+++ b/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py@@ -245,6 +245,35 @@ def test_bigquery_select_clause_rejects_injection_attempts(malicious_column):_bq_select_clause([malicious_column], primary_keys=None, incremental_field=None)+ "observed_error",+ [+ # Rotated/revoked service account private key.+ "('invalid_grant: Invalid JWT Signature.', {'error': 'invalid_grant', 'error_description': 'Invalid JWT Signature.'})",+ # Deleted service account.+ "('invalid_grant: Invalid grant: account not found', {'error': 'invalid_grant', 'error_description': 'Invalid grant: account not found'})",+ ],+)+def test_non_retryable_errors_match_rejected_credentials(observed_error):+ """A `RefreshError` carrying the OAuth2 `invalid_grant` code means Google rejected the+ service account grant — retrying can't recover, so the sync must be disabled."""+ non_retryable_errors = BigQuerySource().get_non_retryable_errors()+ assert any(key in observed_error for key in non_retryable_errors)+++ "transient_error",+ [+ # A token refresh that failed for a transient reason must stay retryable.+ "RefreshError: ('Failed to retrieve token', {'error': 'internal_failure'})",+ "RefreshError: HTTPError 503 Service Unavailable",+ ],+)+def test_non_retryable_errors_does_not_match_transient_refresh_failures(transient_error):+ non_retryable_errors = BigQuerySource().get_non_retryable_errors()+ assert not any(key in transient_error for key in non_retryable_errors)++def _run_delete_all_temp_destination_tables(side_effect, logger):bq = mock.MagicMock()bq.list_tables.side_effect = side_effect