Error message when JDBC driver is missing is not helpful (#22833)

Closes #22795
This commit is contained in:
Martin Bartoš 2023-09-04 14:38:17 +02:00 committed by GitHub
parent a60849a86c
commit b05c79d591
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,26 @@
/*
* Copyright 2023 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.quarkus.deployment;
import io.quarkus.builder.item.EmptyBuildItem;
/**
* A barrier build item that can be consumed by other build steps when JDBC Driver is checked
*/
public class CheckJdbcBuildStep extends EmptyBuildItem {
}

View File

@ -18,7 +18,9 @@
package org.keycloak.quarkus.deployment;
import io.quarkus.agroal.spi.JdbcDataSourceBuildItem;
import io.quarkus.agroal.spi.JdbcDriverBuildItem;
import io.quarkus.arc.deployment.BuildTimeConditionBuildItem;
import io.quarkus.bootstrap.logging.InitialConfigurator;
import io.quarkus.datasource.deployment.spi.DevServicesDatasourceResultBuildItem;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildProducer;
@ -39,6 +41,7 @@ import io.quarkus.hibernate.orm.deployment.PersistenceXmlDescriptorBuildItem;
import io.quarkus.hibernate.orm.deployment.integration.HibernateOrmIntegrationRuntimeConfiguredBuildItem;
import io.quarkus.hibernate.orm.deployment.spi.AdditionalJpaModelBuildItem;
import io.quarkus.resteasy.server.common.deployment.ResteasyDeploymentCustomizerBuildItem;
import io.quarkus.runtime.configuration.ConfigurationException;
import io.quarkus.runtime.configuration.ProfileManager;
import io.quarkus.vertx.http.deployment.FilterBuildItem;
import io.quarkus.vertx.http.deployment.NonApplicationRootPathBuildItem;
@ -134,6 +137,7 @@ import java.util.function.Function;
import java.util.function.Predicate;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Handler;
import static org.keycloak.connections.jpa.util.JpaUtils.loadSpecificNamedQueries;
import static org.keycloak.quarkus.runtime.Environment.getCurrentOrCreateFeatureProfile;
@ -229,6 +233,28 @@ class KeycloakProcessor {
recorder.configureProfile(profile.getName(), profile.getFeatures());
}
/**
* Check whether JDBC driver is present for the specified DB
*
* @param ignore used for changing build items execution order with regards to AgroalProcessor
*/
@BuildStep(onlyIf = IsJpaStoreEnabled.class)
@Produce(CheckJdbcBuildStep.class)
void checkJdbcDriver(BuildProducer<JdbcDriverBuildItem> ignore) {
final Optional<String> dbDriver = Configuration.getOptionalValue("quarkus.datasource.jdbc.driver");
if (dbDriver.isPresent()) {
try {
// We do not want to initialize the JDBC driver class
Class.forName(dbDriver.get(), false, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
// Ignore queued TRACE and DEBUG messages for not initialized log handlers
InitialConfigurator.DELAYED_HANDLER.setBuildTimeHandlers(new Handler[]{});
throw new ConfigurationException(String.format("Unable to find the JDBC driver (%s). You need to install it.", dbDriver.get()));
}
}
}
/**
* <p>Configures the persistence unit for Quarkus.
*
@ -278,6 +304,7 @@ class KeycloakProcessor {
}
@BuildStep(onlyIf = IsJpaStoreEnabled.class)
@Consume(CheckJdbcBuildStep.class)
void produceDefaultPersistenceUnit(BuildProducer<PersistenceXmlDescriptorBuildItem> producer) {
String storage = Configuration.getRawValue(
NS_KEYCLOAK_PREFIX.concat(StorageOptions.STORAGE.getKey()));
@ -555,11 +582,13 @@ class KeycloakProcessor {
}
@BuildStep(onlyIf = IsJpaStoreEnabled.class, onlyIfNot = IsLegacyStoreEnabled.class)
@Consume(CheckJdbcBuildStep.class)
void indexNewJpaStore(BuildProducer<IndexDependencyBuildItem> indexDependencyBuildItemBuildProducer) {
indexDependencyBuildItemBuildProducer.produce(new IndexDependencyBuildItem("org.keycloak", "keycloak-model-map-jpa"));
}
@BuildStep(onlyIf = IsLegacyStoreEnabled.class)
@Consume(CheckJdbcBuildStep.class)
void indexLegacyJpaStore(BuildProducer<IndexDependencyBuildItem> indexDependencyBuildItemBuildProducer) {
indexDependencyBuildItemBuildProducer.produce(new IndexDependencyBuildItem("org.keycloak", "keycloak-model-jpa"));
}

View File

@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.keycloak.quarkus.runtime.cli.command.AbstractStartCommand.OPTIMIZED_BUILD_OPTION_LONG;
import org.junit.jupiter.api.Test;
import org.keycloak.config.database.Database;
import org.keycloak.it.junit5.extension.CLIResult;
import org.keycloak.it.junit5.extension.DistributionTest;
@ -76,4 +77,16 @@ class BuildCommandDistTest {
CLIResult result = distribution.run("start", "--hostname=mykeycloak", OPTIMIZED_BUILD_OPTION_LONG);
result.assertMessage("Key material not provided to setup HTTPS");
}
@Test
@RawDistOnly(reason = "Containers are immutable")
@Launch({"build", "--db=oracle"})
void missingOracleJdbcDriver(LaunchResult result) {
CLIResult cliResult = (CLIResult) result;
String dbDriver = Database.getDriver("oracle", true).orElse("");
cliResult.assertError(String.format("ERROR: Unable to find the JDBC driver (%s). You need to install it.", dbDriver));
cliResult.assertNoBuild();
}
}