From f107f0596ea38b182090bc5cf6f8ca00750132b9 Mon Sep 17 00:00:00 2001 From: Pedro Igor Date: Thu, 3 Feb 2022 19:22:59 -0300 Subject: [PATCH] Rename h2-file and h2-mem and removing defaults from production databases Closes #9973 --- docs/guides/src/main/server/db.adoc | 4 +-- .../src/test/resources/keycloak.conf | 2 +- .../mappers/DatabasePropertyMappers.java | 30 +++++++++++++++++-- .../configuration/mappers/PropertyMapper.java | 8 +---- .../runtime/storage/database/Database.java | 4 +-- .../src/main/resources/META-INF/keycloak.conf | 4 +-- .../configuration/test/ConfigurationTest.java | 21 +++++++++---- .../keycloak/it/cli/OptionValidationTest.java | 4 +-- .../org/keycloak/it/cli/StartCommandTest.java | 4 +-- .../keycloak/it/cli/StartDevCommandTest.java | 2 +- .../it/cli/dist/OptionValidationDistTest.java | 2 +- .../it/cli/dist/StartAutoBuildDistTest.java | 4 +-- .../ShowConfigCommandTest/keycloak.conf | 2 +- ...HelpCommandTest.testBuildHelp.approved.txt | 2 +- ...mmandTest.testStartDevHelpAll.approved.txt | 2 +- .../src/main/content/conf/keycloak.conf | 2 +- 16 files changed, 62 insertions(+), 35 deletions(-) diff --git a/docs/guides/src/main/server/db.adoc b/docs/guides/src/main/server/db.adoc index 91d26b95c6..6bfe804aaa 100644 --- a/docs/guides/src/main/server/db.adoc +++ b/docs/guides/src/main/server/db.adoc @@ -27,8 +27,8 @@ The list of supported databases and their corresponding versions are: |postgres| 10 |=== -By default, the server uses the `h2-file` database. This is the default database that the server will use to persist data and -only exists for development use-cases. The `h2-file` database is not suitable for production use-cases, and must be replaced before deploying to production. +By default, the server uses the `dev-file` database. This is the default database that the server will use to persist data and +only exists for development use-cases. The `dev-file` database is not suitable for production use-cases, and must be replaced before deploying to production. == Configuring a database diff --git a/quarkus/deployment/src/test/resources/keycloak.conf b/quarkus/deployment/src/test/resources/keycloak.conf index f001b7432b..ce1b4537cb 100644 --- a/quarkus/deployment/src/test/resources/keycloak.conf +++ b/quarkus/deployment/src/test/resources/keycloak.conf @@ -2,7 +2,7 @@ http-enabled=true cluster=local hostname-strict=false hostname-strict-https=false -db=h2-mem +db=dev-mem db-username = sa db-password = keycloak metrics-enabled=true \ No newline at end of file diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/DatabasePropertyMappers.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/DatabasePropertyMappers.java index a5c7048eb7..3e9d44efbb 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/DatabasePropertyMappers.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/DatabasePropertyMappers.java @@ -1,5 +1,6 @@ package org.keycloak.quarkus.runtime.configuration.mappers; +import io.quarkus.datasource.common.runtime.DatabaseKind; import io.smallrye.config.ConfigSourceInterceptorContext; import org.keycloak.quarkus.runtime.storage.database.Database; @@ -20,12 +21,12 @@ final class DatabasePropertyMappers { .mapFrom("db") .to("quarkus.hibernate-orm.dialect") .isBuildTimeProperty(true) - .transformer((db, context) -> Database.getDialect(db).orElse(Database.getDialect("h2-file").get())) + .transformer((db, context) -> Database.getDialect(db).orElse(Database.getDialect("dev-file").get())) .hidden(true) .build(), builder().from("db-driver") .mapFrom("db") - .defaultValue(Database.getDriver("h2-file").get()) + .defaultValue(Database.getDriver("dev-file").get()) .to("quarkus.datasource.jdbc.driver") .transformer((db, context) -> Database.getDriver(db).orElse(db)) .hidden(true) @@ -68,11 +69,15 @@ final class DatabasePropertyMappers { .build(), builder().from("db-username") .to("quarkus.datasource.username") + .mapFrom("db") + .transformer(DatabasePropertyMappers::resolveUsername) .description("The username of the database user.") .paramLabel("username") .build(), builder().from("db-password") .to("quarkus.datasource.password") + .mapFrom("db") + .transformer(DatabasePropertyMappers::resolvePassword) .description("The password of the database user.") .paramLabel("password") .isMasked(true) @@ -118,4 +123,25 @@ final class DatabasePropertyMappers { private static PropertyMapper.Builder builder() { return PropertyMapper.builder(ConfigCategory.DATABASE); } + + private static String resolveUsername(String value, ConfigSourceInterceptorContext context) { + if (isDevModeDatabase(context)) { + return "sa"; + } + + return Database.getDatabaseKind(value).isEmpty() ? value : null; + } + + private static String resolvePassword(String value, ConfigSourceInterceptorContext context) { + if (isDevModeDatabase(context)) { + return "password"; + } + + return Database.getDatabaseKind(value).isEmpty() ? value : null; + } + + private static boolean isDevModeDatabase(ConfigSourceInterceptorContext context) { + String db = context.proceed("kc.db").getValue(); + return Database.getDatabaseKind(db).get().equals(DatabaseKind.H2); + } } diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/PropertyMapper.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/PropertyMapper.java index 257288fbac..4e648864ec 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/PropertyMapper.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/PropertyMapper.java @@ -117,13 +117,7 @@ public class PropertyMapper { } if (parentValue != null) { - ConfigValue value = transformValue(parentValue.getValue(), context); - - if (value != null) { - return value; - } - - return parentValue; + return transformValue(parentValue.getValue(), context); } } diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/database/Database.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/database/Database.java index c5d9bac860..7ffd6606df 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/database/Database.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/database/Database.java @@ -99,7 +99,7 @@ public final class Database { new Function() { @Override public String apply(String alias) { - if ("h2-file".equalsIgnoreCase(alias)) { + if ("dev-file".equalsIgnoreCase(alias)) { return "jdbc:h2:file:${kc.home.dir:${kc.db-url-path:~}}" + File.separator + "${kc.data.dir:data}" + File.separator + "h2" + File.separator + "keycloakdb${kc.db-url-properties:;;AUTO_SERVER=TRUE}"; @@ -108,7 +108,7 @@ public final class Database { } }, asList("liquibase.database.core.H2Database"), - "h2-mem", "h2-file" + "dev-mem", "dev-file" ), MYSQL("mysql", "com.mysql.cj.jdbc.MysqlXADataSource", diff --git a/quarkus/runtime/src/main/resources/META-INF/keycloak.conf b/quarkus/runtime/src/main/resources/META-INF/keycloak.conf index 757e91c8d4..8160c4c20b 100644 --- a/quarkus/runtime/src/main/resources/META-INF/keycloak.conf +++ b/quarkus/runtime/src/main/resources/META-INF/keycloak.conf @@ -1,7 +1,5 @@ # Default and non-production grade database vendor -db=h2-file -db-username = sa -db-password = keycloak +db=dev-file # Insecure requests are disabled by default http-enabled=false diff --git a/quarkus/runtime/src/test/java/org/keycloak/quarkus/runtime/configuration/test/ConfigurationTest.java b/quarkus/runtime/src/test/java/org/keycloak/quarkus/runtime/configuration/test/ConfigurationTest.java index 31108d6158..5a153b37b8 100644 --- a/quarkus/runtime/src/test/java/org/keycloak/quarkus/runtime/configuration/test/ConfigurationTest.java +++ b/quarkus/runtime/src/test/java/org/keycloak/quarkus/runtime/configuration/test/ConfigurationTest.java @@ -234,16 +234,28 @@ public class ConfigurationTest { @Test public void testDatabaseDefaults() { - System.setProperty(CLI_ARGS, "--db=h2-file"); + System.setProperty(CLI_ARGS, "--db=dev-file"); SmallRyeConfig config = createConfig(); assertEquals(QuarkusH2Dialect.class.getName(), config.getConfigValue("quarkus.hibernate-orm.dialect").getValue()); assertEquals("jdbc:h2:file:~/data/h2/keycloakdb;;AUTO_SERVER=TRUE", config.getConfigValue("quarkus.datasource.jdbc.url").getValue()); - System.setProperty(CLI_ARGS, "--db=h2-mem"); + System.setProperty(CLI_ARGS, "--db=dev-mem"); config = createConfig(); assertEquals(QuarkusH2Dialect.class.getName(), config.getConfigValue("quarkus.hibernate-orm.dialect").getValue()); assertEquals("jdbc:h2:mem:keycloakdb", config.getConfigValue("quarkus.datasource.jdbc.url").getValue()); assertEquals("h2", config.getConfigValue("quarkus.datasource.db-kind").getValue()); + + System.setProperty(CLI_ARGS, "--db=dev-mem" + ARG_SEPARATOR + "--db-username=other"); + config = createConfig(); + assertEquals("sa", config.getConfigValue("quarkus.datasource.username").getValue()); + + System.setProperty(CLI_ARGS, "--db=postgres" + ARG_SEPARATOR + "--db-username=other"); + config = createConfig(); + assertEquals("other", config.getConfigValue("quarkus.datasource.username").getValue()); + + System.setProperty(CLI_ARGS, "--db=postgres"); + config = createConfig(); + assertEquals(null, config.getConfigValue("quarkus.datasource.username").getValue()); } @Test @@ -280,7 +292,7 @@ public class ConfigurationTest { public void testDatabaseProperties() { System.setProperty("kc.db-url-properties", ";;test=test;test1=test1"); System.setProperty("kc.db-url-path", "test-dir"); - System.setProperty(CLI_ARGS, "--db=h2-file"); + System.setProperty(CLI_ARGS, "--db=dev-file"); SmallRyeConfig config = createConfig(); assertEquals(QuarkusH2Dialect.class.getName(), config.getConfigValue("quarkus.hibernate-orm.dialect").getValue()); assertEquals("jdbc:h2:file:test-dir" + File.separator + "data" + File.separator + "h2" + File.separator + "keycloakdb;;test=test;test1=test1", config.getConfigValue("quarkus.datasource.jdbc.url").getValue()); @@ -398,9 +410,6 @@ public class ConfigurationTest { Environment.setProfile("prod"); assertEquals("true", createConfig().getConfigValue("kc.hostname-strict").getValue()); - - Environment.setProfile("prod"); - assertEquals("false", createConfig().getConfigValue("kc.spi-sticky-session-encoder-infinispan-should-attach-route").getValue()); } private Config.Scope initConfig(String... scope) { diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/OptionValidationTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/OptionValidationTest.java index 14785de1c9..53ec93316d 100644 --- a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/OptionValidationTest.java +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/OptionValidationTest.java @@ -32,13 +32,13 @@ public class OptionValidationTest { @Test @Launch({"build", "--db"}) public void failMissingOptionValue(LaunchResult result) { - assertTrue(result.getErrorOutput().contains("Missing required value for option '--db' (vendor). Expected values are: h2-file, h2-mem, mariadb, mssql, mysql, oracle, postgres")); + assertTrue(result.getErrorOutput().contains("Missing required value for option '--db' (vendor). Expected values are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres")); } @Test @Launch({"build", "--db", "foo", "bar"}) public void failMultipleOptionValue(LaunchResult result) { - assertTrue(result.getErrorOutput().contains("Option '--db' expects a single value (vendor) Expected values are: h2-file, h2-mem, mariadb, mssql, mysql, oracle, postgres")); + assertTrue(result.getErrorOutput().contains("Option '--db' expects a single value (vendor) Expected values are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres")); } @Test diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/StartCommandTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/StartCommandTest.java index fbc4b084bf..09c5723408 100644 --- a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/StartCommandTest.java +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/StartCommandTest.java @@ -51,9 +51,9 @@ public class StartCommandTest { } @Test - @Launch({ "-v", "start", "--db=h2-mem" }) + @Launch({ "-v", "start", "--db=dev-mem" }) void failBuildPropertyNotAvailable(LaunchResult result) { CLIResult cliResult = (CLIResult) result; - cliResult.assertError("Unknown option: '--db=h2-mem'"); + cliResult.assertError("Unknown option: '--db=dev-mem'"); } } diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/StartDevCommandTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/StartDevCommandTest.java index b2c0042117..5790dc78c8 100644 --- a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/StartDevCommandTest.java +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/StartDevCommandTest.java @@ -35,7 +35,7 @@ public class StartDevCommandTest { } @Test - @Launch({ "start-dev", "--db=h2-mem" }) + @Launch({ "start-dev", "--db=dev-mem" }) void testBuildPropertyAvailable(LaunchResult result) { CLIResult cliResult = (CLIResult) result; cliResult.assertStartedDevMode(); diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/OptionValidationDistTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/OptionValidationDistTest.java index f319bd6ee6..3623795a79 100644 --- a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/OptionValidationDistTest.java +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/OptionValidationDistTest.java @@ -31,6 +31,6 @@ public class OptionValidationDistTest { @Test @Launch({"build", "--db=invalid"}) public void failInvalidOptionValue(LaunchResult result) { - Assertions.assertTrue(result.getErrorOutput().contains("Invalid value for option '--db': invalid. Expected values are: h2-file, h2-mem, mariadb, mssql, mysql, oracle, postgres")); + Assertions.assertTrue(result.getErrorOutput().contains("Invalid value for option '--db': invalid. Expected values are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres")); } } diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/StartAutoBuildDistTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/StartAutoBuildDistTest.java index 68925b5750..567e10d16e 100644 --- a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/StartAutoBuildDistTest.java +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/StartAutoBuildDistTest.java @@ -61,7 +61,7 @@ public class StartAutoBuildDistTest { } @Test - @Launch({ "start", "--auto-build", "--db=h2-mem", "--http-enabled=true", "--hostname-strict=false", "--cache=local" }) + @Launch({ "start", "--auto-build", "--db=dev-mem", "--http-enabled=true", "--hostname-strict=false", "--cache=local" }) @Order(3) void testShouldReAugIfConfigChanged(LaunchResult result) { CLIResult cliResult = (CLIResult) result; @@ -70,7 +70,7 @@ public class StartAutoBuildDistTest { } @Test - @Launch({ "start", "--auto-build", "--db=h2-mem", "--http-enabled=true", "--hostname-strict=false", "--cache=local" }) + @Launch({ "start", "--auto-build", "--db=dev-mem", "--http-enabled=true", "--hostname-strict=false", "--cache=local" }) @Order(4) void testShouldNotReAugIfSameDatabase(LaunchResult result) { CLIResult cliResult = (CLIResult) result; diff --git a/quarkus/tests/integration/src/test/resources/ShowConfigCommandTest/keycloak.conf b/quarkus/tests/integration/src/test/resources/ShowConfigCommandTest/keycloak.conf index e14c68df62..2aa6e936d5 100644 --- a/quarkus/tests/integration/src/test/resources/ShowConfigCommandTest/keycloak.conf +++ b/quarkus/tests/integration/src/test/resources/ShowConfigCommandTest/keycloak.conf @@ -1,5 +1,5 @@ # Default and non-production grade database vendor -db=h2-file +db=dev-file db-username = sa db-password = keycloak diff --git a/quarkus/tests/integration/src/test/resources/org/keycloak/it/cli/approvals/cli/help/HelpCommandTest.testBuildHelp.approved.txt b/quarkus/tests/integration/src/test/resources/org/keycloak/it/cli/approvals/cli/help/HelpCommandTest.testBuildHelp.approved.txt index 0de0c370e6..5efa1434cd 100644 --- a/quarkus/tests/integration/src/test/resources/org/keycloak/it/cli/approvals/cli/help/HelpCommandTest.testBuildHelp.approved.txt +++ b/quarkus/tests/integration/src/test/resources/org/keycloak/it/cli/approvals/cli/help/HelpCommandTest.testBuildHelp.approved.txt @@ -36,7 +36,7 @@ Cluster: Database: ---db The database vendor. Possible values are: h2-file, h2-mem, mariadb, mssql, +--db The database vendor. Possible values are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres Feature: diff --git a/quarkus/tests/integration/src/test/resources/org/keycloak/it/cli/approvals/cli/help/HelpCommandTest.testStartDevHelpAll.approved.txt b/quarkus/tests/integration/src/test/resources/org/keycloak/it/cli/approvals/cli/help/HelpCommandTest.testStartDevHelpAll.approved.txt index 54be33a9d3..a91dfb6efc 100644 --- a/quarkus/tests/integration/src/test/resources/org/keycloak/it/cli/approvals/cli/help/HelpCommandTest.testStartDevHelpAll.approved.txt +++ b/quarkus/tests/integration/src/test/resources/org/keycloak/it/cli/approvals/cli/help/HelpCommandTest.testStartDevHelpAll.approved.txt @@ -26,7 +26,7 @@ Cluster: Database: ---db The database vendor. Possible values are: h2-file, h2-mem, mariadb, mssql, +--db The database vendor. Possible values are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres --db-password The password of the database user. diff --git a/testsuite/integration-arquillian/servers/auth-server/quarkus/src/main/content/conf/keycloak.conf b/testsuite/integration-arquillian/servers/auth-server/quarkus/src/main/content/conf/keycloak.conf index fed3044098..cf04ad3251 100644 --- a/testsuite/integration-arquillian/servers/auth-server/quarkus/src/main/content/conf/keycloak.conf +++ b/testsuite/integration-arquillian/servers/auth-server/quarkus/src/main/content/conf/keycloak.conf @@ -1,5 +1,5 @@ # H2 -db=h2-file +db=dev-file db-username = sa db-password = keycloak