From 5a96efad11bdbd52779d32001cb11d3573e29bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Barto=C5=A1?= Date: Mon, 24 Apr 2023 18:11:54 +0200 Subject: [PATCH] Do not display error log for initial admin creation Closes #15789 Co-authored-by: Steve Weixel --- .../quarkus/runtime/KeycloakMain.java | 3 -- .../it/cli/dist/BuildAndStartDistTest.java | 30 +++++++++++++++++++ .../org/keycloak/services/ServicesLogger.java | 4 +++ .../services/managers/ApplianceBootstrap.java | 5 +++- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/KeycloakMain.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/KeycloakMain.java index 6b2092093b..a98145af8f 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/KeycloakMain.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/KeycloakMain.java @@ -156,10 +156,7 @@ public class KeycloakMain implements QuarkusApplication { try { KeycloakModelUtils.runJobInTransaction(sessionFactory, session -> { new ApplianceBootstrap(session).createMasterRealmUser(adminUserName, adminPassword); - ServicesLogger.LOGGER.addUserSuccess(adminUserName, Config.getAdminRealm()); }); - } catch (IllegalStateException e) { - ServicesLogger.LOGGER.addUserFailedUserExists(adminUserName, Config.getAdminRealm()); } catch (Throwable t) { ServicesLogger.LOGGER.addUserFailed(t, adminUserName, Config.getAdminRealm()); } diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/BuildAndStartDistTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/BuildAndStartDistTest.java index cfe612d19c..5682f21bc6 100644 --- a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/BuildAndStartDistTest.java +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/BuildAndStartDistTest.java @@ -17,15 +17,19 @@ package org.keycloak.it.cli.dist; +import io.quarkus.test.junit.main.Launch; +import io.quarkus.test.junit.main.LaunchResult; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; import org.keycloak.it.junit5.extension.CLIResult; import org.keycloak.it.junit5.extension.DistributionTest; import org.keycloak.it.junit5.extension.RawDistOnly; +import org.keycloak.it.junit5.extension.WithEnvVars; import org.keycloak.it.utils.KeycloakDistribution; import org.keycloak.it.utils.RawKeycloakDistribution; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.keycloak.quarkus.runtime.cli.command.AbstractStartCommand.OPTIMIZED_BUILD_OPTION_LONG; @DistributionTest @@ -63,4 +67,30 @@ public class BuildAndStartDistTest { cliResult.assertBuild(); cliResult.assertStarted(); } + + @Test + @WithEnvVars({"KEYCLOAK_ADMIN", "admin123", "KEYCLOAK_ADMIN_PASSWORD", "admin123"}) + @Launch({"start-dev"}) + void testCreateAdmin(KeycloakDistribution dist, LaunchResult result) { + assertAdminCreation(dist, result, "admin123", "admin123", "admin123"); + } + + @Test + @WithEnvVars({"KEYCLOAK_ADMIN", "admin123", "KEYCLOAK_ADMIN_PASSWORD", "admin123"}) + @Launch({"start-dev"}) + void testCreateDifferentAdmin(KeycloakDistribution dist, LaunchResult result) { + assertAdminCreation(dist, result, "admin123", "new-admin", "new-admin"); + } + + private void assertAdminCreation(KeycloakDistribution dist, LaunchResult result, String initialUsername, String nextUsername, String password) { + assertTrue(result.getOutput().contains("Added user '" + initialUsername + "' to realm 'master'"), + () -> "The Output:\n" + result.getOutput() + "doesn't contains the expected string."); + + dist.setEnvVar("KEYCLOAK_ADMIN", nextUsername); + dist.setEnvVar("KEYCLOAK_ADMIN_PASSWORD", password); + CLIResult cliResult = dist.run("start-dev", "--log-level=debug"); + + cliResult.assertMessage("Skipping create admin user. Admin already exists in realm 'master'."); + cliResult.assertStartedDevMode(); + } } diff --git a/services/src/main/java/org/keycloak/services/ServicesLogger.java b/services/src/main/java/org/keycloak/services/ServicesLogger.java index cb4c15d0b8..53a080def6 100644 --- a/services/src/main/java/org/keycloak/services/ServicesLogger.java +++ b/services/src/main/java/org/keycloak/services/ServicesLogger.java @@ -467,4 +467,8 @@ public interface ServicesLogger extends BasicLogger { @Message(id=106, value="Created script engine '%s', version '%s' for the mime type '%s'") @Once void scriptEngineCreated(String engineName, String engineVersion, String mimeType); + + @LogMessage(level = DEBUG) + @Message(id=107, value="Skipping create admin user. Admin already exists in realm '%s'.") + void addAdminUserFailedAdminExists(String realm); } diff --git a/services/src/main/java/org/keycloak/services/managers/ApplianceBootstrap.java b/services/src/main/java/org/keycloak/services/managers/ApplianceBootstrap.java index e22be98a0d..10b6a924ce 100755 --- a/services/src/main/java/org/keycloak/services/managers/ApplianceBootstrap.java +++ b/services/src/main/java/org/keycloak/services/managers/ApplianceBootstrap.java @@ -97,7 +97,8 @@ public class ApplianceBootstrap { session.getContext().setRealm(realm); if (session.users().getUsersCount(realm) > 0) { - throw new IllegalStateException("Can't create initial user as users already exists"); + ServicesLogger.LOGGER.addAdminUserFailedAdminExists(Config.getAdminRealm()); + return; } UserModel adminUser = session.users().addUser(realm, username); @@ -108,6 +109,8 @@ public class ApplianceBootstrap { RoleModel adminRole = realm.getRole(AdminRoles.ADMIN); adminUser.grantRole(adminRole); + + ServicesLogger.LOGGER.addUserSuccess(username, Config.getAdminRealm()); } }