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
feat: support self-signed jwt in requests and urllib3 transports #679
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import google.auth | ||
import google.auth.credentials | ||
import google.auth.transport.requests | ||
from google.oauth2 import service_account | ||
|
||
|
||
def test_authorized_session_with_service_account_and_self_signed_jwt(): | ||
credentials, project_id = google.auth.default() | ||
|
||
credentials = credentials.with_scopes( | ||
scopes=[], | ||
default_scopes=["https://www.googleapis.com/auth/pubsub"], | ||
) | ||
|
||
session = google.auth.transport.requests.AuthorizedSession( | ||
credentials=credentials, default_host="pubsub.googleapis.com" | ||
) | ||
|
||
# List Pub/Sub Topics through the REST API | ||
# https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/list | ||
response = session.get("https://pubsub.googleapis.com/v1/projects/{}/topics".format(project_id)) | ||
response.raise_for_status() | ||
|
||
# Check that self-signed JWT was created and is being used | ||
assert credentials._jwt_credentials is not None | ||
assert credentials._jwt_credentials.token == credentials.token | ||
Comment on lines
+21
to
+40
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. I initially tried to write this test with the Storage API, but requests seemed to fail with invalid credentials (a test with the service account passed). Are there any APIs where the self-signed JWT approach won't work? @bshaffer |
This file contains 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import google.auth | ||
import google.auth.credentials | ||
import google.auth.transport.requests | ||
from google.oauth2 import service_account | ||
|
||
|
||
def test_authorized_session_with_service_account_and_self_signed_jwt(): | ||
credentials, project_id = google.auth.default() | ||
|
||
credentials = credentials.with_scopes( | ||
scopes=[], | ||
default_scopes=["https://www.googleapis.com/auth/pubsub"], | ||
) | ||
|
||
http = google.auth.transport.urllib3.AuthorizedHttp( | ||
credentials=credentials, default_host="pubsub.googleapis.com" | ||
) | ||
|
||
# List Pub/Sub Topics through the REST API | ||
# https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/list | ||
response = http.urlopen( | ||
method="GET", | ||
url="https://pubsub.googleapis.com/v1/projects/{}/topics".format(project_id) | ||
) | ||
|
||
assert response.status == 200 | ||
|
||
# Check that self-signed JWT was created and is being used | ||
assert credentials._jwt_credentials is not None | ||
assert credentials._jwt_credentials.token == credentials.token |
This file contains 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 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
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.
I have a little bit concern about setting the
default_host
in ctor. There are 2 default endpoints, for instance,pubsub.googleapis.com
andpubsub.mtls.googleapis.com
. mtls has auto switch logic to switch from the regular endpoint to mtls endpoint. It works as follows:(1) client creates an AuthorizedSession
(2) client calls AuthorizedSession.is_mtls (with other conditions like the GOOGLE_API_USE_CLIENT_CERTIFICATE env var) to decide which endpoint to use
So this means in the AuthorizedSession ctor, we may not know exactly which default endpoint will be used. So if both endpoints use the regular endpoint as the audience, then this code works fine. Otherwise, we need to find a way.
@bshaffer
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.
mtls only works with user credentials, so this should be fine since service account credentials will be rejected by the server anyway.