Skip to content

Commit 88700c3

Browse files
author
Jon Wayne Parrott
authored
Add more auth samples (GoogleCloudPlatform#981)
Add samples for explicitly using compute and app engine credentials.
1 parent c0abbe5 commit 88700c3

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

auth/api-client/snippets.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,40 @@ def explicit(project):
4848
print(buckets)
4949

5050

51+
def explicit_compute_engine(project):
52+
from google.auth import compute_engine
53+
import googleapiclient.discovery
54+
55+
# Explicitly use Compute Engine credentials. These credentials are
56+
# available on Compute Engine, App Engine Flexible, and Container Engine.
57+
credentials = compute_engine.Credentials()
58+
59+
# Explicitly pass the credentials to the client library.
60+
storage_client = googleapiclient.discovery.build(
61+
'storage', 'v1', credentials=credentials)
62+
63+
# Make an authenticated API request
64+
buckets = storage_client.buckets().list(project=project).execute()
65+
print(buckets)
66+
67+
68+
def explicit_app_engine(project):
69+
from google.auth import app_engine
70+
import googleapiclient.discovery
71+
72+
# Explicitly use App Engine credentials. These credentials are
73+
# only available when running on App Engine Standard.
74+
credentials = app_engine.Credentials()
75+
76+
# Explicitly pass the credentials to the client library.
77+
storage_client = googleapiclient.discovery.build(
78+
'storage', 'v1', credentials=credentials)
79+
80+
# Make an authenticated API request
81+
buckets = storage_client.buckets().list(project=project).execute()
82+
print(buckets)
83+
84+
5185
if __name__ == '__main__':
5286
parser = argparse.ArgumentParser(
5387
description=__doc__,
@@ -57,10 +91,18 @@ def explicit(project):
5791
subparsers = parser.add_subparsers(dest='command')
5892
subparsers.add_parser('implicit', help=implicit.__doc__)
5993
subparsers.add_parser('explicit', help=explicit.__doc__)
94+
subparsers.add_parser(
95+
'explicit_compute_engine', help=explicit_compute_engine.__doc__)
96+
subparsers.add_parser(
97+
'explicit_app_engine', help=explicit_app_engine.__doc__)
6098

6199
args = parser.parse_args()
62100

63101
if args.command == 'implicit':
64102
implicit(args.project)
65103
elif args.command == 'explicit':
66104
explicit(args.project)
105+
elif args.command == 'explicit_compute_engine':
106+
explicit_compute_engine(args.project)
107+
elif args.command == 'explicit_app_engine':
108+
explicit_app_engine(args.project)

auth/api-client/snippets_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import os
1616

17+
import google.auth
1718
import mock
1819

1920
import snippets
@@ -31,3 +32,21 @@ def test_explicit():
3132

3233
with mock.patch('io.open', open_mock):
3334
snippets.explicit(os.environ['GCLOUD_PROJECT'])
35+
36+
37+
def test_explicit_compute_engine():
38+
adc, project = google.auth.default()
39+
credentials_patch = mock.patch(
40+
'google.auth.compute_engine.Credentials', return_value=adc)
41+
42+
with credentials_patch:
43+
snippets.explicit_compute_engine(project)
44+
45+
46+
def test_explicit_app_engine():
47+
adc, project = google.auth.default()
48+
credentials_patch = mock.patch(
49+
'google.auth.app_engine.Credentials', return_value=adc)
50+
51+
with credentials_patch:
52+
snippets.explicit_app_engine(project)

auth/cloud-client/snippets.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ def explicit():
4545
print(buckets)
4646

4747

48+
def explicit_compute_engine(project):
49+
from google.auth import compute_engine
50+
from google.cloud import storage
51+
52+
# Explicitly use Compute Engine credentials. These credentials are
53+
# available on Compute Engine, App Engine Flexible, and Container Engine.
54+
credentials = compute_engine.Credentials()
55+
56+
# Create the client using the credentials and specifying a project ID.
57+
storage_client = storage.Client(credentials=credentials, project=project)
58+
59+
# Make an authenticated API request
60+
buckets = list(storage_client.list_buckets())
61+
print(buckets)
62+
63+
4864
if __name__ == '__main__':
4965
parser = argparse.ArgumentParser(
5066
description=__doc__,
@@ -53,10 +69,15 @@ def explicit():
5369
subparsers = parser.add_subparsers(dest='command')
5470
subparsers.add_parser('implicit', help=implicit.__doc__)
5571
subparsers.add_parser('explicit', help=explicit.__doc__)
72+
explicit_gce_parser = subparsers.add_parser(
73+
'explicit_compute_engine', help=explicit_compute_engine.__doc__)
74+
explicit_gce_parser.add_argument('project')
5675

5776
args = parser.parse_args()
5877

5978
if args.command == 'implicit':
6079
implicit()
6180
elif args.command == 'explicit':
6281
explicit()
82+
elif args.command == 'explicit_compute_engine':
83+
explicit_compute_engine(args.project)

auth/cloud-client/snippets_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import os
1616

17+
import google.auth
1718
import mock
1819

1920
import snippets
@@ -31,3 +32,12 @@ def test_explicit():
3132

3233
with mock.patch('io.open', open_mock):
3334
snippets.explicit()
35+
36+
37+
def test_explicit_compute_engine():
38+
adc, project = google.auth.default()
39+
credentials_patch = mock.patch(
40+
'google.auth.compute_engine.Credentials', return_value=adc)
41+
42+
with credentials_patch:
43+
snippets.explicit_compute_engine(project)

0 commit comments

Comments
 (0)