Repository navigation
fix(auth): refresh URL-derived self-signed JWTs on 401 across HTTP transports #19282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
parthea
wants to merge
1
commit into
fix/auth-sa-url-jwt-audience
Choose a base branch
from
fix/auth-sa-url-jwt-401-retry
base: fix/auth-sa-url-jwt-audience
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+393
−45
Open
Changes from all commits
Commits
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
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
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 |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| # limitations under the License. | ||
|
|
||
| import asyncio | ||
| import datetime | ||
| import http.client as http_client | ||
| import json | ||
| import threading | ||
|
|
@@ -24,7 +25,7 @@ | |
|
|
||
| import google.auth.credentials | ||
| import google.auth.transport.requests | ||
| from google.auth import crypt, jwt | ||
| from google.auth import _helpers, crypt, jwt | ||
| from google.auth.aio.credentials import AnonymousCredentials, Credentials | ||
| from google.auth.aio.transport import ( | ||
| _DEFAULT_TIMEOUT_SECONDS, | ||
|
|
@@ -386,6 +387,64 @@ async def test_request_non_default_universe_domain_service_account(self): | |
| # Concurrent requests to the same host share the cached token. | ||
| assert len(tokens_by_url[target_url]) == 1 | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_request_non_default_universe_domain_service_account_401_retries( | ||
| self, | ||
| ): | ||
| """AsyncAuthorizedSession refreshes URL-derived JWT on 401 and retries.""" | ||
| signer = crypt.RSASigner.from_string(pytest.private_key_bytes, "1") | ||
| sync_credentials = service_account.Credentials( | ||
| signer, | ||
| "[email protected]", | ||
| service_account._GOOGLE_OAUTH2_TOKEN_ENDPOINT, | ||
| universe_domain="universe.foo", | ||
| ) | ||
| unauthorized_response = MockResponse(status_code=http_client.UNAUTHORIZED) | ||
| ok_response = MockResponse(status_code=http_client.OK) | ||
|
|
||
| real_now = _helpers.utcnow() | ||
| current_time = [real_now - datetime.timedelta(seconds=30)] | ||
| responses_iter = iter([unauthorized_response, ok_response]) | ||
|
|
||
| async def mock_auth_request(*args, **kwargs): | ||
| resp = next(responses_iter) | ||
| current_time[0] = real_now | ||
| return resp | ||
|
|
||
| auth_request = AsyncMock(side_effect=mock_auth_request) | ||
| mock_sync_request = Mock() | ||
| mock_sync_request.session = Mock() | ||
|
|
||
| target_url = "https://storage.universe.foo/storage/v1/b" | ||
| with ( | ||
| patch( | ||
| "google.auth.transport.requests.Request", | ||
| return_value=mock_sync_request, | ||
| ), | ||
| patch.object(_helpers, "utcnow", side_effect=lambda: current_time[0]), | ||
| ): | ||
| authed_session = sessions.AsyncAuthorizedSession( | ||
| sync_credentials, auth_request=auth_request | ||
| ) | ||
| response = await authed_session.request("GET", target_url) | ||
| await authed_session.close() | ||
|
|
||
| assert response is ok_response | ||
| mock_sync_request.assert_not_called() | ||
| assert auth_request.call_count == 2 | ||
| first_token = ( | ||
| auth_request.call_args_list[0].args[3]["authorization"].split(" ", 1)[1] | ||
| ) | ||
| second_token = ( | ||
| auth_request.call_args_list[1].args[3]["authorization"].split(" ", 1)[1] | ||
| ) | ||
| assert first_token != second_token | ||
| first_payload = jwt.decode(first_token, pytest.public_cert_bytes) | ||
| second_payload = jwt.decode(second_token, pytest.public_cert_bytes) | ||
| assert first_payload["aud"] == "https://storage.universe.foo/" | ||
| assert second_payload["aud"] == "https://storage.universe.foo/" | ||
| assert second_payload["iat"] > first_payload["iat"] | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_request_with_mock_service_account_credentials_still_refreshes(self): | ||
| """Mocked service_account.Credentials still refreshes when invalid.""" | ||
|
|
||
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.