Skip to content

Commit 1ab11e6

Browse files
authored
fix: refresh token prior to metadata exchange (#351)
In heavy usage, there can be cases where the client does not refresh the token but then the token expires prior to the metadata exchange. This commit ensures that the token is always fresh before proceeding to the metadata exchange. Fixes #346
1 parent 21a54c1 commit 1ab11e6

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

google/cloud/alloydb/connector/connector.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
from typing import Any, Dict, Optional, Type, TYPE_CHECKING, Union
2424

2525
from google.auth import default
26+
from google.auth.credentials import TokenState
2627
from google.auth.credentials import with_scopes_if_required
28+
from google.auth.transport import requests
2729

2830
from google.cloud.alloydb.connector.client import AlloyDBClient
2931
from google.cloud.alloydb.connector.enums import IPTypes
@@ -258,6 +260,10 @@ def metadata_exchange(
258260
if enable_iam_auth:
259261
auth_type = connectorspb.MetadataExchangeRequest.AUTO_IAM
260262

263+
# Ensure the credentials are in fact valid before proceeding.
264+
if not self._credentials.token_state == TokenState.FRESH:
265+
self._credentials.refresh(requests.Request())
266+
261267
# form metadata exchange request
262268
req = connectorspb.MetadataExchangeRequest(
263269
user_agent=f"{self._client._user_agent}", # type: ignore

0 commit comments

Comments
 (0)