This repository was archived by the owner on Mar 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 351
feat: adding support for url-based credential files #645
Merged
+387
−87
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6d9efce
feat: Adding support for url-based credential files
96d61db
Making changes requested by Bassam
333899b
More changes requested by bojeil-google
12feca3
Adding tests that include headers
a76c698
changes requested by @busunkim96
b1eb9ea
Making changes suggested by @bojeil-google to pass Kokoro Run
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 |
|---|---|---|
|
|
@@ -61,9 +61,10 @@ class TestCredentials(object): | |
| "file": SUBJECT_TOKEN_JSON_FILE, | ||
| "format": {"type": "json", "subject_token_field_name": "access_token"}, | ||
| } | ||
| CREDENTIAL_SOURCE_TEXT_URL = {"url": "http://fakeurl.com"} | ||
| CREDENTIAL_URL = "http://fakeurl.com" | ||
| CREDENTIAL_SOURCE_TEXT_URL = {"url": CREDENTIAL_URL} | ||
| CREDENTIAL_SOURCE_JSON_URL = { | ||
| "url": "http://fakeurl.com", | ||
| "url": CREDENTIAL_URL, | ||
| "format": {"type": "json", "subject_token_field_name": "access_token"}, | ||
| } | ||
| SUCCESS_RESPONSE = { | ||
|
|
@@ -104,6 +105,15 @@ def make_mock_request( | |
|
|
||
| return request | ||
|
|
||
| @classmethod | ||
| def assert_credential_request_kwargs( | ||
| cls, request_kwargs, url=CREDENTIAL_URL | ||
| ): | ||
| assert request_kwargs["url"] == url | ||
| assert request_kwargs["method"] == "GET" | ||
| assert request_kwargs["headers"] is None | ||
| assert request_kwargs.get("body", None) is None | ||
|
|
||
| @classmethod | ||
| def assert_token_request_kwargs( | ||
| cls, request_kwargs, headers, request_data, token_url=TOKEN_URL | ||
|
|
@@ -209,6 +219,10 @@ def assert_underlying_credentials_refresh( | |
| credentials.refresh(request) | ||
|
|
||
| assert len(request.call_args_list) == len(requests) | ||
| if credential_data: | ||
| cls.assert_credential_request_kwargs( | ||
| request.call_args_list[0].kwargs, | ||
| ) | ||
| # Verify token exchange request parameters. | ||
| cls.assert_token_request_kwargs( | ||
| request.call_args_list[token_request_index].kwargs, | ||
|
|
@@ -566,41 +580,32 @@ def test_refresh_with_retrieve_subject_token_error(self): | |
| ) | ||
|
|
||
| def test_retrieve_subject_token_from_url(self): | ||
| credential_source = { | ||
| "url": "http://fakeurl.com", | ||
| } | ||
| credentials = self.make_credentials(credential_source=credential_source) | ||
| credentials = self.make_credentials(credential_source=self.CREDENTIAL_SOURCE_TEXT_URL) | ||
| subject_token = credentials.retrieve_subject_token( | ||
| self.make_mock_request(token_data=TEXT_FILE_SUBJECT_TOKEN)) | ||
|
|
||
| assert subject_token == TEXT_FILE_SUBJECT_TOKEN | ||
|
|
||
| def test_retrieve_subject_token_from_url_json(self): | ||
| credential_source = { | ||
| "url": "http://fakeurl.com", | ||
| "format": {"type": "json", "subject_token_field_name": "access_token"}, | ||
| } | ||
| credentials = self.make_credentials(credential_source=credential_source) | ||
| credentials = self.make_credentials(credential_source=self.CREDENTIAL_SOURCE_JSON_URL) | ||
| subject_token = credentials.retrieve_subject_token( | ||
| self.make_mock_request(token_data=JSON_FILE_CONTENT)) | ||
|
|
||
| assert subject_token == JSON_FILE_SUBJECT_TOKEN | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also assert the request was sent with the expected parameters? eg. the expected URL, GET method and headers and no body? We don't have any coverage for this at the moment. |
||
|
|
||
| def test_retrieve_subject_token_from_url_not_found(self): | ||
| credential_source = { | ||
| "url": "http://fakeurl.com", | ||
| } | ||
| credentials = self.make_credentials(credential_source=credential_source) | ||
| credentials = self.make_credentials(credential_source=self.CREDENTIAL_SOURCE_TEXT_URL) | ||
| with pytest.raises(exceptions.RefreshError) as excinfo: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you assert the error message too? |
||
| credentials.retrieve_subject_token( | ||
| self.make_mock_request( | ||
| token_status=500, | ||
| token_status=404, | ||
| token_data=JSON_FILE_CONTENT)) | ||
|
|
||
| assert excinfo.match("Unable to retrieve Identity Pool subject token") | ||
|
|
||
| def test_retrieve_subject_token_from_url_json_invalid_field(self): | ||
| url = "http://fakeurl.com" | ||
| credential_source = { | ||
| "url": url, | ||
| "url": self.CREDENTIAL_URL, | ||
| "format": {"type": "json", "subject_token_field_name": "not_found"}, | ||
| } | ||
| credentials = self.make_credentials(credential_source=credential_source) | ||
|
|
@@ -611,25 +616,20 @@ def test_retrieve_subject_token_from_url_json_invalid_field(self): | |
|
|
||
| assert excinfo.match( | ||
| "Unable to parse subject_token from JSON file '{}' using key '{}'".format( | ||
| url, "not_found" | ||
| self.CREDENTIAL_URL, "not_found" | ||
| ) | ||
| ) | ||
|
|
||
| def test_retrieve_subject_token_from_url_json_invalid_format(self): | ||
| url = "http://fakeurl.com" | ||
| credential_source = { | ||
| "url": url, | ||
| "format": {"type": "json", "subject_token_field_name": "access_token"}, | ||
| } | ||
| credentials = self.make_credentials(credential_source=credential_source) | ||
| credentials = self.make_credentials(credential_source=self.CREDENTIAL_SOURCE_JSON_URL) | ||
|
|
||
| with pytest.raises(exceptions.RefreshError) as excinfo: | ||
| credentials.retrieve_subject_token( | ||
| self.make_mock_request(token_data="{")) | ||
|
|
||
| assert excinfo.match( | ||
| "Unable to parse subject_token from JSON file '{}' using key '{}'".format( | ||
| url, "access_token" | ||
| self.CREDENTIAL_URL, "access_token" | ||
| ) | ||
| ) | ||
|
|
||
|
|
@@ -722,9 +722,8 @@ def test_refresh_json_file_success_with_impersonation_url(self): | |
| ) | ||
|
|
||
| def test_refresh_with_retrieve_subject_token_error_url(self): | ||
| url = "http://fakeurl.com" | ||
| credential_source = { | ||
| "url": url, | ||
| "url": self.CREDENTIAL_URL, | ||
| "format": {"type": "json", "subject_token_field_name": "not_found"}, | ||
| } | ||
| credentials = self.make_credentials(credential_source=credential_source) | ||
|
|
@@ -735,6 +734,6 @@ def test_refresh_with_retrieve_subject_token_error_url(self): | |
|
|
||
| assert excinfo.match( | ||
| "Unable to parse subject_token from JSON file '{}' using key '{}'".format( | ||
| url, "not_found" | ||
| self.CREDENTIAL_URL, "not_found" | ||
| ) | ||
| ) | ||
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.
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.
We still need a test with non-none headers. eg: