rework event trigger tasks
Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
This commit is contained in:
@ -535,7 +535,7 @@ class Notification(SerializerModel):
|
|||||||
verbose_name_plural = _("Notifications")
|
verbose_name_plural = _("Notifications")
|
||||||
|
|
||||||
|
|
||||||
class NotificationRule(SerializerModel, PolicyBindingModel):
|
class NotificationRule(TasksModel, SerializerModel, PolicyBindingModel):
|
||||||
"""Decide when to create a Notification based on policies attached to this object."""
|
"""Decide when to create a Notification based on policies attached to this object."""
|
||||||
|
|
||||||
name = models.TextField(unique=True)
|
name = models.TextField(unique=True)
|
||||||
|
|||||||
@ -111,10 +111,9 @@ def on_password_changed(sender, user: User, password: str, request: HttpRequest
|
|||||||
@receiver(post_save, sender=Event)
|
@receiver(post_save, sender=Event)
|
||||||
def event_post_save_notification(sender, instance: Event, **_):
|
def event_post_save_notification(sender, instance: Event, **_):
|
||||||
"""Start task to check if any policies trigger an notification on this event"""
|
"""Start task to check if any policies trigger an notification on this event"""
|
||||||
from authentik.events.tasks import event_trigger_handler
|
from authentik.events.tasks import event_trigger_dispatch
|
||||||
|
|
||||||
for trigger in NotificationRule.objects.all():
|
event_trigger_dispatch.send(instance.event_uuid)
|
||||||
event_trigger_handler.send(instance.event_uuid, trigger.name)
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(pre_delete, sender=User)
|
@receiver(pre_delete, sender=User)
|
||||||
|
|||||||
@ -23,13 +23,27 @@ from authentik.tasks.models import Task
|
|||||||
LOGGER = get_logger()
|
LOGGER = get_logger()
|
||||||
|
|
||||||
|
|
||||||
@actor(description=_("Check if policies attached to NotificationRule match event."))
|
@actor(description=_("Dispatch new event notifications."))
|
||||||
|
def event_trigger_dispatch(event_uuid: UUID):
|
||||||
|
for trigger in NotificationRule.objects.all():
|
||||||
|
event_trigger_handler.send_with_options(args=(event_uuid, trigger.name), rel_obj=trigger)
|
||||||
|
|
||||||
|
|
||||||
|
@actor(
|
||||||
|
description=_(
|
||||||
|
"Check if policies attached to NotificationRule match event "
|
||||||
|
"and dispatch notification tasks."
|
||||||
|
)
|
||||||
|
)
|
||||||
def event_trigger_handler(event_uuid: UUID, trigger_name: str):
|
def event_trigger_handler(event_uuid: UUID, trigger_name: str):
|
||||||
"""Check if policies attached to NotificationRule match event"""
|
"""Check if policies attached to NotificationRule match event"""
|
||||||
|
self: Task = CurrentTask.get_task()
|
||||||
|
|
||||||
event: Event = Event.objects.filter(event_uuid=event_uuid).first()
|
event: Event = Event.objects.filter(event_uuid=event_uuid).first()
|
||||||
if not event:
|
if not event:
|
||||||
LOGGER.warning("event doesn't exist yet or anymore", event_uuid=event_uuid)
|
self.warning("event doesn't exist yet or anymore", event_uuid=event_uuid)
|
||||||
return
|
return
|
||||||
|
|
||||||
trigger: NotificationRule | None = NotificationRule.objects.filter(name=trigger_name).first()
|
trigger: NotificationRule | None = NotificationRule.objects.filter(name=trigger_name).first()
|
||||||
if not trigger:
|
if not trigger:
|
||||||
return
|
return
|
||||||
@ -64,9 +78,9 @@ def event_trigger_handler(event_uuid: UUID, trigger_name: str):
|
|||||||
|
|
||||||
LOGGER.debug("e(trigger): event trigger matched", trigger=trigger)
|
LOGGER.debug("e(trigger): event trigger matched", trigger=trigger)
|
||||||
# Create the notification objects
|
# Create the notification objects
|
||||||
|
count = 0
|
||||||
for transport in trigger.transports.all():
|
for transport in trigger.transports.all():
|
||||||
for user in trigger.destination_users(event):
|
for user in trigger.destination_users(event):
|
||||||
LOGGER.debug("created notification")
|
|
||||||
notification_transport.send_with_options(
|
notification_transport.send_with_options(
|
||||||
args=(
|
args=(
|
||||||
transport.pk,
|
transport.pk,
|
||||||
@ -76,8 +90,10 @@ def event_trigger_handler(event_uuid: UUID, trigger_name: str):
|
|||||||
),
|
),
|
||||||
rel_obj=transport,
|
rel_obj=transport,
|
||||||
)
|
)
|
||||||
|
count += 1
|
||||||
if transport.send_once:
|
if transport.send_once:
|
||||||
break
|
break
|
||||||
|
self.info(f"Created {count} notification tasks")
|
||||||
|
|
||||||
|
|
||||||
@actor(description=_("Send notification."))
|
@actor(description=_("Send notification."))
|
||||||
@ -95,7 +111,7 @@ def notification_transport(transport_pk: int, event_pk: str, user_pk: int, trigg
|
|||||||
notification = Notification(
|
notification = Notification(
|
||||||
severity=trigger.severity, body=event.summary, event=event, user=user
|
severity=trigger.severity, body=event.summary, event=event, user=user
|
||||||
)
|
)
|
||||||
transport = NotificationTransport.objects.filter(pk=transport_pk).first()
|
transport: NotificationTransport = NotificationTransport.objects.filter(pk=transport_pk).first()
|
||||||
if not transport:
|
if not transport:
|
||||||
return
|
return
|
||||||
transport.send(notification)
|
transport.send(notification)
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import "@goauthentik/admin/events/RuleForm";
|
import "@goauthentik/admin/events/RuleForm";
|
||||||
import "@goauthentik/admin/policies/BoundPoliciesList";
|
import "@goauthentik/admin/policies/BoundPoliciesList";
|
||||||
import "@goauthentik/admin/rbac/ObjectPermissionModal";
|
import "@goauthentik/admin/rbac/ObjectPermissionModal";
|
||||||
|
import "@goauthentik/admin/system-tasks/TaskList";
|
||||||
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
|
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
|
||||||
import { severityToLabel } from "@goauthentik/common/labels";
|
import { severityToLabel } from "@goauthentik/common/labels";
|
||||||
import "@goauthentik/components/ak-status-label";
|
import "@goauthentik/components/ak-status-label";
|
||||||
@ -18,6 +19,7 @@ import { customElement, property } from "lit/decorators.js";
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
EventsApi,
|
EventsApi,
|
||||||
|
ModelEnum,
|
||||||
NotificationRule,
|
NotificationRule,
|
||||||
RbacPermissionsAssignedByUsersListModelEnum,
|
RbacPermissionsAssignedByUsersListModelEnum,
|
||||||
} from "@goauthentik/api";
|
} from "@goauthentik/api";
|
||||||
@ -124,6 +126,7 @@ export class RuleListPage extends TablePage<NotificationRule> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
renderExpanded(item: NotificationRule): TemplateResult {
|
renderExpanded(item: NotificationRule): TemplateResult {
|
||||||
|
const [appLabel, modelName] = ModelEnum.AuthentikEventsNotificationrule.split(".");
|
||||||
return html` <td role="cell" colspan="4">
|
return html` <td role="cell" colspan="4">
|
||||||
<div class="pf-c-table__expandable-row-content">
|
<div class="pf-c-table__expandable-row-content">
|
||||||
<p>
|
<p>
|
||||||
@ -133,6 +136,22 @@ Bindings to groups/users are checked against the user of the event.`,
|
|||||||
)}
|
)}
|
||||||
</p>
|
</p>
|
||||||
<ak-bound-policies-list .target=${item.pk}> </ak-bound-policies-list>
|
<ak-bound-policies-list .target=${item.pk}> </ak-bound-policies-list>
|
||||||
|
<dl class="pf-c-description-list pf-m-horizontal">
|
||||||
|
<div class="pf-c-description-list__group">
|
||||||
|
<dt class="pf-c-description-list__term">
|
||||||
|
<span class="pf-c-description-list__text">${msg("Tasks")}</span>
|
||||||
|
</dt>
|
||||||
|
<dd class="pf-c-description-list__description">
|
||||||
|
<div class="pf-c-description-list__text">
|
||||||
|
<ak-task-list
|
||||||
|
.relObjAppLabel=${appLabel}
|
||||||
|
.relObjModel=${modelName}
|
||||||
|
.relObjId="${item.pk}"
|
||||||
|
></ak-task-list>
|
||||||
|
</div>
|
||||||
|
</dd>
|
||||||
|
</div>
|
||||||
|
</dl>
|
||||||
</div>
|
</div>
|
||||||
</td>`;
|
</td>`;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user