def patch_cloudstorage_lib(service_account_key): """Makes cloudstorage library talk to real GCS using our own token. Note that cloudstorage.set_access_token() is partially broken. _RestApi class ignores it. See rest_api._RestApi.urlfetch_async (get_token_async call that unconditionally overwrites previously set token). Setting the token disables the usage of local mocks though, so we set it anyway (to some garbage, it doesn't matter). """ assert utils.is_local_dev_server() common.set_access_token('lalala') global _original_get_token_async if _original_get_token_async is None: logging.warning('Monkey patching GCS library to use valid token') _original_get_token_async = rest_api._RestApi.get_token_async # pylint: disable=unused-argument @functools.wraps(_original_get_token_async) def patched_get_token_async(self, refresh=False): fut = ndb.Future() fut.set_result( auth.get_access_token(self.scopes, service_account_key)[0]) return fut rest_api._RestApi.get_token_async = patched_get_token_async
def patch_cloudstorage_lib(service_account_key): """Makes cloudstorage library talk to real GCS using our own token. Note that cloudstorage.set_access_token() is partially broken. _RestApi class ignores it. See rest_api._RestApi.urlfetch_async (get_token_async call that unconditionally overwrites previously set token). Setting the token disables the usage of local mocks though, so we set it anyway (to some garbage, it doesn't matter). """ assert utils.is_local_dev_server() common.set_access_token('lalala') global _original_get_token_async if _original_get_token_async is None: logging.warning('Monkey patching GCS library to use valid token') _original_get_token_async = rest_api._RestApi.get_token_async # pylint: disable=unused-argument @functools.wraps(_original_get_token_async) def patched_get_token_async(self, refresh=False): fut = ndb.Future() fut.set_result(auth.get_access_token(self.scopes, service_account_key)[0]) return fut rest_api._RestApi.get_token_async = patched_get_token_async
def UseRemoteGCS(): """Use remote GCS via a signed certificate.""" logging.debug('Using remote gcs.') try: from oauth2client.client import SignedJwtAssertionCredentials except ImportError: logging.error('For local testing with remote GCS, install pycrypto.') return http_object = httplib2.Http(timeout=60) service_account = config.service_account private_key_pem_file = config.private_key_pem_file scope = 'https://www.googleapis.com/auth/devstorage.full_control' private_key = file(private_key_pem_file, 'rb').read() certificate = SignedJwtAssertionCredentials(service_account, private_key, scope) certificate.refresh(http_object) gcs_common.set_access_token(certificate.access_token)
def UseLocalGCS(): """Use the local GCS stub, great for testing locally..""" gcs_common.set_access_token(None)