Skip to content

refactor: solving lint issue for background_task manager #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: ui-improvements
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 54 additions & 19 deletions Access/background_task_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
""" This file contains backgroud task managers functions"""

import json
import threading
import traceback
Expand All @@ -20,11 +22,14 @@


def background_task(func, *args):
"""
func: The function that needs to be executed as a background task
"""
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":
if func == "test_grant":
test_grant.delay(*args)
elif func == "run_accept_request":
run_accept_request.delay(*args)
Expand All @@ -34,17 +39,17 @@ def background_task(func, *args):
else:
if func == "run_access_grant":
request_id = args[0]
accessAcceptThread = threading.Thread(
access_accept_thread = threading.Thread(
target=run_access_grant,
args=(request_id,),
)
accessAcceptThread.start()
access_accept_thread.start()
elif func == "run_accept_request":
accessAcceptThread = threading.Thread(
access_accept_thread = threading.Thread(
target=run_accept_request,
args=args,
)
accessAcceptThread.start()
access_accept_thread.start()
elif func == "run_access_revoke":
access_revoke_thread = threading.Thread(target=run_access_revoke, args=args)

Expand All @@ -55,6 +60,9 @@ def background_task(func, *args):
autoretry_for=(Exception,), retry_kwargs={"max_retries": 3, "countdown": 5}
)
def run_access_grant(request_id):
"""
request_id: a unique identifier for a specific access request.
"""
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
Expand All @@ -72,7 +80,10 @@ def run_access_grant(request_id):
}
)
return False
elif user_access_mapping.user_identity.identity == {} and access_module.get_identity_template() != "":
if (
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"
)
Expand Down Expand Up @@ -101,7 +112,7 @@ def run_access_grant(request_id):
request=user_access_mapping,
is_group=False,
)
if type(response) is bool:
if isinstance(response, bool):
approve_success = response
else:
approve_success = response[0]
Expand Down Expand Up @@ -162,11 +173,14 @@ def run_access_grant(request_id):
autoretry_for=(Exception,), retry_kwargs={"max_retries": 3, "countdown": 5}
)
def run_access_revoke(request_id):
"""
request_id: a unique identifier for a specific access request.
"""
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":
if access_mapping.status == "Revoked":
logger.debug(f"The request with id {request_id} is already revoked.")
return True
access = access_mapping.access
Expand All @@ -184,20 +198,19 @@ def run_access_revoke(request_id):
response = access_module.revoke(
user_identity.user, user_identity, access.access_label, access_mapping
)
if type(response) is bool:
if isinstance(response, bool):
revoke_success = response
message = None
else:
revoke_success = response[0]
message = str(response[1])
except Exception as e:
except Exception:
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(
Expand All @@ -209,31 +222,33 @@ def run_access_revoke(request_id):
}
)
else:
access_mapping.revoke_failed(
fail_reason="Error while running revoke in module"
)
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
"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_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)}")
except Exception as exc:
logger.debug(
f"Failed to send Revoke failed mail due to exception: {str(exc)}"
)
raise Exception("Failed to revoke the access due to: " + str(message))

return True
Expand All @@ -254,17 +269,26 @@ def task_failure(sender=None, **kwargs):


def success_func():
"""
Displays success log.
"""
logger.info("task successful")


def fail_func():
"""
Displays failed log.
"""
logger.info("task failed")


@shared_task(
autoretry_for=(Exception,), retry_kwargs={"max_retries": 3, "countdown": 5}
)
def test_grant():
"""
The function returns the access desc of the tags from confluence module"
"""
access_module = helpers.get_available_access_module_from_tag("confluence_module")

# call access_desc method of confluence module here
Expand All @@ -276,6 +300,9 @@ def test_grant():
autoretry_for=(Exception,), retry_kwargs={"max_retries": 3, "countdown": 5}
)
def run_accept_request(data):
"""
data: details of the user.
"""
data = json.loads(data)
request_id = data["request_id"]
user_access_mapping = UserAccessMapping.get_access_request(data["request_id"])
Expand Down Expand Up @@ -304,6 +331,9 @@ def run_accept_request(data):


def accept_request(user_access_mapping):
"""
user_access_mapping (dict): maps users to their access levels.
"""
result = None
try:
result = run_access_grant.delay(user_access_mapping.request_id)
Expand All @@ -316,8 +346,13 @@ def accept_request(user_access_mapping):


def revoke_request(user_access_mapping, revoker=None):
"""
user_access_mapping (dict): maps users to their access levels.

revoker: The revoker parameter is an optional argument that represents the user who is
revoking the access request.
"""
result = None
# change the status to revoke processing
user_access_mapping.revoking(revoker)
try:
result = run_access_revoke.delay(user_access_mapping.request_id)
Expand Down