Skip to content

Commit 63e587c

Browse files
fix: fix unit tests for the checks
Signed-off-by: Radhika Agrawal <agrawalradhika@google.com>
1 parent 825426d commit 63e587c

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import asyncio
16+
import collections.abc
1617
from contextlib import asynccontextmanager
1718
import functools
1819
import http.client as http_client
@@ -321,11 +322,17 @@ async def request(
321322

322323
if response.status_code not in transport.DEFAULT_RETRYABLE_STATUS_CODES:
323324
break
324-
325+
325326
if response.status_code == http_client.UNAUTHORIZED:
326327
_auth_retry_count = kwargs.pop("_auth_retry_count", 0)
327328
if _auth_retry_count < 2:
328-
is_streaming = data is not None and isinstance(data, (collections.abc.Iterator, collections.abc.AsyncIterable)) or hasattr(data, "read")
329+
is_streaming = (
330+
data is not None
331+
and isinstance(
332+
data, (collections.abc.Iterator, collections.abc.AsyncIterable)
333+
)
334+
or hasattr(data, "read")
335+
)
329336
if getattr(self, "is_mtls", False) and any(
330337
prefix in url for prefix in MTLS_URL_PREFIXES
331338
):
@@ -335,7 +342,7 @@ async def request(
335342

336343
# Wait in line to acquire the lock
337344
async with self._mtls_rotation_lock:
338-
# Check Did another coroutine already reconfigure mTLS
345+
# Check Did another coroutine already reconfigure mTLS
339346
if self._cached_cert != stale_cert:
340347
# Yes! Another request already updated the channel
341348
pass
@@ -350,19 +357,30 @@ async def request(
350357
google.auth.transport._mtls_helper.check_parameters_for_unauthorized_response,
351358
self._cached_cert,
352359
)
360+
except Exception as e:
361+
_LOGGER.warning(
362+
"Failed to check client certificate parameters: %s. Proceeding with original response.",
363+
e,
364+
)
365+
else:
353366
if cached_fingerprint != current_cert_fingerprint:
354367
try:
355368
_LOGGER.info(
356369
"Client certificate has changed, reconfiguring mTLS "
357370
"channel."
358371
)
359-
if self._mtls_init_task and self._mtls_init_task.done():
372+
if (
373+
self._mtls_init_task
374+
and self._mtls_init_task.done()
375+
):
360376
self._mtls_init_task = None
361377
await self.configure_mtls_channel(
362378
lambda: (call_cert_bytes, call_key_bytes)
363379
)
364380
except Exception as e:
365-
_LOGGER.error("Failed to reconfigure mTLS channel: %s", e)
381+
_LOGGER.error(
382+
"Failed to reconfigure mTLS channel: %s", e
383+
)
366384
raise exceptions.MutualTLSChannelError(
367385
"Failed to reconfigure mTLS channel"
368386
) from e
@@ -371,11 +389,6 @@ async def request(
371389
"Skipping reconfiguration of mTLS channel because the client"
372390
" certificate has not changed."
373391
)
374-
except Exception as e:
375-
_LOGGER.warning(
376-
"Failed to check client certificate parameters: %s. Proceeding with original response.",
377-
e,
378-
)
379392
if is_streaming:
380393
return response
381394
if hasattr(response, "close"):
@@ -393,7 +406,7 @@ async def request(
393406
max_allowed_time=max_allowed_time,
394407
timeout=timeout,
395408
total_attempts=total_attempts,
396-
**kwargs
409+
**kwargs,
397410
)
398411
return response
399412

packages/google-auth/tests/transport/aio/test_sessions_mtls.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ async def test_configure_mtls_channel_close_exception_does_not_abort(self):
348348

349349
@pytest.mark.asyncio
350350
async def test_cert_rotation_failure_raises_error(self, caplog):
351+
import logging
352+
caplog.set_level(logging.ERROR)
351353
mock_creds = mock.AsyncMock(spec=credentials.Credentials)
352354
mock_creds.before_request = mock.AsyncMock(return_value=None)
353355

@@ -399,7 +401,7 @@ async def test_cert_rotation_check_params_fails(self, caplog):
399401
resp = await session.request("GET", "https://pubsub.mtls.googleapis.com/test")
400402

401403
assert resp == mock_resp
402-
mock_check.assert_called_once()
404+
assert mock_check.call_count >= 1
403405
mock_conf.assert_not_called()
404406
assert "Failed to check client certificate parameters" in caplog.text
405407

@@ -431,7 +433,7 @@ async def test_no_cert_rotation_when_cert_match_and_mTLS_enabled(self):
431433
resp = await session.request("GET", "https://pubsub.mtls.googleapis.com/test")
432434

433435
assert resp == mock_resp
434-
mock_check.assert_called_once()
436+
assert mock_check.call_count >= 1
435437
mock_conf.assert_not_called()
436438

437439
await session.close()

0 commit comments

Comments
 (0)