Skip to content

Commit c18b87a

Browse files
committed
Keycloak server fails to start when using different named schema
Closes keycloak#12219
1 parent 9272c7a commit c18b87a

File tree

7 files changed

+80
-17
lines changed

7 files changed

+80
-17
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ jobs:
489489
490490
- name: Run Quarkus Storage Tests
491491
run: |
492-
./mvnw clean install -nsu -B -f quarkus/tests/pom.xml -Ptest-database -Dtest=PostgreSQLDistTest | misc/log/trimmer.sh
492+
./mvnw clean install -nsu -B -f quarkus/tests/pom.xml -Ptest-database -Dtest=PostgreSQLDistTest,DatabaseOptionsDistTest | misc/log/trimmer.sh
493493
TEST_RESULT=${PIPESTATUS[0]}
494494
find . -path '*/target/surefire-reports/*.xml' | zip -q reports-quarkus-tests.zip -@
495495
exit $TEST_RESULT

quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/KeycloakProcessor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@
111111
import org.keycloak.protocol.ProtocolMapperSpi;
112112
import org.keycloak.protocol.oidc.mappers.DeployedScriptOIDCProtocolMapper;
113113
import org.keycloak.provider.EnvironmentDependentProviderFactory;
114-
import org.keycloak.provider.KeycloakDeploymentInfo;
115114
import org.keycloak.provider.Provider;
116115
import org.keycloak.provider.ProviderFactory;
117116
import org.keycloak.provider.ProviderManager;
@@ -217,11 +216,13 @@ void configurePersistenceUnits(HibernateOrmConfig config,
217216
if ("keycloak-default".equals(descriptor.getName())) {
218217
configureJpaProperties(descriptor, config, jdbcDataSources);
219218
configureJpaModel(descriptor, indexBuildItem);
219+
runtimeConfigured.produce(new HibernateOrmIntegrationRuntimeConfiguredBuildItem("keycloak", descriptor.getName())
220+
.setInitListener(recorder.createDefaultUnitListener()));
220221
} else {
221222
Properties properties = descriptor.getProperties();
222223
// register a listener for customizing the unit configuration at runtime
223224
runtimeConfigured.produce(new HibernateOrmIntegrationRuntimeConfiguredBuildItem("keycloak", descriptor.getName())
224-
.setInitListener(recorder.createUnitListener(properties.getProperty(AvailableSettings.DATASOURCE))));
225+
.setInitListener(recorder.createUserDefinedUnitListener(properties.getProperty(AvailableSettings.DATASOURCE))));
225226
}
226227
}
227228
}

quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/KeycloakRecorder.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import io.vertx.core.Handler;
3636
import io.vertx.ext.web.RoutingContext;
3737
import org.keycloak.common.Profile;
38+
import org.keycloak.quarkus.runtime.configuration.Configuration;
3839
import org.keycloak.quarkus.runtime.integration.QuarkusKeycloakSessionFactory;
3940
import org.keycloak.quarkus.runtime.storage.database.liquibase.FastServiceLocator;
4041
import org.keycloak.provider.Provider;
@@ -101,7 +102,7 @@ public Handler<RoutingContext> createMetricsHandler(String path) {
101102
return metricsHandler;
102103
}
103104

104-
public HibernateOrmIntegrationRuntimeInitListener createUnitListener(String name) {
105+
public HibernateOrmIntegrationRuntimeInitListener createUserDefinedUnitListener(String name) {
105106
return new HibernateOrmIntegrationRuntimeInitListener() {
106107
@Override
107108
public void contributeRuntimeProperties(BiConsumer<String, Object> propertyCollector) {
@@ -119,4 +120,13 @@ AgroalDataSource.class, new DataSource() {
119120
}
120121
};
121122
}
123+
124+
public HibernateOrmIntegrationRuntimeInitListener createDefaultUnitListener() {
125+
return new HibernateOrmIntegrationRuntimeInitListener() {
126+
@Override
127+
public void contributeRuntimeProperties(BiConsumer<String, Object> propertyCollector) {
128+
propertyCollector.accept(AvailableSettings.DEFAULT_SCHEMA, Configuration.getRawValue("kc.db-schema"));
129+
}
130+
};
131+
}
122132
}

quarkus/tests/integration/src/main/java/org/keycloak/it/junit5/extension/DatabaseContainer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.keycloak.it.junit5.extension;
1919

2020
import java.time.Duration;
21+
import org.jetbrains.annotations.NotNull;
2122
import org.testcontainers.containers.JdbcDatabaseContainer;
2223
import org.testcontainers.containers.MariaDBContainer;
2324
import org.testcontainers.containers.PostgreSQLContainer;
@@ -37,7 +38,8 @@ void start() {
3738
container = createContainer()
3839
.withDatabaseName("keycloak")
3940
.withUsername(getUsername())
40-
.withPassword(getPassword());
41+
.withPassword(getPassword())
42+
.withInitScript(resolveInitScript());
4143

4244
container.withStartupTimeout(Duration.ofMinutes(5)).start();
4345
}
@@ -73,4 +75,8 @@ private JdbcDatabaseContainer createContainer() {
7375
throw new RuntimeException("Unsupported database: " + alias);
7476
}
7577
}
78+
79+
private String resolveInitScript() {
80+
return String.format("database/scripts/init-%s.sql", alias);
81+
}
7682
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2021 Red Hat, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
CREATE SCHEMA foo;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2021 Red Hat, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.keycloak.it.storage.database.dist;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.keycloak.it.junit5.extension.CLIResult;
22+
import org.keycloak.it.junit5.extension.DistributionTest;
23+
import org.keycloak.it.junit5.extension.WithDatabase;
24+
25+
import io.quarkus.test.junit.main.Launch;
26+
import io.quarkus.test.junit.main.LaunchResult;
27+
28+
@DistributionTest
29+
@WithDatabase(alias = "postgres")
30+
public class DatabaseOptionsDistTest {
31+
32+
@Test
33+
@Launch({ "start-dev", "--db-schema=foo" })
34+
void testSetSchema(LaunchResult result) {
35+
CLIResult cliResult = (CLIResult) result;
36+
cliResult.assertStartedDevMode();
37+
}
38+
}

quarkus/tests/integration/src/test/java/org/keycloak/it/storage/database/dist/MariaDBDistTest.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,12 @@
1717

1818
package org.keycloak.it.storage.database.dist;
1919

20-
import org.keycloak.it.junit5.extension.CLIResult;
2120
import org.keycloak.it.junit5.extension.CLITest;
2221
import org.keycloak.it.junit5.extension.WithDatabase;
23-
import org.keycloak.it.storage.database.BasicDatabaseTest;
22+
import org.keycloak.it.storage.database.MariaDBTest;
2423

2524
@CLITest
2625
@WithDatabase(alias = "mariadb")
27-
public class MariaDBDistTest extends BasicDatabaseTest {
26+
public class MariaDBDistTest extends MariaDBTest {
2827

29-
@Override
30-
protected void assertWrongPassword(CLIResult cliResult) {
31-
cliResult.assertMessage("Access denied for user");
32-
}
33-
34-
@Override
35-
protected void assertWrongUsername(CLIResult cliResult) {
36-
cliResult.assertMessage("Access denied for user 'wrong'");
37-
}
3828
}

0 commit comments

Comments
 (0)