Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """AuthHandler plugin for gsutil's boto to support LOAS based auth.""" | |
| 6 | |
| 7 import getpass | |
| 8 import json | |
| 9 import os | |
| 10 import re | |
| 11 import subprocess | |
| 12 import time | |
| 13 import urllib2 | |
| 14 | |
| 15 from boto.auth_handler import AuthHandler | |
| 16 from boto.auth_handler import NotReadyToAuthenticate | |
| 17 | |
| 18 CMD = ['stubby', '--proto2', 'call', 'blade:sso', 'CorpLogin.Exchange'] | |
| 19 | |
| 20 STUBBY_CMD = """target: { | |
| 21 scope: GAIA_USER | |
| 22 name: "%s" | |
| 23 } | |
| 24 target_credential: { | |
| 25 type: OAUTH2_TOKEN | |
| 26 oauth2_attributes: { | |
| 27 scope: 'https://www.googleapis.com/auth/devstorage.read_only' | |
| 28 } | |
| 29 }""" | |
| 30 | |
| 31 COOKIE_LOCATION = os.path.expanduser('~/.devstore_token') | |
| 32 | |
| 33 TOKEN_EXPIRY = 300 | |
| 34 | |
| 35 | |
| 36 class SSOAuthError(Exception): | |
| 37 pass | |
| 38 | |
| 39 | |
| 40 class SSOAuth(AuthHandler): | |
| 41 """SSO based auth handler.""" | |
| 42 | |
| 43 capability = ['google-oauth2', 's3'] | |
| 44 | |
| 45 def __init__(self, path, config, provider): | |
| 46 if provider.name == 'google' and self.has_prodaccess(): | |
| 47 # If we don't have a loas token, then bypass this auth handler. | |
| 48 if subprocess.call('loas_check', | |
| 49 stdout=subprocess.PIPE, | |
| 50 stderr=subprocess.PIPE): | |
| 51 raise NotReadyToAuthenticate() | |
| 52 else: | |
| 53 raise NotReadyToAuthenticate() | |
| 54 self.token = None | |
| 55 self.expire = 0 | |
| 56 | |
| 57 def GetAccessToken(self): | |
| 58 # Return from in-memory cache if its there already. | |
| 59 if self.token and self.expire > time.time(): | |
| 60 return self.token | |
| 61 | |
| 62 # Try to retrieve token from filesystem cache. | |
| 63 if os.path.exists(COOKIE_LOCATION): | |
| 64 last_modified = os.path.getmtime(COOKIE_LOCATION) | |
| 65 if time.time() - last_modified < TOKEN_EXPIRY: | |
| 66 with open(COOKIE_LOCATION, 'rb') as f: | |
| 67 self.token = f.read() | |
| 68 self.expire = last_modified + TOKEN_EXPIRY | |
| 69 return self.token | |
| 70 | |
| 71 # If the token is not in either caches, or has expired, then fetch token. | |
| 72 username = '%s@google.com' % getpass.getuser() | |
| 73 proc = subprocess.Popen(CMD, stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
| 74 out, err = proc.communicate(STUBBY_CMD % username) | |
| 75 if proc.returncode: | |
| 76 raise SSOAuthError('Stubby returned %d\n%s' % (proc.returncode, err)) | |
| 77 token_match = re.search(r'oauth2_token: "(.*)"$', out) | |
| 78 | |
| 79 if not token_match: | |
| 80 raise SSOAuthError('Oauth2 token not found in %s' % out) | |
| 81 | |
| 82 token = token_match.group(1) | |
| 83 self.token = token | |
| 84 self.expire = time.time() + TOKEN_EXPIRY | |
| 85 with os.fdopen(os.open(COOKIE_LOCATION, | |
| 86 os.O_WRONLY | os.O_CREAT, | |
| 87 0600), 'wb') as f: | |
| 88 f.write(token) | |
| 89 os.chmod(COOKIE_LOCATION, 0600) # Mark cookie read-only to user. | |
|
Vadim Sh.
2013/12/04 00:08:03
It's no longer necessary.
| |
| 90 return token | |
| 91 | |
| 92 def add_auth(self, http_request): | |
| 93 http_request.headers['Authorization'] = 'OAuth %s' % self.GetAccessToken() | |
| 94 | |
| 95 @staticmethod | |
| 96 def has_prodaccess(): | |
| 97 for path in os.environ['PATH'].split(os.pathsep): | |
| 98 exe_file = os.path.join(path, 'prodaccess') | |
| 99 if os.path.exists(exe_file) and os.access(exe_file, os.X_OK): | |
| 100 return True | |
| 101 return False | |
| OLD | NEW |