blob: b8e9ff016a1c4394c5e3d5448268da33dbd47bad [file] [log] [blame]
Joe Gregorio9da2ad82011-09-11 14:04:44 -04001# Copyright 2011 Google Inc. All Rights Reserved.
2
3"""Multi-credential file store with lock support.
4
5This module implements a JSON credential store where multiple
6credentials can be stored in one file. That file supports locking
7both in a single process and across processes.
8
9The credential themselves are keyed off of:
10* client_id
11* user_agent
12* scope
13
14The 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 },
Joe Gregorio562b7312011-09-15 09:06:38 -040024 'credential': {
25 # JSON serialized Credentials.
26 }
Joe Gregorio9da2ad82011-09-11 14:04:44 -040027 }
28 ]
29}
30"""
31
32__author__ = 'jbeda@google.com (Joe Beda)'
33
34import base64
Joe Gregorio9b8bec62012-01-17 11:35:32 -050035import errno
Joe Gregorio9da2ad82011-09-11 14:04:44 -040036import logging
37import os
Joe Gregorio9da2ad82011-09-11 14:04:44 -040038import threading
39
Joe Gregorio549230c2012-01-11 10:38:05 -050040from anyjson import simplejson
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040041from oauth2client.client import Storage as BaseStorage
42from oauth2client.client import Credentials
43from oauth2client import util
Joe Gregorio2dfd74e2012-06-13 12:18:30 -040044from locked_file import LockedFile
Joe Gregorio9da2ad82011-09-11 14:04:44 -040045
46logger = logging.getLogger(__name__)
47
48# A dict from 'filename'->_MultiStore instances
49_multistores = {}
50_multistores_lock = threading.Lock()
51
52
53class Error(Exception):
54 """Base error for this module."""
55 pass
56
57
58class NewerCredentialStoreError(Error):
59 """The credential store is a newer version that supported."""
60 pass
61
62
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040063@util.positional(4)
Joe Gregorio9da2ad82011-09-11 14:04:44 -040064def get_credential_storage(filename, client_id, user_agent, scope,
65 warn_on_readonly=True):
66 """Get a Storage instance for a credential.
67
68 Args:
69 filename: The JSON file storing a set of credentials
70 client_id: The client_id for the credential
71 user_agent: The user agent for the credential
Joe Gregorio5cf5d122012-11-16 16:36:12 -050072 scope: string or iterable of strings, Scope(s) being requested
Joe Gregorio9da2ad82011-09-11 14:04:44 -040073 warn_on_readonly: if True, log a warning if the store is readonly
74
75 Returns:
76 An object derived from client.Storage for getting/setting the
77 credential.
78 """
Joe Gregorio0fd18532012-08-24 15:54:40 -040079 filename = os.path.expanduser(filename)
Joe Gregorio9da2ad82011-09-11 14:04:44 -040080 _multistores_lock.acquire()
81 try:
82 multistore = _multistores.setdefault(
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040083 filename, _MultiStore(filename, warn_on_readonly=warn_on_readonly))
Joe Gregorio9da2ad82011-09-11 14:04:44 -040084 finally:
85 _multistores_lock.release()
Joe Gregorio5cf5d122012-11-16 16:36:12 -050086 scope = util.scopes_to_string(scope)
Joe Gregorio9da2ad82011-09-11 14:04:44 -040087 return multistore._get_storage(client_id, user_agent, scope)
88
89
90class _MultiStore(object):
91 """A file backed store for multiple credentials."""
92
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040093 @util.positional(2)
Joe Gregorio9da2ad82011-09-11 14:04:44 -040094 def __init__(self, filename, warn_on_readonly=True):
95 """Initialize the class.
96
97 This will create the file if necessary.
98 """
Joe Gregorio2dfd74e2012-06-13 12:18:30 -040099 self._file = LockedFile(filename, 'r+b', 'rb')
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400100 self._thread_lock = threading.Lock()
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400101 self._read_only = False
102 self._warn_on_readonly = warn_on_readonly
103
104 self._create_file_if_needed()
105
106 # Cache of deserialized store. This is only valid after the
107 # _MultiStore is locked or _refresh_data_cache is called. This is
108 # of the form of:
109 #
110 # (client_id, user_agent, scope) -> OAuth2Credential
111 #
112 # If this is None, then the store hasn't been read yet.
113 self._data = None
114
115 class _Storage(BaseStorage):
116 """A Storage object that knows how to read/write a single credential."""
117
118 def __init__(self, multistore, client_id, user_agent, scope):
119 self._multistore = multistore
120 self._client_id = client_id
121 self._user_agent = user_agent
122 self._scope = scope
123
124 def acquire_lock(self):
125 """Acquires any lock necessary to access this Storage.
126
127 This lock is not reentrant.
128 """
129 self._multistore._lock()
130
131 def release_lock(self):
132 """Release the Storage lock.
133
134 Trying to release a lock that isn't held will result in a
135 RuntimeError.
136 """
137 self._multistore._unlock()
138
139 def locked_get(self):
140 """Retrieve credential.
141
142 The Storage lock must be held when this is called.
143
144 Returns:
145 oauth2client.client.Credentials
146 """
147 credential = self._multistore._get_credential(
148 self._client_id, self._user_agent, self._scope)
149 if credential:
150 credential.set_store(self)
151 return credential
152
153 def locked_put(self, credentials):
154 """Write a credential.
155
156 The Storage lock must be held when this is called.
157
158 Args:
159 credentials: Credentials, the credentials to store.
160 """
161 self._multistore._update_credential(credentials, self._scope)
162
Joe Gregorioec75dc12012-02-06 13:40:42 -0500163 def locked_delete(self):
164 """Delete a credential.
165
166 The Storage lock must be held when this is called.
167
168 Args:
169 credentials: Credentials, the credentials to store.
170 """
171 self._multistore._delete_credential(self._client_id, self._user_agent,
172 self._scope)
173
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400174 def _create_file_if_needed(self):
175 """Create an empty file if necessary.
176
177 This method will not initialize the file. Instead it implements a
178 simple version of "touch" to ensure the file has been created.
179 """
Joe Gregorio2dfd74e2012-06-13 12:18:30 -0400180 if not os.path.exists(self._file.filename()):
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400181 old_umask = os.umask(0177)
182 try:
Joe Gregorio2dfd74e2012-06-13 12:18:30 -0400183 open(self._file.filename(), 'a+b').close()
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400184 finally:
185 os.umask(old_umask)
186
187 def _lock(self):
188 """Lock the entire multistore."""
189 self._thread_lock.acquire()
Joe Gregorio2dfd74e2012-06-13 12:18:30 -0400190 self._file.open_and_lock()
191 if not self._file.is_locked():
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400192 self._read_only = True
193 if self._warn_on_readonly:
194 logger.warn('The credentials file (%s) is not writable. Opening in '
195 'read-only mode. Any refreshed credentials will only be '
Joe Gregorio2dfd74e2012-06-13 12:18:30 -0400196 'valid for this run.' % self._file.filename())
197 if os.path.getsize(self._file.filename()) == 0:
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400198 logger.debug('Initializing empty multistore file')
199 # The multistore is empty so write out an empty file.
200 self._data = {}
201 self._write()
202 elif not self._read_only or self._data is None:
203 # Only refresh the data if we are read/write or we haven't
204 # cached the data yet. If we are readonly, we assume is isn't
205 # changing out from under us and that we only have to read it
206 # once. This prevents us from whacking any new access keys that
207 # we have cached in memory but were unable to write out.
208 self._refresh_data_cache()
209
210 def _unlock(self):
211 """Release the lock on the multistore."""
Joe Gregorio2dfd74e2012-06-13 12:18:30 -0400212 self._file.unlock_and_close()
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400213 self._thread_lock.release()
214
215 def _locked_json_read(self):
216 """Get the raw content of the multistore file.
217
218 The multistore must be locked when this is called.
219
220 Returns:
221 The contents of the multistore decoded as JSON.
222 """
223 assert self._thread_lock.locked()
Joe Gregorio2dfd74e2012-06-13 12:18:30 -0400224 self._file.file_handle().seek(0)
225 return simplejson.load(self._file.file_handle())
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400226
227 def _locked_json_write(self, data):
228 """Write a JSON serializable data structure to the multistore.
229
230 The multistore must be locked when this is called.
231
232 Args:
233 data: The data to be serialized and written.
234 """
235 assert self._thread_lock.locked()
236 if self._read_only:
237 return
Joe Gregorio2dfd74e2012-06-13 12:18:30 -0400238 self._file.file_handle().seek(0)
239 simplejson.dump(data, self._file.file_handle(), sort_keys=True, indent=2)
240 self._file.file_handle().truncate()
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400241
242 def _refresh_data_cache(self):
243 """Refresh the contents of the multistore.
244
245 The multistore must be locked when this is called.
246
247 Raises:
248 NewerCredentialStoreError: Raised when a newer client has written the
249 store.
250 """
251 self._data = {}
252 try:
253 raw_data = self._locked_json_read()
254 except Exception:
255 logger.warn('Credential data store could not be loaded. '
256 'Will ignore and overwrite.')
257 return
258
259 version = 0
260 try:
261 version = raw_data['file_version']
262 except Exception:
263 logger.warn('Missing version for credential data store. It may be '
264 'corrupt or an old version. Overwriting.')
265 if version > 1:
266 raise NewerCredentialStoreError(
267 'Credential file has file_version of %d. '
268 'Only file_version of 1 is supported.' % version)
269
270 credentials = []
271 try:
272 credentials = raw_data['data']
273 except (TypeError, KeyError):
274 pass
275
276 for cred_entry in credentials:
277 try:
278 (key, credential) = self._decode_credential_from_json(cred_entry)
279 self._data[key] = credential
280 except:
281 # If something goes wrong loading a credential, just ignore it
282 logger.info('Error decoding credential, skipping', exc_info=True)
283
284 def _decode_credential_from_json(self, cred_entry):
285 """Load a credential from our JSON serialization.
286
287 Args:
288 cred_entry: A dict entry from the data member of our format
289
290 Returns:
291 (key, cred) where the key is the key tuple and the cred is the
292 OAuth2Credential object.
293 """
294 raw_key = cred_entry['key']
295 client_id = raw_key['clientId']
296 user_agent = raw_key['userAgent']
297 scope = raw_key['scope']
298 key = (client_id, user_agent, scope)
Joe Gregorio562b7312011-09-15 09:06:38 -0400299 credential = None
300 credential = Credentials.new_from_json(simplejson.dumps(cred_entry['credential']))
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400301 return (key, credential)
302
303 def _write(self):
304 """Write the cached data back out.
305
306 The multistore must be locked.
307 """
308 raw_data = {'file_version': 1}
309 raw_creds = []
310 raw_data['data'] = raw_creds
311 for (cred_key, cred) in self._data.items():
312 raw_key = {
313 'clientId': cred_key[0],
314 'userAgent': cred_key[1],
315 'scope': cred_key[2]
316 }
Joe Gregorio562b7312011-09-15 09:06:38 -0400317 raw_cred = simplejson.loads(cred.to_json())
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400318 raw_creds.append({'key': raw_key, 'credential': raw_cred})
319 self._locked_json_write(raw_data)
320
321 def _get_credential(self, client_id, user_agent, scope):
322 """Get a credential from the multistore.
323
324 The multistore must be locked.
325
326 Args:
327 client_id: The client_id for the credential
328 user_agent: The user agent for the credential
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400329 scope: A string for the scope(s) being requested
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400330
331 Returns:
332 The credential specified or None if not present
333 """
334 key = (client_id, user_agent, scope)
Joe Gregorio562b7312011-09-15 09:06:38 -0400335
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400336 return self._data.get(key, None)
337
338 def _update_credential(self, cred, scope):
339 """Update a credential and write the multistore.
340
341 This must be called when the multistore is locked.
342
343 Args:
344 cred: The OAuth2Credential to update/set
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400345 scope: The scope(s) that this credential covers
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400346 """
347 key = (cred.client_id, cred.user_agent, scope)
348 self._data[key] = cred
349 self._write()
350
Joe Gregorioec75dc12012-02-06 13:40:42 -0500351 def _delete_credential(self, client_id, user_agent, scope):
352 """Delete a credential and write the multistore.
353
354 This must be called when the multistore is locked.
355
356 Args:
357 client_id: The client_id for the credential
358 user_agent: The user agent for the credential
359 scope: The scope(s) that this credential covers
360 """
361 key = (client_id, user_agent, scope)
362 try:
363 del self._data[key]
364 except KeyError:
365 pass
366 self._write()
367
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400368 def _get_storage(self, client_id, user_agent, scope):
369 """Get a Storage object to get/set a credential.
370
371 This Storage is a 'view' into the multistore.
372
373 Args:
374 client_id: The client_id for the credential
375 user_agent: The user agent for the credential
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400376 scope: A string for the scope(s) being requested
Joe Gregorio9da2ad82011-09-11 14:04:44 -0400377
378 Returns:
379 A Storage object that can be used to get/set this cred
380 """
381 return self._Storage(self, client_id, user_agent, scope)