|
40 | 40 | ClientTimeout = None |
41 | 41 |
|
42 | 42 | _LOGGER = logging.getLogger(__name__) |
| 43 | +MTLS_URL_PREFIXES = ["mtls.googleapis.com", "mtls.sandbox.googleapis.com"] |
43 | 44 |
|
44 | 45 |
|
45 | 46 | # Tracks the internal aiohttp installation and usage |
@@ -315,43 +316,46 @@ async def request( |
315 | 316 | ) |
316 | 317 | ) |
317 | 318 | 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: |
330 | 350 | _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." |
338 | 353 | ) |
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, |
349 | 358 | ) |
350 | | - except Exception as e: |
351 | | - _LOGGER.warning( |
352 | | - "Failed to check client certificate parameters: %s. Proceeding with original response.", |
353 | | - e, |
354 | | - ) |
355 | 359 |
|
356 | 360 | if response.status_code not in transport.DEFAULT_RETRYABLE_STATUS_CODES: |
357 | 361 | break |
|
0 commit comments