Keycloak wrongly assumes that the default datasource is the first one

Closes #15608
This commit is contained in:
Pedro Igor 2022-11-25 08:23:43 -03:00 committed by Václav Muzikář
parent 20592dda29
commit 17bf092da6
2 changed files with 19 additions and 4 deletions

View file

@ -258,7 +258,7 @@ class KeycloakProcessor {
if ("keycloak-default".equals(descriptor.getName())) {
defaultUnitDescriptor = descriptor;
configureDefaultPersistenceUnitProperties(defaultUnitDescriptor, config, jdbcDataSources);
configureDefaultPersistenceUnitProperties(defaultUnitDescriptor, config, getDefaultDataSource(jdbcDataSources));
runtimeConfigured.produce(new HibernateOrmIntegrationRuntimeConfiguredBuildItem("keycloak", defaultUnitDescriptor.getName())
.setInitListener(recorder.createDefaultUnitListener()));
} else {
@ -295,7 +295,11 @@ class KeycloakProcessor {
}
private void configureDefaultPersistenceUnitProperties(ParsedPersistenceXmlDescriptor descriptor, HibernateOrmConfig config,
List<JdbcDataSourceBuildItem> jdbcDataSources) {
JdbcDataSourceBuildItem defaultDataSource) {
if (defaultDataSource == null || !defaultDataSource.isDefault()) {
throw new RuntimeException("The server datasource must be the default datasource.");
}
Properties unitProperties = descriptor.getProperties();
unitProperties.setProperty(AvailableSettings.DIALECT, config.defaultPersistenceUnit.dialect.dialect.orElse(null));
@ -320,7 +324,7 @@ class KeycloakProcessor {
unitProperties.setProperty(AvailableSettings.QUERY_STARTUP_CHECKING, Boolean.FALSE.toString());
String dbKind = jdbcDataSources.get(0).getDbKind();
String dbKind = defaultDataSource.getDbKind();
for (Entry<Object, Object> query : loadSpecificNamedQueries(dbKind.toLowerCase()).entrySet()) {
unitProperties.setProperty(QUERY_PROPERTY_PREFIX + query.getKey(), query.getValue().toString());
@ -826,4 +830,14 @@ class KeycloakProcessor {
private boolean isHealthEnabled() {
return Configuration.getOptionalBooleanValue(MicroProfileConfigProvider.NS_KEYCLOAK_PREFIX.concat("health-enabled")).orElse(false);
}
static JdbcDataSourceBuildItem getDefaultDataSource(List<JdbcDataSourceBuildItem> jdbcDataSources) {
for (JdbcDataSourceBuildItem jdbcDataSource : jdbcDataSources) {
if (jdbcDataSource.isDefault()) {
return jdbcDataSource;
}
}
throw new RuntimeException("No default datasource found. The server datasource must be the default datasource.");
}
}

View file

@ -32,6 +32,7 @@ import liquibase.sqlgenerator.SqlGenerator;
import org.keycloak.quarkus.runtime.KeycloakRecorder;
import static org.keycloak.config.StorageOptions.STORAGE;
import static org.keycloak.quarkus.deployment.KeycloakProcessor.getDefaultDataSource;
import static org.keycloak.quarkus.runtime.configuration.Configuration.getOptionalValue;
import static org.keycloak.quarkus.runtime.configuration.MicroProfileConfigProvider.NS_KEYCLOAK_PREFIX;
@ -43,7 +44,7 @@ class LiquibaseProcessor {
DotName liquibaseServiceName = DotName.createSimple(LiquibaseService.class.getName());
Map<String, List<String>> services = new HashMap<>();
IndexView index = indexBuildItem.getIndex();
JdbcDataSourceBuildItem dataSourceBuildItem = jdbcDataSources.get(0);
JdbcDataSourceBuildItem dataSourceBuildItem = getDefaultDataSource(jdbcDataSources);
String dbKind = dataSourceBuildItem.getDbKind();
for (Class<?> c : Arrays.asList(liquibase.diff.compare.DatabaseObjectComparator.class,