-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.py
283 lines (215 loc) · 10.4 KB
/
auth.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import authx.auth
import os
import re
import json
import jwt
import requests
import urllib.parse
def is_default_site_admin_set():
if os.getenv("DEFAULT_SITE_ADMIN_USER") is not None:
result, status_code = authx.auth.get_service_store_secret("opa", key=f"site_roles")
if status_code == 200:
if 'admin' in result['site_roles']:
return os.getenv("DEFAULT_SITE_ADMIN_USER") in ",".join(result['site_roles']['admin'])
raise Exception(f"ERROR: Unable to list site administrators {result} {status_code}")
return False
def get_user_name(token):
user_key = os.getenv("CANDIG_USER_KEY")
decoded_jwt = jwt.decode(token, options={"verify_signature": False})
if decoded_jwt is not None and user_key is not None and user_key in decoded_jwt:
return decoded_jwt[user_key]
return None
def is_site_admin(token):
if (authx.auth.is_site_admin(None, token=token)):
return True
return False
def get_refresh_token(token):
client_secret = authx.auth.get_service_store_secret(service="keycloak", key="client-secret")
return authx.auth.get_oauth_response(
client_secret = client_secret,
refresh_token=token
)
#####
# AWS stuff
#####
def get_minio_client(token, s3_endpoint, bucket, access_key=None, secret_key=None, region=None, secure=True):
return authx.auth.get_minio_client(token=token, s3_endpoint=s3_endpoint, bucket=bucket, access_key=access_key, secret_key=secret_key, region=region, secure=secure)
def parse_s3_credential(awsfile):
# parse the awsfile:
access = None
secret = None
with open(awsfile) as f:
lines = f.readlines()
while len(lines) > 0 and (access is None or secret is None):
line = lines.pop(0)
parse_access = re.match(r"(aws_access_key_id|AWSAccessKeyId)\s*=\s*(.+)$", line)
if parse_access is not None:
access = parse_access.group(2)
parse_secret = re.match(r"(aws_secret_access_key|AWSSecretKey)\s*=\s*(.+)$", line)
if parse_secret is not None:
secret = parse_secret.group(2)
if access is None:
return {"error": "awsfile did not contain access ID"}
if secret is None:
return {"error": "awsfile did not contain secret key"}
return {"access": access, "secret": secret}
#####
# AWS credentials
#####
def store_s3_credential(endpoint, bucket, access, secret, token):
if not is_site_admin(token):
return {"error": "Only site admins can store aws credentials"}, 403
return authx.auth.store_aws_credential(endpoint=endpoint, bucket=bucket, access=access, secret=secret)
def get_s3_credential(endpoint, bucket, token):
if not is_site_admin(token):
return {"error": "Only site admins can view aws credentials"}, 403
return authx.auth.get_aws_credential(endpoint=endpoint, bucket=bucket)
def remove_s3_credential(endpoint, bucket, token):
if not is_site_admin(token):
return {"error": "Only site admins can remove aws credentials"}, 403
return authx.auth.remove_aws_credential(endpoint=endpoint, bucket=bucket)
#####
# Site roles
#####
def get_role_type_in_opa(role_type, token):
if not is_site_admin(token):
return {"error": "Only site admins can view site roles"}, 403
result, status_code = authx.auth.get_service_store_secret("opa", key=f"site_roles")
if status_code == 200:
if role_type in result['site_roles']:
return {role_type: result['site_roles'][role_type]}, 200
return {"error": f"role type {role_type} does not exist"}, 404
return result, status_code
def set_role_type_in_opa(role_type, members, token):
if not is_site_admin(token):
return {"error": "Only site admins can view site roles"}, 403
result, status_code = authx.auth.get_service_store_secret("opa", key=f"site_roles")
if status_code == 200:
if role_type in result['site_roles']:
result['site_roles'][role_type] = members
result, status_code = authx.auth.set_service_store_secret("opa", key=f"site_roles", value=json.dumps(result))
if status_code == 200:
return result['site_roles'][role_type], status_code
return {"error": f"role type {role_type} does not exist"}, 404
return result, status_code
#####
# Program authorizations
#####
def add_program_to_opa(program_dict, token):
# check to see if the user is allowed to add program authorizations:
if not authx.auth.is_action_allowed_for_program(token, method="POST", path="/ingest/program", program=program_dict['program_id']):
return {"error": f"User not authorized to add program authorizations for program {program_dict['program_id']}"}, 403
response, status_code = authx.auth.add_program_to_opa(program_dict)
return response, status_code
def get_program_in_opa(program_id, token):
# check to see if the user is allowed to add program authorizations:
if not authx.auth.is_action_allowed_for_program(token, method="POST", path="/ingest/program", program=program_id):
return {"error": "User not authorized to add program authorizations"}, 403
response, status_code = authx.auth.get_program_in_opa(program_id)
return response, status_code
def list_programs_in_opa(token):
response, status_code = authx.auth.list_programs_in_opa()
if status_code == 200:
return response, 200
return {"error": response}, status_code
def remove_program_from_opa(program_id, token):
# check to see if the user is allowed to add program authorizations:
if not authx.auth.is_action_allowed_for_program(token, method="POST", path="/ingest/program", program=program_id):
return {"error": "User not authorized to add program authorizations"}, 403
response, status_code = authx.auth.remove_program_from_opa(program_id)
return response, status_code
#####
# Pending user authorizations
#####
def add_pending_user_to_opa(user_token):
# NB: any user that has been authenticated by the IDP should be able to add themselves to the pending user list
response, status_code = authx.auth.get_service_store_secret("opa", key=f"pending_users")
if status_code != 200:
return response, status_code
user_name = get_user_name(user_token)
if user_name is None:
return {"error": "Could not verify jwt or obtain user ID"}, 403
user_dict = {
"user": {
"user_name": user_name,
"sample_jwt": user_token
},
"programs": {}
}
response["pending_users"][user_name] = user_dict
response, status_code = authx.auth.set_service_store_secret("opa", key=f"pending_users", value=json.dumps(response))
return response, status_code
def list_pending_users_in_opa(token):
if not is_site_admin(token):
return {"error": f"User not authorized to list pending users"}, 403
response, status_code = authx.auth.get_service_store_secret("opa", key=f"pending_users")
if status_code == 200:
response = list(response["pending_users"].keys())
return response, status_code
def is_self_pending(token):
response, status_code = authx.auth.get_service_store_secret("opa", key=f"pending_users")
if status_code == 200:
user_name = get_user_name(token)
response = user_name in response["pending_users"]
else:
response = False
return response, status_code
def approve_pending_user_in_opa(user_name, token):
if not is_site_admin(token):
return {"error": f"User not authorized to approve pending users"}, 403
response, status_code = authx.auth.get_service_store_secret("opa", key=f"pending_users")
if status_code != 200:
return response, status_code
pending_users = response["pending_users"]
if user_name in pending_users:
user_dict = pending_users[user_name]
response2, status_code = write_user_in_opa(user_dict, token)
if status_code == 200:
pending_users.pop(user_name)
response3, status_code = authx.auth.set_service_store_secret("opa", key=f"pending_users", value=json.dumps(response))
else:
return {"error": f"no pending user with ID {user_name}"}, 404
return response, status_code
def reject_pending_user_in_opa(user_name, token):
if not is_site_admin(token):
return {"error": f"User not authorized to reject pending users"}, 403
response, status_code = authx.auth.get_service_store_secret("opa", key=f"pending_users")
if status_code != 200:
return response, status_code
pending_users = response["pending_users"]
if user_name in pending_users:
pending_users.pop(user_name)
response, status_code = authx.auth.set_service_store_secret("opa", key=f"pending_users", value=json.dumps(response))
else:
return {"error": f"no pending user with ID {user_name}"}, 404
return response, status_code
def clear_pending_users_in_opa(token):
if not is_site_admin(token):
return {"error": f"User not authorized to clear pending users"}, 403
response, status_code = authx.auth.set_service_store_secret("opa", key="pending_users", value=json.dumps({"pending_users": {}}))
return response, status_code
#####
# DAC authorization for users
#####
def write_user_in_opa(user_dict, token):
if not is_site_admin(token):
return {"error": f"User not authorized to add users"}, 403
safe_name = urllib.parse.quote_plus(user_dict['user']['user_name'])
response, status_code = authx.auth.set_service_store_secret("opa", key=f"users/{safe_name}", value=json.dumps(user_dict))
return response, status_code
def get_user_in_opa(user_name, token):
if not is_site_admin(token):
return {"error": f"User not authorized to view users"}, 403
safe_name = urllib.parse.quote_plus(user_name)
response, status_code = authx.auth.get_service_store_secret("opa", key=f"users/{safe_name}")
return response, status_code
def get_self_in_opa(token):
safe_name = urllib.parse.quote_plus(get_user_name(token))
response, status_code = authx.auth.get_service_store_secret("opa", key=f"users/{safe_name}")
return response, status_code
def remove_user_from_opa(user_name, token):
if not is_site_admin(token):
return {"error": f"User not authorized to remove users"}, 403
safe_name = urllib.parse.quote_plus(user_name)
response, status_code = authx.auth.delete_service_store_secret("opa", key=f"users/{safe_name}")
return response, status_code