gcloud 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,379 @@
|
|
|
1
|
+
# Copyright 2011 Google Inc. All Rights Reserved.
|
|
2
|
+
|
|
3
|
+
"""Multi-credential file store with lock support.
|
|
4
|
+
|
|
5
|
+
This module implements a JSON credential store where multiple
|
|
6
|
+
credentials can be stored in one file. That file supports locking
|
|
7
|
+
both in a single process and across processes.
|
|
8
|
+
|
|
9
|
+
The credential themselves are keyed off of:
|
|
10
|
+
* client_id
|
|
11
|
+
* user_agent
|
|
12
|
+
* scope
|
|
13
|
+
|
|
14
|
+
The format of the stored data is like so:
|
|
15
|
+
{
|
|
16
|
+
'file_version': 1,
|
|
17
|
+
'data': [
|
|
18
|
+
{
|
|
19
|
+
'key': {
|
|
20
|
+
'clientId': '<client id>',
|
|
21
|
+
'userAgent': '<user agent>',
|
|
22
|
+
'scope': '<scope>'
|
|
23
|
+
},
|
|
24
|
+
'credential': {
|
|
25
|
+
# JSON serialized Credentials.
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
import base64
|
|
35
|
+
import errno
|
|
36
|
+
import logging
|
|
37
|
+
import os
|
|
38
|
+
import threading
|
|
39
|
+
|
|
40
|
+
from anyjson import simplejson
|
|
41
|
+
from client import Storage as BaseStorage
|
|
42
|
+
from client import Credentials
|
|
43
|
+
from locked_file import LockedFile
|
|
44
|
+
|
|
45
|
+
logger = logging.getLogger(__name__)
|
|
46
|
+
|
|
47
|
+
# A dict from 'filename'->_MultiStore instances
|
|
48
|
+
_multistores = {}
|
|
49
|
+
_multistores_lock = threading.Lock()
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class Error(Exception):
|
|
53
|
+
"""Base error for this module."""
|
|
54
|
+
pass
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class NewerCredentialStoreError(Error):
|
|
58
|
+
"""The credential store is a newer version that supported."""
|
|
59
|
+
pass
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def get_credential_storage(filename, client_id, user_agent, scope,
|
|
63
|
+
warn_on_readonly=True):
|
|
64
|
+
"""Get a Storage instance for a credential.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
filename: The JSON file storing a set of credentials
|
|
68
|
+
client_id: The client_id for the credential
|
|
69
|
+
user_agent: The user agent for the credential
|
|
70
|
+
scope: string or list of strings, Scope(s) being requested
|
|
71
|
+
warn_on_readonly: if True, log a warning if the store is readonly
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
An object derived from client.Storage for getting/setting the
|
|
75
|
+
credential.
|
|
76
|
+
"""
|
|
77
|
+
filename = os.path.realpath(os.path.expanduser(filename))
|
|
78
|
+
_multistores_lock.acquire()
|
|
79
|
+
try:
|
|
80
|
+
multistore = _multistores.setdefault(
|
|
81
|
+
filename, _MultiStore(filename, warn_on_readonly))
|
|
82
|
+
finally:
|
|
83
|
+
_multistores_lock.release()
|
|
84
|
+
if type(scope) is list:
|
|
85
|
+
scope = ' '.join(scope)
|
|
86
|
+
return multistore._get_storage(client_id, user_agent, scope)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class _MultiStore(object):
|
|
90
|
+
"""A file backed store for multiple credentials."""
|
|
91
|
+
|
|
92
|
+
def __init__(self, filename, warn_on_readonly=True):
|
|
93
|
+
"""Initialize the class.
|
|
94
|
+
|
|
95
|
+
This will create the file if necessary.
|
|
96
|
+
"""
|
|
97
|
+
self._file = LockedFile(filename, 'r+b', 'rb')
|
|
98
|
+
self._thread_lock = threading.Lock()
|
|
99
|
+
self._read_only = False
|
|
100
|
+
self._warn_on_readonly = warn_on_readonly
|
|
101
|
+
|
|
102
|
+
self._create_file_if_needed()
|
|
103
|
+
|
|
104
|
+
# Cache of deserialized store. This is only valid after the
|
|
105
|
+
# _MultiStore is locked or _refresh_data_cache is called. This is
|
|
106
|
+
# of the form of:
|
|
107
|
+
#
|
|
108
|
+
# (client_id, user_agent, scope) -> OAuth2Credential
|
|
109
|
+
#
|
|
110
|
+
# If this is None, then the store hasn't been read yet.
|
|
111
|
+
self._data = None
|
|
112
|
+
|
|
113
|
+
class _Storage(BaseStorage):
|
|
114
|
+
"""A Storage object that knows how to read/write a single credential."""
|
|
115
|
+
|
|
116
|
+
def __init__(self, multistore, client_id, user_agent, scope):
|
|
117
|
+
self._multistore = multistore
|
|
118
|
+
self._client_id = client_id
|
|
119
|
+
self._user_agent = user_agent
|
|
120
|
+
self._scope = scope
|
|
121
|
+
|
|
122
|
+
def acquire_lock(self):
|
|
123
|
+
"""Acquires any lock necessary to access this Storage.
|
|
124
|
+
|
|
125
|
+
This lock is not reentrant.
|
|
126
|
+
"""
|
|
127
|
+
self._multistore._lock()
|
|
128
|
+
|
|
129
|
+
def release_lock(self):
|
|
130
|
+
"""Release the Storage lock.
|
|
131
|
+
|
|
132
|
+
Trying to release a lock that isn't held will result in a
|
|
133
|
+
RuntimeError.
|
|
134
|
+
"""
|
|
135
|
+
self._multistore._unlock()
|
|
136
|
+
|
|
137
|
+
def locked_get(self):
|
|
138
|
+
"""Retrieve credential.
|
|
139
|
+
|
|
140
|
+
The Storage lock must be held when this is called.
|
|
141
|
+
|
|
142
|
+
Returns:
|
|
143
|
+
oauth2client.client.Credentials
|
|
144
|
+
"""
|
|
145
|
+
credential = self._multistore._get_credential(
|
|
146
|
+
self._client_id, self._user_agent, self._scope)
|
|
147
|
+
if credential:
|
|
148
|
+
credential.set_store(self)
|
|
149
|
+
return credential
|
|
150
|
+
|
|
151
|
+
def locked_put(self, credentials):
|
|
152
|
+
"""Write a credential.
|
|
153
|
+
|
|
154
|
+
The Storage lock must be held when this is called.
|
|
155
|
+
|
|
156
|
+
Args:
|
|
157
|
+
credentials: Credentials, the credentials to store.
|
|
158
|
+
"""
|
|
159
|
+
self._multistore._update_credential(credentials, self._scope)
|
|
160
|
+
|
|
161
|
+
def locked_delete(self):
|
|
162
|
+
"""Delete a credential.
|
|
163
|
+
|
|
164
|
+
The Storage lock must be held when this is called.
|
|
165
|
+
|
|
166
|
+
Args:
|
|
167
|
+
credentials: Credentials, the credentials to store.
|
|
168
|
+
"""
|
|
169
|
+
self._multistore._delete_credential(self._client_id, self._user_agent,
|
|
170
|
+
self._scope)
|
|
171
|
+
|
|
172
|
+
def _create_file_if_needed(self):
|
|
173
|
+
"""Create an empty file if necessary.
|
|
174
|
+
|
|
175
|
+
This method will not initialize the file. Instead it implements a
|
|
176
|
+
simple version of "touch" to ensure the file has been created.
|
|
177
|
+
"""
|
|
178
|
+
if not os.path.exists(self._file.filename()):
|
|
179
|
+
old_umask = os.umask(0177)
|
|
180
|
+
try:
|
|
181
|
+
open(self._file.filename(), 'a+b').close()
|
|
182
|
+
finally:
|
|
183
|
+
os.umask(old_umask)
|
|
184
|
+
|
|
185
|
+
def _lock(self):
|
|
186
|
+
"""Lock the entire multistore."""
|
|
187
|
+
self._thread_lock.acquire()
|
|
188
|
+
self._file.open_and_lock()
|
|
189
|
+
if not self._file.is_locked():
|
|
190
|
+
self._read_only = True
|
|
191
|
+
if self._warn_on_readonly:
|
|
192
|
+
logger.warn('The credentials file (%s) is not writable. Opening in '
|
|
193
|
+
'read-only mode. Any refreshed credentials will only be '
|
|
194
|
+
'valid for this run.' % self._file.filename())
|
|
195
|
+
if os.path.getsize(self._file.filename()) == 0:
|
|
196
|
+
logger.debug('Initializing empty multistore file')
|
|
197
|
+
# The multistore is empty so write out an empty file.
|
|
198
|
+
self._data = {}
|
|
199
|
+
self._write()
|
|
200
|
+
elif not self._read_only or self._data is None:
|
|
201
|
+
# Only refresh the data if we are read/write or we haven't
|
|
202
|
+
# cached the data yet. If we are readonly, we assume is isn't
|
|
203
|
+
# changing out from under us and that we only have to read it
|
|
204
|
+
# once. This prevents us from whacking any new access keys that
|
|
205
|
+
# we have cached in memory but were unable to write out.
|
|
206
|
+
self._refresh_data_cache()
|
|
207
|
+
|
|
208
|
+
def _unlock(self):
|
|
209
|
+
"""Release the lock on the multistore."""
|
|
210
|
+
self._file.unlock_and_close()
|
|
211
|
+
self._thread_lock.release()
|
|
212
|
+
|
|
213
|
+
def _locked_json_read(self):
|
|
214
|
+
"""Get the raw content of the multistore file.
|
|
215
|
+
|
|
216
|
+
The multistore must be locked when this is called.
|
|
217
|
+
|
|
218
|
+
Returns:
|
|
219
|
+
The contents of the multistore decoded as JSON.
|
|
220
|
+
"""
|
|
221
|
+
assert self._thread_lock.locked()
|
|
222
|
+
self._file.file_handle().seek(0)
|
|
223
|
+
return simplejson.load(self._file.file_handle())
|
|
224
|
+
|
|
225
|
+
def _locked_json_write(self, data):
|
|
226
|
+
"""Write a JSON serializable data structure to the multistore.
|
|
227
|
+
|
|
228
|
+
The multistore must be locked when this is called.
|
|
229
|
+
|
|
230
|
+
Args:
|
|
231
|
+
data: The data to be serialized and written.
|
|
232
|
+
"""
|
|
233
|
+
assert self._thread_lock.locked()
|
|
234
|
+
if self._read_only:
|
|
235
|
+
return
|
|
236
|
+
self._file.file_handle().seek(0)
|
|
237
|
+
simplejson.dump(data, self._file.file_handle(), sort_keys=True, indent=2)
|
|
238
|
+
self._file.file_handle().truncate()
|
|
239
|
+
|
|
240
|
+
def _refresh_data_cache(self):
|
|
241
|
+
"""Refresh the contents of the multistore.
|
|
242
|
+
|
|
243
|
+
The multistore must be locked when this is called.
|
|
244
|
+
|
|
245
|
+
Raises:
|
|
246
|
+
NewerCredentialStoreError: Raised when a newer client has written the
|
|
247
|
+
store.
|
|
248
|
+
"""
|
|
249
|
+
self._data = {}
|
|
250
|
+
try:
|
|
251
|
+
raw_data = self._locked_json_read()
|
|
252
|
+
except Exception:
|
|
253
|
+
logger.warn('Credential data store could not be loaded. '
|
|
254
|
+
'Will ignore and overwrite.')
|
|
255
|
+
return
|
|
256
|
+
|
|
257
|
+
version = 0
|
|
258
|
+
try:
|
|
259
|
+
version = raw_data['file_version']
|
|
260
|
+
except Exception:
|
|
261
|
+
logger.warn('Missing version for credential data store. It may be '
|
|
262
|
+
'corrupt or an old version. Overwriting.')
|
|
263
|
+
if version > 1:
|
|
264
|
+
raise NewerCredentialStoreError(
|
|
265
|
+
'Credential file has file_version of %d. '
|
|
266
|
+
'Only file_version of 1 is supported.' % version)
|
|
267
|
+
|
|
268
|
+
credentials = []
|
|
269
|
+
try:
|
|
270
|
+
credentials = raw_data['data']
|
|
271
|
+
except (TypeError, KeyError):
|
|
272
|
+
pass
|
|
273
|
+
|
|
274
|
+
for cred_entry in credentials:
|
|
275
|
+
try:
|
|
276
|
+
(key, credential) = self._decode_credential_from_json(cred_entry)
|
|
277
|
+
self._data[key] = credential
|
|
278
|
+
except:
|
|
279
|
+
# If something goes wrong loading a credential, just ignore it
|
|
280
|
+
logger.info('Error decoding credential, skipping', exc_info=True)
|
|
281
|
+
|
|
282
|
+
def _decode_credential_from_json(self, cred_entry):
|
|
283
|
+
"""Load a credential from our JSON serialization.
|
|
284
|
+
|
|
285
|
+
Args:
|
|
286
|
+
cred_entry: A dict entry from the data member of our format
|
|
287
|
+
|
|
288
|
+
Returns:
|
|
289
|
+
(key, cred) where the key is the key tuple and the cred is the
|
|
290
|
+
OAuth2Credential object.
|
|
291
|
+
"""
|
|
292
|
+
raw_key = cred_entry['key']
|
|
293
|
+
client_id = raw_key['clientId']
|
|
294
|
+
user_agent = raw_key['userAgent']
|
|
295
|
+
scope = raw_key['scope']
|
|
296
|
+
key = (client_id, user_agent, scope)
|
|
297
|
+
credential = None
|
|
298
|
+
credential = Credentials.new_from_json(simplejson.dumps(cred_entry['credential']))
|
|
299
|
+
return (key, credential)
|
|
300
|
+
|
|
301
|
+
def _write(self):
|
|
302
|
+
"""Write the cached data back out.
|
|
303
|
+
|
|
304
|
+
The multistore must be locked.
|
|
305
|
+
"""
|
|
306
|
+
raw_data = {'file_version': 1}
|
|
307
|
+
raw_creds = []
|
|
308
|
+
raw_data['data'] = raw_creds
|
|
309
|
+
for (cred_key, cred) in self._data.items():
|
|
310
|
+
raw_key = {
|
|
311
|
+
'clientId': cred_key[0],
|
|
312
|
+
'userAgent': cred_key[1],
|
|
313
|
+
'scope': cred_key[2]
|
|
314
|
+
}
|
|
315
|
+
raw_cred = simplejson.loads(cred.to_json())
|
|
316
|
+
raw_creds.append({'key': raw_key, 'credential': raw_cred})
|
|
317
|
+
self._locked_json_write(raw_data)
|
|
318
|
+
|
|
319
|
+
def _get_credential(self, client_id, user_agent, scope):
|
|
320
|
+
"""Get a credential from the multistore.
|
|
321
|
+
|
|
322
|
+
The multistore must be locked.
|
|
323
|
+
|
|
324
|
+
Args:
|
|
325
|
+
client_id: The client_id for the credential
|
|
326
|
+
user_agent: The user agent for the credential
|
|
327
|
+
scope: A string for the scope(s) being requested
|
|
328
|
+
|
|
329
|
+
Returns:
|
|
330
|
+
The credential specified or None if not present
|
|
331
|
+
"""
|
|
332
|
+
key = (client_id, user_agent, scope)
|
|
333
|
+
|
|
334
|
+
return self._data.get(key, None)
|
|
335
|
+
|
|
336
|
+
def _update_credential(self, cred, scope):
|
|
337
|
+
"""Update a credential and write the multistore.
|
|
338
|
+
|
|
339
|
+
This must be called when the multistore is locked.
|
|
340
|
+
|
|
341
|
+
Args:
|
|
342
|
+
cred: The OAuth2Credential to update/set
|
|
343
|
+
scope: The scope(s) that this credential covers
|
|
344
|
+
"""
|
|
345
|
+
key = (cred.client_id, cred.user_agent, scope)
|
|
346
|
+
self._data[key] = cred
|
|
347
|
+
self._write()
|
|
348
|
+
|
|
349
|
+
def _delete_credential(self, client_id, user_agent, scope):
|
|
350
|
+
"""Delete a credential and write the multistore.
|
|
351
|
+
|
|
352
|
+
This must be called when the multistore is locked.
|
|
353
|
+
|
|
354
|
+
Args:
|
|
355
|
+
client_id: The client_id for the credential
|
|
356
|
+
user_agent: The user agent for the credential
|
|
357
|
+
scope: The scope(s) that this credential covers
|
|
358
|
+
"""
|
|
359
|
+
key = (client_id, user_agent, scope)
|
|
360
|
+
try:
|
|
361
|
+
del self._data[key]
|
|
362
|
+
except KeyError:
|
|
363
|
+
pass
|
|
364
|
+
self._write()
|
|
365
|
+
|
|
366
|
+
def _get_storage(self, client_id, user_agent, scope):
|
|
367
|
+
"""Get a Storage object to get/set a credential.
|
|
368
|
+
|
|
369
|
+
This Storage is a 'view' into the multistore.
|
|
370
|
+
|
|
371
|
+
Args:
|
|
372
|
+
client_id: The client_id for the credential
|
|
373
|
+
user_agent: The user agent for the credential
|
|
374
|
+
scope: A string for the scope(s) being requested
|
|
375
|
+
|
|
376
|
+
Returns:
|
|
377
|
+
A Storage object that can be used to get/set this cred
|
|
378
|
+
"""
|
|
379
|
+
return self._Storage(self, client_id, user_agent, scope)
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# Copyright (C) 2010 Google Inc.
|
|
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
|
+
"""Command-line tools for authenticating via OAuth 2.0
|
|
16
|
+
|
|
17
|
+
Do the OAuth 2.0 Web Server dance for a command line application. Stores the
|
|
18
|
+
generated credentials in a common file that is used by other example apps in
|
|
19
|
+
the same directory.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
__all__ = ['run']
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
import BaseHTTPServer
|
|
27
|
+
import gflags
|
|
28
|
+
import socket
|
|
29
|
+
import sys
|
|
30
|
+
import webbrowser
|
|
31
|
+
|
|
32
|
+
from client import FlowExchangeError
|
|
33
|
+
from client import OOB_CALLBACK_URN
|
|
34
|
+
|
|
35
|
+
try:
|
|
36
|
+
from urlparse import parse_qsl
|
|
37
|
+
except ImportError:
|
|
38
|
+
from cgi import parse_qsl
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
FLAGS = gflags.FLAGS
|
|
42
|
+
|
|
43
|
+
gflags.DEFINE_boolean('auth_local_webserver', True,
|
|
44
|
+
('Run a local web server to handle redirects during '
|
|
45
|
+
'OAuth authorization.'))
|
|
46
|
+
|
|
47
|
+
gflags.DEFINE_string('auth_host_name', 'localhost',
|
|
48
|
+
('Host name to use when running a local web server to '
|
|
49
|
+
'handle redirects during OAuth authorization.'))
|
|
50
|
+
|
|
51
|
+
gflags.DEFINE_multi_int('auth_host_port', [8080, 8090],
|
|
52
|
+
('Port to use when running a local web server to '
|
|
53
|
+
'handle redirects during OAuth authorization.'))
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class ClientRedirectServer(BaseHTTPServer.HTTPServer):
|
|
57
|
+
"""A server to handle OAuth 2.0 redirects back to localhost.
|
|
58
|
+
|
|
59
|
+
Waits for a single request and parses the query parameters
|
|
60
|
+
into query_params and then stops serving.
|
|
61
|
+
"""
|
|
62
|
+
query_params = {}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class ClientRedirectHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|
66
|
+
"""A handler for OAuth 2.0 redirects back to localhost.
|
|
67
|
+
|
|
68
|
+
Waits for a single request and parses the query parameters
|
|
69
|
+
into the servers query_params and then stops serving.
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
def do_GET(s):
|
|
73
|
+
"""Handle a GET request.
|
|
74
|
+
|
|
75
|
+
Parses the query parameters and prints a message
|
|
76
|
+
if the flow has completed. Note that we can't detect
|
|
77
|
+
if an error occurred.
|
|
78
|
+
"""
|
|
79
|
+
s.send_response(200)
|
|
80
|
+
s.send_header("Content-type", "text/html")
|
|
81
|
+
s.end_headers()
|
|
82
|
+
query = s.path.split('?', 1)[-1]
|
|
83
|
+
query = dict(parse_qsl(query))
|
|
84
|
+
s.server.query_params = query
|
|
85
|
+
s.wfile.write("<html><head><title>Authentication Status</title></head>")
|
|
86
|
+
s.wfile.write("<body><p>The authentication flow has completed.</p>")
|
|
87
|
+
s.wfile.write("</body></html>")
|
|
88
|
+
|
|
89
|
+
def log_message(self, format, *args):
|
|
90
|
+
"""Do not log messages to stdout while running as command line program."""
|
|
91
|
+
pass
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def run(flow, storage, http=None):
|
|
95
|
+
"""Core code for a command-line application.
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
flow: Flow, an OAuth 2.0 Flow to step through.
|
|
99
|
+
storage: Storage, a Storage to store the credential in.
|
|
100
|
+
http: An instance of httplib2.Http.request
|
|
101
|
+
or something that acts like it.
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
Credentials, the obtained credential.
|
|
105
|
+
"""
|
|
106
|
+
if FLAGS.auth_local_webserver:
|
|
107
|
+
success = False
|
|
108
|
+
port_number = 0
|
|
109
|
+
for port in FLAGS.auth_host_port:
|
|
110
|
+
port_number = port
|
|
111
|
+
try:
|
|
112
|
+
httpd = ClientRedirectServer((FLAGS.auth_host_name, port),
|
|
113
|
+
ClientRedirectHandler)
|
|
114
|
+
except socket.error, e:
|
|
115
|
+
pass
|
|
116
|
+
else:
|
|
117
|
+
success = True
|
|
118
|
+
break
|
|
119
|
+
FLAGS.auth_local_webserver = success
|
|
120
|
+
if not success:
|
|
121
|
+
print 'Failed to start a local webserver listening on either port 8080'
|
|
122
|
+
print 'or port 9090. Please check your firewall settings and locally'
|
|
123
|
+
print 'running programs that may be blocking or using those ports.'
|
|
124
|
+
print
|
|
125
|
+
print 'Falling back to --noauth_local_webserver and continuing with',
|
|
126
|
+
print 'authorization.'
|
|
127
|
+
print
|
|
128
|
+
|
|
129
|
+
if FLAGS.auth_local_webserver:
|
|
130
|
+
oauth_callback = 'http://%s:%s/' % (FLAGS.auth_host_name, port_number)
|
|
131
|
+
else:
|
|
132
|
+
oauth_callback = OOB_CALLBACK_URN
|
|
133
|
+
authorize_url = flow.step1_get_authorize_url(oauth_callback)
|
|
134
|
+
|
|
135
|
+
if FLAGS.auth_local_webserver:
|
|
136
|
+
webbrowser.open(authorize_url, new=1, autoraise=True)
|
|
137
|
+
print 'Your browser has been opened to visit:'
|
|
138
|
+
print
|
|
139
|
+
print ' ' + authorize_url
|
|
140
|
+
print
|
|
141
|
+
print 'If your browser is on a different machine then exit and re-run this'
|
|
142
|
+
print 'application with the command-line parameter '
|
|
143
|
+
print
|
|
144
|
+
print ' --noauth_local_webserver'
|
|
145
|
+
print
|
|
146
|
+
else:
|
|
147
|
+
print 'Go to the following link in your browser:'
|
|
148
|
+
print
|
|
149
|
+
print ' ' + authorize_url
|
|
150
|
+
print
|
|
151
|
+
|
|
152
|
+
code = None
|
|
153
|
+
if FLAGS.auth_local_webserver:
|
|
154
|
+
httpd.handle_request()
|
|
155
|
+
if 'error' in httpd.query_params:
|
|
156
|
+
sys.exit('Authentication request was rejected.')
|
|
157
|
+
if 'code' in httpd.query_params:
|
|
158
|
+
code = httpd.query_params['code']
|
|
159
|
+
else:
|
|
160
|
+
print 'Failed to find "code" in the query parameters of the redirect.'
|
|
161
|
+
sys.exit('Try running with --noauth_local_webserver.')
|
|
162
|
+
else:
|
|
163
|
+
code = raw_input('Enter verification code: ').strip()
|
|
164
|
+
|
|
165
|
+
try:
|
|
166
|
+
credential = flow.step2_exchange(code, http)
|
|
167
|
+
except FlowExchangeError, e:
|
|
168
|
+
sys.exit('Authentication has failed: %s' % e)
|
|
169
|
+
|
|
170
|
+
storage.put(credential)
|
|
171
|
+
credential.set_store(storage)
|
|
172
|
+
print 'Authentication successful.'
|
|
173
|
+
|
|
174
|
+
return credential
|