From 24d404682eb6d9faa2c257566ee2a9bee1ff5b9e Mon Sep 17 00:00:00 2001 From: Michele Arciprete Date: Fri, 24 Nov 2023 19:12:21 +0100 Subject: [PATCH] Using the keycloak session to put the realm name in the routing key instead if the UUID (#39) --- pom.xml | 14 ++++++------- .../event/provider/RabbitMqConfig.java | 21 +++++++++---------- .../RabbitMqEventListenerProvider.java | 9 +++++--- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/pom.xml b/pom.xml index 8400167..e291ce9 100644 --- a/pom.xml +++ b/pom.xml @@ -101,13 +101,13 @@ - - org.wildfly.plugins - wildfly-maven-plugin - - false - - + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/github/aznamier/keycloak/event/provider/RabbitMqConfig.java b/src/main/java/com/github/aznamier/keycloak/event/provider/RabbitMqConfig.java index 0b7bee9..bbb42a7 100644 --- a/src/main/java/com/github/aznamier/keycloak/event/provider/RabbitMqConfig.java +++ b/src/main/java/com/github/aznamier/keycloak/event/provider/RabbitMqConfig.java @@ -1,17 +1,16 @@ package com.github.aznamier.keycloak.event.provider; -import java.util.Locale; -import java.util.regex.Pattern; import org.jboss.logging.Logger; import org.keycloak.Config.Scope; import org.keycloak.events.Event; import org.keycloak.events.admin.AdminEvent; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import org.keycloak.models.KeycloakSession; import org.keycloak.util.JsonSerialization; +import java.util.Locale; +import java.util.regex.Pattern; + public class RabbitMqConfig { @@ -38,11 +37,11 @@ public class RabbitMqConfig { private String exchange; - public static String calculateRoutingKey(AdminEvent adminEvent) { + public static String calculateRoutingKey(AdminEvent adminEvent, KeycloakSession session) { //KK.EVENT.ADMIN.... String routingKey = ROUTING_KEY_PREFIX + ".ADMIN" - + "." + removeDots(adminEvent.getRealmId()) + + "." + removeDots(session.realms().getRealm(adminEvent.getRealmId()).getName()) + "." + (adminEvent.getError() != null ? "ERROR" : "SUCCESS") + "." + adminEvent.getResourceTypeAsString() + "." + adminEvent.getOperationType().toString() @@ -51,11 +50,11 @@ public static String calculateRoutingKey(AdminEvent adminEvent) { return normalizeKey(routingKey); } - public static String calculateRoutingKey(Event event) { + public static String calculateRoutingKey(Event event, KeycloakSession session) { //KK.EVENT.CLIENT.... String routingKey = ROUTING_KEY_PREFIX + ".CLIENT" - + "." + removeDots(event.getRealmId()) + + "." + removeDots(session.realms().getRealm(event.getRealmId()).getName()) + "." + (event.getError() != null ? "ERROR" : "SUCCESS") + "." + removeDots(event.getClientId()) + "." + event.getType(); @@ -63,7 +62,7 @@ public static String calculateRoutingKey(Event event) { return normalizeKey(routingKey); } - //Remove all characters apart a-z, A-Z, 0-9, space, underscore, eplace all spaces and hyphens with underscore + //Remove all characters apart a-z, A-Z, 0-9, space, underscore, replace all spaces and hyphens with underscore public static String normalizeKey(CharSequence stringToNormalize) { return SPACE.matcher(SPECIAL_CHARACTERS.matcher(stringToNormalize).replaceAll("")) .replaceAll("_"); @@ -86,7 +85,7 @@ public static String writeAsJson(Object object, boolean isPretty) { } catch (Exception e) { log.error("Could not serialize to JSON", e); } - return "unparsable"; + return "unparseable"; } diff --git a/src/main/java/com/github/aznamier/keycloak/event/provider/RabbitMqEventListenerProvider.java b/src/main/java/com/github/aznamier/keycloak/event/provider/RabbitMqEventListenerProvider.java index 9fb9699..5ae539c 100644 --- a/src/main/java/com/github/aznamier/keycloak/event/provider/RabbitMqEventListenerProvider.java +++ b/src/main/java/com/github/aznamier/keycloak/event/provider/RabbitMqEventListenerProvider.java @@ -21,13 +21,16 @@ public class RabbitMqEventListenerProvider implements EventListenerProvider { private static final Logger log = Logger.getLogger(RabbitMqEventListenerProvider.class); private final RabbitMqConfig cfg; - private Channel channel; + private final Channel channel; + + private final KeycloakSession session; private final EventListenerTransaction tx = new EventListenerTransaction(this::publishAdminEvent, this::publishEvent); public RabbitMqEventListenerProvider(Channel channel, KeycloakSession session, RabbitMqConfig cfg) { this.cfg = cfg; this.channel = channel; + this.session = session; session.getTransactionManager().enlistAfterCompletion(tx); } @@ -48,7 +51,7 @@ public void onEvent(AdminEvent adminEvent, boolean includeRepresentation) { private void publishEvent(Event event) { EventClientNotificationMqMsg msg = EventClientNotificationMqMsg.create(event); - String routingKey = RabbitMqConfig.calculateRoutingKey(event); + String routingKey = RabbitMqConfig.calculateRoutingKey(event, session); String messageString = RabbitMqConfig.writeAsJson(msg, true); BasicProperties msgProps = RabbitMqEventListenerProvider.getMessageProps(EventClientNotificationMqMsg.class.getName()); @@ -57,7 +60,7 @@ private void publishEvent(Event event) { private void publishAdminEvent(AdminEvent adminEvent, boolean includeRepresentation) { EventAdminNotificationMqMsg msg = EventAdminNotificationMqMsg.create(adminEvent); - String routingKey = RabbitMqConfig.calculateRoutingKey(adminEvent); + String routingKey = RabbitMqConfig.calculateRoutingKey(adminEvent, session); String messageString = RabbitMqConfig.writeAsJson(msg, true); BasicProperties msgProps = RabbitMqEventListenerProvider.getMessageProps(EventAdminNotificationMqMsg.class.getName());