Skip to content

Commit 0f15338

Browse files
export_jobs: check job ACL has an owner -if no owner, the job is marked as error
1 parent 0584869 commit 0f15338

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

dbclient/JobsClient.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import logging
34
import logging_utils
@@ -99,14 +100,19 @@ def log_job_configs(self, users_list=None, groups_list = None, log_file='jobs.lo
99100
jobs_log = self.get_export_dir() + log_file
100101
acl_jobs_log = self.get_export_dir() + acl_file
101102
error_logger = logging_utils.get_error_logger(wmconstants.WM_EXPORT, wmconstants.JOB_OBJECT, self.get_export_dir())
103+
failed_log_file = logging_utils.get_error_log_file(
104+
wmconstants.WM_EXPORT, wmconstants.JOB_OBJECT, self.get_export_dir()
105+
)
102106
# pinned by cluster_user is a flag per cluster
103107
jl_full = self.get_jobs_list(False)
104108
if users_list:
105109
# filter the jobs list to only contain users that exist within this list
106110
jl = list(filter(lambda x: x.get('creator_user_name', '') in users_list, jl_full))
107111
else:
108112
jl = jl_full
109-
with open(jobs_log, "w") as log_fp, open(acl_jobs_log, 'w') as acl_fp:
113+
with open(jobs_log, "w") as log_fp, \
114+
open(acl_jobs_log, 'w') as acl_fp, \
115+
open(failed_log_file, "w") as failed_log_fp:
110116
for x in jl:
111117
job_id = x['job_id']
112118
new_job_name = x['settings']['name'] + ':::' + str(job_id)
@@ -116,11 +122,30 @@ def log_job_configs(self, users_list=None, groups_list = None, log_file='jobs.lo
116122
job_settings['name'] = new_job_name
117123
# reset the original struct with the new settings
118124
x['settings'] = job_settings
119-
log_fp.write(json.dumps(x) + '\n')
125+
126+
# get ACLs and check that the job has one owner before writing
120127
job_perms = self.get(f'/preview/permissions/jobs/{job_id}')
121128
if not logging_utils.log_response_error(error_logger, job_perms):
122-
job_perms['job_name'] = new_job_name
123-
acl_fp.write(json.dumps(job_perms) + '\n')
129+
valid_acl = False
130+
acls = job_perms.get("access_control_list")
131+
if acls:
132+
for acl in acls:
133+
for permission in acl.get("all_permissions"):
134+
if permission.get("permission_level") == "IS_OWNER":
135+
valid_acl = True
136+
# TODO remove
137+
# if acl.get("user_name") == "[email protected]":
138+
# valid_acl = False
139+
if valid_acl:
140+
# job and job_acl are fine, writing both to the output files
141+
log_fp.write(json.dumps(x) + '\n')
142+
143+
job_perms['job_name'] = new_job_name
144+
acl_fp.write(json.dumps(job_perms) + '\n')
145+
else:
146+
# job_acl is malformed, the job is written to error output file
147+
logging.error(f"The following job id {job_id} has malformed permissions: {json.dumps(job_perms)}")
148+
failed_log_fp.write(json.dumps(x) + '\n')
124149

125150
def import_job_configs(self, log_file='jobs.log', acl_file='acl_jobs.log', job_map_file='job_id_map.log'):
126151
jobs_log = self.get_export_dir() + log_file

0 commit comments

Comments
 (0)