-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbackground_task_manager.py
329 lines (285 loc) · 10.1 KB
/
background_task_manager.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
317
318
319
320
321
322
323
324
325
326
327
328
329
import json
import threading
import traceback
import logging
from celery import shared_task
from celery.signals import task_success, task_failure
from Access import helpers
from Access.models import UserAccessMapping
from Access import notifications
logger = logging.getLogger(__name__)
with open("config.json") as data_file:
background_task_manager_type = json.load(data_file)["background_task_manager"][
"type"
]
def background_task(func, *args):
if background_task_manager_type == "celery":
if func == "run_access_grant":
request_id = args[0]
run_access_grant.delay(request_id)
elif func == "test_grant":
test_grant.delay(*args)
elif func == "run_accept_request":
run_accept_request.delay(*args)
elif func == "run_access_revoke":
request_id = args[0]
run_access_revoke.delay(request_id)
else:
if func == "run_access_grant":
request_id = args[0]
accessAcceptThread = threading.Thread(
target=run_access_grant,
args=(request_id,),
)
accessAcceptThread.start()
elif func == "run_accept_request":
accessAcceptThread = threading.Thread(
target=run_accept_request,
args=args,
)
accessAcceptThread.start()
elif func == "run_access_revoke":
access_revoke_thread = threading.Thread(target=run_access_revoke, args=args)
access_revoke_thread.start()
@shared_task(
autoretry_for=(Exception,), retry_kwargs={"max_retries": 3, "countdown": 5}
)
def run_access_grant(request_id):
user_access_mapping = UserAccessMapping.get_access_request(request_id=request_id)
access_tag = user_access_mapping.access.access_tag
user = user_access_mapping.user_identity.user
approver = user_access_mapping.approver_1.user
message = ""
access_module = helpers.get_available_access_module_from_tag(access_tag)
if not user_access_mapping.user_identity.user.is_active():
user_access_mapping.decline_access(decline_reason="User is not active")
logger.debug(
{
"requestId": request_id,
"status": "Declined",
"by": approver.username,
"response": message,
}
)
return False
elif user_access_mapping.user_identity.identity == {} and access_module.get_identity_template() != "":
user_access_mapping.grant_fail_access(
fail_reason="Failed since identity is blank for user identity"
)
notifications.send_mail_for_request_granted_failure(
user, approver, access_tag, request_id
)
logger.debug(
{
"requestId": request_id,
"status": "GrantFailed",
"by": approver,
"response": message,
}
)
return False
if not access_module:
return False
try:
response = access_module.approve(
user_identity=user_access_mapping.user_identity,
labels=[user_access_mapping.access.access_label],
approver=approver,
request=user_access_mapping,
is_group=False,
)
if type(response) is bool:
approve_success = response
else:
approve_success = response[0]
message = str(response[1])
except Exception:
logger.exception(
"Error while running approval module: " + str(traceback.format_exc())
)
approve_success = False
message = str(traceback.format_exc())
if approve_success:
user_access_mapping.approve_access()
logger.debug(
{
"requestId": request_id,
"status": "Approved",
"by": approver,
"response": message,
}
)
else:
user_access_mapping.grant_fail_access(
fail_reason="Error while running approve in module"
)
logger.debug(
{
"requestId": request_id,
"status": "GrantFailed",
"by": approver,
"response": message,
}
)
try:
destination = access_module.access_mark_revoke_permission(access_tag)
notifications.send_mail_for_access_grant_failed(
destination,
access_tag.upper(),
user.email,
request_id=request_id,
message=message,
)
logger.debug("Sending Grant Failed email - " + str(destination))
except Exception:
logger.debug(
"Grant Failed - Error while sending email - "
+ request_id
+ "-"
+ str(str(traceback.format_exc()))
)
# For generic modules, approve method will send an email on "Access granted",
# additional email of "Access approved" is not needed
return True
@shared_task(
autoretry_for=(Exception,), retry_kwargs={"max_retries": 3, "countdown": 5}
)
def run_access_revoke(request_id):
access_mapping = UserAccessMapping.get_access_request(request_id=request_id)
if not access_mapping:
logger.debug(f"Cannot find access mapping with id: {request_id}")
return False
elif access_mapping.status == "Revoked":
logger.debug(f"The request with id {request_id} is already revoked.")
return True
access = access_mapping.access
user_identity = access_mapping.user_identity
revoker = access_mapping.revoker
access_modules = helpers.get_available_access_modules()
access_module = access_modules[access.access_tag]
if not revoker:
logger.debug(f"The revoker is not set for the request with id {request_id}")
access_mapping.revoke_failed("Revoker was not set.")
return False
try:
response = access_module.revoke(
user_identity.user, user_identity, access.access_label, access_mapping
)
if type(response) is bool:
revoke_success = response
message = None
else:
revoke_success = response[0]
message = str(response[1])
except Exception as e:
logger.exception(
"Error while running revoke function: " + str(traceback.format_exc())
)
revoke_success = False
message = str(traceback.format_exc())
if revoke_success:
access_mapping.revoke()
logger.debug(
{
"requestId": request_id,
"status": "revoked",
"by": revoker,
"response": message,
}
)
else:
access_mapping.revoke_failed(
fail_reason="Error while running revoke in module"
)
logger.debug(
{
"requestId": request_id,
"status": "RevokeFailed",
"by": revoker,
"response": message,
"retry_count": run_access_revoke.request.retries
}
)
if run_access_revoke.request.retries == 3:
logger.info("Sending the notification for failure")
try:
notifications.send_revoke_failure_mail(
access_module.access_mark_revoke_permission(access_mapping.access_type),
access_mapping.request_id,
revoker.email,
run_access_revoke.request.retries,
message,
access.access_tag,
)
except Exception as e:
logger.debug(f"Failed to send Revoke failed mail due to exception: {str(e)}")
raise Exception("Failed to revoke the access due to: " + str(message))
return True
@task_success.connect(sender=run_access_grant)
def task_success(sender=None, **kwargs):
success_func()
logger.info("you are in task success middleman")
return
@task_failure.connect(sender=run_access_grant)
def task_failure(sender=None, **kwargs):
fail_func()
logger.info("you are in task fail middleman")
return
def success_func():
logger.info("task successful")
def fail_func():
logger.info("task failed")
@shared_task(
autoretry_for=(Exception,), retry_kwargs={"max_retries": 3, "countdown": 5}
)
def test_grant():
access_module = helpers.get_available_access_module_from_tag("confluence_module")
# call access_desc method of confluence module here
# and return the result to the caller of this function
return access_module.access_desc()
@shared_task(
autoretry_for=(Exception,), retry_kwargs={"max_retries": 3, "countdown": 5}
)
def run_accept_request(data):
data = json.loads(data)
request_id = data["request_id"]
user_access_mapping = UserAccessMapping.get_access_request(data["request_id"])
approver = user_access_mapping.approver_1.user
user = user_access_mapping.user_identity.user
access_type = data["access_type"]
response = ""
result = background_task("run_access_grant", request_id)
if result:
return {"status": True}
notifications.send_mail_for_request_granted_failure(
user, approver, access_type, request_id
)
logger.debug(
{
"requestId": request_id,
"status": "GrantFailed",
"By": approver,
"response": str(response),
}
)
return {"status": False}
def accept_request(user_access_mapping):
result = None
try:
result = run_access_grant.delay(user_access_mapping.request_id)
except Exception:
user_access_mapping.grant_fail_access(fail_reason="Task could not be queued")
if result:
return True
return False
def revoke_request(user_access_mapping, revoker=None):
result = None
# change the status to revoke processing
user_access_mapping.revoking(revoker)
try:
result = run_access_revoke.delay(user_access_mapping.request_id)
except Exception:
user_access_mapping.RevokeFailed(fail_reason="Task could not be queued")
if result:
return True
return False