gcloud 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +2 -3
- data/CHANGELOG +4 -0
- data/LICENSE +674 -0
- data/Manifest +111 -0
- data/README.md +4 -3
- data/bin/gcutil +53 -0
- data/gcloud.gemspec +4 -3
- data/packages/gcutil-1.7.1/CHANGELOG +197 -0
- data/packages/gcutil-1.7.1/LICENSE +202 -0
- data/packages/gcutil-1.7.1/VERSION +1 -0
- data/packages/gcutil-1.7.1/gcutil +53 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/LICENSE +23 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/apiclient/__init__.py +1 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/apiclient/discovery.py +743 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/apiclient/errors.py +123 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/apiclient/ext/__init__.py +0 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/apiclient/http.py +1443 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/apiclient/mimeparse.py +172 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/apiclient/model.py +385 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/apiclient/schema.py +303 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/__init__.py +1 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/anyjson.py +32 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/appengine.py +528 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/client.py +1139 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/clientsecrets.py +105 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/crypt.py +244 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/django_orm.py +124 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/file.py +107 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.py +343 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/multistore_file.py +379 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/tools.py +174 -0
- data/packages/gcutil-1.7.1/lib/google_api_python_client/uritemplate/__init__.py +147 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/LICENSE +202 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/google/__init__.py +3 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/google/apputils/__init__.py +3 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/google/apputils/app.py +356 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/google/apputils/appcommands.py +783 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/google/apputils/basetest.py +1260 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/google/apputils/datelib.py +421 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/google/apputils/debug.py +60 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/google/apputils/file_util.py +181 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/google/apputils/resources.py +67 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/google/apputils/run_script_module.py +217 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/google/apputils/setup_command.py +159 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/google/apputils/shellutil.py +49 -0
- data/packages/gcutil-1.7.1/lib/google_apputils/google/apputils/stopwatch.py +204 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/__init__.py +0 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/auth_helper.py +140 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/auth_helper_test.py +149 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/auto_auth.py +130 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/auto_auth_test.py +75 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/basic_cmds.py +128 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/basic_cmds_test.py +111 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/command_base.py +1808 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/command_base_test.py +1651 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/compute/v1beta13.json +2851 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/compute/v1beta14.json +3361 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/disk_cmds.py +342 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/disk_cmds_test.py +474 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/firewall_cmds.py +344 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/firewall_cmds_test.py +231 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/flags_cache.py +274 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/gcutil +89 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/gcutil_logging.py +69 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/image_cmds.py +262 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/image_cmds_test.py +172 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/instance_cmds.py +1506 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/instance_cmds_test.py +1904 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/kernel_cmds.py +91 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/kernel_cmds_test.py +56 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/machine_type_cmds.py +106 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/machine_type_cmds_test.py +59 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/metadata.py +96 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/metadata_lib.py +357 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/metadata_test.py +84 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/mock_api.py +420 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/mock_metadata.py +58 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/move_cmds.py +824 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/move_cmds_test.py +307 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/network_cmds.py +178 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/network_cmds_test.py +133 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/operation_cmds.py +181 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/operation_cmds_test.py +196 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/path_initializer.py +38 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/project_cmds.py +173 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/project_cmds_test.py +111 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/scopes.py +61 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/scopes_test.py +50 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/snapshot_cmds.py +276 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/snapshot_cmds_test.py +260 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/ssh_keys.py +266 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/ssh_keys_test.py +128 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/table_formatter.py +563 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/thread_pool.py +188 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/thread_pool_test.py +88 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/utils.py +208 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/utils_test.py +193 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/version.py +17 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/version_checker.py +246 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/version_checker_test.py +271 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/zone_cmds.py +151 -0
- data/packages/gcutil-1.7.1/lib/google_compute_engine/gcutil/zone_cmds_test.py +60 -0
- data/packages/gcutil-1.7.1/lib/httplib2/LICENSE +21 -0
- data/packages/gcutil-1.7.1/lib/httplib2/httplib2/__init__.py +1630 -0
- data/packages/gcutil-1.7.1/lib/httplib2/httplib2/cacerts.txt +714 -0
- data/packages/gcutil-1.7.1/lib/httplib2/httplib2/iri2uri.py +110 -0
- data/packages/gcutil-1.7.1/lib/httplib2/httplib2/socks.py +438 -0
- data/packages/gcutil-1.7.1/lib/iso8601/LICENSE +20 -0
- data/packages/gcutil-1.7.1/lib/iso8601/iso8601/__init__.py +1 -0
- data/packages/gcutil-1.7.1/lib/iso8601/iso8601/iso8601.py +102 -0
- data/packages/gcutil-1.7.1/lib/iso8601/iso8601/test_iso8601.py +111 -0
- data/packages/gcutil-1.7.1/lib/python_gflags/AUTHORS +2 -0
- data/packages/gcutil-1.7.1/lib/python_gflags/LICENSE +28 -0
- data/packages/gcutil-1.7.1/lib/python_gflags/gflags.py +2862 -0
- data/packages/gcutil-1.7.1/lib/python_gflags/gflags2man.py +544 -0
- data/packages/gcutil-1.7.1/lib/python_gflags/gflags_validators.py +187 -0
- metadata +118 -5
- metadata.gz.sig +0 -0
@@ -0,0 +1,149 @@
|
|
1
|
+
#!/usr/bin/python
|
2
|
+
#
|
3
|
+
# Copyright 2012 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
|
+
"""Tests for auth_helper."""
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
import path_initializer
|
22
|
+
path_initializer.InitializeSysPath()
|
23
|
+
|
24
|
+
import datetime
|
25
|
+
|
26
|
+
|
27
|
+
import apiclient
|
28
|
+
import oauth2client.client as oauth2_client
|
29
|
+
import oauth2client.multistore_file as oauth2_multistore_file
|
30
|
+
import oauth2client.tools as oauth2_tools
|
31
|
+
|
32
|
+
import gflags as flags
|
33
|
+
import unittest
|
34
|
+
from gcutil import auth_helper
|
35
|
+
from gcutil import mock_metadata
|
36
|
+
|
37
|
+
|
38
|
+
FLAGS = flags.FLAGS
|
39
|
+
|
40
|
+
CREDS_FILENAME = './unused_filename'
|
41
|
+
|
42
|
+
|
43
|
+
class AuthHelperTest(unittest.TestCase):
|
44
|
+
|
45
|
+
class MockCred(object):
|
46
|
+
pass
|
47
|
+
|
48
|
+
class MockCredStorage(object):
|
49
|
+
def __init__(self, cred):
|
50
|
+
self.cred = cred
|
51
|
+
|
52
|
+
def get(self):
|
53
|
+
return self.cred
|
54
|
+
|
55
|
+
@staticmethod
|
56
|
+
def MockGetCredentialStorage(credentials_file,
|
57
|
+
client_id,
|
58
|
+
user_agent,
|
59
|
+
scopes):
|
60
|
+
cred = AuthHelperTest.MockCred()
|
61
|
+
storage = AuthHelperTest.MockCredStorage(cred)
|
62
|
+
cred.credentials_file = credentials_file
|
63
|
+
cred.client_id = client_id
|
64
|
+
cred.user_agent = user_agent
|
65
|
+
cred.scopes = scopes
|
66
|
+
cred.invalid = False
|
67
|
+
return storage
|
68
|
+
|
69
|
+
@staticmethod
|
70
|
+
def MockOAuthFlowRun(flow, unused_storage):
|
71
|
+
cred = AuthHelperTest.MockCred()
|
72
|
+
cred.client_id = flow.client_id
|
73
|
+
cred.client_secret = flow.client_secret
|
74
|
+
cred.scopes = flow.scope
|
75
|
+
cred.user_agent = flow.user_agent
|
76
|
+
return cred
|
77
|
+
|
78
|
+
def setUp(self):
|
79
|
+
FLAGS.credentials_file = CREDS_FILENAME
|
80
|
+
|
81
|
+
def testGetValidCred(self):
|
82
|
+
oauth2_multistore_file.get_credential_storage = (
|
83
|
+
self.MockGetCredentialStorage)
|
84
|
+
cred = auth_helper.GetCredentialFromStore(['a', 'b'])
|
85
|
+
self.assertEqual(cred.credentials_file, CREDS_FILENAME)
|
86
|
+
self.assertEqual(cred.client_id, auth_helper.OAUTH2_CLIENT_ID)
|
87
|
+
self.assertEqual(cred.user_agent, auth_helper.USER_AGENT)
|
88
|
+
self.assertEqual(cred.scopes, 'a b')
|
89
|
+
self.assertEqual(cred.invalid, False)
|
90
|
+
|
91
|
+
def testSortScopes(self):
|
92
|
+
oauth2_multistore_file.get_credential_storage = (
|
93
|
+
self.MockGetCredentialStorage)
|
94
|
+
cred = auth_helper.GetCredentialFromStore(['b', 'a'])
|
95
|
+
self.assertEqual(cred.credentials_file, CREDS_FILENAME)
|
96
|
+
self.assertEqual(cred.client_id, auth_helper.OAUTH2_CLIENT_ID)
|
97
|
+
self.assertEqual(cred.user_agent, auth_helper.USER_AGENT)
|
98
|
+
self.assertEqual(cred.scopes, 'a b')
|
99
|
+
self.assertEqual(cred.invalid, False)
|
100
|
+
|
101
|
+
def testNoAskuser(self):
|
102
|
+
oauth2_multistore_file.get_credential_storage = (
|
103
|
+
self.MockGetCredentialStorage)
|
104
|
+
cred = auth_helper.GetCredentialFromStore(['b', 'a'],
|
105
|
+
force_reauth=True,
|
106
|
+
ask_user=False)
|
107
|
+
self.assertEqual(cred.credentials_file, CREDS_FILENAME)
|
108
|
+
self.assertEqual(cred.client_id, auth_helper.OAUTH2_CLIENT_ID)
|
109
|
+
self.assertEqual(cred.user_agent, auth_helper.USER_AGENT)
|
110
|
+
self.assertEqual(cred.scopes, 'a b')
|
111
|
+
self.assertEqual(cred.invalid, True)
|
112
|
+
|
113
|
+
def testReauthFlow(self):
|
114
|
+
oauth2_multistore_file.get_credential_storage = (
|
115
|
+
self.MockGetCredentialStorage)
|
116
|
+
oauth2_tools.run = self.MockOAuthFlowRun
|
117
|
+
cred = auth_helper.GetCredentialFromStore(['b', 'a'],
|
118
|
+
force_reauth=True,
|
119
|
+
ask_user=True)
|
120
|
+
self.assertEqual(cred.client_id, auth_helper.OAUTH2_CLIENT_ID)
|
121
|
+
self.assertEqual(cred.client_secret, auth_helper.OAUTH2_CLIENT_SECRET)
|
122
|
+
self.assertEqual(cred.user_agent, auth_helper.USER_AGENT)
|
123
|
+
self.assertEqual(cred.scopes, 'a b')
|
124
|
+
|
125
|
+
def testMetadataAuth(self):
|
126
|
+
metadata = mock_metadata.MockMetadata()
|
127
|
+
metadata.ExpectIsPresent(True)
|
128
|
+
|
129
|
+
token = 'access token'
|
130
|
+
expiry = datetime.datetime(1970, 1, 1)
|
131
|
+
metadata.ExpectGetAccessToken((token, expiry))
|
132
|
+
|
133
|
+
scope1 = 'https://www.googleapis.com/auth/compute'
|
134
|
+
scope2 = 'scope2'
|
135
|
+
|
136
|
+
metadata.ExpectGetAccessScopes([scope1, scope2])
|
137
|
+
|
138
|
+
oauth2_multistore_file.get_credential_storage = (
|
139
|
+
self.MockGetCredentialStorage)
|
140
|
+
cred = auth_helper.GetCredentialFromStore([scope1, scope2],
|
141
|
+
metadata=metadata)
|
142
|
+
self.assertTrue(cred is not None)
|
143
|
+
self.assertEquals(token, cred.access_token)
|
144
|
+
self.assertEquals(expiry, cred.token_expiry)
|
145
|
+
self.assertFalse(metadata.ExpectsMoreCalls())
|
146
|
+
|
147
|
+
|
148
|
+
if __name__ == '__main__':
|
149
|
+
unittest.main()
|
@@ -0,0 +1,130 @@
|
|
1
|
+
# Copyright 2012 Google Inc. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
"""Interface for accessing auto-provided Google Compute Engine access tokens.
|
16
|
+
|
17
|
+
Once a Google Compute Engine instance has been started with
|
18
|
+
--service_account=default \
|
19
|
+
--service_account_scopes="...."
|
20
|
+
|
21
|
+
This module allows access tokens for the specified account and scopes to be
|
22
|
+
fetched from the Google Compute Engine instance automatically.
|
23
|
+
"""
|
24
|
+
|
25
|
+
|
26
|
+
import json
|
27
|
+
import logging
|
28
|
+
|
29
|
+
import apiclient
|
30
|
+
import httplib2
|
31
|
+
import oauth2client.client as oauth2client
|
32
|
+
|
33
|
+
from gcutil import metadata_lib
|
34
|
+
from gcutil import scopes
|
35
|
+
|
36
|
+
|
37
|
+
_logger = logging.getLogger(__name__)
|
38
|
+
|
39
|
+
|
40
|
+
class CredentialsError(oauth2client.Error):
|
41
|
+
"""Credentials could not be instantiated."""
|
42
|
+
pass
|
43
|
+
|
44
|
+
|
45
|
+
class CredentialsNotPresentError(CredentialsError):
|
46
|
+
"""Credentials are not present."""
|
47
|
+
pass
|
48
|
+
|
49
|
+
|
50
|
+
class Credentials(oauth2client.OAuth2Credentials):
|
51
|
+
"""Credentials object that gets credentials from Google Compute Engine."""
|
52
|
+
|
53
|
+
def __init__(self, metadata, service_account, requested_scopes,
|
54
|
+
available_scopes=None, access_token=None, token_expiry=None,
|
55
|
+
any_available=False):
|
56
|
+
self._metadata = metadata
|
57
|
+
self.service_account = service_account
|
58
|
+
self.requested_scopes = requested_scopes
|
59
|
+
self.available_scopes = available_scopes
|
60
|
+
self.any_available = any_available
|
61
|
+
|
62
|
+
if access_token:
|
63
|
+
self.invalid = False
|
64
|
+
else:
|
65
|
+
self.invalid = True
|
66
|
+
try:
|
67
|
+
# Check to see if the tokens are there for fetching.
|
68
|
+
available_scopes = self._metadata.GetAccessScopes(
|
69
|
+
service_account=self.service_account)
|
70
|
+
# Keep track of whatever scopes were actually present.
|
71
|
+
self.available_scopes = list(
|
72
|
+
set(available_scopes).intersection(set(self.requested_scopes)))
|
73
|
+
|
74
|
+
# Check that we have compute scopes.
|
75
|
+
def HasComputeScope(scope_list):
|
76
|
+
return (scopes.COMPUTE_RW_SCOPE in scope_list) or (
|
77
|
+
scopes.COMPUTE_RO_SCOPE in scope_list)
|
78
|
+
|
79
|
+
if not HasComputeScope(self.available_scopes):
|
80
|
+
raise CredentialsNotPresentError('No compute scopes available')
|
81
|
+
|
82
|
+
(access_token, token_expiry) = self._InternalRefresh()
|
83
|
+
except metadata_lib.MetadataNotFoundError, e:
|
84
|
+
# Metadata server says token is not present.
|
85
|
+
raise CredentialsNotPresentError('Service account not found')
|
86
|
+
except metadata_lib.MetadataError, e:
|
87
|
+
raise CredentialsError('Metadata server failure: %s', e)
|
88
|
+
|
89
|
+
oauth2client.OAuth2Credentials.__init__(
|
90
|
+
self,
|
91
|
+
access_token,
|
92
|
+
None,
|
93
|
+
None,
|
94
|
+
None,
|
95
|
+
token_expiry,
|
96
|
+
None,
|
97
|
+
None)
|
98
|
+
self.invalid = False
|
99
|
+
|
100
|
+
def _refresh(self, _):
|
101
|
+
(self.access_token, self.token_expiry) = self._InternalRefresh()
|
102
|
+
|
103
|
+
def _InternalRefresh(self):
|
104
|
+
try:
|
105
|
+
self.invalid = False
|
106
|
+
return self._metadata.GetAccessToken(
|
107
|
+
self.requested_scopes, service_account=self.service_account,
|
108
|
+
any_available=self.any_available)
|
109
|
+
except metadata_lib.MetadataError:
|
110
|
+
self.invalid = True
|
111
|
+
return (None, None)
|
112
|
+
|
113
|
+
@classmethod
|
114
|
+
def from_json(cls, s):
|
115
|
+
"""Create an Credentials from a json string.
|
116
|
+
|
117
|
+
Args:
|
118
|
+
s: The json string.
|
119
|
+
Returns:
|
120
|
+
An Credentials object.
|
121
|
+
"""
|
122
|
+
data = json.loads(s)
|
123
|
+
return Credentials(
|
124
|
+
metadata_lib.Metadata(),
|
125
|
+
data['service_account'],
|
126
|
+
data['requested_scopes'],
|
127
|
+
data['available_scopes'],
|
128
|
+
data['access_token'],
|
129
|
+
data['token_expiry'],
|
130
|
+
data['any_available'])
|
@@ -0,0 +1,75 @@
|
|
1
|
+
#!/usr/bin/python
|
2
|
+
#
|
3
|
+
# Copyright 2012 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
|
+
"""Tests for auto_auth."""
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
import path_initializer
|
22
|
+
path_initializer.InitializeSysPath()
|
23
|
+
|
24
|
+
import datetime
|
25
|
+
|
26
|
+
import unittest
|
27
|
+
from gcutil import auto_auth
|
28
|
+
from gcutil import mock_metadata
|
29
|
+
|
30
|
+
SCOPE1 = 'https://www.googleapis.com/auth/compute'
|
31
|
+
SCOPE2 = 'https://www.googleapis.com/auth/devstorage.full_control'
|
32
|
+
|
33
|
+
class GoogleComputeAutoAuthTest(unittest.TestCase):
|
34
|
+
|
35
|
+
def testCredentials(self):
|
36
|
+
metadata = mock_metadata.MockMetadata()
|
37
|
+
|
38
|
+
token1 = 'access token1'
|
39
|
+
expiry1 = datetime.datetime(1970, 1, 1)
|
40
|
+
token2 = 'access token2'
|
41
|
+
expiry2 = datetime.datetime(1970, 1, 2)
|
42
|
+
|
43
|
+
metadata.ExpectGetAccessScopes([SCOPE1, SCOPE2])
|
44
|
+
metadata.ExpectGetAccessToken((token1, expiry1))
|
45
|
+
metadata.ExpectGetAccessToken((token2, expiry2))
|
46
|
+
|
47
|
+
credentials = auto_auth.Credentials(
|
48
|
+
metadata, 'default', [SCOPE1, SCOPE2])
|
49
|
+
self.assertTrue(SCOPE1 in credentials.available_scopes)
|
50
|
+
self.assertTrue(SCOPE2 in credentials.available_scopes)
|
51
|
+
self.assertEquals(2, len(credentials.available_scopes))
|
52
|
+
self.assertEquals(token1, credentials.access_token)
|
53
|
+
self.assertEquals(expiry1, credentials.token_expiry)
|
54
|
+
|
55
|
+
credentials._refresh(None)
|
56
|
+
self.assertEquals(token2, credentials.access_token)
|
57
|
+
self.assertEquals(expiry2, credentials.token_expiry)
|
58
|
+
|
59
|
+
self.assertFalse(metadata.ExpectsMoreCalls())
|
60
|
+
|
61
|
+
def testWithNoComputeScope(self):
|
62
|
+
metadata = mock_metadata.MockMetadata()
|
63
|
+
metadata.ExpectGetAccessScopes([SCOPE2])
|
64
|
+
|
65
|
+
success = False
|
66
|
+
try:
|
67
|
+
credentials = auto_auth.Credentials(
|
68
|
+
metadata, 'default', [SCOPE2])
|
69
|
+
except auto_auth.CredentialsNotPresentError:
|
70
|
+
success = True
|
71
|
+
self.assertTrue(success, 'Failed to throw exception without compute scope')
|
72
|
+
|
73
|
+
|
74
|
+
if __name__ == '__main__':
|
75
|
+
unittest.main()
|
@@ -0,0 +1,128 @@
|
|
1
|
+
# Copyright 2012 Google Inc. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
"""Commands for interacting with Google Compute Engine."""
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
import httplib2
|
20
|
+
import json
|
21
|
+
|
22
|
+
|
23
|
+
from google.apputils import appcommands
|
24
|
+
import gflags as flags
|
25
|
+
|
26
|
+
from gcutil import auth_helper
|
27
|
+
from gcutil import command_base
|
28
|
+
from gcutil import gcutil_logging
|
29
|
+
from gcutil import metadata
|
30
|
+
from gcutil import scopes
|
31
|
+
from gcutil import version
|
32
|
+
|
33
|
+
|
34
|
+
FLAGS = flags.FLAGS
|
35
|
+
LOGGER = gcutil_logging.LOGGER
|
36
|
+
|
37
|
+
|
38
|
+
class AuthCommand(command_base.GoogleComputeCommand):
|
39
|
+
"""Class for forcing client authorization."""
|
40
|
+
|
41
|
+
def __init__(self, name, flag_values):
|
42
|
+
super(AuthCommand, self).__init__(name, flag_values)
|
43
|
+
flags.DEFINE_boolean('force_reauth',
|
44
|
+
True,
|
45
|
+
'If True, will force user to reauthorize',
|
46
|
+
flag_values=flag_values)
|
47
|
+
flags.DEFINE_boolean('just_check_auth',
|
48
|
+
False,
|
49
|
+
'If True, just check if auth exists',
|
50
|
+
flag_values=flag_values)
|
51
|
+
flags.DEFINE_boolean('confirm_email',
|
52
|
+
True,
|
53
|
+
'Get info about the user and echo the email',
|
54
|
+
flag_values=flag_values)
|
55
|
+
|
56
|
+
def RunWithFlagsAndPositionalArgs(self,
|
57
|
+
flag_values,
|
58
|
+
unused_pos_arg_values):
|
59
|
+
"""Run the command, returning the result.
|
60
|
+
|
61
|
+
Args:
|
62
|
+
flag_values: The parsed FlagValues instance.
|
63
|
+
unused_pos_arg_values: The positional args.
|
64
|
+
|
65
|
+
Raises:
|
66
|
+
command_base.CommandError: If valid credentials cannot be retrieved.
|
67
|
+
|
68
|
+
Returns:
|
69
|
+
0 if the command completes successfully, otherwise 1.
|
70
|
+
|
71
|
+
Raises:
|
72
|
+
CommandError: if valid credentials are not located.
|
73
|
+
"""
|
74
|
+
cred = auth_helper.GetCredentialFromStore(
|
75
|
+
scopes=scopes.DEFAULT_AUTH_SCOPES,
|
76
|
+
ask_user=not flag_values.just_check_auth,
|
77
|
+
force_reauth=flag_values.force_reauth)
|
78
|
+
if not cred:
|
79
|
+
raise command_base.CommandError(
|
80
|
+
'Could not get valid credentials for API.')
|
81
|
+
|
82
|
+
if flag_values.confirm_email:
|
83
|
+
http = self._AuthenticateWrapper(httplib2.Http())
|
84
|
+
resp, content = http.request(
|
85
|
+
'https://www.googleapis.com/userinfo/v2/me', 'GET')
|
86
|
+
if resp.status != 200:
|
87
|
+
LOGGER.info('Could not get user info for token. <%d %s>',
|
88
|
+
resp.status, resp.reason)
|
89
|
+
userinfo = json.loads(content)
|
90
|
+
if 'email' in userinfo and userinfo['email']:
|
91
|
+
LOGGER.info('Authorization succeeded for user %s', userinfo['email'])
|
92
|
+
else:
|
93
|
+
LOGGER.info('Could not get email for token.')
|
94
|
+
else:
|
95
|
+
LOGGER.info('Authentication succeeded.')
|
96
|
+
return (None, [])
|
97
|
+
|
98
|
+
def PrintResult(self, result):
|
99
|
+
"""Print the result of the authentication command.
|
100
|
+
|
101
|
+
Args:
|
102
|
+
result: The result of the authentication command.
|
103
|
+
"""
|
104
|
+
pass
|
105
|
+
|
106
|
+
|
107
|
+
class GetVersion(command_base.GoogleComputeCommand):
|
108
|
+
"""Get the current version of this command."""
|
109
|
+
|
110
|
+
def __init__(self, name, flag_values):
|
111
|
+
super(GetVersion, self).__init__(name, flag_values)
|
112
|
+
|
113
|
+
def Run(self, unused_argv):
|
114
|
+
"""Return the current version information.
|
115
|
+
|
116
|
+
Args:
|
117
|
+
None expected.
|
118
|
+
|
119
|
+
Returns:
|
120
|
+
Version of this command.
|
121
|
+
"""
|
122
|
+
print version.__version__
|
123
|
+
return 0
|
124
|
+
|
125
|
+
|
126
|
+
def AddCommands():
|
127
|
+
appcommands.AddCmd('auth', AuthCommand)
|
128
|
+
appcommands.AddCmd('version', GetVersion)
|