-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetaccess.py
316 lines (277 loc) · 15.1 KB
/
setaccess.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
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
import sys
import shutil
import os
import socket
from pathlib import Path
import requests
import glob
# Group accounts have different names in the SDC and LDC datacenters which are:
# cmoigo -> GRP_CMO_SEQ
# bicigo -> GRP_BIC_SEQ
# isabl -> grp_papaemme_seq
# shahbot-> SVC_shahs3_bot
special_group_accounts = ["cmoigo", "bicigo", "isabl"]
bic_group_members = ["byrnec","kazmierk","khaninr","pirunm","soccin","songt","sonzoge","vurals","webbera","wilsonm2"]
cmo_group_members = ["ivkovics","pankeyd","kumarn1","bolipatc","buehlere","vanna1","shahr2","sumans","noronhaa","orgeraj","gongy","pricea2","pintoa1"]
DLP_REQUIRED_ACCESS_LIST = ["havasove", "shahbot", "mcphera1", "grewald"]
TCRSEQ_REQUIRED_ACCESS_LIST = ["elhanaty","greenbab","lih7","havasove"]
ACL_TEMP_DIR = "/tmp/acls/"
NGS_STATS_ENDPOINT = "http://igodb.mskcc.org:8080/ngs-stats/permissions/getRequestPermissions/"
NGS_STATS_ENDPOINT_LAB = "http://igodb.mskcc.org:8080/ngs-stats/permissions/getLabPermissions/"
NGS_STATS_ENDPOINT_RECENT = "http://igodb.mskcc.org:8080/ngs-stats/rundone/getRecentlyArchivedRequests/"
DOMAIN = "LDC"
LAB_SHARE_PATH = "/igo/delivery/share/"
ACL_DOMAIN = "@hpc.private"
LAB_SHARE_PATH_SDC = "/ifs/datadelivery/igo_core/share/"
LAB_SHARE_FASTQ_PATH_SDC = "/ifs/datadelivery/igo_core/FASTQ/"
NANOPORE_DELIVERY_PATH_SDC = "/ifs/datadelivery/igo_core/nanopore/"
ACL_DOMAIN_SDC = "@mskcc.org"
if socket.gethostname().startswith("isvigoacl01"):
print("Setting default paths for SDC - " + LAB_SHARE_PATH_SDC)
LAB_SHARE_PATH = LAB_SHARE_PATH_SDC
ACL_DOMAIN = ACL_DOMAIN_SDC
DOMAIN = "SDC"
# lab_name is optional
def set_request_acls(request, lab_name):
print("Setting ACLs for request {} ".format(request))
request_perms = get_request_metadata(request, "none")
if request_perms is None:
# Maybe the request is older than the LIMS, just give access to all lab members based on the folder such as 'pamere'
if lab_name == "":
print("Unknown request ID {}, quitting.".format(request))
return
else:
print("No known LIMS data, granting permissions based on the lab folder: " + lab_name)
request_perms = get_request_metadata(request, lab_name)
temp_acl_file = request_perms.write_acl_temp_file()
request_perms.grant_share_acls(temp_acl_file, True)
request_perms.grant_fastq_acls(temp_acl_file)
print("---")
return
temp_acl_file = request_perms.write_acl_temp_file()
request_perms.grant_fastq_acls(temp_acl_file)
if request_perms.request_name.startswith("Nanopore"):
request_perms.grant_share_acls(temp_acl_file, True)
else:
request_perms.grant_share_acls(temp_acl_file, False)
print("---")
def get_request_metadata(request, lab_name):
url = NGS_STATS_ENDPOINT + request + "/" + lab_name
print("Sending request {}".format(url))
r = requests.get(url).json()
# 'status': 500, 'error': 'Internal Server Error',
if 'status' in r.keys() and r['status'] == 500:
return None
return RequestPermissions(r['labName'], r['labMembers'], r['request'], r['requestName'], r['requestReadAccess'], r['requestGroups'], r['dataAccessEmails'], r['fastqs'], r['isDLP'])
def get_lab_metadata(lab_name, request):
url = NGS_STATS_ENDPOINT_LAB + lab_name
print("Sending request {}".format(url))
r = requests.get(url).json()
# 'status': 500, 'error': 'Internal Server Error',
if 'status' in r.keys() and r['status'] == 500:
return None
return RequestPermissions(r['labName'], r['labMembers'], request, '', '', '', '', '', '')
# fields from LIMS and fastq databases used to determine all ACLs
class RequestPermissions:
def __init__(self, lab, members, request, requestName, request_members, groups, dataAccessEmails, fastqs, isDLP):
self.lab = lab
self.members = members # members of the lab
self.request = request # request ID such as 08822_X
self.request_name = requestName
self.request_members = request_members # individuals/groups granted read access to the request
self.groups = groups # groups that need access per request
self.data_access_emails = list(dataAccessEmails)
self.fastqs = fastqs # list of fastqs per request
self.request_share_path = LAB_SHARE_PATH + self.lab + "/Project_" + self.request
self.isDLP = isDLP
if self.isDLP:
print("DLP requests must give access to {} ".format(DLP_REQUIRED_ACCESS_LIST))
self.data_access_emails.extend(DLP_REQUIRED_ACCESS_LIST)
if "TCRSeq" in self.request_name:
print("TCRSeq requests must give access to {} ".format(TCRSEQ_REQUIRED_ACCESS_LIST))
self.data_access_emails.extend(TCRSEQ_REQUIRED_ACCESS_LIST)
# Grants ACLs to all fastq.gz files in a project, parent folders for the fastqs and SampleSheet.csv
# For DLP runs ending in 'DLP' DIANA_0294_AHTGLJDSXY_DLP, grants read access to the 'Reports' and 'Stats' folders
def grant_fastq_acls(self, temp_file_path):
print("Setting ACLS for all {} fastqs in the request.".format(len(self.fastqs)))
# nfs4_setfacl -S "/igo/delivery/FASTQ/acl_entries.txt" /igo/delivery/share/bergerm1/Project_06302_AM
command_prefix = "nfs4_setfacl -S \"{}\"".format(temp_file_path)
sample_folders = set()
for fastq in self.fastqs:
if DOMAIN == "SDC":
fastq = fastq.replace("/igo/delivery/FASTQ", LAB_SHARE_FASTQ_PATH_SDC)
fastq_path = Path(fastq)
if not fastq_path.exists():
print("{} fastq.gz does not exist. Moving on.".format(fastq))
continue
# /igo/delivery/share/labname/Project_12345/MICHELLE_0682/Sample_mysample
sample_folders.add(fastq_path.parent)
set_acl_command = "{} {}".format(command_prefix, fastq)
print(set_acl_command)
result = os.system(set_acl_command)
if result != 0:
print("ERROR SETTING ACL - ".format(set_acl_command))
project_folders = set()
for sample_folder in sample_folders:
set_acl_command = "{} {}".format(command_prefix, sample_folder)
parent_path = Path(sample_folder).parent
project_folders.add(parent_path)
print(set_acl_command)
result = os.system(set_acl_command)
if result != 0:
print("ERROR SETTING ACL at sample level - ".format(set_acl_command))
sample_sheet = sample_folder.joinpath(Path("SampleSheet.csv"))
if sample_sheet.exists(): # if SampleSheet.csv exists make it readable too
set_acl_command = "{} {}".format(command_prefix, sample_sheet)
os.system(set_acl_command)
# check if *.yaml file exists for DLP projects and grant same access to it
if glob.glob("{}/*.yaml".format(sample_folder)): # if os.system("test -f {}/*.yaml".format(sample_folder)) == 0:
set_acl_command_yaml = "{} {}/*.yaml".format(command_prefix, sample_folder)
print(set_acl_command_yaml)
os.system(set_acl_command_yaml)
if glob.glob("{}/*.fld".format(sample_folder)): # if os.system("test -f {}/*.fld".format(sample_folder)) == 0:
set_acl_command_fld = "{} {}/*.fld".format(command_prefix, sample_folder)
print(set_acl_command_fld)
os.system(set_acl_command_fld)
run_folder = str(sample_folder.parent)
if run_folder.endswith("DLP"):
reports_folder = run_folder + "/Reports"
reports_status = os.stat(reports_folder)
# mask 4 means readable by all
if str(oct(reports_status.st_mode)[-1:]) == "4":
print("Reports folder {} is already readable by all".format(reports_folder))
else:
set_acl_reports = "nfs4_setfacl -R -S \"{}\" {}".format(temp_file_path, reports_folder)
print(set_acl_reports)
result = os.system(set_acl_reports)
# /igo/delivery/FASTQ/MICHELLE_0284_BHVKK3DMXX/Project_09641_AS
for project_folder in project_folders:
set_acl_command = "{} {}".format(command_prefix, project_folder)
print(set_acl_command)
result = os.system(set_acl_command)
if result != 0:
print("ERROR SETTING ACL at project level - ".format(set_acl_command))
def grant_share_acls(self, temp_file_path, recursively):
if self.request_share_exists():
print("Setting ACLS for all request share folders below " + self.request_share_path)
# nfs4_setfacl -R(recursive)
# nfs4_setfacl -R -S "/igo/archive/FASTQ/acl_entries.txt" /igo/delivery/share/lab/request
set_acl_command = "nfs4_setfacl -R -S \"{}\" {}".format(temp_file_path, self.request_share_path)
if recursively:
set_acl_command = "nfs4_setfacl -R -L -S \"{}\" {}".format(temp_file_path, self.request_share_path)
print(set_acl_command)
result = os.system(set_acl_command)
if result != 0:
print("ERROR SETTING ACL - ".format(set_acl_command))
#verify lab directory is readable by all since it has no ACLs set
parent = os.path.dirname(self.request_share_path)
mask = str(oct(os.stat(parent).st_mode)[-3:])
if mask != "775":
command = "chmod +rx {}".format(parent)
print(command)
os.system(command)
else:
print("{} share does not exist ".format(self.request_share_path))
# Write the file with the ACLs to a temp location
def write_acl_temp_file(self): # just leave the temp files around for possible reference later
temp_file_path = ACL_TEMP_DIR + self.lab + "_" + self.request + ".txt"
temp_file = open(temp_file_path, "w") # open to overwrite any existing content
temp_file.write(self.get_acls())
temp_file.close()
if self.request_share_exists():
acl_file_copy_path = self.request_share_path + "/acl_permissions.txt"
print("Copying ACL permissions file to {}".format(acl_file_copy_path))
shutil.copy(temp_file_path, acl_file_copy_path)
return temp_file_path
# create the ACL list with all lab members and groups
def get_acls(self):
# make a set of users and set of groups from the labMembers table, request access table & dataAccessEmails
# groups must be separate set because the ACL format is different ie "A:g:[email protected]:rxtncy"
users_set = set()
groups_set = set()
# Already filtered via ngs_stats code -
# email.endsWith("mskcc.org") & & !email.contains("zzPDL")) then dataAccessIDs.add(email.split("@")[0])
print("Adding data access emails {}".format(self.data_access_emails))
for email_id in self.data_access_emails:
email_id = str(email_id).strip()
if email_id != "skicmopm":
users_set.add(email_id.lower())
for group_name in self.groups:
groups_set.add(group_name)
for lab_member in self.members:
if lab_member["group"]:
groups_set.add(lab_member["member"])
else:
users_set.add(lab_member["member"])
for r_member in self.request_members:
if r_member["group"]:
groups_set.add(r_member["member"])
else:
users_set.add(r_member["member"])
acls = ""
# Let's put groups on the top then individuals
for group_name in groups_set: # group names are different in each data center
# TODO check if group name is valid?
if DOMAIN == "LDC": # first data center, these are the group names used in the database
acls += "A:g:" + group_name + ACL_DOMAIN + ":rxtncy\n"
if DOMAIN == "SDC" and group_name == "isabl":
group_name = "grp_papaemme_seq"
acls += "A:g:" + group_name + ACL_DOMAIN + ":rxtncy\n"
if DOMAIN == "SDC" and group_name == "bicigo":
print("Adding bic group user list to access")
users_set.update(bic_group_members)
if DOMAIN == "SDC" and group_name == "cmoigo":
print("Adding cmo group user list to access")
users_set.update(cmo_group_members)
print("Checking if each account exists for all user IDs before trying to add the ACL with the id command")
for user in users_set:
if DOMAIN == "SDC" and user == "shahbot":
user = 'SVC_shahs3_bot'
user_exists_command = "id -u %s" % (user)
user_exists_result = os.system(user_exists_command)
if user_exists_result != 0: # try again, for some reason the command occasionally fails when the id is valid
user_exists_result = os.system(user_exists_command)
if user_exists_result == 0:
acls += "A::" + user + ACL_DOMAIN + ":rxtncy\n"
else:
print("User {} does not exist, they should be removed from the DB.".format(user))
owner_group_everyone = "A::OWNER@:rwaDxtTnNcCoy\nA::GROUP@:rxtncCy\nA::EVERYONE@:tncy\n"
return acls + owner_group_everyone
def request_share_exists(self):
return os.path.isdir(self.request_share_path)
def main():
if len(sys.argv) != 2:
print("Usage: python3 setaccess.py REQUEST=<request> | LABSHAREDIR=<directory> | ARCHIVEDWITHINLAST=<minutes>")
return
args = sys.argv[1]
if not os.path.exists(ACL_TEMP_DIR):
os.makedirs(ACL_TEMP_DIR)
if args.startswith("REQUEST="):
request = args[8:]
set_request_acls(request, '')
if args.startswith("LABSHAREDIR="):
lab_folder = args[12:]
print("Granting access permissions for {}".format(lab_folder))
lab_name = os.path.basename(lab_folder)
project_folders_list = [f.path for f in os.scandir(lab_folder) if f.is_dir()]
for project_folder in project_folders_list:
folder_name = os.path.basename(project_folder)
# ./weigeltb/Project_05851_I_p1,Project_04495_si, ./rosenbj1/Project_06179_s1, ./rudinc/Project_06437_F_p1_Zp
# ./bergerm1/Project_05500_AF_Im
if folder_name.count("_") >= 3 or folder_name.find("_s") > 0:
continue
if folder_name.startswith("Project_"): # ignore non Project_ folders
request = folder_name.replace("Project_", "")
set_request_acls(request, lab_name)
if args.startswith("ARCHIVEDWITHINLAST="):
minutes = args[19:]
if int(minutes) > 70000:
print("This request would take too long, exiting.")
return
getArchivedProjectsURL = NGS_STATS_ENDPOINT_RECENT + minutes
print("Sending request {}".format(getArchivedProjectsURL))
archivedProjects = requests.get(getArchivedProjectsURL).json()
for project in archivedProjects:
set_request_acls(project, '')
if __name__ == '__main__':
main()