This repository was archived by the owner on Mar 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 351
feat: mTLS configuration via x.509 for asynchronous session in google-auth #1959
Merged
+355
−5
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f1f7c45
feat: Add helper methods for async mTLS support for google-auth
agrawalradhika-cell 0d45640
fix: Add test cases for helper method
agrawalradhika-cell 07d7818
chore: Correct lint and imports, plus add testcases for exceptions check
agrawalradhika-cell be10a50
chore: Add dependencies and async function related wrapper
agrawalradhika-cell 7f23594
fix: Update based on gemini-assit comments to make robust callback by…
agrawalradhika-cell 8110a6f
chore: Correct based on minor comments
agrawalradhika-cell fce3c71
chore: Update based on reviewer comments, updated helpers so that the…
agrawalradhika-cell dbd40d0
feat: mTLS configuration via x.509 for asynchronous session in google…
agrawalradhika-cell 984e71c
fix: lint fixes and typo fixes
agrawalradhika-cell a3b4ad7
fix: Make systems tests resilient and fix the unit tests for async flow
agrawalradhika-cell 806c329
Merge branch 'main' into mtls-async-support
agrawalradhika-cell af21156
chore: Add support for other error types in async/mtls and take care …
agrawalradhika-cell 2e64a1c
fix: fix the mypy.py
agrawalradhika-cell da33b9c
fix: fix mytest for timeout
agrawalradhika-cell 4013354
fix: fix mypy. for timeouts specifically handling None
agrawalradhika-cell 8e0bc8d
Merge branch 'main' into mtls-async-support
agrawalradhika-cell 064d524
chore: fix: final mypy suppression for aiohttp timeout union
agrawalradhika-cell b27687b
Merge branch 'mtls-async-support' of https://github.com/googleapis/go…
agrawalradhika-cell 382fb70
fix: fix checks support via typing
agrawalradhika-cell 6f34fc2
chore: respond to reviewer comments to add docstrings and fix timeout…
agrawalradhika-cell 29b9625
chore: fix the sequence flow and add docstrings and comment where nec…
agrawalradhika-cell 952a407
chore: Add try catch block for exceptions if returned from configure_…
agrawalradhika-cell ae56ecd
fix: Suppress exception from mtls_init_task in requests.py
agrawalradhika-cell cd6095e
chore: Update sessions.py to use ClientTimeout
agrawalradhika-cell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import json | ||
| import os | ||
| import ssl | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| from google.auth import exceptions | ||
| from google.auth.aio import credentials | ||
| from google.auth.aio.transport import sessions | ||
|
|
||
| # This is the valid "workload" format the library expects | ||
| VALID_WORKLOAD_CONFIG = { | ||
| "version": 1, | ||
| "cert_configs": { | ||
| "workload": {"cert_path": "/tmp/mock_cert.pem", "key_path": "/tmp/mock_key.pem"} | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| class TestSessionsMtls: | ||
| @pytest.mark.asyncio | ||
| async def test_configure_mtls_channel(self): | ||
| """ | ||
| Tests that the mTLS channel configures correctly when a | ||
| valid workload config is mocked. | ||
| """ | ||
| with mock.patch.dict( | ||
| os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} | ||
| ), mock.patch("os.path.exists") as mock_exists, mock.patch( | ||
| "builtins.open", mock.mock_open(read_data=json.dumps(VALID_WORKLOAD_CONFIG)) | ||
| ), mock.patch( | ||
| "google.auth.aio.transport.mtls.get_client_cert_and_key" | ||
| ) as mock_helper, mock.patch( | ||
| "google.auth.aio.transport.mtls.make_client_cert_ssl_context" | ||
| ) as mock_make_context: | ||
| mock_exists.return_value = True | ||
| mock_helper.return_value = (True, b"fake_cert_data", b"fake_key_data") | ||
|
|
||
| mock_context = mock.Mock(spec=ssl.SSLContext) | ||
| mock_make_context.return_value = mock_context | ||
|
|
||
| mock_creds = mock.AsyncMock(spec=credentials.Credentials) | ||
| session = sessions.AsyncAuthorizedSession(mock_creds) | ||
|
|
||
| await session.configure_mtls_channel() | ||
|
|
||
| assert session._is_mtls is True | ||
| mock_make_context.assert_called_once_with( | ||
| b"fake_cert_data", b"fake_key_data" | ||
| ) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_configure_mtls_channel_disabled(self): | ||
| """ | ||
| Tests behavior when the config file does not exist. | ||
| """ | ||
| with mock.patch.dict( | ||
| os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} | ||
| ), mock.patch("os.path.exists") as mock_exists: | ||
| mock_exists.return_value = False | ||
| mock_creds = mock.AsyncMock(spec=credentials.Credentials) | ||
| session = sessions.AsyncAuthorizedSession(mock_creds) | ||
|
|
||
| await session.configure_mtls_channel() | ||
|
|
||
| # If the file doesn't exist, it shouldn't error; it just won't use mTLS | ||
| assert session._is_mtls is False | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_configure_mtls_channel_invalid_format(self): | ||
| """ | ||
| Verifies that the MutualTLSChannelError is raised for bad formats. | ||
| """ | ||
| with mock.patch.dict( | ||
| os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} | ||
| ), mock.patch("os.path.exists") as mock_exists, mock.patch( | ||
| "builtins.open", mock.mock_open(read_data='{"invalid": "format"}') | ||
| ): | ||
| mock_exists.return_value = True | ||
| mock_creds = mock.AsyncMock(spec=credentials.Credentials) | ||
| session = sessions.AsyncAuthorizedSession(mock_creds) | ||
|
|
||
| with pytest.raises(exceptions.MutualTLSChannelError): | ||
| await session.configure_mtls_channel() | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_configure_mtls_channel_mock_callback(self): | ||
| """ | ||
| Tests mTLS configuration using bytes-returning callback. | ||
| """ | ||
|
|
||
| def mock_callback(): | ||
| return (b"fake_cert_bytes", b"fake_key_bytes") | ||
|
|
||
| with mock.patch.dict( | ||
| os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} | ||
| ), mock.patch( | ||
| "google.auth.transport.mtls.has_default_client_cert_source", | ||
| return_value=True, | ||
| ), mock.patch( | ||
| "ssl.SSLContext.load_cert_chain" | ||
| ): | ||
| mock_creds = mock.AsyncMock(spec=credentials.Credentials) | ||
| session = sessions.AsyncAuthorizedSession(mock_creds) | ||
|
|
||
| await session.configure_mtls_channel(client_cert_callback=mock_callback) | ||
|
|
||
| assert session._is_mtls is True |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.