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
Add system tests for service account credentials #51
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Copyright 2015 Google Inc. All rights reserved. | ||
| # | ||
| # 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. | ||
|
|
||
| DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
| ROOT=$( dirname "$DIR" ) | ||
|
|
||
| # Work from the project root. | ||
| cd $ROOT | ||
|
|
||
| openssl aes-256-cbc -k "$1" \ | ||
| -in system_tests/secrets.tar.enc \ | ||
| -out system_tests/secrets.tar -d | ||
| tar xvf system_tests/secrets.tar | ||
| rm system_tests/secrets.tar |
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,32 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Copyright 2015 Google Inc. All rights reserved. | ||
| # | ||
| # 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. | ||
|
|
||
| DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
| ROOT=$( dirname "$DIR" ) | ||
|
|
||
| # Work from the project root. | ||
| cd $ROOT | ||
|
|
||
| read -s -p "Enter password for encryption: " PASSWORD | ||
| echo | ||
|
|
||
| tar cvf system_tests/secrets.tar system_tests/data | ||
| openssl aes-256-cbc -k "$PASSWORD" \ | ||
| -in system_tests/secrets.tar \ | ||
| -out system_tests/secrets.tar.enc | ||
| rm system_tests/secrets.tar | ||
|
|
||
| travis encrypt "SECRETS_PASSWORD=$PASSWORD" --add --override |
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,36 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Copyright 2015 Google Inc. All rights reserved. | ||
| # | ||
| # 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. | ||
|
|
||
| DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
| ROOT=$( dirname "$DIR" ) | ||
|
|
||
| # Work from the project root. | ||
| cd $ROOT | ||
|
|
||
| # Decrypt secrets and run system tests if not on an external PR. | ||
| if [[ -n $SYSTEM_TEST ]]; then | ||
| if [[ $TRAVIS_SECURE_ENV_VARS == "true" ]]; then | ||
| echo 'Extracting secrets.' | ||
| scripts/decrypt-secrets.sh "$SECRETS_PASSWORD" | ||
| else | ||
| # This is an external PR, so just mark system tests as green. | ||
| echo 'In system test but secrets are not available, skipping.' | ||
| exit 0 | ||
| fi | ||
| fi | ||
|
|
||
| # Run tox. | ||
| tox |
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 |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| data | ||
| secrets.tar |
Empty file.
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,74 @@ | ||
| # Copyright 2016 Google Inc. | ||
| # | ||
| # 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 os | ||
|
|
||
| from google.auth import _helpers | ||
| import google.auth.transport.urllib3 | ||
| import pytest | ||
| import urllib3 | ||
|
|
||
|
|
||
| HERE = os.path.dirname(__file__) | ||
| DATA_DIR = os.path.join(HERE, 'data') | ||
| HTTP = urllib3.PoolManager() | ||
| TOKEN_INFO_URL = 'https://www.googleapis.com/oauth2/v3/tokeninfo' | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def service_account_file(): | ||
| """The full path to a valid service account key file.""" | ||
| yield os.path.join(DATA_DIR, 'service_account.json') | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def request(): | ||
| """A transport.request object.""" | ||
| yield google.auth.transport.urllib3.Request(HTTP) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def token_info(request): | ||
| """Returns a function that obtains OAuth2 token info.""" | ||
| def _token_info(access_token=None, id_token=None): | ||
| query_params = {} | ||
|
|
||
| if access_token is not None: | ||
| query_params['access_token'] = access_token | ||
| elif id_token is not None: | ||
| query_params['id_token'] = id_token | ||
| else: | ||
| raise ValueError('No token specified.') | ||
|
|
||
| url = _helpers.update_query(TOKEN_INFO_URL, query_params) | ||
|
|
||
| response = request(url=url, method='GET') | ||
|
|
||
| return json.loads(response.data.decode('utf-8')) | ||
|
|
||
| yield _token_info | ||
|
|
||
|
|
||
| def verify_environment(): | ||
| """Checks to make sure that requisite data files are available.""" | ||
| if not os.path.isdir(DATA_DIR): | ||
| raise EnvironmentError( | ||
| 'In order to run system tests, test data must exist in ' | ||
| 'system_tests/data. See CONTRIBUTING.rst for details.') | ||
|
|
||
|
|
||
| def pytest_configure(config): | ||
| """Pytest hook that runs before Pytest collects any tests.""" | ||
| verify_environment() |
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,43 @@ | ||
| # Copyright 2016 Google Inc. | ||
| # | ||
| # 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. | ||
|
|
||
| from google.auth import exceptions | ||
| from google.oauth2 import service_account | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def credentials(service_account_file): | ||
| yield service_account.Credentials.from_service_account_file( | ||
| service_account_file) | ||
|
|
||
|
|
||
| def test_refresh_no_scopes(request, credentials): | ||
| with pytest.raises(exceptions.RefreshError): | ||
| credentials.refresh(request) | ||
|
|
||
|
|
||
| def test_refresh_success(request, credentials, token_info): | ||
| credentials = credentials.with_scopes(['email', 'profile']) | ||
|
|
||
| credentials.refresh(request) | ||
|
|
||
| assert credentials.token | ||
|
|
||
| info = token_info(credentials.token) | ||
|
|
||
| assert info['email'] == credentials._service_account_email | ||
| assert info['scope'] == ( | ||
| 'https://www.googleapis.com/auth/userinfo.email ' | ||
| 'https://www.googleapis.com/auth/userinfo.profile') | ||
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.