Do not display error log for initial admin creation

Closes #15789

Co-authored-by: Steve Weixel <steve.weixel@quantum.com>
This commit is contained in:
Martin Bartoš 2023-04-24 18:11:54 +02:00 committed by Václav Muzikář
parent dcb7c498a4
commit 5a96efad11
4 changed files with 38 additions and 4 deletions

View file

@ -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());
}

View file

@ -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();
}
}

View file

@ -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);
}

View file

@ -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());
}
}