Skip to content
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

events: include event user in webhook notification #5524

Merged
merged 2 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
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
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