Keycloak server fails to start when using different named schema

Closes #12219
This commit is contained in:
Pedro Igor 2022-05-30 18:17:32 -03:00
parent 9272c7a5ec
commit c18b87af5f
7 changed files with 80 additions and 17 deletions

View file

@ -489,7 +489,7 @@ jobs:
- name: Run Quarkus Storage Tests
run: |
./mvnw clean install -nsu -B -f quarkus/tests/pom.xml -Ptest-database -Dtest=PostgreSQLDistTest | misc/log/trimmer.sh
./mvnw clean install -nsu -B -f quarkus/tests/pom.xml -Ptest-database -Dtest=PostgreSQLDistTest,DatabaseOptionsDistTest | misc/log/trimmer.sh
TEST_RESULT=${PIPESTATUS[0]}
find . -path '*/target/surefire-reports/*.xml' | zip -q reports-quarkus-tests.zip -@
exit $TEST_RESULT

View file

@ -111,7 +111,6 @@ import org.keycloak.policy.BlacklistPasswordPolicyProviderFactory;
import org.keycloak.protocol.ProtocolMapperSpi;
import org.keycloak.protocol.oidc.mappers.DeployedScriptOIDCProtocolMapper;
import org.keycloak.provider.EnvironmentDependentProviderFactory;
import org.keycloak.provider.KeycloakDeploymentInfo;
import org.keycloak.provider.Provider;
import org.keycloak.provider.ProviderFactory;
import org.keycloak.provider.ProviderManager;
@ -217,11 +216,13 @@ class KeycloakProcessor {
if ("keycloak-default".equals(descriptor.getName())) {
configureJpaProperties(descriptor, config, jdbcDataSources);
configureJpaModel(descriptor, indexBuildItem);
runtimeConfigured.produce(new HibernateOrmIntegrationRuntimeConfiguredBuildItem("keycloak", descriptor.getName())
.setInitListener(recorder.createDefaultUnitListener()));
} else {
Properties properties = descriptor.getProperties();
// register a listener for customizing the unit configuration at runtime
runtimeConfigured.produce(new HibernateOrmIntegrationRuntimeConfiguredBuildItem("keycloak", descriptor.getName())
.setInitListener(recorder.createUnitListener(properties.getProperty(AvailableSettings.DATASOURCE))));
.setInitListener(recorder.createUserDefinedUnitListener(properties.getProperty(AvailableSettings.DATASOURCE))));
}
}
}

View file

@ -35,6 +35,7 @@ import io.quarkus.smallrye.metrics.runtime.SmallRyeMetricsHandler;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import org.keycloak.common.Profile;
import org.keycloak.quarkus.runtime.configuration.Configuration;
import org.keycloak.quarkus.runtime.integration.QuarkusKeycloakSessionFactory;
import org.keycloak.quarkus.runtime.storage.database.liquibase.FastServiceLocator;
import org.keycloak.provider.Provider;
@ -101,7 +102,7 @@ public class KeycloakRecorder {
return metricsHandler;
}
public HibernateOrmIntegrationRuntimeInitListener createUnitListener(String name) {
public HibernateOrmIntegrationRuntimeInitListener createUserDefinedUnitListener(String name) {
return new HibernateOrmIntegrationRuntimeInitListener() {
@Override
public void contributeRuntimeProperties(BiConsumer<String, Object> propertyCollector) {
@ -119,4 +120,13 @@ public class KeycloakRecorder {
}
};
}
public HibernateOrmIntegrationRuntimeInitListener createDefaultUnitListener() {
return new HibernateOrmIntegrationRuntimeInitListener() {
@Override
public void contributeRuntimeProperties(BiConsumer<String, Object> propertyCollector) {
propertyCollector.accept(AvailableSettings.DEFAULT_SCHEMA, Configuration.getRawValue("kc.db-schema"));
}
};
}
}

View file

@ -18,6 +18,7 @@
package org.keycloak.it.junit5.extension;
import java.time.Duration;
import org.jetbrains.annotations.NotNull;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.MariaDBContainer;
import org.testcontainers.containers.PostgreSQLContainer;
@ -37,7 +38,8 @@ public class DatabaseContainer {
container = createContainer()
.withDatabaseName("keycloak")
.withUsername(getUsername())
.withPassword(getPassword());
.withPassword(getPassword())
.withInitScript(resolveInitScript());
container.withStartupTimeout(Duration.ofMinutes(5)).start();
}
@ -73,4 +75,8 @@ public class DatabaseContainer {
throw new RuntimeException("Unsupported database: " + alias);
}
}
private String resolveInitScript() {
return String.format("database/scripts/init-%s.sql", alias);
}
}

View file

@ -0,0 +1,18 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
CREATE SCHEMA foo;

View file

@ -0,0 +1,38 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.it.storage.database.dist;
import org.junit.jupiter.api.Test;
import org.keycloak.it.junit5.extension.CLIResult;
import org.keycloak.it.junit5.extension.DistributionTest;
import org.keycloak.it.junit5.extension.WithDatabase;
import io.quarkus.test.junit.main.Launch;
import io.quarkus.test.junit.main.LaunchResult;
@DistributionTest
@WithDatabase(alias = "postgres")
public class DatabaseOptionsDistTest {
@Test
@Launch({ "start-dev", "--db-schema=foo" })
void testSetSchema(LaunchResult result) {
CLIResult cliResult = (CLIResult) result;
cliResult.assertStartedDevMode();
}
}

View file

@ -17,22 +17,12 @@
package org.keycloak.it.storage.database.dist;
import org.keycloak.it.junit5.extension.CLIResult;
import org.keycloak.it.junit5.extension.CLITest;
import org.keycloak.it.junit5.extension.WithDatabase;
import org.keycloak.it.storage.database.BasicDatabaseTest;
import org.keycloak.it.storage.database.MariaDBTest;
@CLITest
@WithDatabase(alias = "mariadb")
public class MariaDBDistTest extends BasicDatabaseTest {
public class MariaDBDistTest extends MariaDBTest {
@Override
protected void assertWrongPassword(CLIResult cliResult) {
cliResult.assertMessage("Access denied for user");
}
@Override
protected void assertWrongUsername(CLIResult cliResult) {
cliResult.assertMessage("Access denied for user 'wrong'");
}
}