events: add event context to slack webhook
This commit is contained in:
parent
eef111bcfd
commit
42c6401ba7
|
@ -139,9 +139,7 @@ class Event(models.Model):
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if not self._state.adding:
|
if not self._state.adding:
|
||||||
raise ValidationError(
|
raise ValidationError("you may not edit an existing Event")
|
||||||
"you may not edit an existing %s" % self._meta.model_name
|
|
||||||
)
|
|
||||||
LOGGER.debug(
|
LOGGER.debug(
|
||||||
"Created Event",
|
"Created Event",
|
||||||
action=self.action,
|
action=self.action,
|
||||||
|
@ -209,7 +207,7 @@ class NotificationTransport(models.Model):
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
except RequestException as exc:
|
except RequestException as exc:
|
||||||
raise NotificationTransportError from exc
|
raise NotificationTransportError(exc.response.text) from exc
|
||||||
return [
|
return [
|
||||||
response.status_code,
|
response.status_code,
|
||||||
response.text,
|
response.text,
|
||||||
|
@ -217,17 +215,7 @@ class NotificationTransport(models.Model):
|
||||||
|
|
||||||
def send_webhook_slack(self, notification: "Notification") -> list[str]:
|
def send_webhook_slack(self, notification: "Notification") -> list[str]:
|
||||||
"""Send notification to slack or slack-compatible endpoints"""
|
"""Send notification to slack or slack-compatible endpoints"""
|
||||||
body = {
|
fields = [
|
||||||
"username": "authentik",
|
|
||||||
"icon_url": "https://goauthentik.io/img/icon.png",
|
|
||||||
"attachments": [
|
|
||||||
{
|
|
||||||
"author_name": "authentik",
|
|
||||||
"author_link": "https://goauthentik.io",
|
|
||||||
"author_icon": "https://goauthentik.io/img/icon.png",
|
|
||||||
"title": notification.body,
|
|
||||||
"color": "#fd4b2d",
|
|
||||||
"fields": [
|
|
||||||
{
|
{
|
||||||
"title": _("Severity"),
|
"title": _("Severity"),
|
||||||
"value": notification.severity,
|
"value": notification.severity,
|
||||||
|
@ -238,7 +226,26 @@ class NotificationTransport(models.Model):
|
||||||
"value": str(notification.user),
|
"value": str(notification.user),
|
||||||
"short": True,
|
"short": True,
|
||||||
},
|
},
|
||||||
],
|
]
|
||||||
|
if notification.event:
|
||||||
|
for key, value in notification.event.context.items():
|
||||||
|
if not isinstance(value, str):
|
||||||
|
continue
|
||||||
|
# https://birdie0.github.io/discord-webhooks-guide/other/field_limits.html
|
||||||
|
if len(fields) >= 25:
|
||||||
|
continue
|
||||||
|
fields.append({"title": key[:256], "value": value[:1024]})
|
||||||
|
body = {
|
||||||
|
"username": "authentik",
|
||||||
|
"icon_url": "https://goauthentik.io/img/icon.png",
|
||||||
|
"attachments": [
|
||||||
|
{
|
||||||
|
"author_name": "authentik",
|
||||||
|
"author_link": "https://goauthentik.io",
|
||||||
|
"author_icon": "https://goauthentik.io/img/icon.png",
|
||||||
|
"title": notification.body,
|
||||||
|
"color": "#fd4b2d",
|
||||||
|
"fields": fields,
|
||||||
"footer": f"authentik v{__version__}",
|
"footer": f"authentik v{__version__}",
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -250,7 +257,7 @@ class NotificationTransport(models.Model):
|
||||||
response = post(self.webhook_url, json=body)
|
response = post(self.webhook_url, json=body)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
except RequestException as exc:
|
except RequestException as exc:
|
||||||
raise NotificationTransportError from exc
|
raise NotificationTransportError(exc.response.text) from exc
|
||||||
return [
|
return [
|
||||||
response.status_code,
|
response.status_code,
|
||||||
response.text,
|
response.text,
|
||||||
|
|
Reference in New Issue