Skip to content

Commit cd1f301

Browse files
committed
initial conversion to using google.auth rather than oauth2client
1 parent d485cf6 commit cd1f301

File tree

5 files changed

+29
-47
lines changed

5 files changed

+29
-47
lines changed

firecloud/api.py

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,47 @@
1414

1515
from six.moves.urllib.parse import urlencode, urljoin
1616
from six import string_types
17-
import requests
18-
from oauth2client.client import GoogleCredentials
17+
import google.auth
18+
from google.auth.transport.requests import AuthorizedSession
1919

2020
from firecloud.errors import FireCloudServerError
2121
from firecloud.fccore import __fcconfig as fcconfig
2222
from firecloud.__about__ import __version__
2323

2424
FISS_USER_AGENT = "FISS/" + __version__
2525

26-
# Set Global credentials
27-
__CREDENTIALS = None
26+
# Set Global Authorized Session
27+
__SESSION = AuthorizedSession(google.auth.default(['https://www.googleapis.com/auth/userinfo.profile',
28+
'https://www.googleapis.com/auth/userinfo.email'])[0])
2829

2930
#################################################
3031
# Utilities
3132
#################################################
32-
def _fiss_access_headers(headers=None):
33+
def _fiss_agent_header(headers=None):
3334
""" Return request headers for fiss.
34-
Retrieves an access token with the user's google crededentials, and
35-
inserts FISS as the User-Agent.
35+
Inserts FISS as the User-Agent.
3636
3737
Args:
3838
headers (dict): Include additional headers as key-value pairs
3939
4040
"""
41-
# By only having a single instance of GoogleCredentials, access token
42-
# caching is enabled
43-
global __CREDENTIALS
44-
if __CREDENTIALS is None:
45-
__CREDENTIALS = GoogleCredentials.get_application_default()
46-
# We need to request userinfo.profile and userinfo.email if we are using a service account to run FISS
47-
__CREDENTIALS = __CREDENTIALS.create_scoped(['https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email'])
48-
fcconfig.set_access_token(__CREDENTIALS.get_access_token().access_token)
49-
fiss_headers = {"Authorization" : "bearer " + fcconfig.access_token}
50-
fiss_headers["User-Agent"] = FISS_USER_AGENT
51-
if headers:
41+
fiss_headers = {"User-Agent" : FISS_USER_AGENT}
42+
if headers is not None:
5243
fiss_headers.update(headers)
5344
return fiss_headers
5445

5546
def __get(methcall, headers=None, root_url=fcconfig.root_url, **kwargs):
5647
if not headers:
57-
headers = _fiss_access_headers()
58-
r = requests.get(urljoin(root_url, methcall), headers=headers, **kwargs)
48+
headers = _fiss_agent_header()
49+
r = __SESSION.get(urljoin(root_url, methcall), headers=headers, **kwargs)
5950
if fcconfig.verbosity > 1:
6051
print('FISSFC call: %s' % r.url, file=sys.stderr)
6152
return r
6253

6354
def __post(methcall, headers=None, root_url=fcconfig.root_url, **kwargs):
6455
if not headers:
65-
headers = _fiss_access_headers({"Content-type": "application/json"})
66-
r = requests.post(urljoin(root_url, methcall), headers=headers, **kwargs)
56+
headers = _fiss_agent_header({"Content-type": "application/json"})
57+
r = __SESSION.post(urljoin(root_url, methcall), headers=headers, **kwargs)
6758
if fcconfig.verbosity > 1:
6859
info = r.url
6960
json = kwargs.get("json", None)
@@ -74,8 +65,8 @@ def __post(methcall, headers=None, root_url=fcconfig.root_url, **kwargs):
7465

7566
def __put(methcall, headers=None, root_url=fcconfig.root_url, **kwargs):
7667
if not headers:
77-
headers = _fiss_access_headers()
78-
r = requests.put(urljoin(root_url, methcall), headers=headers, **kwargs)
68+
headers = _fiss_agent_header()
69+
r = __SESSION.put(urljoin(root_url, methcall), headers=headers, **kwargs)
7970
if fcconfig.verbosity > 1:
8071
info = r.url
8172
json = kwargs.get("json", None)
@@ -86,8 +77,8 @@ def __put(methcall, headers=None, root_url=fcconfig.root_url, **kwargs):
8677

8778
def __delete(methcall, headers=None, root_url=fcconfig.root_url):
8879
if not headers:
89-
headers = _fiss_access_headers()
90-
r = requests.delete(urljoin(root_url, methcall), headers=headers)
80+
headers = _fiss_agent_header()
81+
r = __SESSION.delete(urljoin(root_url, methcall), headers=headers)
9182
if fcconfig.verbosity > 1:
9283
print('FISSFC call: DELETE %s' % r.url, file=sys.stderr)
9384
return r
@@ -138,7 +129,7 @@ def list_entity_types(namespace, workspace):
138129
Swagger:
139130
https://api.firecloud.org/#!/Entities/getEntityTypes
140131
"""
141-
headers = _fiss_access_headers({"Content-type": "application/json"})
132+
headers = _fiss_agent_header({"Content-type": "application/json"})
142133
uri = "workspaces/{0}/{1}/entities".format(namespace, workspace)
143134
return __get(uri, headers=headers)
144135

@@ -154,7 +145,7 @@ def upload_entities(namespace, workspace, entity_data):
154145
https://api.firecloud.org/#!/Entities/importEntities
155146
"""
156147
body = urlencode({"entities" : entity_data})
157-
headers = _fiss_access_headers({
148+
headers = _fiss_agent_header({
158149
'Content-type': "application/x-www-form-urlencoded"
159150
})
160151
uri = "workspaces/{0}/{1}/importEntities".format(namespace, workspace)
@@ -429,12 +420,12 @@ def update_entity(namespace, workspace, etype, ename, updates):
429420
Swagger:
430421
https://api.firecloud.org/#!/Entities/update_entity
431422
"""
432-
headers = _fiss_access_headers({"Content-type": "application/json"})
423+
headers = _fiss_agent_header({"Content-type": "application/json"})
433424
uri = "{0}workspaces/{1}/{2}/entities/{3}/{4}".format(fcconfig.root_url,
434425
namespace, workspace, etype, ename)
435426

436427
# FIXME: create __patch method, akin to __get, __delete etc
437-
return requests.patch(uri, headers=headers, json=updates)
428+
return __SESSION.patch(uri, headers=headers, json=updates)
438429

439430
###############################
440431
### 1.2 Method Configurations
@@ -525,7 +516,7 @@ def update_workspace_config(namespace, workspace, cnamespace, configname, body):
525516
Swagger:
526517
https://api.firecloud.org/#!/Method_Configurations/updateWorkspaceMethodConfig
527518
"""
528-
headers = _fiss_access_headers({"Content-type": "application/json"})
519+
headers = _fiss_agent_header({"Content-type": "application/json"})
529520
body = json.dumps(body)
530521
uri = "workspaces/{0}/{1}/method_configs/{2}/{3}".format(namespace,
531522
workspace, cnamespace, configname)
@@ -1121,9 +1112,9 @@ def update_workspace_acl(namespace, workspace, acl_updates):
11211112
"""
11221113
uri = "{0}workspaces/{1}/{2}/acl".format(fcconfig.root_url,
11231114
namespace, workspace)
1124-
headers = _fiss_access_headers({"Content-type": "application/json"})
1115+
headers = _fiss_agent_header({"Content-type": "application/json"})
11251116
# FIXME: create __patch method, akin to __get, __delete etc
1126-
return requests.patch(uri, headers=headers, data=json.dumps(acl_updates))
1117+
return __SESSION.patch(uri, headers=headers, data=json.dumps(acl_updates))
11271118

11281119
def clone_workspace(from_namespace, from_workspace, to_namespace, to_workspace,
11291120
authorizationDomain=""):
@@ -1209,13 +1200,13 @@ def update_workspace_attributes(namespace, workspace, attrs):
12091200
Swagger:
12101201
https://api.firecloud.org/#!/Workspaces/updateAttributes
12111202
"""
1212-
headers = _fiss_access_headers({"Content-type": "application/json"})
1203+
headers = _fiss_agent_header({"Content-type": "application/json"})
12131204
uri = "{0}workspaces/{1}/{2}/updateAttributes".format(fcconfig.root_url,
12141205
namespace, workspace)
12151206
body = json.dumps(attrs)
12161207

12171208
# FIXME: create __patch method, akin to __get, __delete etc
1218-
return requests.patch(uri, headers=headers, data=body)
1209+
return __SESSION.patch(uri, headers=headers, data=body)
12191210

12201211
# Helper functions to create attribute update dictionaries
12211212

firecloud/fccore.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ def config_set(name, value):
6161
# FIXME: should validate critical variables, e.g. that type is not changed
6262
__fcconfig[name] = value
6363

64-
def __set_access_token(access_token):
65-
__fcconfig.access_token = access_token
66-
6764
def __set_verbosity(verbosity):
6865
previous_value = __fcconfig.verbosity
6966
try:
@@ -99,8 +96,6 @@ def __set_root_url(url):
9996
'workspace' : '',
10097
'method_ns' : '',
10198
'entity_type' : 'sample_set',
102-
'access_token' : '',
103-
'set_access_token' : __set_access_token,
10499
'get_verbosity' : __get_verbosity,
105100
'set_verbosity' : __set_verbosity,
106101
'set_root_url' : __set_root_url

firecloud/supervisor.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from six import print_
21
import time
32
import pydot
43
import logging
@@ -9,9 +8,6 @@
98
logging.basicConfig(format='%(asctime)s::%(levelname)s %(message)s',
109
datefmt='%Y-%m-%d %I:%M:%S', level=logging.INFO)
1110

12-
# Quiet requests, oauth
13-
logging.getLogger("requests").setLevel(logging.WARNING)
14-
logging.getLogger("oauth2client").setLevel(logging.WARNING)
1511

1612
def supervise(project, workspace, namespace, workflow,
1713
sample_sets, recovery_file):
@@ -241,7 +237,7 @@ def supervise_until_complete(monitor_data, dependencies, args, recovery_file):
241237
else:
242238
# None of the attempts above succeeded, log an error, mark as failed
243239
logging.error("Maximum retries exceeded")
244-
task_data['state'] == 'Completed'
240+
task_data['state'] = 'Completed'
245241
task_data['evaluated'] = True
246242
task_data['succeeded'] = False
247243

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
requests
22
nose
33
pylint
4-
oauth2client
4+
google-auth
55
six

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
test_suite = 'nose.collector',
2424
install_requires = [
25-
'oauth2client',
25+
'google-auth',
2626
'pydot',
2727
'requests',
2828
'six',

0 commit comments

Comments
 (0)