Search code examples
javahibernatejpakeycloakquarkus

How to use a configurable schema name in Keycloak User Storage SPI / Quarkus


I am implementing a custom user storage provider which gets the user data from an external PostgreSQL database. Since I have several schemas I want to create 2 instances of that provider where I am using the schema name to query the database.

I am about to use this example which is using quarkus and JPA(Hibernate) to encapsulate the database access.

How can I set the schema (within UserEntity?) programmatically to achieve that.

Thanks and Regards matt


Solution

  • The solution is quite obvious but sometimes one does not to see the wood for the trees:

    The config metadata has to be configured with and to be returned from the public List<ProviderConfigProperty> getConfigProperties() Method within the UserStorageProviderFactory

    Then the name of the datasource (persistent unit) e.g. my-user-store which must be filled when the instance of the Provider is created/configured. The name then can be passed to the constructor of the UserStorageProvider which then can initialize the entity manager with

    em = session.getProvider(JpaConnectionProvider.class, datasourceName).getEntityManager();

    Within quarkus.properties and persistence.xml all datasources that should be used are configured. The schema is set with quarkus.datasource.my-user-store.jdbc.new-connection-sql=SET search_path TO my-schema