Added Config to centralize system properties used to configure KC

This commit is contained in:
Stian Thorgersen 2014-03-05 15:19:37 +00:00
parent 0219aa1e4c
commit 60bb05e6ca
18 changed files with 133 additions and 54 deletions

View file

@ -71,15 +71,15 @@ module.controller('GlobalCtrl', function($scope, $http, Auth, Current, $location
});
});
module.controller('HomeCtrl', function(Realm, $location) {
module.controller('HomeCtrl', function(Realm, Auth, $location) {
Realm.query(null, function(realms) {
var realm;
if (realms.length == 1) {
realm = realms[0].realm;
} else if (realms.length == 2) {
if (realms[0].realm == 'keycloak-admin') {
if (realms[0].realm == Auth.user.realm) {
realm = realms[1].realm;
} else if (realms[1].realm == 'administration') {
} else if (realms[1].realm == Auth.user.realm) {
realm = realms[0].realm;
}
}

View file

@ -16,7 +16,7 @@
{{auth.user.displayName}}<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="/auth/rest/realms/keycloak-admin/account?referrer=admin-console">Manage Account</a></li>
<li><a href="/auth/rest/realms/{{auth.user.realm}}/account?referrer=admin-console">Manage Account</a></li>
<li class="separator"><a href="/auth/rest/admin/logout">Sign Out</a></li>
</ul>
</li>

View file

@ -18,6 +18,11 @@
<artifactId>keycloak-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>

View file

@ -1,6 +1,7 @@
package org.keycloak.freemarker;
import org.jboss.resteasy.logging.Logger;
import org.keycloak.models.Config;
import org.keycloak.util.ProviderLoader;
import java.io.IOException;
@ -19,12 +20,10 @@ import java.util.Properties;
public class ThemeLoader {
private static final Logger logger = Logger.getLogger(ThemeLoader.class);
private static String DEFAULT = "keycloak";
public static final String BASE = "base";
public static Theme createTheme(String name, Theme.Type type) throws FreeMarkerException {
if (name == null) {
name = DEFAULT;
name = Config.getThemeDefault();
}
List<ThemeProvider> providers = new LinkedList();
@ -61,21 +60,21 @@ public class ThemeLoader {
try {
return p.createTheme(name, type);
} catch (IOException e) {
if (name.equals(BASE)) {
if (name.equals(Config.getThemeBase())) {
throw new FreeMarkerException("Failed to create " + type.toString().toLowerCase() + " theme", e);
} else {
logger.error("Failed to create " + type.toString().toLowerCase() + " theme", e);
return findTheme(providers, BASE, type);
return findTheme(providers, Config.getThemeBase(), type);
}
}
}
}
if (name.equals(BASE)) {
if (name.equals(Config.getThemeBase())) {
throw new FreeMarkerException(type.toString().toLowerCase() + " theme '" + name + "' not found");
} else {
logger.error(type.toString().toLowerCase() + " theme '" + name + "' not found");
return findTheme(providers, BASE, type);
return findTheme(providers, Config.getThemeBase(), type);
}
}

View file

@ -18,6 +18,8 @@ public class ClassLoaderTheme implements Theme {
private final Type type;
private final ClassLoader classLoader;
private final String templateRoot;
private final String resourceRoot;
@ -26,9 +28,10 @@ public class ClassLoaderTheme implements Theme {
private final Properties properties;
public ClassLoaderTheme(String name, Type type) throws IOException {
public ClassLoaderTheme(String name, Type type, ClassLoader classLoader) throws IOException {
this.name = name;
this.type = type;
this.classLoader = classLoader;
String themeRoot = "theme/" + type.toString().toLowerCase() + "/" + name + "/";
@ -37,7 +40,7 @@ public class ClassLoaderTheme implements Theme {
this.messages = themeRoot + "messages/messages.properties";
this.properties = new Properties();
URL p = getClass().getClassLoader().getResource(themeRoot + "theme.properties");
URL p = classLoader.getResource(themeRoot + "theme.properties");
if (p != null) {
properties.load(p.openStream());
this.parentName = properties.getProperty("parent");
@ -63,28 +66,28 @@ public class ClassLoaderTheme implements Theme {
@Override
public URL getTemplate(String name) {
return getClass().getClassLoader().getResource(templateRoot + name);
return classLoader.getResource(templateRoot + name);
}
@Override
public InputStream getTemplateAsStream(String name) {
return getClass().getClassLoader().getResourceAsStream(templateRoot + name);
return classLoader.getResourceAsStream(templateRoot + name);
}
@Override
public URL getResource(String path) {
return getClass().getClassLoader().getResource(resourceRoot + path);
return classLoader.getResource(resourceRoot + path);
}
@Override
public InputStream getResourceAsStream(String path) {
return getClass().getClassLoader().getResourceAsStream(resourceRoot + path);
return classLoader.getResourceAsStream(resourceRoot + path);
}
@Override
public Properties getMessages() throws IOException {
Properties m = new Properties();
URL url = getClass().getClassLoader().getResource(this.messages);
URL url = classLoader.getResource(this.messages);
if (url != null) {
m.load(url.openStream());
}

View file

@ -3,6 +3,7 @@ package org.keycloak.theme;
import org.keycloak.freemarker.Theme;
import org.keycloak.freemarker.ThemeLoader;
import org.keycloak.freemarker.ThemeProvider;
import org.keycloak.models.Config;
import java.io.IOException;
import java.util.Collections;
@ -14,13 +15,14 @@ import java.util.Set;
*/
public class DefaultLoginThemeProvider implements ThemeProvider {
public static final String BASE = "base";
public static final String PATTERNFLY = "patternfly";
public static final String KEYCLOAK = "keycloak";
private static Set<String> defaultThemes = new HashSet<String>();
static {
defaultThemes.add(ThemeLoader.BASE);
defaultThemes.add(BASE);
defaultThemes.add(PATTERNFLY);
defaultThemes.add(KEYCLOAK);
}
@ -33,7 +35,7 @@ public class DefaultLoginThemeProvider implements ThemeProvider {
@Override
public Theme createTheme(String name, Theme.Type type) throws IOException {
if (hasTheme(name, type)) {
return new ClassLoaderTheme(name, type);
return new ClassLoaderTheme(name, type, getClass().getClassLoader());
} else {
return null;
}

View file

@ -2,6 +2,7 @@ package org.keycloak.theme;
import org.keycloak.freemarker.Theme;
import org.keycloak.freemarker.ThemeProvider;
import org.keycloak.models.Config;
import java.io.File;
import java.io.FileFilter;
@ -18,7 +19,7 @@ public class FolderThemeProvider implements ThemeProvider {
private File rootDir;
public FolderThemeProvider() {
String d = System.getProperty("keycloak.theme.dir");
String d = Config.getThemeDir();
if (d != null) {
rootDir = new File(d);
}

View file

@ -0,0 +1,66 @@
package org.keycloak.models;
import java.io.File;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class Config {
public static final String ADMIN_REALM_KEY = "keycloak.admin.realm";
public static final String ADMIN_REALM_DEFAULT = "keycloak-admin";
public static final String MODEL_PROVIDER_KEY = "keycloak.model";
public static final String THEME_BASE_KEY = "keycloak.theme.base";
public static final String THEME_BASE_DEFAULT = "base";
public static final String THEME_DEFAULT_KEY = "keycloak.theme.default";
public static final String THEME_DEFAULT_DEFAULT = "keycloak";
public static final String THEME_DIR_KEY = "keycloak.theme.dir";
public static final String JBOSS_SERVER_CONFIG_DIR_KEY = "jboss.server.config.dir";
public static String getAdminRealm() {
return System.getProperty(ADMIN_REALM_KEY, ADMIN_REALM_DEFAULT);
}
public static void setAdminRealm(String realm) {
System.setProperty(ADMIN_REALM_KEY, realm);
}
public static String getModelProvider() {
return System.getProperty(MODEL_PROVIDER_KEY);
}
public static void setModelProvider(String provider) {
System.setProperty(MODEL_PROVIDER_KEY, provider);
}
public static String getThemeDir() {
String themeDir = System.getProperty(THEME_DIR_KEY);
if (themeDir == null && System.getProperties().containsKey(JBOSS_SERVER_CONFIG_DIR_KEY)) {
themeDir = System.getProperty(JBOSS_SERVER_CONFIG_DIR_KEY) + File.separator + "themes";
}
return themeDir;
}
public static void setThemeDir(String dir) {
System.setProperty(THEME_DIR_KEY, dir);
}
public static String getThemeBase() {
return System.getProperty(THEME_BASE_KEY, THEME_BASE_DEFAULT);
}
public static void setThemeBase(String baseTheme) {
System.setProperty(THEME_BASE_KEY, baseTheme);
}
public static String getThemeDefault() {
return System.getProperty(THEME_DEFAULT_KEY, THEME_DEFAULT_DEFAULT);
}
public static void setThemeDefault(String defaultTheme) {
System.setProperty(THEME_DEFAULT_KEY, defaultTheme);
}
}

View file

@ -5,7 +5,6 @@ package org.keycloak.models;
* @version $Revision: 1 $
*/
public interface Constants {
String ADMIN_REALM = "keycloak-admin";
String ADMIN_CONSOLE_APPLICATION = "admin-console";
String INTERNAL_ROLE = "KEYCLOAK_";

View file

@ -2,6 +2,7 @@ package org.keycloak.models.utils;
import java.util.ServiceLoader;
import org.keycloak.models.Config;
import org.keycloak.models.ModelProvider;
/**
@ -9,7 +10,6 @@ import org.keycloak.models.ModelProvider;
*/
public class ModelProviderUtils {
public static final String MODEL_PROVIDER = "keycloak.model";
public static final String DEFAULT_MODEL_PROVIDER = "jpa";
public static Iterable<ModelProvider> getRegisteredProviders() {
@ -17,7 +17,7 @@ public class ModelProviderUtils {
}
public static ModelProvider getConfiguredModelProvider(Iterable<ModelProvider> providers) {
String configuredProvider = System.getProperty(MODEL_PROVIDER);
String configuredProvider = Config.getModelProvider();
ModelProvider provider = null;
if (configuredProvider != null) {
@ -46,5 +46,4 @@ public class ModelProviderUtils {
return getConfiguredModelProvider(getRegisteredProviders());
}
}

View file

@ -1,6 +1,7 @@
package org.keycloak.server;
import org.jboss.resteasy.logging.Logger;
import org.keycloak.models.Config;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.representations.idm.RealmRepresentation;
@ -30,15 +31,6 @@ public class KeycloakServerApplication extends KeycloakApplication {
RealmRepresentation rep = loadJson(new FileInputStream(importRealm), RealmRepresentation.class);
importRealm(session, rep);
}
String themeDir = System.getProperty("keycloak.theme.dir");
if (themeDir == null) {
String jbossConfigDir = System.getProperty("jboss.server.config.dir");
if (jbossConfigDir != null) {
themeDir = jbossConfigDir + File.separator + "themes";
System.setProperty("keycloak.theme.dir", themeDir);
}
}
}
public void importRealm(KeycloakSession session, RealmRepresentation rep) {

View file

@ -3,6 +3,7 @@ package org.keycloak.services.managers;
import org.jboss.resteasy.logging.Logger;
import org.keycloak.models.AdminRoles;
import org.keycloak.models.ApplicationModel;
import org.keycloak.models.Config;
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
@ -34,15 +35,17 @@ public class ApplianceBootstrap {
}
public void bootstrap(KeycloakSession session) {
if (session.getRealm(Constants.ADMIN_REALM) != null) {
if (session.getRealm(Config.getAdminRealm()) != null) {
return;
}
logger.info("Initializing " + Constants.ADMIN_REALM + " realm");
String adminRealmName = Config.getAdminRealm();
logger.info("Initializing " + adminRealmName + " realm");
RealmManager manager = new RealmManager(session);
RealmModel realm = manager.createRealm(Constants.ADMIN_REALM, Constants.ADMIN_REALM);
realm.setName(Constants.ADMIN_REALM);
RealmModel realm = manager.createRealm(adminRealmName, adminRealmName);
realm.setName(adminRealmName);
realm.setEnabled(true);
realm.addRequiredCredential(CredentialRepresentation.PASSWORD);
realm.setCentralLoginLifespan(3000);
@ -54,9 +57,6 @@ public class ApplianceBootstrap {
realm.setRegistrationAllowed(false);
manager.generateRealmKeys(realm);
realm.setLoginTheme("keycloak");
realm.setAccountTheme("keycloak");
ApplicationModel adminConsole = new ApplicationManager(manager).createApplication(realm, Constants.ADMIN_CONSOLE_APPLICATION);
adminConsole.setBaseUrl("/auth/admin/index.html");
adminConsole.setEnabled(true);

View file

@ -5,6 +5,7 @@ import org.keycloak.models.AccountRoles;
import org.keycloak.models.AdminRoles;
import org.keycloak.models.ApplicationModel;
import org.keycloak.models.ClientModel;
import org.keycloak.models.Config;
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.OAuthClientModel;
@ -51,7 +52,7 @@ public class RealmManager {
}
public RealmModel getKeycloakAdminstrationRealm() {
return getRealm(Constants.ADMIN_REALM);
return getRealm(Config.getAdminRealm());
}
public RealmModel getRealm(String id) {
@ -153,7 +154,7 @@ public class RealmManager {
RealmModel adminRealm;
RoleModel adminRole;
if (realm.getName().equals(Constants.ADMIN_REALM)) {
if (realm.getName().equals(Config.getAdminRealm())) {
adminRealm = realm;
adminRole = realm.addRole(AdminRoles.ADMIN);
@ -161,7 +162,7 @@ public class RealmManager {
RoleModel createRealmRole = realm.addRole(AdminRoles.CREATE_REALM);
adminRole.addCompositeRole(createRealmRole);
} else {
adminRealm = identitySession.getRealmByName(Constants.ADMIN_REALM);
adminRealm = identitySession.getRealmByName(Config.getAdminRealm());
adminRole = adminRealm.getRole(AdminRoles.ADMIN);
}

View file

@ -51,8 +51,6 @@ public class AccountService {
private static final Logger logger = Logger.getLogger(AccountService.class);
public static final String ACCOUNT_IDENTITY_COOKIE = "KEYCLOAK_ACCOUNT_IDENTITY";
private RealmModel realm;
@Context

View file

@ -8,6 +8,7 @@ import org.jboss.resteasy.spi.HttpResponse;
import org.keycloak.jaxrs.JaxrsOAuthClient;
import org.keycloak.models.AdminRoles;
import org.keycloak.models.ApplicationModel;
import org.keycloak.models.Config;
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
@ -84,6 +85,7 @@ public class AdminService {
public static class WhoAmI {
protected String userId;
protected String realm;
protected String displayName;
@JsonProperty("createRealm")
@ -94,8 +96,9 @@ public class AdminService {
public WhoAmI() {
}
public WhoAmI(String userId, String displayName, boolean createRealm, Map<String, Set<String>> realmAccess) {
public WhoAmI(String userId, String realm, String displayName, boolean createRealm, Map<String, Set<String>> realmAccess) {
this.userId = userId;
this.realm = realm;
this.displayName = displayName;
this.createRealm = createRealm;
this.realmAccess = realmAccess;
@ -109,6 +112,14 @@ public class AdminService {
this.userId = userId;
}
public String getRealm() {
return realm;
}
public void setRealm(String realm) {
this.realm = realm;
}
public String getDisplayName() {
return displayName;
}
@ -185,7 +196,7 @@ public class AdminService {
Map<String, Set<String>> realmAccess = new HashMap<String, Set<String>>();
addRealmAdminAccess(realmAccess, auth.getRealm().getRoleMappings(auth.getUser()));
return Response.ok(new WhoAmI(user.getId(), displayName, createRealm, realmAccess)).build();
return Response.ok(new WhoAmI(user.getId(), Config.getAdminRealm(), displayName, createRealm, realmAccess)).build();
}
private void addRealmAdminAccess(Map<String, Set<String>> realmAdminAccess, Set<RoleModel> roles) {
@ -283,7 +294,7 @@ public class AdminService {
expireCookie();
JaxrsOAuthClient oauth = new JaxrsOAuthClient();
String authUrl = TokenService.loginPageUrl(uriInfo).build(Constants.ADMIN_REALM).toString();
String authUrl = TokenService.loginPageUrl(uriInfo).build(Config.getAdminRealm()).toString();
logger.debug("authUrl: {0}", authUrl);
oauth.setAuthUrl(authUrl);
oauth.setClientId(Constants.ADMIN_CONSOLE_APPLICATION);
@ -309,7 +320,7 @@ public class AdminService {
protected Response redirectOnLoginError(String message) {
URI uri = uriInfo.getBaseUriBuilder().path(AdminService.class).path(AdminService.class, "errorOnLoginRedirect").queryParam("error", message).build();
URI logout = TokenService.logoutUrl(uriInfo).queryParam("redirect_uri", uri.toString()).build(Constants.ADMIN_REALM);
URI logout = TokenService.logoutUrl(uriInfo).queryParam("redirect_uri", uri.toString()).build(Config.getAdminRealm());
return Response.status(302).location(logout).build();
}

View file

@ -37,6 +37,7 @@ import io.undertow.servlet.api.ServletInfo;
import org.jboss.resteasy.logging.Logger;
import org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.keycloak.models.Config;
import org.keycloak.theme.DefaultLoginThemeProvider;
import org.keycloak.services.tmp.TmpAdminRedirectServlet;
import org.keycloak.util.JsonSerialization;
@ -136,9 +137,9 @@ public class KeycloakServer {
throw new RuntimeException("Invalid resources directory");
}
if (!System.getProperties().containsKey("keycloak.theme.dir")) {
if (Config.getThemeDir() == null) {
System.setProperty(DefaultLoginThemeProvider.class.getName() + ".disabled", "");
System.setProperty("keycloak.theme.dir", file(dir.getAbsolutePath(), "forms", "common-themes", "src", "main", "resources", "theme").getAbsolutePath());
Config.setThemeDir(file(dir.getAbsolutePath(), "forms", "common-themes", "src", "main", "resources", "theme").getAbsolutePath());
}
config.setResourcesHome(dir.getAbsolutePath());

View file

@ -3,6 +3,7 @@ package org.keycloak.testsuite.rule;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.ServletInfo;
import org.junit.rules.ExternalResource;
import org.keycloak.models.Config;
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
@ -58,7 +59,7 @@ public abstract class AbstractKeycloakRule extends ExternalResource {
try {
RealmManager manager = new RealmManager(session);
RealmModel adminstrationRealm = manager.getRealm(Constants.ADMIN_REALM);
RealmModel adminstrationRealm = manager.getRealm(Config.getAdminRealm());
configure(manager, adminstrationRealm);

View file

@ -21,6 +21,7 @@
*/
package org.keycloak.testsuite.rule;
import org.keycloak.models.Config;
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
@ -66,7 +67,7 @@ public class KeycloakRule extends AbstractKeycloakRule {
try {
RealmManager manager = new RealmManager(session);
RealmModel adminstrationRealm = manager.getRealm(Constants.ADMIN_REALM);
RealmModel adminstrationRealm = manager.getRealm(Config.getAdminRealm());
RealmModel appRealm = manager.getRealm("test");
configurer.config(manager, adminstrationRealm, appRealm);