Reputation: 364
I have implemented a custom Event Listener provider.
I'm able to receive all the events except the realm creation event (new realm creation). I would like to get the event during realm creation as well.
Is this supported by Keycloak ? If not, any other possibilities to achieve this ?
I'm using Keycloak version 4.5.0.
Thanks in Advance.
Upvotes: 3
Views: 3250
Reputation: 364
After doing some research on keycloak code, I came to conclusion that keycloak is not providing that event by default.
So I modified below files from keycloak which will help to capture Realm creation and deletion events.
Change 1 (Most Important) :
File:
keycloak/services/src/main/java/org/keycloak/services/managers/RealmManager.java
Function:
protected void setupRealmDefaults
In above function you should add your event listener to the realm during realm creation.
Set<String> eventListenerSet = new HashSet<>();
eventListenerSet.add("jboss-logging"); //This listener will be there by default
eventListenerSet.add("EVENT_LISTENER_YOU_WANT_TO_RECEIVE_EVENT");
realm.setEventsListeners(eventListenerSet);
Change 2 :
File:
keycloak/services/src/main/java/org/keycloak/services/resources/admin/RealmsAdminResource.java
Function:
public Response importRealm
In above function add below lines before returning response
Line 1: Create object for admin event
Line 2: Prepare event to trigger with appropriate event type and representation, in this case Create
AdminEventBuilder adminEvent = new AdminEventBuilder(realm, auth, session, clientConnection);
adminEvent.operation(OperationType.CREATE).resource(ResourceType.REALM).representation(StripSecretsUtils.strip(rep)).success();
Change 3 (Needed only if delete event is required)
File:
keycloak/services/src/main/java/org/keycloak/services/resources/admin/RealmAdminResource.java
Function:
public void deleteRealm
Add the below code after the First Line
AdminAuth adminAuth = auth.adminAuth();
RealmRepresentation realmRepresentation = new RealmRepresentation();
realmRepresentation.setRealm(realm.getName());
AdminEventBuilder adminEvent = new AdminEventBuilder(realm, adminAuth, session, connection);
adminEvent.operation(OperationType.DELETE).resource(ResourceType.REALM).representation(realmRepresentation).success();
Upvotes: 4