Skip to content

Commit

Permalink
Fix admin user creation message when calling quarkus welcomepage from…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
DGuhr committed Feb 22, 2022
1 parent 8c3fc5a commit 9358535
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Expand Up @@ -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");

Expand All @@ -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();
Expand Down
18 changes: 9 additions & 9 deletions themes/src/main/resources/theme/keycloak/welcome/index.ftl
Expand Up @@ -60,15 +60,15 @@
<p class="alert error">${errorMessage}</p>
<h3><img src="welcome-content/user.png">Administration Console</h3>
<#elseif bootstrap>
<#if localUser>
<h3><img src="welcome-content/user.png">Administration Console</h3>
<p>Please create an initial admin user to get started.</p>
<#else>
<p class="welcome-message">
<img src="welcome-content/alert.png">You need local access to create the initial admin user. <br><br>Open <a href="http://localhost:8080/auth">http://localhost:8080/auth</a>
<br>or use the add-user-keycloak script.
</p>
</#if>
<#if localUser>
<h3><img src="welcome-content/user.png">Administration Console</h3>
<p>Please create an initial admin user to get started.</p>
<#else>
<p class="welcome-message">
<img src="welcome-content/alert.png">You need local access to create the initial admin user. <br><br>Open <a href="${localAdminUrl}">${localAdminUrl}</a>
<br>${adminUserCreationMessage}.
</p>
</#if>
</#if>

<#if bootstrap && localUser>
Expand Down

0 comments on commit 9358535

Please sign in to comment.