Skip to content

Commit

Permalink
events: include event user in webhook notification (#5524)
Browse files Browse the repository at this point in the history
* events: include event user in webhook notification

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* update other transports

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

---------

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
  • Loading branch information
BeryJu committed May 8, 2023
1 parent 9bddc9b commit 8215ee1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
19 changes: 18 additions & 1 deletion authentik/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ def send_webhook(self, notification: "Notification") -> list[str]:
"user_email": notification.user.email,
"user_username": notification.user.username,
}
if notification.event and notification.event.user:
default_body["event_user_email"] = notification.event.user.get("email", None)
default_body["event_user_username"] = notification.event.user.get("username", None)
if self.webhook_mapping:
default_body = sanitize_item(
self.webhook_mapping.evaluate(
Expand Down Expand Up @@ -391,6 +394,14 @@ def send_webhook_slack(self, notification: "Notification") -> list[str]:
},
]
if notification.event:
if notification.event.user:
fields.append(
{
"title": _("Event user"),
"value": str(notification.event.user.get("username")),
"short": True,
},
)
for key, value in notification.event.context.items():
if not isinstance(value, str):
continue
Expand Down Expand Up @@ -429,7 +440,13 @@ def send_webhook_slack(self, notification: "Notification") -> list[str]:
def send_email(self, notification: "Notification") -> list[str]:
"""Send notification via global email configuration"""
subject = "authentik Notification: "
key_value = {}
key_value = {
"user_email": notification.user.email,
"user_username": notification.user.username,
}
if notification.event and notification.event.user:
key_value["event_user_email"] = notification.event.user.get("email", None)
key_value["event_user_username"] = notification.event.user.get("username", None)
if notification.event:
subject += notification.event.action
for key, value in notification.event.context.items():
Expand Down
3 changes: 3 additions & 0 deletions authentik/events/tests/test_transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def test_transport_webhook(self):
"severity": "alert",
"user_email": self.user.email,
"user_username": self.user.username,
"event_user_email": self.user.email,
"event_user_username": self.user.username,
},
)

Expand Down Expand Up @@ -107,6 +109,7 @@ def test_transport_webhook_slack(self):
"value": self.user.username,
"short": True,
},
{"short": True, "title": "Event user", "value": self.user.username},
{"title": "foo", "value": "bar,"},
],
"footer": f"authentik {get_full_version()}",
Expand Down
1 change: 0 additions & 1 deletion authentik/providers/oauth2/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class OAuthTestCase(TestCase):
def setUpClass(cls) -> None:
cls.keypair = create_test_cert()
super().setUpClass()
cls.maxDiff = None

def assert_non_none_or_unset(self, container: dict, key: str):
"""Check that a key, if set, is not none"""
Expand Down
1 change: 1 addition & 0 deletions authentik/root/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from authentik.lib.sentry import sentry_init
from tests.e2e.utils import get_docker_tag

# globally set maxDiff to none to show full assert error
TestCase.maxDiff = None


Expand Down
2 changes: 0 additions & 2 deletions tests/e2e/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ def setUp(self):
if IS_CI:
print("::group::authentik Logs", file=stderr)
super().setUp()
# pylint: disable=invalid-name
self.maxDiff = None
self.wait_timeout = 60
self.driver = self._get_driver()
self.driver.implicitly_wait(30)
Expand Down
8 changes: 6 additions & 2 deletions website/docs/events/transports.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ This will send a POST request to the given URL with the following contents:
{
"body": "body of the notification message",
"severity": "severity level as configured in the trigger",
"user_email": "user's email",
"user_username": "user's username"
// User that the notification was created for, i.e. a member of the group selected in the rule
"user_email": "notification user's email",
"user_username": "notification user's username",
// User that created the event
"event_user_email": "event user's email",
"event_user_username": "event user's username"
}
```

Expand Down

0 comments on commit 8215ee1

Please sign in to comment.