Is that possible to make a Keycloak listener based on Keycloak SPI to copy the configuration from realm to new created realm?

We have a use case when the user create a new realm (E.g.: “realmNew”)in Keycloak, some configs and resource in an existed realm (e.g.: all roles in “realmOld”) needs to be copied into the new created realm.

I did some research and found Keycloak provides SPI to implement the plugin which listen the specific event and do the operation. In my case, the event will be realm creation. When I try to implement the interface EventListenerProvider in plugin, I found the method in this interface:

void onEvent(AdminEvent event, boolean includeRepresentation);

The input is the AdminEvent class. So it means the listener has no way to access any internal resources of keycloak (E.g.: realm, roles, users …) In this case, it seems that the listener plugin based on Keycloak SPI won’t work for my design. I should find another approach.

Anyone has idea whether my understanding is correct ? Or any suggestions ?

Every Provider class has an corresponding ProviderFactory class/interface, in which is something like a create() or init() method (I don’t have the sources at hand, atm, so something around this or similar should be available), where an instance of the KeycloakSession is being available at runtime. So, you can pass this session object down to your event listener and you’ll have a lot of Keycloak stuff at hand. If everything is available what you need, you’ll have to see.

That sounds promising. I will check that.

Thanks very much.