Repository navigation
fix(oauth2): verify universe domain for UserAccessTokenCredentials #19249
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
6
commits into
main
Choose a base branch
from
fix/verify-user-token-universe
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9d5581a
fix(oauth2): verify universe domain for UserAccessTokenCredentials
parthea 2ed6269
fix(oauth2): lazily resolve and cache UserAccessTokenCredentials.univ…
parthea 95fd169
work in progress
parthea 03e72dc
Merge remote-tracking branch 'origin/main' into fix/verify-user-token…
parthea 8fa6bf2
refactor(oauth2): extract _resolve_universe_domain and expand univers…
parthea 691a7fb
test(oauth2): use with mock.patch for get_universe_domain in refresh …
parthea 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 |
|---|---|---|
|
|
@@ -1065,13 +1065,17 @@ def test_instance(self): | |
|
|
||
| @mock.patch("google.auth._cloud_sdk.get_auth_access_token", autospec=True) | ||
| def test_refresh(self, get_auth_access_token): | ||
| with pytest.warns( | ||
| UserWarning, match="UserAccessTokenCredentials is deprecated" | ||
| with mock.patch( | ||
| "google.auth._cloud_sdk.get_universe_domain", return_value=None | ||
| ): | ||
| get_auth_access_token.return_value = "access_token" | ||
| cred = credentials.UserAccessTokenCredentials() | ||
| cred.refresh(None) | ||
| assert cred.token == "access_token" | ||
| with pytest.warns( | ||
| UserWarning, match="UserAccessTokenCredentials is deprecated" | ||
| ): | ||
| get_auth_access_token.return_value = "access_token" | ||
| cred = credentials.UserAccessTokenCredentials() | ||
| cred.refresh(None) | ||
| assert cred.token == "access_token" | ||
| assert cred.universe_domain == "googleapis.com" | ||
|
|
||
| def test_with_quota_project(self): | ||
| with pytest.warns( | ||
|
|
@@ -1097,3 +1101,141 @@ def test_before_request(self, refresh, apply): | |
| cred.before_request(mock.Mock(), "GET", "https://example.com", {}) | ||
| refresh.assert_called() | ||
| apply.assert_called() | ||
|
|
||
| @mock.patch( | ||
| "google.auth._cloud_sdk.get_universe_domain", | ||
| return_value="custom-universe.goog", | ||
| ) | ||
| @mock.patch( | ||
| "google.auth._cloud_sdk.get_auth_access_token", return_value="mock-token-custom" | ||
| ) | ||
| def test_universe_domain_resolves_lazily_and_updates_property( | ||
| self, mock_get_token, mock_get_ud | ||
| ): | ||
| with pytest.warns( | ||
| UserWarning, match="UserAccessTokenCredentials is deprecated" | ||
| ): | ||
| creds = credentials.UserAccessTokenCredentials() | ||
|
|
||
| # Accessing universe_domain before refresh resolves and caches it | ||
| assert creds.universe_domain == "custom-universe.goog" | ||
| assert mock_get_ud.call_count == 1 | ||
|
|
||
| # Refresh reuses the cached universe domain without another lookup | ||
| creds.refresh(None) | ||
| assert creds.token == "mock-token-custom" | ||
| assert creds.universe_domain == "custom-universe.goog" | ||
| assert mock_get_ud.call_count == 1 | ||
|
|
||
| @mock.patch( | ||
| "google.auth._cloud_sdk.get_universe_domain", | ||
| return_value="custom-universe.goog", | ||
| ) | ||
| @mock.patch( | ||
| "google.auth._cloud_sdk.get_auth_access_token", return_value="mock-token" | ||
| ) | ||
| def test_refresh_updates_public_universe_property( | ||
| self, mock_get_token, mock_get_ud | ||
| ): | ||
| with pytest.warns( | ||
| UserWarning, match="UserAccessTokenCredentials is deprecated" | ||
| ): | ||
| creds = credentials.UserAccessTokenCredentials() | ||
|
|
||
| creds.refresh(None) | ||
|
|
||
| assert creds.token == "mock-token" | ||
| assert creds.universe_domain == "custom-universe.goog" | ||
| assert mock_get_ud.call_count == 1 | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "resolved_universe,expected_universe", | ||
| [ | ||
| ("googleapis.com", "googleapis.com"), | ||
| (None, "googleapis.com"), | ||
| ("custom-universe.goog", "custom-universe.goog"), | ||
| ], | ||
| ) | ||
| @mock.patch( | ||
| "google.auth._cloud_sdk.get_auth_access_token", return_value="mock-token" | ||
| ) | ||
| def test_refresh_caches_resolved_universe_domain( | ||
| self, mock_get_token, resolved_universe, expected_universe | ||
| ): | ||
| with mock.patch( | ||
| "google.auth._cloud_sdk.get_universe_domain", | ||
| return_value=resolved_universe, | ||
| ) as mock_get_ud: | ||
| with pytest.warns( | ||
| UserWarning, match="UserAccessTokenCredentials is deprecated" | ||
| ): | ||
| creds = credentials.UserAccessTokenCredentials() | ||
|
|
||
| creds.refresh(None) | ||
| creds.refresh(None) | ||
|
|
||
| assert creds.universe_domain == expected_universe | ||
| assert mock_get_ud.call_count == 1 | ||
| assert mock_get_token.call_count == 2 | ||
|
|
||
| @mock.patch("google.auth._cloud_sdk.get_auth_access_token") | ||
| def test_refresh_blocks_invalid_universe_format(self, mock_get_token): | ||
| with mock.patch( | ||
| "google.auth._cloud_sdk.get_universe_domain", return_value="invalid@domain" | ||
| ): | ||
| with pytest.warns( | ||
| UserWarning, match="UserAccessTokenCredentials is deprecated" | ||
| ): | ||
| creds = credentials.UserAccessTokenCredentials() | ||
|
|
||
| with pytest.raises(exceptions.InvalidValue) as exc_info: | ||
| _ = creds.universe_domain | ||
|
|
||
| assert "Universe domain must be a valid non-empty domain string" in str( | ||
| exc_info.value | ||
| ) | ||
| assert not creds._universe_domain_cached | ||
|
|
||
| with pytest.raises(exceptions.InvalidValue) as exc_info: | ||
| creds.refresh(None) | ||
|
|
||
| assert "Universe domain must be a valid non-empty domain string" in str( | ||
| exc_info.value | ||
| ) | ||
| assert not creds._universe_domain_cached | ||
| mock_get_token.assert_not_called() | ||
|
|
||
| @mock.patch( | ||
| "google.auth._cloud_sdk.get_universe_domain", | ||
| return_value="custom-universe.goog", | ||
| ) | ||
| def test_with_account_and_quota_project_preserve_cached_universe(self, mock_get_ud): | ||
| with pytest.warns( | ||
| UserWarning, match="UserAccessTokenCredentials is deprecated" | ||
| ): | ||
| creds = credentials.UserAccessTokenCredentials() | ||
| assert creds.universe_domain == "custom-universe.goog" | ||
|
|
||
| with_acct = creds.with_account("[email protected]") | ||
| with_quota = creds.with_quota_project("project-foo") | ||
|
|
||
| assert with_acct.universe_domain == "custom-universe.goog" | ||
| assert with_quota.universe_domain == "custom-universe.goog" | ||
| assert mock_get_ud.call_count == 1 | ||
|
|
||
| @mock.patch( | ||
| "google.auth._cloud_sdk.get_universe_domain", | ||
| return_value="custom-universe.goog", | ||
| ) | ||
| def test_with_account_and_quota_project_uncached_resolve_lazily(self, mock_get_ud): | ||
| with pytest.warns( | ||
| UserWarning, match="UserAccessTokenCredentials is deprecated" | ||
| ): | ||
| creds = credentials.UserAccessTokenCredentials() | ||
| with_acct = creds.with_account("[email protected]") | ||
| with_quota = creds.with_quota_project("project-foo") | ||
|
|
||
| assert mock_get_ud.call_count == 0 | ||
| assert with_acct.universe_domain == "custom-universe.goog" | ||
| assert with_quota.universe_domain == "custom-universe.goog" | ||
| assert mock_get_ud.call_count == 2 | ||
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
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.