Skip to content

Commit ba40de0

Browse files
committed
fix(auth): avoid creating mTLS SSL context for custom async transports
If a custom async transport is used (rather than the default AiohttpRequest), the library warns that it cannot configure mTLS. In this case, we should also skip creating the client cert SSL context, preventing unexpected errors or warnings during SSL context generation when a custom transport is in use. Fixes: b/530712364
1 parent 6702c7a commit ba40de0

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,13 @@ async def _do_configure():
192192
) = await mtls.get_client_cert_and_key(client_cert_callback)
193193

194194
if is_mtls:
195-
ssl_context = await mtls._run_in_executor(
196-
mtls.make_client_cert_ssl_context, cert, key
197-
)
198-
199195
# Re-create the auth request with the new SSL context
200196
if AIOHTTP_INSTALLED and isinstance(
201197
self._auth_request, AiohttpRequest
202198
):
199+
ssl_context = await mtls._run_in_executor(
200+
mtls.make_client_cert_ssl_context, cert, key
201+
)
203202
connector = aiohttp.TCPConnector(ssl=ssl_context)
204203
new_session = aiohttp.ClientSession(connector=connector)
205204

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,32 @@ async def test_http_delete_method_success(self):
335335
assert await response.read() == expected_payload
336336
response = await authed_session.close()
337337

338+
@pytest.mark.asyncio
339+
async def test_configure_mtls_channel_with_custom_transport_and_broken_cert(self):
340+
auth_request = MockRequest()
341+
authed_session = sessions.AsyncAuthorizedSession(
342+
self.credentials, auth_request=auth_request
343+
)
344+
345+
with patch(
346+
"google.auth.transport._mtls_helper.check_use_client_cert",
347+
return_value=True,
348+
):
349+
350+
def callback():
351+
return b"invalid-cert", b"invalid-key"
352+
353+
with pytest.warns(
354+
UserWarning,
355+
match="Attempted to establish mTLS, but a custom async transport was provided",
356+
):
357+
await authed_session.configure_mtls_channel(callback)
358+
359+
assert authed_session._is_mtls is False
360+
assert authed_session._cached_cert is None
361+
362+
await authed_session.close()
363+
338364

339365
def test_mock_request_clone():
340366
request = MockRequest()

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,7 @@ async def test_configure_mtls_channel_custom_request(self):
193193
# If the request handler is not an AiohttpRequest, the library cannot configure
194194
# the connection to use mTLS, so _is_mtls must be False to reflect this unconfigured state.
195195
assert session._is_mtls is False
196-
mock_make_context.assert_called_once_with(
197-
b"fake_cert_data", b"fake_key_data"
198-
)
196+
mock_make_context.assert_not_called()
199197
await session.close()
200198

201199
@pytest.mark.asyncio

0 commit comments

Comments
 (0)