Skip to content

Commit

Permalink
fix(backup): Pull correct GCP credentials file path (#60038)
Browse files Browse the repository at this point in the history
  • Loading branch information
azaslavsky authored Nov 15, 2023
1 parent 3c4c8a6 commit 398c381
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/sentry/utils/env.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys

import requests
from django.conf import settings

from sentry.utils import json
Expand All @@ -14,12 +15,22 @@ def gcp_project_id() -> str:
if in_test_environment():
return "__test_gcp_project__"

adc_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", "")
if adc_path:
with open(adc_path) as fp:
adc = json.load(fp)
if adc.get("quota_project_id") is not None:
return adc.get("quota_project_id")
# Try the metadata endpoint, if possible. If not, we'll assume a local environment as use the
# app credentials env variable instead.
try:
return requests.get(
"http://metadata.google.internal/computeMetadata/v1/project/project-id",
headers={
"Metadata-Flavor": "Google",
},
).text
except requests.exceptions.RequestException:
adc_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", "")
if adc_path:
with open(adc_path) as fp:
adc = json.load(fp)
if adc.get("quota_project_id") is not None:
return adc.get("quota_project_id")

return "__unknown_gcp_project__"

Expand Down

0 comments on commit 398c381

Please sign in to comment.