Ignore JDBC Drivers artifacts (#22443)

Closes #22442

Signed-off-by: Martin Bartoš <mabartos@redhat.com>
This commit is contained in:
Martin Bartoš 2023-11-22 15:13:31 +01:00 committed by GitHub
parent 030f42ec83
commit d0bfbde7ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 140 additions and 18 deletions

View file

@ -18,6 +18,7 @@
package org.keycloak.config.database;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@ -51,6 +52,12 @@ public final class Database {
return false;
}
public static Optional<Vendor> getVendorByDbKind(String dbKind) {
return Arrays.stream(Vendor.values())
.filter(v -> v.isOfKind(dbKind))
.findAny();
}
public static Optional<String> getDatabaseKind(String alias) {
Vendor vendor = DATABASES.get(alias);

View file

@ -17,9 +17,13 @@
package org.keycloak.quarkus.runtime.configuration;
import org.keycloak.common.Profile;
import org.keycloak.config.database.Database;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.keycloak.quarkus.runtime.Environment.getCurrentOrCreateFeatureProfile;
@ -29,9 +33,12 @@ import static org.keycloak.quarkus.runtime.Environment.getCurrentOrCreateFeature
public class IgnoredArtifacts {
public static Set<String> getDefaultIgnoredArtifacts() {
return new Builder()
.append(fips())
.build();
return Stream.of(
fips(),
jdbcDrivers()
)
.flatMap(Collection::stream)
.collect(Collectors.toUnmodifiableSet());
}
// FIPS
@ -56,23 +63,71 @@ public class IgnoredArtifacts {
return isFipsEnabled ? FIPS_ENABLED : FIPS_DISABLED;
}
/**
* Builder for artifacts aggregation
*/
private static final class Builder {
private final Set<String> finalIgnoredArtifacts;
// JDBC Drivers
public static final Set<String> JDBC_H2 = Set.of(
"io.quarkus:quarkus-jdbc-h2",
"io.quarkus:quarkus-jdbc-h2-deployment",
"com.h2database:h2"
);
public Builder() {
this.finalIgnoredArtifacts = new HashSet<>();
}
public static final Set<String> JDBC_POSTGRES = Set.of(
"io.quarkus:quarkus-jdbc-postgresql",
"io.quarkus:quarkus-jdbc-postgresql-deployment",
"org.postgresql:postgresql"
);
public Builder append(Set<String> ignoredArtifacts) {
finalIgnoredArtifacts.addAll(ignoredArtifacts);
return this;
}
public static final Set<String> JDBC_MARIADB = Set.of(
"io.quarkus:quarkus-jdbc-mariadb",
"io.quarkus:quarkus-jdbc-mariadb-deployment",
"org.mariadb.jdbc:mariadb-java-client"
);
public Set<String> build() {
return finalIgnoredArtifacts;
}
public static final Set<String> JDBC_MYSQL = Set.of(
"io.quarkus:quarkus-jdbc-mysql",
"io.quarkus:quarkus-jdbc-mysql-deployment",
"mysql:mysql-connector-java"
);
public static final Set<String> JDBC_MSSQL = Set.of(
"io.quarkus:quarkus-jdbc-mssql",
"io.quarkus:quarkus-jdbc-mssql-deployment",
"com.microsoft.sqlserver:mssql-jdbc"
);
public static final Set<String> JDBC_ORACLE = Set.of(
"io.quarkus:quarkus-jdbc-oracle",
"io.quarkus:quarkus-jdbc-oracle-deployment",
"com.oracle.database.jdbc:ojdbc11",
"com.oracle.database.nls:orai18n"
);
public static final Set<String> JDBC_DRIVERS = Stream.of(
JDBC_H2,
JDBC_POSTGRES,
JDBC_MARIADB,
JDBC_MYSQL,
JDBC_MSSQL,
JDBC_ORACLE
)
.flatMap(Collection::stream)
.collect(Collectors.toUnmodifiableSet());
private static Set<String> jdbcDrivers() {
final Database.Vendor vendor = Configuration.getOptionalValue("quarkus.datasource.db-kind")
.flatMap(Database::getVendorByDbKind)
.orElse(Database.Vendor.H2);
final Set<String> jdbcArtifacts = switch (vendor) {
case H2 -> JDBC_H2;
case MYSQL -> JDBC_MYSQL;
case MARIADB -> JDBC_MARIADB;
case POSTGRES -> JDBC_POSTGRES;
case MSSQL -> JDBC_MSSQL;
case ORACLE -> JDBC_ORACLE;
};
final Set<String> allJdbcDrivers = new HashSet<>(JDBC_DRIVERS);
allJdbcDrivers.removeAll(jdbcArtifacts);
return allJdbcDrivers;
}
}

View file

@ -17,15 +17,27 @@
package org.keycloak.quarkus.runtime.configuration.test;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.keycloak.common.Profile;
import org.keycloak.common.profile.PropertiesProfileConfigResolver;
import org.keycloak.config.DatabaseOptions;
import org.keycloak.quarkus.runtime.configuration.IgnoredArtifacts;
import org.keycloak.quarkus.runtime.configuration.MicroProfileConfigProvider;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.keycloak.quarkus.runtime.configuration.IgnoredArtifacts.JDBC_H2;
import static org.keycloak.quarkus.runtime.configuration.IgnoredArtifacts.JDBC_MARIADB;
import static org.keycloak.quarkus.runtime.configuration.IgnoredArtifacts.JDBC_MSSQL;
import static org.keycloak.quarkus.runtime.configuration.IgnoredArtifacts.JDBC_MYSQL;
import static org.keycloak.quarkus.runtime.configuration.IgnoredArtifacts.JDBC_ORACLE;
import static org.keycloak.quarkus.runtime.configuration.IgnoredArtifacts.JDBC_POSTGRES;
public class IgnoredArtifactsTest {
@ -49,4 +61,52 @@ public class IgnoredArtifactsTest {
var ignoredArtifacts = IgnoredArtifacts.getDefaultIgnoredArtifacts();
assertThat(ignoredArtifacts.containsAll(IgnoredArtifacts.FIPS_ENABLED), is(true));
}
@Test
public void jdbcH2() {
assertJdbc("h2", JDBC_H2);
}
@Test
public void jdbcMssql() {
assertJdbc("mssql", JDBC_MSSQL);
}
@Test
public void jdbcMariadb() {
assertJdbc("mariadb", JDBC_MARIADB);
}
@Test
public void jdbcMysql() {
assertJdbc("mysql", JDBC_MYSQL);
}
@Test
public void jdbcOracle() {
assertJdbc("oracle", JDBC_ORACLE);
}
@Test
public void jdbcPostgres() {
assertJdbc("postgres", JDBC_POSTGRES);
}
private void assertJdbc(String vendor, Set<String> notIgnored) {
System.setProperty(MicroProfileConfigProvider.NS_KEYCLOAK_PREFIX + DatabaseOptions.DB.getKey(), vendor);
try {
final var resultArtifacts = IgnoredArtifacts.getDefaultIgnoredArtifacts();
assertThat(String.format("Ignored artifacts does not comply with the specified artifacts for '%s' JDBC driver", vendor),
resultArtifacts,
not(CoreMatchers.hasItems(notIgnored.toArray(new String[0]))));
final var includedArtifacts = new HashSet<>(IgnoredArtifacts.JDBC_DRIVERS);
includedArtifacts.removeAll(notIgnored);
assertThat("Ignored artifacts does not contain items for the other JDBC drivers",
resultArtifacts,
CoreMatchers.hasItems(includedArtifacts.toArray(new String[0])));
} finally {
System.setProperty(MicroProfileConfigProvider.NS_KEYCLOAK_PREFIX + DatabaseOptions.DB.getKey(), "");
}
}
}