keycloak / keycloak-quickstarts

Apache License 2.0
1.98k stars 984 forks source link

Custom User Storage Provider JPA runtime provided properties #570

Open carlosmtobon opened 4 months ago

carlosmtobon commented 4 months ago

Describe the bug

Hello I have a Custom User Storage provider that connects to a MySQL database using JPA. The user will provides the config details using the UI (via ProviderConfigurationBuilder). In the Factory class I am trying to do something like this

        Map<String, String> properties = new HashMap<>();
        properties.put("jakarta.persistence.jdbc.url", model.get(CONFIG_KEY_JDBC_URL));
        properties.put("jakarta.persistence.jdbc.user", model.get(CONFIG_KEY_DB_USERNAME));
        properties.put("jakarta.persistence.jdbc.password", model.get(CONFIG_KEY_DB_PASSWORD));
        properties.put("jakarta.persistence.jdbc.driver", model.get(CONFIG_KEY_JDBC_DRIVER));
        properties.put("hibernate.hbm2ddl.auto", "update");
        properties.put("hibernate.show_sql", "true");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("dynamic-persistence-unit", properties);

but I am getting Caused by: jakarta.persistence.PersistenceException: The FastbootHibernateProvider PersistenceProvider can not support runtime provided properties. Make sure you set all properties you need in the configuration resources before building the application.

What is the correct way to handle a runtime configuration?

Things I've tried: I've tried adding quarkus.properties and matching the data source names with the persistence.xml

Sorry if this has been answered before, just not clear since examples I see don't provide runtime configurations.

Version

25.0.1

Expected behavior

Allow runtime configuration for external database connection for user storage

Actual behavior

getting an error about needing to provide the configuration at build time

How to Reproduce?

Anything else?

The only time I have seen this provider work for me was when I initially tested against a Keycloak embedded Spring boot app using this article.

carlosmtobon commented 4 months ago

link to article