From fc5656e7c1566c7020ef6f304876c8590f01c339 Mon Sep 17 00:00:00 2001 From: Simon Seyock Date: Mon, 28 Mar 2022 15:48:14 +0200 Subject: [PATCH] feat: add keycloak events for role changes --- .../lib/controller/WebhookController.java | 95 +++++++++++-------- .../shogun/lib/event/KeycloakEventType.java | 8 +- 2 files changed, 58 insertions(+), 45 deletions(-) diff --git a/shogun-lib/src/main/java/de/terrestris/shogun/lib/controller/WebhookController.java b/shogun-lib/src/main/java/de/terrestris/shogun/lib/controller/WebhookController.java index dfdf092c8..c17b1b5a3 100644 --- a/shogun-lib/src/main/java/de/terrestris/shogun/lib/controller/WebhookController.java +++ b/shogun-lib/src/main/java/de/terrestris/shogun/lib/controller/WebhookController.java @@ -28,8 +28,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.Set; - @Log4j2 @RestController @RequestMapping("/webhooks") @@ -39,53 +37,66 @@ public class WebhookController { @PostMapping(value = "/keycloak") public void handleKeyCloakEvent(@RequestBody KeycloakEventDto event) { - Set relevantResourceTypes = Set.of( - "GROUP_MEMBERSHIP", - "GROUP", - "USER" - ); - log.debug("Keycloak webhook called with event: {}", event); String resourceType = event.getResourceType(); String eventType = event.getType(); - if (relevantResourceTypes.contains(resourceType)) { - String resourcePath = event.getResourcePath(); - if (StringUtils.isNotEmpty(resourcePath)) { - String[] split = resourcePath.split("/"); - if (StringUtils.equals(resourceType, "GROUP_MEMBERSHIP")) { + + String resourcePath = event.getResourcePath(); + if (StringUtils.isEmpty(resourcePath)) { + return; + } + + String[] split = resourcePath.split("/"); + + switch (resourceType) { + case "GROUP_MEMBERSHIP" -> applicationEventPublisher.publishEvent(new KeycloakEvent( + this, + KeycloakEventType.USER_GROUP_MEMBERSHIP_CHANGED, + split[1] + )); + case "USER" -> { + if (StringUtils.equals(eventType, "CREATE")) { + applicationEventPublisher.publishEvent(new KeycloakEvent( + this, + KeycloakEventType.USER_CREATED, + split[1] + )); + } else if (StringUtils.equals(eventType, "DELETE")) { + applicationEventPublisher.publishEvent(new KeycloakEvent( + this, + KeycloakEventType.USER_DELETED, + split[1] + )); + } + } + case "GROUP" -> { + if (StringUtils.equals(eventType, "CREATE")) { + applicationEventPublisher.publishEvent(new KeycloakEvent( + this, + KeycloakEventType.GROUP_CREATED, + split[1] + )); + } else if (StringUtils.equals(eventType, "DELETE")) { + applicationEventPublisher.publishEvent(new KeycloakEvent( + this, + KeycloakEventType.GROUP_DELETED, + split[1] + )); + } + } + case "REALM_ROLE_MAPPING", "CLIENT_ROLE_MAPPING" -> { + if (split[0].equals("users")) { + applicationEventPublisher.publishEvent(new KeycloakEvent( + this, + KeycloakEventType.USER_ROLES_CHANGED, + split[1] + )); + } else if (split[0].equals("groups")) { applicationEventPublisher.publishEvent(new KeycloakEvent( this, - KeycloakEventType.USER_GROUP_MEMBERSHIP_CHANGED, + KeycloakEventType.GROUP_ROLES_CHANGED, split[1] )); - } else if (StringUtils.equals(resourceType, "USER")) { - if (StringUtils.equals(eventType, "CREATE")) { - applicationEventPublisher.publishEvent(new KeycloakEvent( - this, - KeycloakEventType.USER_CREATED, - split[1] - )); - } else if (StringUtils.equals(eventType, "DELETE")) { - applicationEventPublisher.publishEvent(new KeycloakEvent( - this, - KeycloakEventType.USER_DELETED, - split[1] - )); - } - } else if (StringUtils.equals(resourceType, "GROUP")) { - if (StringUtils.equals(eventType, "CREATE")) { - applicationEventPublisher.publishEvent(new KeycloakEvent( - this, - KeycloakEventType.GROUP_CREATED, - split[1] - )); - } else if (StringUtils.equals(eventType, "DELETE")) { - applicationEventPublisher.publishEvent(new KeycloakEvent( - this, - KeycloakEventType.GROUP_DELETED, - split[1] - )); - } } } } diff --git a/shogun-lib/src/main/java/de/terrestris/shogun/lib/event/KeycloakEventType.java b/shogun-lib/src/main/java/de/terrestris/shogun/lib/event/KeycloakEventType.java index 4517fd7ee..7a02bd873 100644 --- a/shogun-lib/src/main/java/de/terrestris/shogun/lib/event/KeycloakEventType.java +++ b/shogun-lib/src/main/java/de/terrestris/shogun/lib/event/KeycloakEventType.java @@ -18,9 +18,11 @@ package de.terrestris.shogun.lib.event; public enum KeycloakEventType { + USER_CREATED, + USER_ROLES_CHANGED, + USER_DELETED, + USER_GROUP_MEMBERSHIP_CHANGED, GROUP_CREATED, GROUP_DELETED, - USER_GROUP_MEMBERSHIP_CHANGED, - USER_CREATED, - USER_DELETED + GROUP_ROLES_CHANGED }