-
Notifications
You must be signed in to change notification settings - Fork 345
fix: validate urls for external accounts #1031
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3c31c75
[Bug 193694420] adding validation of token url and service account im…
BigTailWolf b8b273a
Merge branch 'main' into main
lsirac 5682255
adding coverage for service account impersonate url validation
BigTailWolf d5beae7
using urllib3 instead of urllib since it's the project test requireme…
BigTailWolf 32f0c9e
fix format
BigTailWolf ba78e44
addressing comments
BigTailWolf 9c68168
remove redundant
BigTailWolf 89ed17e
fix tests since python 3.6 cannot have same string constants interpre…
BigTailWolf b14b004
Merge branch 'main' into main
lsirac 4840ffa
fix lint and fix the flaky test in an alternate way
BigTailWolf e2ff166
revert the debugging output
BigTailWolf c2e204e
fix lint
BigTailWolf 7748a13
Merge branch 'main' into main
arithmetic1728 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -275,9 +275,77 @@ def assert_resource_manager_request_kwargs( | |
| assert request_kwargs["headers"] == headers | ||
| assert "body" not in request_kwargs | ||
|
|
||
| def test_valid_token_url_shall_pass_validation(self): | ||
| # valid url doesn't throw exception, a None value should be return | ||
BigTailWolf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert not external_account.Credentials.validate_token_url(self.TOKEN_URL) | ||
|
|
||
| def test_token_url_pattern_matching(self): | ||
| # matching *.sts.googleapis.com | ||
BigTailWolf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert external_account.Credentials.is_valid_url( | ||
| external_account._TOKEN_URL_PATTERNS, | ||
| "https://auth.sts.googleapis.com/v1/token", | ||
| ) | ||
| # matching sts.googleapis.com | ||
| assert external_account.Credentials.is_valid_url( | ||
| external_account._TOKEN_URL_PATTERNS, "https://sts.googleapis.com/v1/token" | ||
| ) | ||
| # matching sts.*.googleapis.com | ||
| assert external_account.Credentials.is_valid_url( | ||
| external_account._TOKEN_URL_PATTERNS, | ||
| "https://sts.auth.googleapis.com/v1/token", | ||
| ) | ||
| # matching *-sts.googleapis.com | ||
| assert external_account.Credentials.is_valid_url( | ||
| external_account._TOKEN_URL_PATTERNS, | ||
| "https://auth-sts.googleapis.com/v1/token", | ||
| ) | ||
| # invalid url cannot match | ||
| assert not external_account.Credentials.is_valid_url( | ||
| external_account._TOKEN_URL_PATTERNS, "https:///v1/token" | ||
| ) | ||
| assert not external_account.Credentials.is_valid_url( | ||
| external_account._TOKEN_URL_PATTERNS, "https://some-invalid-url/v1/token" | ||
| ) | ||
|
|
||
| def test_service_account_impersonation_url_matching(self): | ||
| # matching *.iamcredentials.googleapis.com | ||
| assert external_account.Credentials.is_valid_url( | ||
| external_account._SERVICE_ACCOUNT_IMPERSONATION_URL_PATTERNS, | ||
| "https://fooauth.iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/[email protected]:generateAccessToken", | ||
| ) | ||
| # matching iamcredentials.googleapis.com | ||
| assert external_account.Credentials.is_valid_url( | ||
| external_account._SERVICE_ACCOUNT_IMPERSONATION_URL_PATTERNS, | ||
| "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/[email protected]:generateAccessToken", | ||
| ) | ||
| # matching iamcredentials.*.googleapis.com | ||
| assert external_account.Credentials.is_valid_url( | ||
| external_account._SERVICE_ACCOUNT_IMPERSONATION_URL_PATTERNS, | ||
| "https://iamcredentials.fooauth.googleapis.com/v1/projects/-/serviceAccounts/[email protected]:generateAccessToken", | ||
| ) | ||
| # matching *-iamcredentials.googleapis.com | ||
| assert external_account.Credentials.is_valid_url( | ||
| external_account._SERVICE_ACCOUNT_IMPERSONATION_URL_PATTERNS, | ||
| "https://us-east1-iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/[email protected]:generateAccessToken", | ||
| ) | ||
| # invalid url cannot match | ||
| assert not external_account.Credentials.is_valid_url( | ||
| external_account._SERVICE_ACCOUNT_IMPERSONATION_URL_PATTERNS, | ||
| "https:///v1/accesstoken", | ||
| ) | ||
| assert not external_account.Credentials.is_valid_url( | ||
| external_account._SERVICE_ACCOUNT_IMPERSONATION_URL_PATTERNS, | ||
| "https://some-invalid-url/v1", | ||
| ) | ||
|
|
||
| def test_default_state(self): | ||
| credentials = self.make_credentials() | ||
| credentials = self.make_credentials( | ||
| service_account_impersonation_url=self.SERVICE_ACCOUNT_IMPERSONATION_URL | ||
| ) | ||
|
|
||
| # Token url and service account impersonation url should be set | ||
| assert credentials._token_url | ||
| assert credentials._service_account_impersonation_url | ||
| # Not token acquired yet | ||
| assert not credentials.token | ||
| assert not credentials.valid | ||
|
|
@@ -289,6 +357,31 @@ def test_default_state(self): | |
| assert credentials.requires_scopes | ||
| assert not credentials.quota_project_id | ||
|
|
||
| def test_invalid_token_url(self): | ||
| with pytest.raises(ValueError) as excinfo: | ||
| CredentialsImpl( | ||
| audience=self.AUDIENCE, | ||
| subject_token_type=self.SUBJECT_TOKEN_TYPE, | ||
| token_url="https:///v1/token", | ||
| credential_source=self.CREDENTIAL_SOURCE, | ||
| ) | ||
|
|
||
| assert excinfo.match("The provided token URL is invalid.") | ||
|
|
||
| def test_invalid_service_account_impersonate_url(self): | ||
| with pytest.raises(ValueError) as excinfo: | ||
| CredentialsImpl( | ||
| audience=self.AUDIENCE, | ||
| subject_token_type=self.SUBJECT_TOKEN_TYPE, | ||
| token_url=self.TOKEN_URL, | ||
| credential_source=self.CREDENTIAL_SOURCE, | ||
| service_account_impersonation_url=12345, # create an exception by sending to parse url | ||
| ) | ||
|
|
||
| assert excinfo.match( | ||
| "The provided service account impersonation URL is invalid." | ||
| ) | ||
|
|
||
| def test_nonworkforce_with_workforce_pool_user_project(self): | ||
| with pytest.raises(ValueError) as excinfo: | ||
| CredentialsImpl( | ||
|
|
||
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.