|
24 | 24 | from dataclasses import dataclass
|
25 | 25 |
|
26 | 26 | from google.auth.compute_engine import Credentials as ComputeEngineCredentials
|
| 27 | +from google.auth.credentials import TokenState |
27 | 28 | from google.auth.exceptions import RefreshError
|
28 | 29 | from google.auth.transport import requests as google_auth_requests
|
29 | 30 |
|
@@ -103,12 +104,6 @@ def __init__(self, app: App):
|
103 | 104 | 'GOOGLE_CLOUD_PROJECT environment variable.')
|
104 | 105 |
|
105 | 106 | self._credential = app.credential.get_credential()
|
106 |
| - try: |
107 |
| - # Refresh the credential to ensure all attributes (e.g. service_account_email) |
108 |
| - # are populated, preventing cold start errors. |
109 |
| - self._credential.refresh(google_auth_requests.Request()) |
110 |
| - except RefreshError as err: |
111 |
| - raise ValueError(f'Initial credential refresh failed: {err}') from err |
112 | 107 | self._http_client = _http_client.JsonHttpClient(credential=self._credential)
|
113 | 108 |
|
114 | 109 | def task_queue(self, function_name: str, extension_id: Optional[str] = None) -> TaskQueue:
|
@@ -294,6 +289,15 @@ def _update_task_payload(self, task: Task, resource: Resource, extension_id: str
|
294 | 289 | # Get function url from task or generate from resources
|
295 | 290 | if not _Validators.is_non_empty_string(task.http_request['url']):
|
296 | 291 | task.http_request['url'] = self._get_url(resource, _FIREBASE_FUNCTION_URL_FORMAT)
|
| 292 | + |
| 293 | + # Refresh the credential to ensure all attributes (e.g. service_account_email, id_token) |
| 294 | + # are populated, preventing cold start errors. |
| 295 | + if self._credential.token_state != TokenState.FRESH: |
| 296 | + try: |
| 297 | + self._credential.refresh(google_auth_requests.Request()) |
| 298 | + except RefreshError as err: |
| 299 | + raise ValueError(f'Initial task payload credential refresh failed: {err}') from err |
| 300 | + |
297 | 301 | # If extension id is provided, it emplies that it is being run from a deployed extension.
|
298 | 302 | # Meaning that it's credential should be a Compute Engine Credential.
|
299 | 303 | if _Validators.is_non_empty_string(extension_id) and \
|
|
0 commit comments