forked from Inria-Empenn/shanoir_downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshanoir_util.py
235 lines (193 loc) · 9.43 KB
/
shanoir_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import os
import datetime
import requests
import json
import getpass
import sys
import logging
import http.client as http_client
from pathlib import Path
def init_logging(args):
verbose = args.verbose
logfile = Path(args.log_file)
logfile.parent.mkdir(exist_ok=True, parents=True)
logging.basicConfig(
level=logging.INFO, # if verbose else logging.ERROR,
format="%(asctime)s [%(levelname)s] %(message)s",
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[
logging.FileHandler(str(logfile)),
logging.StreamHandler(sys.stdout)
]
)
if verbose:
http_client.HTTPConnection.debuglevel = 1
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
def initialize(args):
server_domain = args.domain
username = args.username
init_logging(args)
verify = args.certificate if hasattr(args, 'certificate') and args.certificate != '' else True
proxy_url = None # 'user:pass@host:port'
if hasattr(args, 'proxy_url') and args.proxy_url is not None:
proxy_a = args.proxy_url.split('@')
proxy_user = proxy_a[0]
proxy_host = proxy_a[1]
proxy_password = getpass.getpass(prompt='Proxy password for user ' + proxy_user + ' and host ' + proxy_host + ': ', stream=None)
proxy_url = proxy_user + ':' + proxy_password + '@' + proxy_host
else:
configuration_folder = None
if hasattr(args, 'configuration_folder') and args.configuration_folder:
configuration_folder = Path(args.configuration_folder)
else:
cfs = sorted(list(Path.home().glob('.su_v*')))
configuration_folder = cfs[-1] if len(cfs) > 0 else Path().home()
proxy_settings = configuration_folder / 'proxy.properties'
proxy_config = {}
if proxy_settings.exists():
with open(proxy_settings) as file:
for line in file:
if line.startswith('proxy.'):
line_s = line.split('=')
proxy_key = line_s[0]
proxy_value = line_s[1].strip()
proxy_key = proxy_key.split('.')[-1]
proxy_config[proxy_key] = proxy_value
if 'enabled' not in proxy_config or proxy_config['enabled'] == 'true':
if 'user' in proxy_config and len(proxy_config['user']) > 0 and 'password' in proxy_config and len(proxy_config['password']) > 0:
proxy_url = proxy_config['user'] + ':' + proxy_config['password']
proxy_url += '@' + proxy_config['host'] + ':' + proxy_config['port']
else:
print("Proxy configuration file not found. Proxy will be ignored.")
proxies = None
if proxy_url:
proxies = {
'http': 'http://' + proxy_url,
# 'https': 'https://' + proxy_url,
}
return { 'domain': server_domain,
'username': username,
'verify': verify,
'proxies': proxies,
'timeout': args.timeout,
}
access_token = None
refresh_token = None
# using user's password, get the first access token and the refresh token
def ask_access_token(config):
try:
password = os.environ['shanoir_password'] if 'shanoir_password' in os.environ else getpass.getpass(prompt='Password for Shanoir user ' + config['username'] + ': ', stream=None)
except:
sys.exit(0)
url = 'https://' + config['domain'] + '/auth/realms/shanoir-ng/protocol/openid-connect/token'
payload = {
'client_id' : 'shanoir-uploader',
'grant_type' : 'password',
'username' : config['username'],
'password' : password,
'scope' : 'offline_access'
}
# curl -d '{"client_id":"shanoir-uploader", "grant_type":"password", "username": "amasson", "password": "", "scope": "offline_access" }' -H "Content-Type: application/json" -X POST
headers = {'content-type': 'application/x-www-form-urlencoded'}
print('get keycloak token...')
response = requests.post(url, data=payload, headers=headers, proxies=config['proxies'], verify=config['verify'], timeout=config['timeout'])
if not hasattr(response, 'status_code') or response.status_code != 200:
print('Failed to connect, make sur you have a certified IP or are connected on a valid VPN.')
raise ConnectionError(response.status_code)
response_json = json.loads(response.text)
if 'error_description' in response_json and response_json['error_description'] == 'Invalid user credentials':
print('bad username or password')
sys.exit(1)
global refresh_token
refresh_token = response_json['refresh_token']
return response_json['access_token']
# get a new acess token using the refresh token
def refresh_access_token(config):
url = 'https://' + config['domain'] + '/auth/realms/shanoir-ng/protocol/openid-connect/token'
payload = {
'grant_type' : 'refresh_token',
'refresh_token' : refresh_token,
'client_id' : 'shanoir-uploader'
}
headers = {'content-type': 'application/x-www-form-urlencoded'}
logging.info('refresh keycloak token...')
response = requests.post(url, data=payload, headers=headers, proxies=config['proxies'], verify=config['verify'], timeout=config['timeout'])
if response.status_code != 200:
logging.error('response status : {response.status_code}, {responses[response.status_code]}')
response_json = response.json()
return response_json['access_token']
def perform_rest_request(config, rtype, url, **kwargs):
response = None
if rtype == 'get':
response = requests.get(url, proxies=config['proxies'], verify=config['verify'], timeout=config['timeout'], **kwargs)
elif rtype == 'post':
response = requests.post(url, proxies=config['proxies'], verify=config['verify'], timeout=config['timeout'], **kwargs)
elif rtype == 'delete':
response = requests.delete(url, proxies=config['proxies'], verify=config['verify'], timeout=config['timeout'], **kwargs)
elif rtype == 'put':
response = requests.put(url, proxies=config['proxies'], verify=config['verify'], timeout=config['timeout'], **kwargs)
else:
print('Error: unimplemented request type')
return response
# perform a request on the given url, asks for a new access token if the current one is outdated
def rest_request(config, rtype, url, raise_for_status=True, **kwargs):
global access_token
if access_token is None:
access_token = ask_access_token(config)
headers = {
'Authorization' : 'Bearer ' + access_token,
'content-type' : 'application/json',
'charset' : 'utf-8'
}
response = perform_rest_request(config, rtype, url, headers=headers, **kwargs)
logging.error(response)
# if token is outdated, refresh it and try again
if response.status_code == 401:
access_token = refresh_access_token(config)
headers['Authorization'] = 'Bearer ' + access_token
response = perform_rest_request(config, rtype, url, headers=headers, **kwargs)
if raise_for_status:
response.raise_for_status()
return response
def log_response(e):
logging.error('Response status code: {e.response.status_code}')
logging.error(' reason: {e.response.reason}')
logging.error(' text: {e.response.text}')
logging.error(' headers: {e.response.headers}')
logging.error(str(e))
return
# perform a GET request on the given url, asks for a new access token if the current one is outdated
def rest_get(config, url, params=None, stream=None):
return rest_request(config, 'get', url, params=params, stream=stream)
# perform a POST request on the given url, asks for a new access token if the current one is outdated
def rest_post(config, url, params=None, files=None, stream=None, json=None, data=None, raise_for_status=True):
return rest_request(config, 'post', url, raise_for_status, params=params, files=files, stream=stream, json=json, data=data)
# perform a DELETE request on the given url, asks for a new access token if the current one is outdated
def rest_delete(config, url, params=None, stream=None, raise_for_status=True):
return rest_request(config, 'delete', url, raise_for_status, params=params, stream=stream)
def createExecution(config, execution, silent=False):
global refresh_token
global access_token
if access_token is None:
access_token = ask_access_token(config)
execution["identifier"]=""
execution["name"] += "_" + datetime.datetime.now().strftime("%m%d%Y%H%M%S")
execution["refreshToken"] = refresh_token
execution["exportFormat"] = "dcm"
execution["studyIdentifier"] = 17
execution["client"]="shanoir-uploader"
url = 'https://' + config['domain'] + '/shanoir-ng/datasets/carmin-data/createExecution'
response = rest_post(config, url, {}, data=json.dumps(execution), raise_for_status=False)
if response.status_code == 401:
return "401"
return response.json()
def getExecutionStatus(config, identifier):
url = 'https://' + config['domain'] + '/shanoir-ng/datasets/carmin-data/execution/' + identifier
response = rest_get(config, url)
return response.content
def deleteDataset(config, datasetId):
url = 'https://' + config['domain'] + '/shanoir-ng/datasets/datasets/' + datasetId
response = rest_delete(config, url, raise_for_status=False)
return response.status_code