-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): add auth samples and tests (#1102)
* docs(samples): add auth samples and tests * refactored verifying google token and lint fixed test file * Modified comment acc to review * renamed method acc to review comment * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * added comment acc to review * add samples tests as required checks * use GOOGLE_CLOUD_PROJECT * test new config 1 * adding refresh token for sys test * updating all py verion configs * update 3 * update 4 * update 5 - trimming nox * update 6 - fixing requirements.txt * update 7 - fixing pytest flags * update 8 - fixing sa test cred * update 9- reading sa path from env * update 10- testing explicit * update 11 - fix multi reference * update 12 - remove project id from client params * update 13 - use projectid from default * update 14 - remove project param * update 15- fix assert * update 16 - updating other py versions * update 17: try replacing compute with storage * update 18: fix assert and pass project * update 19: fixing comments * update 20: remove unused Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Sai Sunder Srinivasan <[email protected]>
- Loading branch information
1 parent
3fd4b0d
commit ac87520
Showing
18 changed files
with
633 additions
and
115 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/bin/bash | ||
# Copyright 2018 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 | ||
# | ||
# https://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. | ||
|
||
set -eo pipefail | ||
|
||
if [[ -z "${PROJECT_ROOT:-}" ]]; then | ||
PROJECT_ROOT="github/google-auth-library-python" | ||
fi | ||
|
||
cd "${PROJECT_ROOT}" | ||
|
||
# Disable buffering, so that the logs stream through. | ||
export PYTHONUNBUFFERED=1 | ||
|
||
# Remove old nox | ||
python3 -m pip uninstall --yes --quiet nox-automation | ||
|
||
# Install nox | ||
python3 -m pip install --upgrade --quiet nox | ||
python3 -m nox --version | ||
|
||
# Setup service account credentials. | ||
export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/service-account.json | ||
|
||
# Setup project id. | ||
export PROJECT_ID=$(cat "${KOKORO_GFILE_DIR}/project-id.txt") | ||
|
||
# Activate gcloud with service account credentials | ||
gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS | ||
gcloud config set project ${PROJECT_ID} | ||
|
||
# Decrypt system test secrets | ||
./scripts/decrypt-secrets.sh | ||
|
||
# Run system tests which use a different noxfile | ||
python3 -m nox -f samples/cloud-client/snippets/noxfile.py |
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
55 changes: 55 additions & 0 deletions
55
samples/cloud-client/snippets/authenticate_explicit_with_adc.py
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,55 @@ | ||
# Copyright 2022 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. | ||
|
||
# [START auth_cloud_explicit_adc] | ||
|
||
from google.cloud import storage | ||
|
||
import google.oauth2.credentials | ||
import google.auth | ||
|
||
|
||
def authenticate_explicit_with_adc(): | ||
""" | ||
List storage buckets by authenticating with ADC. | ||
// TODO(Developer): | ||
// 1. Before running this sample, | ||
// set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc | ||
// 2. Replace the project variable. | ||
// 3. Make sure you have the necessary permission to list storage buckets: "storage.buckets.list" | ||
""" | ||
|
||
# Construct the Google credentials object which obtains the default configuration from your | ||
# working environment. | ||
# google.auth.default() will give you ComputeEngineCredentials | ||
# if you are on a GCE (or other metadata server supported environments). | ||
credentials, project_id = google.auth.default() | ||
# If you are authenticating to a Cloud API, you can let the library include the default scope, | ||
# https://www.googleapis.com/auth/cloud-platform, because IAM is used to provide fine-grained | ||
# permissions for Cloud. | ||
# If you need to provide a scope, specify it as follows: | ||
# credentials = google.auth.default(scopes=scope) | ||
# For more information on scopes to use, | ||
# see: https://developers.google.com/identity/protocols/oauth2/scopes | ||
|
||
# Construct the Storage client. | ||
storage_client = storage.Client(credentials=credentials, project=project_id) | ||
buckets = storage_client.list_buckets() | ||
print("Buckets:") | ||
for bucket in buckets: | ||
print(bucket.name) | ||
print("Listed all storage buckets.") | ||
|
||
# [END auth_cloud_explicit_adc] |
Oops, something went wrong.