Skip to content

Commit 36cc336

Browse files
committed
Processor code must use ID token
1 parent 6f67847 commit 36cc336

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

functions-python/reverse_geolocation/src/reverse_geolocation_processor.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
geopolygons_as_string,
2525
)
2626
from parse_request import parse_request_parameters
27-
from shared.database.database import with_db_session, refresh_materialized_view
27+
from shared.database.database import with_db_session
2828
from shared.database_gen.sqlacodegen_models import (
2929
Geopolygon,
3030
Feed,
@@ -38,6 +38,10 @@
3838
)
3939
from shared.helpers.logger import get_logger
4040

41+
import google.auth
42+
from google.auth.transport.requests import Request
43+
import google.auth.transport.requests
44+
4145

4246
@with_db_session
4347
def get_cached_geopolygons(
@@ -378,11 +382,28 @@ def extract_location_aggregates(
378382
db_session.commit()
379383

380384
# Replace direct call to refresh_materialized_view with HTTP request to GCP function
381-
function_url = os.getenv("FUNCTION_URL_REFRESH_MV")
382-
if not function_url:
385+
refresh_url = os.getenv("FUNCTION_URL_REFRESH_MV")
386+
if not refresh_url:
383387
raise ValueError("FUNCTION_URL_REFRESH_MV environment variable is not set")
384388

385-
response = requests.get(f"{function_url}")
389+
# Fetch default credentials
390+
credentials, _ = google.auth.default()
391+
392+
# Get an ID token for the function
393+
id_token_credentials = (
394+
google.auth.transport.requests.IDTokenCredentials.from_credentials(
395+
credentials, target_audience=refresh_url
396+
)
397+
)
398+
399+
# Refresh the token
400+
id_token_credentials.refresh(Request())
401+
402+
# Make the authenticated request
403+
response = requests.get(
404+
refresh_url, headers={"Authorization": f"Bearer {id_token_credentials.token}"}
405+
)
406+
386407
response.raise_for_status()
387408
logger.info("Materialized view refresh event triggered successfully.")
388409

0 commit comments

Comments
 (0)