Skip to content

Commit aed091b

Browse files
committed
refactor: enhance notification manager retrieval and update alert task signature
1 parent 6273fc7 commit aed091b

3 files changed

Lines changed: 33 additions & 24 deletions

File tree

dojo/celery_dispatch.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,9 @@ def dojo_dispatch_task(task_or_sig: _SupportsSi | _SupportsApplyAsync | Signatur
8787
sig_kwargs.pop("sync", None)
8888
sig = sig.clone(kwargs=sig_kwargs)
8989
eager = sig.apply()
90-
return eager.get(propagate=True)
90+
try:
91+
return eager.get(propagate=True)
92+
except RuntimeError:
93+
# Since we are intentionally running synchronously, we can propagate exceptions directly, and enable sync subtasks
94+
# If the requests desires this. Celery docs explain that this is a rare use case, but we support it _just in case_
95+
return eager.get(propagate=True, disable_sync_subtasks=False)

dojo/notifications/helper.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@
4646
labels = get_labels()
4747

4848

49+
def get_manager_class_instance():
50+
default_manager = NotificationManager
51+
notification_manager_class = default_manager
52+
if isinstance(
53+
(
54+
notification_manager := getattr(
55+
settings,
56+
"NOTIFICATION_MANAGER",
57+
default_manager,
58+
)
59+
),
60+
str,
61+
):
62+
with suppress(ModuleNotFoundError):
63+
module_name, _separator, class_name = notification_manager.rpartition(".")
64+
module = importlib.import_module(module_name)
65+
notification_manager_class = getattr(module, class_name)
66+
return notification_manager_class()
67+
68+
4969
def create_notification(
5070
event: str | None = None,
5171
title: str | None = None,
@@ -63,23 +83,7 @@ def create_notification(
6383
**kwargs: dict,
6484
) -> None:
6585
"""Create an instance of a NotificationManager and dispatch the notification."""
66-
default_manager = NotificationManager
67-
notification_manager_class = default_manager
68-
if isinstance(
69-
(
70-
notification_manager := getattr(
71-
settings,
72-
"NOTIFICATION_MANAGER",
73-
default_manager,
74-
)
75-
),
76-
str,
77-
):
78-
with suppress(ModuleNotFoundError):
79-
module_name, _separator, class_name = notification_manager.rpartition(".")
80-
module = importlib.import_module(module_name)
81-
notification_manager_class = getattr(module, class_name)
82-
notification_manager_class().create_notification(
86+
get_manager_class_instance().create_notification(
8387
event=event,
8488
title=title,
8589
finding=finding,
@@ -870,30 +874,30 @@ def _process_notifications(
870874
@app.task
871875
def send_slack_notification(event: str, user_id: int | None = None, **kwargs: dict) -> None:
872876
user = Dojo_User.objects.get(pk=user_id) if user_id else None
873-
SlackNotificationManger().send_slack_notification(event, user=user, **kwargs)
877+
get_manager_class_instance()._get_manager_instance("slack").send_slack_notification(event, user=user, **kwargs)
874878

875879

876880
@app.task
877881
def send_msteams_notification(event: str, user_id: int | None = None, **kwargs: dict) -> None:
878882
user = Dojo_User.objects.get(pk=user_id) if user_id else None
879-
MSTeamsNotificationManger().send_msteams_notification(event, user=user, **kwargs)
883+
get_manager_class_instance()._get_manager_instance("msteams").send_msteams_notification(event, user=user, **kwargs)
880884

881885

882886
@app.task
883887
def send_mail_notification(event: str, user_id: int | None = None, **kwargs: dict) -> None:
884888
user = Dojo_User.objects.get(pk=user_id) if user_id else None
885-
EmailNotificationManger().send_mail_notification(event, user=user, **kwargs)
889+
get_manager_class_instance()._get_manager_instance("mail").send_mail_notification(event, user=user, **kwargs)
886890

887891

888892
@app.task
889893
def send_webhooks_notification(event: str, user_id: int | None = None, **kwargs: dict) -> None:
890894
user = Dojo_User.objects.get(pk=user_id) if user_id else None
891-
WebhookNotificationManger().send_webhooks_notification(event, user=user, **kwargs)
895+
get_manager_class_instance()._get_manager_instance("webhooks").send_webhooks_notification(event, user=user, **kwargs)
892896

893897

894898
@app.task(ignore_result=True)
895899
def webhook_reactivation(endpoint_id: int, **_kwargs: dict) -> None:
896-
WebhookNotificationManger()._webhook_reactivation(endpoint_id=endpoint_id)
900+
get_manager_class_instance()._get_manager_instance("webhooks")._webhook_reactivation(endpoint_id=endpoint_id)
897901

898902

899903
@app.task(ignore_result=True)

dojo/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def log_generic_alert(source, title, description):
3131

3232

3333
@app.task(bind=True)
34-
def add_alerts(self, runinterval):
34+
def add_alerts(self, runinterval, *args, **kwargs):
3535
now = timezone.now()
3636

3737
upcoming_engagements = Engagement.objects.filter(target_start__gt=now + timedelta(days=3), target_start__lt=now + timedelta(days=3) + runinterval).order_by("target_start")

0 commit comments

Comments
 (0)