Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refresh credentials for valid oauth2 token on each refresh #894

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions google/cloud/sql/connector/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from cryptography.x509 import load_pem_x509_certificate

from google.auth.credentials import Credentials
import google.auth.transport.requests
from google.cloud.sql.connector.exceptions import (
AutoIAMAuthNotSupported,
CloudSQLIPTypeError,
Expand Down Expand Up @@ -193,7 +194,7 @@ def _client_session(self) -> aiohttp.ClientSession:
self.__client_session = aiohttp.ClientSession(headers=headers)
return self.__client_session

_credentials: Optional[Credentials] = None
_credentials: Credentials
_keys: asyncio.Future

_instance_connection_string: str
Expand Down Expand Up @@ -287,6 +288,10 @@ async def _perform_refresh(self) -> ConnectionInfo:

logger.debug(f"['{self._instance_connection_string}']: Creating context")

# refresh credentials for fresh OAuth2 token
request = google.auth.transport.requests.Request()
self._credentials.refresh(request)

metadata_task = self._loop.create_task(
_get_metadata(
self._client_session,
Expand Down Expand Up @@ -330,9 +335,10 @@ async def _perform_refresh(self) -> ConnectionInfo:
)
expiration = x509.not_valid_after

# for IAM authentication OAuth2 token is embedded in cert so it
# must still be valid for successful connection
if self._enable_iam_auth:
if self._credentials is not None:
token_expiration: datetime.datetime = self._credentials.expiry
token_expiration: datetime.datetime = self._credentials.expiry
if expiration > token_expiration:
expiration = token_expiration

Expand Down
8 changes: 0 additions & 8 deletions google/cloud/sql/connector/refresh_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ async def _get_metadata(
elif not isinstance(instance, str):
raise TypeError(f"instance must be of type str, got {type(instance)}")

if not credentials.valid:
request = google.auth.transport.requests.Request()
credentials.refresh(request)

headers = {
"Authorization": f"Bearer {credentials.token}",
}
Expand Down Expand Up @@ -181,10 +177,6 @@ async def _get_ephemeral(
elif not isinstance(pub_key, str):
raise TypeError(f"pub_key must be of type str, got {type(pub_key)}")

if not credentials.valid:
request = google.auth.transport.requests.Request()
credentials.refresh(request)

headers = {
"Authorization": f"Bearer {credentials.token}",
}
Expand Down
Loading