-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsram.py
More file actions
375 lines (267 loc) · 13.6 KB
/
sram.py
File metadata and controls
375 lines (267 loc) · 13.6 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""Functions for communicating with SRAM and some utilities."""
__copyright__ = 'Copyright (c) 2023-2026, Utrecht University'
__license__ = 'GPLv3, see LICENSE'
import datetime
import time
from typing import Dict, List, Optional
import genquery
import requests
from util import *
HTTP_OK = 200
HTTP_CREATED = 201
HTTP_NO_CONTENT = 204
def _request_headers() -> Dict[str, str]:
"""Return SRAM API request headers with bearer token."""
return {
'Content-Type': 'application/json',
'charset': 'UTF-8',
'Authorization': f'bearer {config.sram_api_key}'
}
def post_collaboration(ctx: rule.Context, group_name: str, description: str) -> Dict:
"""Create SRAM Collaborative Organisation Identifier.
:param ctx: Combined type of a callback and rei struct
:param group_name: Name of the group to create
:param description: Description of the group to create
:returns: JSON object with new collaboration details
"""
url = f"{config.sram_rest_api_url}/api/collaborations/v1"
group_type = ''
if group_name.split('-')[0] in ('research', 'datamanager', 'priv', 'deposit'):
group_type = group_name.split('-')[0]
# Add current user as CO admin if username is a valid email.
# Fallback to CO default admins if username is not a valid email.
admin_user = user.name(ctx)
administrators = [admin_user] if yoda_names.is_email_username(admin_user) else config.sram_co_default_admins
# Build SRAM payload.
payload = {
"name": 'yoda-' + group_name,
"description": description,
"disable_join_requests": True,
"disclose_member_information": True,
"disclose_email_information": True,
"administrators": administrators,
"logo": config.sram_co_logo,
"tags": [config.sram_co_default_label, group_type]
}
if config.sram_verbose_logging:
log.write(ctx, f"post_collaboration post {url}: {payload}")
response = requests.post(url, json=payload, headers=_request_headers(), timeout=30, verify=config.sram_tls_verify)
data = response.json()
if config.sram_verbose_logging:
log.write(ctx, f"post_collaboration response: {data}")
return data
def delete_collaboration(ctx: rule.Context, co_identifier: str) -> bool:
"""Delete SRAM Collaborative Organisation.
:param ctx: Combined type of a callback and rei struct
:param co_identifier: SRAM CO identifier
:returns: Boolean indicating of deletion of collaboration succeeded
"""
if not misc.is_valid_uuid(co_identifier):
log.write(ctx, f"delete_collaboration error: CO identifier is invalid {co_identifier}")
return False
url = f"{config.sram_rest_api_url}/api/collaborations/v1/{co_identifier}"
if config.sram_verbose_logging:
log.write(ctx, f"delete_collaboration post {url}")
response = requests.delete(url, headers=_request_headers(), timeout=30, verify=config.sram_tls_verify)
if config.sram_verbose_logging:
log.write(ctx, f"delete_collaboration response: {response.status_code}")
return response.status_code == HTTP_NO_CONTENT
def delete_collaboration_membership(ctx: rule.Context, co_identifier: str, uuid: str) -> bool:
"""Delete SRAM Collaborative Organisation membership.
:param ctx: Combined type of a callback and rei struct
:param co_identifier: SRAM CO identifier
:param uuid: Unique id of the user
:returns: Boolean indicating of deletion of collaboration membership succeeded
"""
if not misc.is_valid_uuid(co_identifier):
log.write(ctx, f"delete_collaboration_membership error: CO identifier is invalid {co_identifier}")
return False
url = f"{config.sram_rest_api_url}/api/collaborations/v1/{co_identifier}/members/{uuid}"
if config.sram_verbose_logging:
log.write(ctx, f"delete_collaboration_membership post {url}")
response = requests.delete(url, headers=_request_headers(), timeout=30, verify=config.sram_tls_verify)
if config.sram_verbose_logging:
log.write(ctx, f"delete_collaboration_membership response: {response.status_code}")
return response.status_code == HTTP_NO_CONTENT
def put_collaboration_invitation(ctx: rule.Context, group_name: str, username: str, co_identifier: str) -> bool:
"""Create SRAM Collaborative Organisation Identifier.
:param ctx: Combined type of a ctx and rei struct
:param group_name: Name of the group the user is to invited to join
:param username: Name of the user to be invited
:param co_identifier: SRAM identifier of the group the user is to invited to join
:returns: Boolean indicating if put of new collaboration invitation succeeded
"""
if not misc.is_valid_uuid(co_identifier):
log.write(ctx, f"put_collaboration_invitation error: CO identifier is invalid {co_identifier}")
return False
# Request SRAM to lookup possible existing invitation for this user.
url = f"{config.sram_rest_api_url}/api/invitations/v1/invitations/{co_identifier}"
if config.sram_verbose_logging:
log.write(ctx, f"put_collaboration_invitation get: {url}")
response = requests.get(url, headers=_request_headers(), timeout=30, verify=config.sram_tls_verify)
if config.sram_verbose_logging:
log.write(ctx, f"put_collaboration_invitation response: {response.status_code}")
if response.status_code != HTTP_OK:
log.write(ctx, f"put_collaboration_invitation error: unable to retrieve existing invitations - http {response.status_code}")
return False
for invite in response.json():
if invite['invitation']['email'] == username and invite['status'] == 'open':
if config.sram_verbose_logging:
log.write(ctx, f"put_collaboration_invitation error: invitation for {username} already exists")
return True
# Now plus a year.
expiration_date = datetime.datetime.fromtimestamp(int(time.time() + 3600 * 24 * 365)).strftime('%Y-%m-%d')
# Get epoch expiry date.
date = datetime.datetime.strptime(expiration_date, "%Y-%m-%d")
epoch = datetime.datetime.utcfromtimestamp(0)
epoch_date = int((date - epoch).total_seconds()) * 1000
# Build SRAM payload.
payload = {
"collaboration_identifier": co_identifier,
"message": f"Invitation to join Yoda group {group_name}",
"intended_role": "member",
"invitation_expiry_date": epoch_date,
"invites": [username],
"groups": []
}
url = f"{config.sram_rest_api_url}/api/invitations/v1/collaboration_invites"
if config.sram_verbose_logging:
log.write(ctx, f"put_collaboration_invitation put {url}: {payload}")
response = requests.put(url, json=payload, headers=_request_headers(), timeout=30, verify=config.sram_tls_verify)
if config.sram_verbose_logging:
log.write(ctx, f"put_collaboration_invitation response: {response.status_code}")
return response.status_code == HTTP_CREATED
def connect_service_collaboration(ctx: rule.Context, co_identifier: str) -> bool:
"""Connect a service to an existing SRAM collaboration.
:param ctx: Combined type of a ctx and rei struct
:param co_identifier: SRAM identifier of the Co to connect the service to
:returns: Boolean indicating if connecting a service to an existing collaboration succeeded
"""
if not misc.is_valid_uuid(co_identifier):
log.write(ctx, f"connect_service_collaboration error: CO identifier is invalid {co_identifier}")
return False
url = f"{config.sram_rest_api_url}/api/collaborations_services/v1/connect_collaboration_service/{co_identifier}"
# Build SRAM payload.
payload = {
"service_entity_id": config.sram_service_entity_id
}
if config.sram_verbose_logging:
log.write(ctx, f"connect_service_collaboration put {url}: {payload}")
response = requests.put(url, json=payload, headers=_request_headers(), timeout=30, verify=config.sram_tls_verify)
if config.sram_verbose_logging:
log.write(ctx, f"connect_service_collaboration response: {response.status_code}")
return response.status_code == HTTP_CREATED
def update_collaboration_membership(ctx: rule.Context, co_identifier: str, uuid: str, new_role: str) -> bool:
"""Update SRAM Collaborative Organisation membership.
:param ctx: Combined type of a callback and rei struct
:param co_identifier: SRAM CO identifier
:param uuid: Unique id of the user
:param new_role: New role of the user
:returns: Boolean indicating that updation of collaboration membership succeeded
"""
if not misc.is_valid_uuid(co_identifier):
log.write(ctx, f"update_collaboration_membership error: CO identifier is invalid {co_identifier}")
return False
if not misc.is_valid_uuid(uuid):
log.write(ctx, f"update_collaboration_membership error: User uuid is invalid {uuid}")
return False
url = f"{config.sram_rest_api_url}/api/collaborations/v1/{co_identifier}/members"
payload = {
"role": 'admin' if new_role == 'manager' else 'member',
"uid": uuid
}
if config.sram_verbose_logging:
log.write(ctx, f"update_collaboration_membership put {url}")
response = requests.put(url, json=payload, headers=_request_headers(), timeout=30, verify=config.sram_tls_verify)
if config.sram_verbose_logging:
log.write(ctx, f"update_collaboration_membership response: {response.status_code}")
return response.status_code == HTTP_CREATED
def get_co_members(ctx: rule.Context, co_identifier: str) -> List[Dict[str, str]]:
"""Get SRAM collaboration members with their uids.
:param ctx: Combined type of a callback and rei struct
:param co_identifier: SRAM CO identifier
:returns: List of dicts containing email and uid of SRAM collaboration members
"""
if not misc.is_valid_uuid(co_identifier):
log.write(ctx, f"get_co_members error: CO identifier is invalid {co_identifier}")
return []
url = f"{config.sram_rest_api_url}/api/collaborations/v1/{co_identifier}"
if config.sram_verbose_logging:
log.write(ctx, f"get_co_members get {url}")
response = requests.get(url, headers=_request_headers(), timeout=30, verify=config.sram_tls_verify)
if response.status_code != HTTP_OK:
if config.sram_verbose_logging:
log.write(ctx, f"get_co_members response: {response.status_code}")
return []
data = response.json()
if config.sram_verbose_logging:
log.write(ctx, f"get_co_members response: {data}")
return [
{
'email': member['user']['email'],
'uid': member['user']['uid']
}
for member in data.get('collaboration_memberships', [])
]
def get_co_member_uid(ctx: rule.Context, co_identifier: str, user_name: str) -> str:
"""Get SRAM collaboration member uid.
:param ctx: Combined type of a callback and rei struct
:param co_identifier: SRAM CO identifier
:param user_name: Name of the user
:returns: Unique id of the user
"""
co_members = get_co_members(ctx, co_identifier)
for member in co_members:
if user_name.lower() == member['email'].lower():
if config.sram_verbose_logging:
log.write(ctx, f"get_uid user_name: {user_name}, uuid: {member['uid']}")
return member['uid']
if config.sram_verbose_logging:
log.write(ctx, f"get_uid user_name: {user_name}, uuid: not found")
return ''
def is_user_co_member(ctx: rule.Context, co_identifier: str, user_name: str) -> bool:
"""Check if a user is a member of a SRAM collaboration.
:param ctx: Combined type of a callback and rei struct
:param co_identifier: SRAM CO identifier
:param user_name: Name of the user (email)
:returns: Boolean indicating if the user is a member of the collaboration
"""
co_members = get_co_members(ctx, co_identifier)
is_member = any(user_name.lower() == member['email'].lower() for member in co_members)
if config.sram_verbose_logging:
log.write(ctx, f"is_user_co_member user_name: {user_name}, is_member: {is_member}")
return is_member
def get_co_identifier(ctx: rule.Context, group_name: str) -> Optional[str]:
"""Retrieve the SRAM CO identifier of a Yoda group if the group is a SRAM CO.
:param ctx: Combined type of a ctx and rei struct
:param group_name: Name of the group
:returns: Return SRAM CO identifier if the Yoda group is a SRAM CO else None
"""
if not config.enable_sram:
return None
iter = genquery.row_iterator(
"META_USER_ATTR_VALUE",
f"USER_TYPE = 'rodsgroup' AND META_USER_ATTR_NAME = 'co_identifier' AND USER_GROUP_NAME = '{group_name}'",
genquery.AS_LIST, ctx
)
for row in iter:
if row[0] and misc.is_valid_uuid(row[0]):
return row[0]
return None
def is_user_marked_invited(ctx: rule.Context, username: str, group_name: str) -> bool:
"""Check in user metadata if user is marked as invited to SRAM CO.
:param ctx: Combined type of a ctx and rei struct
:param username: Name of the user
:param group_name: Name of the group user is invited to
:returns: Boolean indicating where the user is marked as invited to SRAM CO
"""
attr_name = f"{constants.UUORGMETADATAPREFIX}sram_invited"
iter = genquery.row_iterator(
"META_USER_ATTR_VALUE",
f"USER_GROUP_NAME = '{username}' AND META_USER_ATTR_NAME = '{attr_name}'",
genquery.AS_LIST, ctx
)
for row in iter:
if row[0] == group_name:
return True
return False