|
7 | 7 | from celery.signals import task_success, task_failure
|
8 | 8 |
|
9 | 9 | from Access import helpers
|
10 |
| -from bootprocess import general |
11 |
| -from EnigmaAutomation.settings import AUTOMATED_EXEC_IDENTIFIER |
12 |
| -from Access.models import UserAccessMapping, ApprovalType |
| 10 | +from Access.models import UserAccessMapping |
13 | 11 | from Access import notifications
|
14 | 12 |
|
15 | 13 | logger = logging.getLogger(__name__)
|
@@ -166,70 +164,76 @@ def run_access_grant(request_id):
|
166 | 164 | def run_access_revoke(request_id):
|
167 | 165 | access_mapping = UserAccessMapping.get_access_request(request_id=request_id)
|
168 | 166 | if not access_mapping:
|
169 |
| - # TODO: Have to add the email targets for failure |
170 |
| - targets = [] |
171 |
| - message = "Request not found" |
172 |
| - notifications.send_revoke_failure_mail( |
173 |
| - targets, request_id, access_mapping.revoker.email, 0, message |
174 |
| - ) |
| 167 | + logger.debug(f"Cannot find access mapping with id: {request_id}") |
175 | 168 | return False
|
176 | 169 | elif access_mapping.status == "Revoked":
|
| 170 | + logger.debug(f"The request with id {request_id} is already revoked.") |
177 | 171 | return True
|
178 | 172 | access = access_mapping.access
|
179 | 173 | user_identity = access_mapping.user_identity
|
180 | 174 |
|
181 | 175 | revoker = access_mapping.revoker
|
182 |
| - if not revoker: |
183 |
| - # TODO: Have to add the email targets for failure |
184 |
| - targets = [] |
185 |
| - message = "Revoker not found" |
186 |
| - notifications.send_revoke_failure_mail( |
187 |
| - targets, |
188 |
| - request_id, |
189 |
| - access_mapping.revoker.email, |
190 |
| - 0, |
191 |
| - message, |
192 |
| - access.access_tag, |
193 |
| - ) |
194 |
| - user_identity.mark_revoke_failed_for_approved_access_mapping(access) |
195 |
| - return False |
196 |
| - |
197 | 176 | access_modules = helpers.get_available_access_modules()
|
198 |
| - |
199 | 177 | access_module = access_modules[access.access_tag]
|
| 178 | + if not revoker: |
| 179 | + logger.debug(f"The revoker is not set for the request with id {request_id}") |
| 180 | + access_mapping.revoke_failed("Revoker was not set.") |
| 181 | + return False |
200 | 182 |
|
201 |
| - response = access_module.revoke( |
202 |
| - user_identity.user, user_identity, access.access_label, access_mapping |
203 |
| - ) |
204 |
| - logger.debug("Response from the revoke function: " + str(response)) |
205 |
| - if type(response) is bool: |
206 |
| - revoke_success = response |
207 |
| - message = None |
208 |
| - else: |
209 |
| - revoke_success = response[0] |
210 |
| - message = str(response[1]) |
| 183 | + try: |
| 184 | + response = access_module.revoke( |
| 185 | + user_identity.user, user_identity, access.access_label, access_mapping |
| 186 | + ) |
| 187 | + if type(response) is bool: |
| 188 | + revoke_success = response |
| 189 | + message = None |
| 190 | + else: |
| 191 | + revoke_success = response[0] |
| 192 | + message = str(response[1]) |
| 193 | + except Exception as e: |
| 194 | + logger.exception( |
| 195 | + "Error while running revoke function: " + str(traceback.format_exc()) |
| 196 | + ) |
| 197 | + revoke_success = False |
| 198 | + message = str(traceback.format_exc()) |
| 199 | + |
211 | 200 |
|
212 | 201 | if revoke_success:
|
213 |
| - if AUTOMATED_EXEC_IDENTIFIER in access_module.revoke_owner(): |
214 |
| - user_identity.revoke_approved_access_mapping(access) |
| 202 | + access_mapping.revoke() |
| 203 | + logger.debug( |
| 204 | + { |
| 205 | + "requestId": request_id, |
| 206 | + "status": "revoked", |
| 207 | + "by": revoker, |
| 208 | + "response": message, |
| 209 | + } |
| 210 | + ) |
215 | 211 | else:
|
| 212 | + access_mapping.revoke_failed( |
| 213 | + fail_reason="Error while running revoke in module" |
| 214 | + ) |
216 | 215 | logger.debug(
|
217 |
| - "Failed to revoke the request: {} due to exception: {}".format( |
218 |
| - access_mapping.request_id, message |
219 |
| - ) |
| 216 | + { |
| 217 | + "requestId": request_id, |
| 218 | + "status": "RevokeFailed", |
| 219 | + "by": revoker, |
| 220 | + "response": message, |
| 221 | + "retry_count": run_access_revoke.request.retries |
| 222 | + } |
220 | 223 | )
|
221 |
| - logger.debug("Retry count: {}".format(run_access_revoke.request.retries)) |
222 | 224 | if run_access_revoke.request.retries == 3:
|
223 | 225 | logger.info("Sending the notification for failure")
|
224 |
| - notifications.send_revoke_failure_mail( |
225 |
| - access_module.access_mark_revoke_permission(access_mapping.access_type), |
226 |
| - access_mapping.request_id, |
227 |
| - revoker.email, |
228 |
| - run_access_revoke.request.retries, |
229 |
| - message, |
230 |
| - access.access_tag, |
231 |
| - ) |
232 |
| - user_identity.mark_revoke_failed_for_approved_access_mapping(access) |
| 226 | + try: |
| 227 | + notifications.send_revoke_failure_mail( |
| 228 | + access_module.access_mark_revoke_permission(access_mapping.access_type), |
| 229 | + access_mapping.request_id, |
| 230 | + revoker.email, |
| 231 | + run_access_revoke.request.retries, |
| 232 | + message, |
| 233 | + access.access_tag, |
| 234 | + ) |
| 235 | + except Exception as e: |
| 236 | + logger.debug(f"Failed to send Revoke failed mail due to exception: {str(e)}") |
233 | 237 | raise Exception("Failed to revoke the access due to: " + str(message))
|
234 | 238 |
|
235 | 239 | return True
|
|
0 commit comments