This repository was archived by the owner on Mar 6, 2026. It is now read-only.
Repository navigation
fix: only add IAM scope to credentials that can change scopes #451
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
27a0b0f
fix: only add IAM scope to credentials that can change scopes
busunkim96 1a78d82
test: add systest for impersonated creds
busunkim96 f61a9a6
test: update unit tests
busunkim96 8dff1c6
chore: fix lint
busunkim96 f8eed76
Merge branch 'master' into impersonated-creds-fix
busunkim96 5a318f6
build: update secrets
busunkim96 d245688
Merge branch 'impersonated-creds-fix' of github.com:googleapis/google…
busunkim96 ff0aed9
docs: update contributing.rst with impersonated creds
busunkim96 3144b0a
docs: fix formatting
busunkim96 6c35e85
fix: use if credentials.Scoped instead of try/except
busunkim96 61064aa
fix: remove except caluse
busunkim96 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
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
Binary file not shown.
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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| # Copyright 2020 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 json | ||
| import pytest | ||
|
|
||
| import google.oauth2.credentials | ||
| from google.oauth2 import service_account | ||
| import google.auth.impersonated_credentials | ||
| from google.auth import _helpers | ||
|
|
||
|
|
||
| GOOGLE_OAUTH2_TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def service_account_credentials(service_account_file): | ||
| yield service_account.Credentials.from_service_account_file(service_account_file) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def impersonated_service_account_credentials(impersonated_service_account_file): | ||
| yield service_account.Credentials.from_service_account_file( | ||
| impersonated_service_account_file | ||
| ) | ||
|
|
||
|
|
||
| def test_refresh_with_user_credentials_as_source( | ||
| authorized_user_file, | ||
| impersonated_service_account_credentials, | ||
| http_request, | ||
| token_info, | ||
| ): | ||
| with open(authorized_user_file, "r") as fh: | ||
| info = json.load(fh) | ||
|
|
||
| source_credentials = google.oauth2.credentials.Credentials( | ||
| None, | ||
| refresh_token=info["refresh_token"], | ||
| token_uri=GOOGLE_OAUTH2_TOKEN_ENDPOINT, | ||
| client_id=info["client_id"], | ||
| client_secret=info["client_secret"], | ||
| # The source credential needs this scope for the generateAccessToken request | ||
| # The user must also have `Service Account Token Creator` on the project | ||
| # that owns the impersonated service account. | ||
| # See https://cloud.google.com/iam/docs/creating-short-lived-service-account-credentials | ||
| scopes=["https://www.googleapis.com/auth/cloud-platform"], | ||
| ) | ||
|
|
||
| source_credentials.refresh(http_request) | ||
|
|
||
| target_scopes = [ | ||
| "https://www.googleapis.com/auth/devstorage.read_only", | ||
| "https://www.googleapis.com/auth/analytics", | ||
| ] | ||
| target_credentials = google.auth.impersonated_credentials.Credentials( | ||
| source_credentials=source_credentials, | ||
| target_principal=impersonated_service_account_credentials.service_account_email, | ||
| target_scopes=target_scopes, | ||
| lifetime=100, | ||
| ) | ||
|
|
||
| target_credentials.refresh(http_request) | ||
| assert target_credentials.token | ||
|
|
||
|
|
||
| def test_refresh_with_service_account_credentials_as_source( | ||
| http_request, | ||
| service_account_credentials, | ||
| impersonated_service_account_credentials, | ||
| token_info, | ||
| ): | ||
| source_credentials = service_account_credentials.with_scopes(["email"]) | ||
| source_credentials.refresh(http_request) | ||
| assert source_credentials.token | ||
|
|
||
| target_scopes = [ | ||
| "https://www.googleapis.com/auth/devstorage.read_only", | ||
| "https://www.googleapis.com/auth/analytics", | ||
| ] | ||
| target_credentials = google.auth.impersonated_credentials.Credentials( | ||
| source_credentials=source_credentials, | ||
| target_principal=impersonated_service_account_credentials.service_account_email, | ||
| target_scopes=target_scopes, | ||
| ) | ||
|
|
||
| target_credentials.refresh(http_request) | ||
| assert target_credentials.token |
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
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.