Skip to content

Commit 6fb1e86

Browse files
chore: Refactor mTLS channel reconfiguration logic for adding mTLS check after 401 check
chore: Refactor mTLS channel reconfiguration logic for adding mTLS check after 401 check
1 parent 1c068dc commit 6fb1e86

1 file changed

Lines changed: 38 additions & 34 deletions

File tree

  • packages/google-auth/google/auth/aio/transport

packages/google-auth/google/auth/aio/transport/sessions.py

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
ClientTimeout = None
4141

4242
_LOGGER = logging.getLogger(__name__)
43+
MTLS_URL_PREFIXES = ["mtls.googleapis.com", "mtls.sandbox.googleapis.com"]
4344

4445

4546
# Tracks the internal aiohttp installation and usage
@@ -315,43 +316,46 @@ async def request(
315316
)
316317
)
317318
if response.status_code == http_client.UNAUTHORIZED:
318-
try:
319-
(
320-
call_cert_bytes,
321-
call_key_bytes,
322-
cached_fingerprint,
323-
current_cert_fingerprint,
324-
) = await mtls._run_in_executor(
325-
google.auth.transport._mtls_helper.check_parameters_for_unauthorized_response,
326-
self._cached_cert,
327-
)
328-
if cached_fingerprint != current_cert_fingerprint:
329-
try:
319+
if getattr(self, "is_mtls", False) and any(
320+
prefix in url for prefix in MTLS_URL_PREFIXES
321+
):
322+
try:
323+
(
324+
call_cert_bytes,
325+
call_key_bytes,
326+
cached_fingerprint,
327+
current_cert_fingerprint,
328+
) = await mtls._run_in_executor(
329+
google.auth.transport._mtls_helper.check_parameters_for_unauthorized_response,
330+
self._cached_cert,
331+
)
332+
if cached_fingerprint != current_cert_fingerprint:
333+
try:
334+
_LOGGER.info(
335+
"Client certificate has changed, reconfiguring mTLS "
336+
"channel."
337+
)
338+
if self._mtls_init_task and self._mtls_init_task.done():
339+
self._mtls_init_task = None
340+
await self.configure_mtls_channel(
341+
lambda: (call_cert_bytes, call_key_bytes)
342+
)
343+
continue
344+
except Exception as e:
345+
_LOGGER.warning(
346+
"Failed to reconfigure mTLS channel: %s. Proceeding with original response.",
347+
e,
348+
)
349+
else:
330350
_LOGGER.info(
331-
"Client certificate has changed, reconfiguring mTLS "
332-
"channel."
333-
)
334-
if self._mtls_init_task and self._mtls_init_task.done():
335-
self._mtls_init_task = None
336-
await self.configure_mtls_channel(
337-
lambda: (call_cert_bytes, call_key_bytes)
351+
"Skipping reconfiguration of mTLS channel because the client"
352+
" certificate has not changed."
338353
)
339-
continue
340-
except Exception as e:
341-
_LOGGER.warning(
342-
"Failed to reconfigure mTLS channel: %s. Proceeding with original response.",
343-
e,
344-
)
345-
else:
346-
_LOGGER.info(
347-
"Skipping reconfiguration of mTLS channel because the client"
348-
" certificate has not changed."
354+
except Exception as e:
355+
_LOGGER.warning(
356+
"Failed to check client certificate parameters: %s. Proceeding with original response.",
357+
e,
349358
)
350-
except Exception as e:
351-
_LOGGER.warning(
352-
"Failed to check client certificate parameters: %s. Proceeding with original response.",
353-
e,
354-
)
355359

356360
if response.status_code not in transport.DEFAULT_RETRYABLE_STATUS_CODES:
357361
break

0 commit comments

Comments
 (0)