Skip to content

Commit

Permalink
Using the keycloak session to put the realm name in the routing key i…
Browse files Browse the repository at this point in the history
…nstead if the UUID (#39)
  • Loading branch information
marciprete authored Nov 24, 2023
1 parent ccf1c37 commit 24d4046
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@
</executions>
</plugin>

<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.wildfly.plugins</groupId>-->
<!-- <artifactId>wildfly-maven-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <skip>false</skip>-->
<!-- </configuration>-->
<!-- </plugin>-->
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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.<REALM>.<RESULT>.<RESOURCE_TYPE>.<OPERATION>
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()
Expand All @@ -51,19 +50,19 @@ 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.<REALM>.<RESULT>.<CLIENT>.<EVENT_TYPE>
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();

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("_");
Expand All @@ -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";
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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());
Expand All @@ -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());
Expand Down

0 comments on commit 24d4046

Please sign in to comment.