Skip to content

Commit eeee7bb

Browse files
authored
chore: Fallback to inputs if impersonation fails (#52052)
1 parent e151aa2 commit eeee7bb

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

posthog/models/integration.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,15 +1189,6 @@ def integration_from_service_account(
11891189
token_uri: str | None = None,
11901190
created_by: User | None = None,
11911191
) -> Integration:
1192-
sensitive_config = {}
1193-
1194-
is_impersonated = True
1195-
if isinstance(private_key, str) and isinstance(private_key_id, str) and isinstance(token_uri, str):
1196-
sensitive_config["private_key"] = private_key
1197-
sensitive_config["private_key_id"] = private_key_id
1198-
sensitive_config["token_uri"] = token_uri
1199-
is_impersonated = False
1200-
12011192
# Do not allow the same project_id in multiple organizations
12021193
same_service_account_integrations = Integration.objects.select_related("team__organization").filter(
12031194
kind="google-cloud-service-account", config__service_account_email=service_account_email
@@ -1206,6 +1197,15 @@ def integration_from_service_account(
12061197
if str(integration.team.organization.id) != str(organization_id):
12071198
raise ValidationError("Cannot create Google Cloud service account integration: Invalid service account")
12081199

1200+
sensitive_config = {}
1201+
is_impersonated = True
1202+
if isinstance(private_key, str) and isinstance(private_key_id, str) and isinstance(token_uri, str):
1203+
sensitive_config["private_key"] = private_key
1204+
sensitive_config["private_key_id"] = private_key_id
1205+
sensitive_config["token_uri"] = token_uri
1206+
1207+
is_impersonated = False
1208+
12091209
variant = "impersonated" if is_impersonated else "key-file"
12101210

12111211
integration, _ = Integration.objects.update_or_create(

products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,22 @@ async def insert_into_bigquery_activity_from_stage(inputs: BigQueryInsertInputs)
13381338
google_cloud_integration.service_account_email, inputs.team_id
13391339
)
13401340
await ensure_our_google_cloud_credentials_are_valid()
1341-
bq_client = BigQueryClient.from_service_account_integration(google_cloud_integration)
1341+
try:
1342+
bq_client = BigQueryClient.from_service_account_integration(google_cloud_integration)
1343+
except Exception:
1344+
LOGGER.exception("Initialize client from service account failed")
1345+
# TODO: Migrate everyone and remove this
1346+
if (
1347+
inputs.private_key is None
1348+
or inputs.private_key_id is None
1349+
or inputs.token_uri is None
1350+
or inputs.client_email is None
1351+
):
1352+
# We cannot fallback to using inputs
1353+
raise
1354+
bq_client = BigQueryClient.from_service_account_inputs(
1355+
inputs.private_key, inputs.private_key_id, inputs.token_uri, inputs.client_email, project_id
1356+
)
13421357

13431358
else:
13441359
# TODO: Migrate everyone and remove this

0 commit comments

Comments
 (0)