Search code examples
javadockerdocker-composekeycloakkeycloak-spi

Pass configuration to Keycloak SPI Provider?


I am trying to create an EventListenerProvider implementation in Keycloak 22.x for the purposes of listening to user account creation events. I've written the provider code itself and it is working without issue. Now I am trying to allow for the configuration to be externalized rather than hard-coded in the code and I am running into issues.

My EventListenerProviderFactory implementation is rather simple and looks like this:

public class MyEventListenerProviderFactory implements EventListenerProviderFactory {

 ...snip...

 public String getId() {
   return "myAppEventListener";
 }

 public void init(Scope config) {
   ...snip...
 }

 public EventListenerProvider create(KeycloakSession session) {
   return new MyEventListenerProvider();
 }

 ...snip...

}

As I understand from the documentation, I should be able to provide my configuration parameters on the command line using the SPI ID. In my case, I should be able to specify a parameter that looks like:

spi-my-app-event-listener-myproperty=mypropertyvalue

That should result in me being able to do the following in the init(Scope config) method:

public void init(Scope config) {
  String myPropertyValue = config.get("myproperty");
  System.out.println("myPropertyValue=" + myPropertyValue);
}

However, I always get a null value for this property. I am using docker-compose and the official Docker container to launch Keycloak, and am setting the property value in the command stanza like this:

  command: start-dev --spi-my-app-event-listener-myproperty=mypropvalue --import-realm

I thought that this would correctly set the property, but it looks like I may be wrong about that. Any idea where my issue lies and why I am unable to set a configuration value and read it in my event listener provider factory?


Solution

  • It looks like the documentation describes the format as:

    spi-<spi-id>-<provider-id>-<property>=<value>
    

    which means you'd need:

    spi-events-listener-my-app-event-listener-myproperty=mypropvalue
    

    If you look at the source, you'll see that EventListenerProviderFactory's SPI is "eventsListener".

    There are a few elements to an SPI:

    1. The SPI itself
    2. Factory
    3. Provider
    4. Metadata