From 9358535161721fb14f6efdb8a68888945de0b6e6 Mon Sep 17 00:00:00 2001 From: Dominik Guhr <89905860+DGuhr@users.noreply.github.com> Date: Tue, 22 Feb 2022 08:19:45 +0100 Subject: [PATCH] Fix admin user creation message when calling quarkus welcomepage from remote (#10362) For wildfly, everything is as before. For Quarkus, we check if http is enabled and provide the right port and scheme if so, and also we are relative-path aware. Closes #10335 --- .../resources/QuarkusWelcomeResource.java | 22 ++++++++++++++++++- .../services/resources/WelcomeResource.java | 3 ++- .../theme/keycloak/welcome/index.ftl | 18 +++++++-------- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/services/resources/QuarkusWelcomeResource.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/services/resources/QuarkusWelcomeResource.java index f5b65ac26bb8..c74a5c9bc1f1 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/services/resources/QuarkusWelcomeResource.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/services/resources/QuarkusWelcomeResource.java @@ -24,10 +24,10 @@ import org.keycloak.common.util.MimeTypeUtil; import org.keycloak.common.util.SecretGenerator; import org.keycloak.models.KeycloakSession; +import org.keycloak.quarkus.runtime.configuration.Configuration; import org.keycloak.services.ForbiddenException; import org.keycloak.services.ServicesLogger; import org.keycloak.services.managers.ApplianceBootstrap; -import org.keycloak.services.resources.WelcomeResource; import org.keycloak.services.util.CacheControlUtil; import org.keycloak.services.util.CookieHelper; import org.keycloak.theme.FreeMarkerUtil; @@ -188,6 +188,11 @@ private Response createWelcomePage(String successMessage, String errorMessage) { boolean isLocal = isLocal(); map.put("localUser", isLocal); + String localAdminUrl = getLocalAdminUrl(); + + map.put("localAdminUrl", localAdminUrl); + map.put("adminUserCreationMessage", "or set the environment variables KEYCLOAK_ADMIN and KEYCLOAK_ADMIN_PASSWORD before starting the server"); + if (isLocal) { String stateChecker = setCsrfCookie(); map.put("stateChecker", stateChecker); @@ -211,6 +216,21 @@ private Response createWelcomePage(String successMessage, String errorMessage) { } } + private String getLocalAdminUrl() { + boolean isHttpEnabled = Boolean.parseBoolean(Configuration.getConfigValue("kc.http-enabled").getValue()); + String configPath = Configuration.getConfigValue("kc.http-relative-path").getValue(); + + if (!configPath.startsWith("/")) { + configPath = "/" + configPath; + } + + String configPort = isHttpEnabled ? Configuration.getConfigValue("kc.http-port").getValue() : Configuration.getConfigValue("kc.https-port").getValue() ; + + String scheme = isHttpEnabled ? "http://" : "https://"; + + return scheme + "localhost:" + configPort + configPath; + } + private Theme getTheme() { try { return session.theme().getTheme(Theme.Type.WELCOME); diff --git a/services/src/main/java/org/keycloak/services/resources/WelcomeResource.java b/services/src/main/java/org/keycloak/services/resources/WelcomeResource.java index 00ffcef47812..e017d485339b 100755 --- a/services/src/main/java/org/keycloak/services/resources/WelcomeResource.java +++ b/services/src/main/java/org/keycloak/services/resources/WelcomeResource.java @@ -181,7 +181,6 @@ private Response createWelcomePage(String successMessage, String errorMessage) { map.put("properties", theme.getProperties()); map.put("adminUrl", session.getContext().getUri(UrlType.ADMIN).getBaseUriBuilder().path("/admin/").build()); - map.put("resourcesPath", "resources/" + Version.RESOURCES_VERSION + "/" + theme.getType().toString().toLowerCase() +"/" + theme.getName()); map.put("resourcesCommonPath", "resources/" + Version.RESOURCES_VERSION + "/common/keycloak"); @@ -190,6 +189,8 @@ private Response createWelcomePage(String successMessage, String errorMessage) { if (bootstrap) { boolean isLocal = isLocal(); map.put("localUser", isLocal); + map.put("localAdminUrl", "http://localhost:8080/auth"); + map.put("adminUserCreationMessage","or use the add-user-keycloak script"); if (isLocal) { String stateChecker = setCsrfCookie(); diff --git a/themes/src/main/resources/theme/keycloak/welcome/index.ftl b/themes/src/main/resources/theme/keycloak/welcome/index.ftl index eba148bd31a9..e46f9e66d5bc 100755 --- a/themes/src/main/resources/theme/keycloak/welcome/index.ftl +++ b/themes/src/main/resources/theme/keycloak/welcome/index.ftl @@ -60,15 +60,15 @@

${errorMessage}

Administration Console

<#elseif bootstrap> - <#if localUser> -

Administration Console

-

Please create an initial admin user to get started.

- <#else> -

- You need local access to create the initial admin user.

Open http://localhost:8080/auth -
or use the add-user-keycloak script. -

- + <#if localUser> +

Administration Console

+

Please create an initial admin user to get started.

+ <#else> +

+ You need local access to create the initial admin user.

Open ${localAdminUrl} +
${adminUserCreationMessage}. +

+ <#if bootstrap && localUser>