Repository navigation
fix(auth): use iam_endpoint_override in sign_bytes and ID token refresh #19219
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
Draft
ohmayr
wants to merge
3
commits into
main
Choose a base branch
from
fix/auth-impersonated-endpoint-override
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,3 +110,103 @@ def test_sign_bytes_retryable_failure(self, mock_time): | |
| with pytest.raises(exceptions.TransportError): | ||
| signer.sign("123") | ||
| request.call_count == 3 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "universe_domain,override,action,expected_url", | ||
| [ | ||
| pytest.param( | ||
| "googleapis.com", | ||
| None, | ||
| "generateAccessToken", | ||
| "https://iamcredentials.googleapis.com/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:generateAccessToken", | ||
| id="default_universe", | ||
| ), | ||
| pytest.param( | ||
| "example.com", | ||
| None, | ||
| "signBlob", | ||
| "https://iamcredentials.example.com/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:signBlob", | ||
| id="custom_universe", | ||
| ), | ||
| pytest.param( | ||
| "example.com", | ||
| "https://us-east1-iamcredentials.example.com/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:generateAccessToken?alt=json", | ||
| "generateIdToken", | ||
| "https://us-east1-iamcredentials.example.com/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:generateIdToken?alt=json", | ||
| id="override_rewrites_action_and_preserves_query", | ||
| ), | ||
| pytest.param( | ||
| "googleapis.com", | ||
| "https://proxy.example.com/iam/", | ||
| "signBlob", | ||
| "https://proxy.example.com/iam/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:signBlob", | ||
| id="override_proxy_path_prefix", | ||
| ), | ||
| pytest.param( | ||
| "googleapis.com", | ||
| "https://us-east1-iamcredentials.googleapis.com", | ||
| "signBlob", | ||
| "https://us-east1-iamcredentials.googleapis.com/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:signBlob", | ||
| id="override_origin_only", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_build_iam_endpoint(universe_domain, override, action, expected_url): | ||
| assert ( | ||
| iam._build_iam_endpoint("[email protected]", universe_domain, override, action) | ||
| == expected_url | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "universe_domain,expected_host", | ||
| [ | ||
| pytest.param( | ||
| "googleapis.com", | ||
| "iamcredentials.mtls.googleapis.com", | ||
| id="default_universe_uses_mtls", | ||
| ), | ||
| pytest.param( | ||
| "example.com", | ||
| "iamcredentials.example.com", | ||
| id="custom_universe_omits_mtls", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_build_iam_endpoint_mtls_scoped_to_default_universe( | ||
| universe_domain, expected_host | ||
| ): | ||
| with mock.patch( | ||
| "google.auth.iam._IAM_DOMAIN", "iamcredentials.mtls.googleapis.com" | ||
| ): | ||
| url = iam._build_iam_endpoint( | ||
| "[email protected]", | ||
| universe_domain, | ||
| None, | ||
| "signBlob", | ||
| ) | ||
| assert ( | ||
| url == f"https://{expected_host}/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:signBlob" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_override", | ||
| [ | ||
| "us-east1-iamcredentials.googleapis.com", | ||
| "https://", | ||
| ], | ||
| ) | ||
| def test_build_iam_endpoint_invalid_override(bad_override): | ||
| with pytest.raises(exceptions.InvalidValue, match="Invalid iam_endpoint_override"): | ||
| iam._build_iam_endpoint( | ||
| "[email protected]", "googleapis.com", bad_override, "signBlob" | ||
| ) | ||
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 |
|---|---|---|
|
|
@@ -1087,6 +1087,95 @@ def test_sign_jwt_request_invalid_response_error(self): | |
| ) | ||
| assert excinfo.value.args[1] == "invalid_data" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "iam_endpoint_override,expected_sign_url", | ||
| [ | ||
| pytest.param( | ||
| "https://us-east1-iamcredentials.googleapis.com/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:generateAccessToken", | ||
| "https://us-east1-iamcredentials.googleapis.com/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:signBlob", | ||
| id="full_generate_access_token_url", | ||
| ), | ||
| pytest.param( | ||
| "https://iamcredentials.example.com/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:generateAccessToken?alt=json", | ||
| "https://iamcredentials.example.com/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:signBlob?alt=json", | ||
| id="full_url_with_query", | ||
| ), | ||
| pytest.param( | ||
| "https://proxy.example.com/iam/", | ||
| "https://proxy.example.com/iam/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:signBlob", | ||
| id="proxy_base_url_with_path_prefix", | ||
| ), | ||
| pytest.param( | ||
| "https://us-east1-iamcredentials.googleapis.com", | ||
| "https://us-east1-iamcredentials.googleapis.com/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:signBlob", | ||
| id="origin_only_override", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_sign_bytes_uses_iam_endpoint_override( | ||
| self, | ||
| iam_endpoint_override, | ||
| expected_sign_url, | ||
| mock_authorizedsession_sign, | ||
| ): | ||
| creds = self.make_credentials( | ||
| lifetime=None, iam_endpoint_override=iam_endpoint_override | ||
| ) | ||
| signature = creds.sign_bytes(b"signed bytes") | ||
|
|
||
| assert signature == b"signature" | ||
| mock_authorizedsession_sign.assert_called_once_with( | ||
| mock.ANY, | ||
| "POST", | ||
| expected_sign_url, | ||
| None, | ||
| json={"payload": "c2lnbmVkIGJ5dGVz", "delegates": []}, | ||
| headers={"Content-Type": "application/json"}, | ||
| ) | ||
|
|
||
| def test_sign_bytes_rejects_schemeless_iam_endpoint_override(self): | ||
| creds = self.make_credentials( | ||
| lifetime=None, | ||
| iam_endpoint_override="us-east1-iamcredentials.googleapis.com", | ||
| ) | ||
| with pytest.raises( | ||
| exceptions.InvalidValue, match="Invalid iam_endpoint_override" | ||
| ): | ||
| creds.sign_bytes(b"signed bytes") | ||
|
|
||
| def test_id_token_refresh_uses_iam_endpoint_override( | ||
| self, mock_authorizedsession_idtoken | ||
| ): | ||
| override = ( | ||
| "https://us-east1-iamcredentials.example.com/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:generateAccessToken" | ||
| ) | ||
| source = service_account.Credentials( | ||
| SIGNER, "[email protected]", TOKEN_URI, universe_domain="example.com" | ||
| ) | ||
| target_creds = self.make_credentials( | ||
| lifetime=None, source_credentials=source, iam_endpoint_override=override | ||
| ) | ||
| id_creds = impersonated_credentials.IDTokenCredentials( | ||
| target_creds, target_audience="https://example.com" | ||
| ) | ||
| request = mock.create_autospec(transport.Request) | ||
|
|
||
| id_creds.refresh(request) | ||
|
|
||
| assert id_creds.token == ID_TOKEN_DATA | ||
| assert ( | ||
| mock_authorizedsession_idtoken.call_args[0][2] | ||
| == "https://us-east1-iamcredentials.example.com/v1/projects/-" | ||
| "/serviceAccounts/[email protected]:generateIdToken" | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "source_info", | ||
| [ | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SYNC WARNING:
I noticed a sync issue between #19194 (
_get_iam_idtoken_endpoint) and #19219 (_build_iam_endpoint), which both add IAM Credentials API URL builders inpackages/google-auth/google/auth/iam.py. And which see to be trying to build out:I recommend the following actions:
google/auth/iam.py(using_build_iam_endpointso it coversgenerateAccessToken,signBlob,generateIdToken, andiam_endpoint_override).mtls.host prefix strictly togoogleapis.com(matching#19194andgoogle.api_core.universe.get_api_endpoint), rather than prependingmtls.to non-default universes.google-authin whichever PR lands second (no# TODO(universe-domain):copy needed since both live ingoogle-auth).I know that you have better context on this than I do, so if any of these suggestions are wrong, please leave a comment here. Gemini will pick them up and learn from them during the next sync sweep.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, I will update _build_iam_endpoint here to only use the mtls. prefix on googleapis.com so it matches #19194 and google-api-core. Whichever PR lands second can drop the duplicate helper and import _build_iam_endpoint directly across google-auth.