Skip to content

Commit 635e904

Browse files
author
Jon Wayne Parrott
committed
Merge pull request #275 from GoogleCloudPlatform/compute-auth
Adding compute auth examples
2 parents 7c4a834 + 133b622 commit 635e904

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

compute/auth/access_token.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Example of authenticating using access tokens directly on Compute Engine.
18+
19+
For more information, see the README.md under /compute.
20+
"""
21+
22+
# [START all]
23+
24+
import argparse
25+
26+
import requests
27+
28+
29+
METADATA_URL = 'http://metadata.google.internal/computeMetadata/v1/'
30+
METADATA_HEADERS = {'Metadata-Flavor': 'Google'}
31+
SERVICE_ACCOUNT = 'default'
32+
33+
34+
def get_access_token():
35+
url = '{}instance/service-accounts/{}/token'.format(
36+
METADATA_URL, SERVICE_ACCOUNT)
37+
38+
# Request an access token from the metadata server.
39+
r = requests.get(url, headers=METADATA_HEADERS)
40+
r.raise_for_status()
41+
42+
# Extract the access token from the respose.
43+
access_token = r.json()['access_token']
44+
45+
return access_token
46+
47+
48+
def list_buckets(project_id, access_token):
49+
url = 'https://www.googleapis.com/storage/v1/b'
50+
params = {
51+
'project': project_id
52+
}
53+
headers = {
54+
'Authenication': 'Bearer {}'.format(access_token)
55+
}
56+
57+
r = requests.get(url, params=params, headers=headers)
58+
r.raise_for_status()
59+
60+
return r.json()
61+
62+
63+
def main(project_id):
64+
access_token = get_access_token()
65+
buckets = list_buckets(project_id, access_token)
66+
print(buckets)
67+
68+
69+
if __name__ == '__main__':
70+
parser = argparse.ArgumentParser(
71+
description=__doc__,
72+
formatter_class=argparse.RawDescriptionHelpFormatter)
73+
parser.add_argument('project_id', help='Your Google Cloud project ID.')
74+
75+
args = parser.parse_args()
76+
77+
main(args.project_id)
78+
# [END all]

compute/auth/access_token_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import access_token
15+
import mock
16+
17+
18+
@mock.patch('access_token.requests')
19+
def test_main(requests_mock, cloud_config):
20+
metadata_response = mock.Mock()
21+
metadata_response.status_code = 200
22+
metadata_response.json.return_value = {
23+
'access_token': '123'
24+
}
25+
bucket_response = mock.Mock()
26+
bucket_response.status_code = 200
27+
bucket_response.json.return_value = [{'bucket': 'name'}]
28+
29+
requests_mock.get.side_effect = [
30+
metadata_response, bucket_response]
31+
32+
access_token.main(cloud_config.project)
33+
34+
assert requests_mock.get.call_count == 2

compute/auth/application_default.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Example of authenticating using Application Default Credentials on
18+
Compute Engine.
19+
20+
For more information, see the README.md under /compute.
21+
"""
22+
23+
# [START all]
24+
25+
import argparse
26+
27+
from googleapiclient import discovery
28+
from oauth2client.client import GoogleCredentials
29+
30+
31+
def create_service():
32+
# Get the application default credentials. When running locally, these are
33+
# available after running `gcloud auth`. When running on compute
34+
# engine, these are available from the environment.
35+
credentials = GoogleCredentials.get_application_default()
36+
37+
# Construct the service object for interacting with the Cloud Storage API -
38+
# the 'storage' service, at version 'v1'.
39+
return discovery.build('storage', 'v1', credentials=credentials)
40+
41+
42+
def list_buckets(service, project_id):
43+
buckets = service.buckets().list(project=project_id).execute()
44+
return buckets
45+
46+
47+
def main(project_id):
48+
service = create_service()
49+
buckets = list_buckets(service, project_id)
50+
print(buckets)
51+
52+
53+
if __name__ == '__main__':
54+
parser = argparse.ArgumentParser(
55+
description=__doc__,
56+
formatter_class=argparse.RawDescriptionHelpFormatter)
57+
parser.add_argument('project_id', help='Your Google Cloud Project ID.')
58+
59+
args = parser.parse_args()
60+
61+
main(args.project_id)
62+
# [END all]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from application_default import main
15+
16+
17+
def test_main(cloud_config):
18+
main(cloud_config.project)

compute/auth/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests==2.9.1
2+
google-api-python-client==1.5.0

0 commit comments

Comments
 (0)