Skip to content

Commit

Permalink
fix: refresh token prior to metadata exchange (#351)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
enocom authored Jul 22, 2024
1 parent 21a54c1 commit 1ab11e6
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions google/cloud/alloydb/connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
from typing import Any, Dict, Optional, Type, TYPE_CHECKING, Union

from google.auth import default
from google.auth.credentials import TokenState
from google.auth.credentials import with_scopes_if_required
from google.auth.transport import requests

from google.cloud.alloydb.connector.client import AlloyDBClient
from google.cloud.alloydb.connector.enums import IPTypes
Expand Down Expand Up @@ -258,6 +260,10 @@ def metadata_exchange(
if enable_iam_auth:
auth_type = connectorspb.MetadataExchangeRequest.AUTO_IAM

# Ensure the credentials are in fact valid before proceeding.
if not self._credentials.token_state == TokenState.FRESH:
self._credentials.refresh(requests.Request())

# form metadata exchange request
req = connectorspb.MetadataExchangeRequest(
user_agent=f"{self._client._user_agent}", # type: ignore
Expand Down

0 comments on commit 1ab11e6

Please sign in to comment.