From b51661b790eab3c7f9f2abf533b1ce6dad8c371e Mon Sep 17 00:00:00 2001 From: Vlastimil Elias Date: Mon, 20 Jul 2015 17:11:04 +0200 Subject: [PATCH 1/5] KEYCLOAK-1542 - added system and memory info into "System Info" page --- .../admin/resources/partials/server-info.html | 75 +++++++- .../models/KeycloakSessionFactory.java | 2 + .../DefaultKeycloakSessionFactory.java | 12 ++ .../admin/ServerInfoAdminResource.java | 173 ++++++++++++++++-- 4 files changed, 249 insertions(+), 13 deletions(-) diff --git a/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info.html b/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info.html index 34c9a845d3..5590f9576e 100755 --- a/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info.html +++ b/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info.html @@ -8,10 +8,81 @@ Server Time - {{serverInfo.serverTime}} (update) + {{serverInfo.serverTime}} (update) + + + Server Uptime + {{serverInfo.serverUptime}} + + + Current Working Directory + {{serverInfo.systemInfo.userDir}} + + + Java Version + {{serverInfo.systemInfo.javaVersion}} + + + Java Vendor + {{serverInfo.systemInfo.javaVendor}} + + + Java Runtime + {{serverInfo.systemInfo.javaRuntime}} + + + Java VM + {{serverInfo.systemInfo.javaVm}} + + + Java VM Version + {{serverInfo.systemInfo.javaVmVersion}} + + + Java Home + {{serverInfo.systemInfo.javaHome}} + + + User Name + {{serverInfo.systemInfo.userName}} + + + User Timezone + {{serverInfo.systemInfo.userTimezone}} + + + User Locale + {{serverInfo.systemInfo.userLocale}} + + + System Encoding + {{serverInfo.systemInfo.fileEncoding}} + + + Operating System + {{serverInfo.systemInfo.osName}} {{serverInfo.systemInfo.osVersion}} + + + OS Architecture + {{serverInfo.systemInfo.osArchitecture}} - + +

Java VM Memory Statistics

+ + + + + + + + + + + + + +
Total Memory{{serverInfo.memoryInfo.totalFormated}}
Free Memory{{serverInfo.memoryInfo.freeFormated}} ({{serverInfo.memoryInfo.freePercentage}}%)
Used Memory{{serverInfo.memoryInfo.usedFormated}}
Providers diff --git a/model/api/src/main/java/org/keycloak/models/KeycloakSessionFactory.java b/model/api/src/main/java/org/keycloak/models/KeycloakSessionFactory.java index cbb3da22d5..82665508c2 100755 --- a/model/api/src/main/java/org/keycloak/models/KeycloakSessionFactory.java +++ b/model/api/src/main/java/org/keycloak/models/KeycloakSessionFactory.java @@ -18,6 +18,8 @@ public interface KeycloakSessionFactory extends ProviderEventManager { ProviderFactory getProviderFactory(Class clazz, String id); List getProviderFactories(Class clazz); + + long getServerStartupTimestamp(); void close(); } diff --git a/services/src/main/java/org/keycloak/services/DefaultKeycloakSessionFactory.java b/services/src/main/java/org/keycloak/services/DefaultKeycloakSessionFactory.java index e312aa0122..c5be21be10 100755 --- a/services/src/main/java/org/keycloak/services/DefaultKeycloakSessionFactory.java +++ b/services/src/main/java/org/keycloak/services/DefaultKeycloakSessionFactory.java @@ -28,6 +28,8 @@ public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory { private Map, Map> factoriesMap = new HashMap, Map>(); protected CopyOnWriteArrayList listeners = new CopyOnWriteArrayList(); + protected long serverStartupTimestamp; + @Override public void register(ProviderEventListener listener) { listeners.add(listener); @@ -46,6 +48,8 @@ public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory { } public void init() { + serverStartupTimestamp = System.currentTimeMillis(); + ProviderManager pm = new ProviderManager(getClass().getClassLoader(), Config.scope().getArray("providers")); for (Spi spi : ServiceLoader.load(Spi.class, getClass().getClassLoader())) { @@ -148,4 +152,12 @@ public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory { return factory.getClass().getPackage().getName().startsWith("org.keycloak"); } + /** + * @return timestamp of Keycloak server startup + */ + @Override + public long getServerStartupTimestamp() { + return serverStartupTimestamp; + } + } diff --git a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java b/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java index 4f1c22575b..c600b92e4b 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java @@ -1,5 +1,18 @@ package org.keycloak.services.resources.admin; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.ServiceLoader; +import java.util.Set; + +import javax.ws.rs.GET; +import javax.ws.rs.core.Context; + import org.keycloak.Version; import org.keycloak.broker.provider.IdentityProvider; import org.keycloak.broker.provider.IdentityProviderFactory; @@ -24,17 +37,6 @@ import org.keycloak.representations.idm.ProtocolMapperRepresentation; import org.keycloak.representations.idm.ProtocolMapperTypeRepresentation; import org.keycloak.social.SocialIdentityProvider; -import javax.ws.rs.GET; -import javax.ws.rs.core.Context; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.ServiceLoader; -import java.util.Set; - /** * @author Stian Thorgersen */ @@ -55,6 +57,7 @@ public class ServerInfoAdminResource { ServerInfoRepresentation info = new ServerInfoRepresentation(); info.version = Version.VERSION; info.serverTime = new Date().toString(); + info.serverStartupTime = session.getKeycloakSessionFactory().getServerStartupTimestamp(); setSocialProviders(info); setIdentityProviders(info); setThemes(info); @@ -187,12 +190,115 @@ public class ServerInfoAdminResource { info.clientImporters.add(data); } } + + public static class MemoryInfo{ + + public long getTotal(){ + return Runtime.getRuntime().maxMemory(); + } + + public String getTotalFormated(){ + return formatMemory(getTotal()); + } + + public long getFree(){ + return getTotal() - getUsed(); + } + public String getFreeFormated(){ + return formatMemory(getFree()); + } + + public long getUsed(){ + return Runtime.getRuntime().totalMemory(); + } + + public String getUsedFormated(){ + return formatMemory(getUsed()); + } + + public long getFreePercentage(){ + return getFree()*100/getTotal(); + } + + private String formatMemory(long bytes){ + if(bytes > 1024L*1024L){ + return bytes/(1024L *1024L) + " MB"; + } else if(bytes > 1024L){ + return bytes/(1024L) + " kB"; + } else { + return bytes + " B"; + } + } + + } + + public static class SystemInfo { + public String getJavaVersion(){ + return System.getProperty("java.version"); + } + + public String getJavaVendor(){ + return System.getProperty("java.vendor"); + } + + public String getJavaVm(){ + return System.getProperty("java.vm.name"); + } + + public String getJavaVmVersion(){ + return System.getProperty("java.vm.version"); + } + + public String getJavaRuntime(){ + return System.getProperty("java.runtime.name"); + } + + public String getJavaHome(){ + return System.getProperty("java.home"); + } + + public String getOsName(){ + return System.getProperty("os.name"); + } + + public String getOsArchitecture(){ + return System.getProperty("os.arch"); + } + + public String getOsVersion(){ + return System.getProperty("os.version"); + } + + public String getFileEncoding(){ + return System.getProperty("file.encoding"); + } + + public String getUserName(){ + return System.getProperty("user.name"); + } + + public String getUserDir(){ + return System.getProperty("user.dir"); + } + + public String getUserTimezone(){ + return System.getProperty("user.timezone"); + } + + public String getUserLocale(){ + return (new Locale(System.getProperty("user.country"),System.getProperty("user.language")).toString()); + } + + } + public static class ServerInfoRepresentation { private String version; private String serverTime; + + private long serverStartupTime; private Map> themes; @@ -211,10 +317,55 @@ public class ServerInfoAdminResource { public ServerInfoRepresentation() { } + + public SystemInfo getSystemInfo(){ + return new SystemInfo(); + } + + public MemoryInfo getMemoryInfo(){ + return new MemoryInfo(); + } public String getServerTime() { return serverTime; } + + /** + * @return server startup time formatted + */ + public String getServerStartupTime() { + return (new Date(serverStartupTime)).toString(); + } + + /** + * @return server uptime in millis + */ + public long getServerUptimeMillis(){ + return System.currentTimeMillis() - serverStartupTime; + } + + /** + * @return server uptime formatted like "0 days, 10 hours, 24 minutes, 55 seconds" + */ + public String getServerUptime(){ + long diffInSeconds = getServerUptimeMillis()/1000; + long diff[] = new long[] { 0, 0, 0, 0 }; + /* sec */diff[3] = (diffInSeconds >= 60 ? diffInSeconds % 60 : diffInSeconds); + /* min */diff[2] = (diffInSeconds = (diffInSeconds / 60)) >= 60 ? diffInSeconds % 60 : diffInSeconds; + /* hours */diff[1] = (diffInSeconds = (diffInSeconds / 60)) >= 24 ? diffInSeconds % 24 : diffInSeconds; + /* days */diff[0] = (diffInSeconds = (diffInSeconds / 24)); + + return String.format( + "%d day%s, %d hour%s, %d minute%s, %d second%s", + diff[0], + diff[0] != 1 ? "s" : "", + diff[1], + diff[1] != 1 ? "s" : "", + diff[2], + diff[2] != 1 ? "s" : "", + diff[3], + diff[3] != 1 ? "s" : ""); + } public String getVersion() { return version; From 84e40cbc2c108aca6503563127b886c98b9fe10a Mon Sep 17 00:00:00 2001 From: Vlastimil Elias Date: Tue, 21 Jul 2015 16:09:47 +0200 Subject: [PATCH 2/5] KEYCLOAK-1542 - Server Info page extended by info about DB and MongoDB. Functional test for /serverinfo REST endpoint added. --- .../DefaultJpaConnectionProviderFactory.java | 341 ++++++++++------- .../jpa/JpaConnectionProviderFactory.java | 5 +- ...DefaultMongoConnectionFactoryProvider.java | 356 ++++++++++-------- .../mongo/MongoConnectionProviderFactory.java | 4 +- .../admin/resources/partials/server-info.html | 130 +++++-- .../provider/MonitorableProviderFactory.java | 18 + .../provider/ProviderOperationalInfo.java | 22 ++ services/pom.xml | 15 + .../admin/ServerInfoAdminResource.java | 154 ++++++-- .../testsuite/admin/AdminAPITest.java | 63 +++- 10 files changed, 720 insertions(+), 388 deletions(-) create mode 100644 model/api/src/main/java/org/keycloak/provider/MonitorableProviderFactory.java create mode 100644 model/api/src/main/java/org/keycloak/provider/ProviderOperationalInfo.java diff --git a/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java b/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java index 61bbafa6bf..36f3c09cce 100755 --- a/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java +++ b/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java @@ -1,189 +1,242 @@ package org.keycloak.connections.jpa; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +import javax.naming.InitialContext; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.sql.DataSource; + import org.hibernate.ejb.AvailableSettings; import org.jboss.logging.Logger; import org.keycloak.Config; import org.keycloak.connections.jpa.updater.JpaUpdaterProvider; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; - -import javax.naming.InitialContext; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.HashMap; -import java.util.Map; +import org.keycloak.provider.ProviderOperationalInfo; /** * @author Stian Thorgersen */ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProviderFactory { - private static final Logger logger = Logger.getLogger(DefaultJpaConnectionProviderFactory.class); + private static final Logger logger = Logger.getLogger(DefaultJpaConnectionProviderFactory.class); - private volatile EntityManagerFactory emf; + private volatile EntityManagerFactory emf; - private Config.Scope config; + private Config.Scope config; - @Override - public JpaConnectionProvider create(KeycloakSession session) { - lazyInit(session); + private DatabaseInfo databaseInfo; - EntityManager em = emf.createEntityManager(); - em = PersistenceExceptionConverter.create(em); - session.getTransaction().enlist(new JpaKeycloakTransaction(em)); - return new DefaultJpaConnectionProvider(em); - } + @Override + public JpaConnectionProvider create(KeycloakSession session) { + lazyInit(session); - @Override - public void close() { - if (emf != null) { - emf.close(); - } - } + EntityManager em = emf.createEntityManager(); + em = PersistenceExceptionConverter.create(em); + session.getTransaction().enlist(new JpaKeycloakTransaction(em)); + return new DefaultJpaConnectionProvider(em); + } - @Override - public String getId() { - return "default"; - } + @Override + public void close() { + if (emf != null) { + emf.close(); + } + } - @Override - public void init(Config.Scope config) { - this.config = config; - } + @Override + public String getId() { + return "default"; + } - @Override - public void postInit(KeycloakSessionFactory factory) { + @Override + public void init(Config.Scope config) { + this.config = config; + } - } + @Override + public void postInit(KeycloakSessionFactory factory) { - private void lazyInit(KeycloakSession session) { - if (emf == null) { - synchronized (this) { - if (emf == null) { - logger.debug("Initializing JPA connections"); + } - Connection connection = null; + private void lazyInit(KeycloakSession session) { + if (emf == null) { + synchronized (this) { + if (emf == null) { + logger.debug("Initializing JPA connections"); - String databaseSchema = config.get("databaseSchema"); + Connection connection = null; - Map properties = new HashMap(); + String databaseSchema = config.get("databaseSchema"); - String unitName = "keycloak-default"; + Map properties = new HashMap(); - String dataSource = config.get("dataSource"); - if (dataSource != null) { - if (config.getBoolean("jta", false)) { - properties.put(AvailableSettings.JTA_DATASOURCE, dataSource); - } else { - properties.put(AvailableSettings.NON_JTA_DATASOURCE, dataSource); - } - } else { - properties.put(AvailableSettings.JDBC_URL, config.get("url")); - properties.put(AvailableSettings.JDBC_DRIVER, config.get("driver")); + String unitName = "keycloak-default"; - String user = config.get("user"); - if (user != null) { - properties.put(AvailableSettings.JDBC_USER, user); - } - String password = config.get("password"); - if (password != null) { - properties.put(AvailableSettings.JDBC_PASSWORD, password); - } - } + String dataSource = config.get("dataSource"); + if (dataSource != null) { + if (config.getBoolean("jta", false)) { + properties.put(AvailableSettings.JTA_DATASOURCE, dataSource); + } else { + properties.put(AvailableSettings.NON_JTA_DATASOURCE, dataSource); + } + } else { + properties.put(AvailableSettings.JDBC_URL, config.get("url")); + properties.put(AvailableSettings.JDBC_DRIVER, config.get("driver")); - String driverDialect = config.get("driverDialect"); - if (driverDialect != null && driverDialect.length() > 0) { - properties.put("hibernate.dialect", driverDialect); - } + String user = config.get("user"); + if (user != null) { + properties.put(AvailableSettings.JDBC_USER, user); + } + String password = config.get("password"); + if (password != null) { + properties.put(AvailableSettings.JDBC_PASSWORD, password); + } + } - String schema = config.get("schema"); - if (schema != null) { - properties.put("hibernate.default_schema", schema); - } + String driverDialect = config.get("driverDialect"); + if (driverDialect != null && driverDialect.length() > 0) { + properties.put("hibernate.dialect", driverDialect); + } - if (databaseSchema != null) { - if (databaseSchema.equals("development-update")) { - properties.put("hibernate.hbm2ddl.auto", "update"); - databaseSchema = null; - } else if (databaseSchema.equals("development-validate")) { - properties.put("hibernate.hbm2ddl.auto", "validate"); - databaseSchema = null; - } - } + String schema = config.get("schema"); + if (schema != null) { + properties.put("hibernate.default_schema", schema); + } - properties.put("hibernate.show_sql", config.getBoolean("showSql", false)); - properties.put("hibernate.format_sql", config.getBoolean("formatSql", true)); + if (databaseSchema != null) { + if (databaseSchema.equals("development-update")) { + properties.put("hibernate.hbm2ddl.auto", "update"); + databaseSchema = null; + } else if (databaseSchema.equals("development-validate")) { + properties.put("hibernate.hbm2ddl.auto", "validate"); + databaseSchema = null; + } + } - if (databaseSchema != null) { - logger.trace("Updating database"); + properties.put("hibernate.show_sql", config.getBoolean("showSql", false)); + properties.put("hibernate.format_sql", config.getBoolean("formatSql", true)); - JpaUpdaterProvider updater = session.getProvider(JpaUpdaterProvider.class); - if (updater == null) { - throw new RuntimeException("Can't update database: JPA updater provider not found"); - } + connection = getConnection(); + prepareDatabaseInfo(connection); - connection = getConnection(); + if (databaseSchema != null) { + logger.trace("Updating database"); - if (databaseSchema.equals("update")) { - String currentVersion = null; - try { - ResultSet resultSet = connection.createStatement().executeQuery(updater.getCurrentVersionSql(schema)); - if (resultSet.next()) { - currentVersion = resultSet.getString(1); - } - } catch (SQLException e) { - } + JpaUpdaterProvider updater = session.getProvider(JpaUpdaterProvider.class); + if (updater == null) { + throw new RuntimeException("Can't update database: JPA updater provider not found"); + } - if (currentVersion == null || !JpaUpdaterProvider.LAST_VERSION.equals(currentVersion)) { - updater.update(session, connection, schema); - } else { - logger.debug("Database is up to date"); - } - } else if (databaseSchema.equals("validate")) { - updater.validate(connection, schema); - } else { - throw new RuntimeException("Invalid value for databaseSchema: " + databaseSchema); - } + if (databaseSchema.equals("update")) { + String currentVersion = null; + try { + ResultSet resultSet = connection.createStatement().executeQuery(updater.getCurrentVersionSql(schema)); + if (resultSet.next()) { + currentVersion = resultSet.getString(1); + } + } catch (SQLException e) { + } - logger.trace("Database update completed"); - } + if (currentVersion == null || !JpaUpdaterProvider.LAST_VERSION.equals(currentVersion)) { + updater.update(session, connection, schema); + } else { + logger.debug("Database is up to date"); + } + } else if (databaseSchema.equals("validate")) { + updater.validate(connection, schema); + } else { + throw new RuntimeException("Invalid value for databaseSchema: " + databaseSchema); + } - logger.trace("Creating EntityManagerFactory"); - emf = Persistence.createEntityManagerFactory(unitName, properties); - logger.trace("EntityManagerFactory created"); + logger.trace("Database update completed"); + } - // Close after creating EntityManagerFactory to prevent in-mem databases from closing - if (connection != null) { - try { - connection.close(); - } catch (SQLException e) { - logger.warn(e); - } - } - } - } - } - } + logger.trace("Creating EntityManagerFactory"); + emf = Persistence.createEntityManagerFactory(unitName, properties); + logger.trace("EntityManagerFactory created"); - private Connection getConnection() { - try { - String dataSourceLookup = config.get("dataSource"); - if (dataSourceLookup != null) { - DataSource dataSource = (DataSource) new InitialContext().lookup(dataSourceLookup); - return dataSource.getConnection(); - } else { - Class.forName(config.get("driver")); - return DriverManager.getConnection(config.get("url"), config.get("user"), config.get("password")); - } - } catch (Exception e) { - throw new RuntimeException("Failed to connect to database", e); - } - } + // Close after creating EntityManagerFactory to prevent in-mem databases from closing + if (connection != null) { + try { + connection.close(); + } catch (SQLException e) { + logger.warn(e); + } + } + } + } + } + } + + protected void prepareDatabaseInfo(Connection connection) { + try { + databaseInfo = new DatabaseInfo(); + DatabaseMetaData md = connection.getMetaData(); + databaseInfo.databaseDriver = md.getDriverName() + " " + md.getDriverVersion(); + databaseInfo.databaseProduct = md.getDatabaseProductName() + " " + md.getDatabaseProductVersion(); + databaseInfo.databaseUser = md.getUserName(); + databaseInfo.jdbcUrl = md.getURL(); + } catch (SQLException e) { + logger.warn("Unable to get database info due " + e.getMessage()); + } + } + + private Connection getConnection() { + try { + String dataSourceLookup = config.get("dataSource"); + if (dataSourceLookup != null) { + DataSource dataSource = (DataSource) new InitialContext().lookup(dataSourceLookup); + return dataSource.getConnection(); + } else { + Class.forName(config.get("driver")); + return DriverManager.getConnection(config.get("url"), config.get("user"), config.get("password")); + } + } catch (Exception e) { + throw new RuntimeException("Failed to connect to database", e); + } + } + + @Override + public DatabaseInfo getOperationalInfo() { + return databaseInfo; + } + + public static class DatabaseInfo implements ProviderOperationalInfo { + protected String jdbcUrl; + protected String databaseUser; + protected String databaseProduct; + protected String databaseDriver; + + public String getJdbcUrl() { + return jdbcUrl; + } + + public String getDatabaseDriver() { + return databaseDriver; + } + + public String getDatabaseUser() { + return databaseUser; + } + + public String getDatabaseProduct() { + return databaseProduct; + } + + @Override + public boolean isOk() { + // TODO KEYCLOAK-1578 - implement operational monitoring of JPA DB connection + return true; + } + } } diff --git a/connections/jpa/src/main/java/org/keycloak/connections/jpa/JpaConnectionProviderFactory.java b/connections/jpa/src/main/java/org/keycloak/connections/jpa/JpaConnectionProviderFactory.java index 1cf4a5f202..ab2b119c29 100644 --- a/connections/jpa/src/main/java/org/keycloak/connections/jpa/JpaConnectionProviderFactory.java +++ b/connections/jpa/src/main/java/org/keycloak/connections/jpa/JpaConnectionProviderFactory.java @@ -1,9 +1,10 @@ package org.keycloak.connections.jpa; -import org.keycloak.provider.ProviderFactory; +import org.keycloak.provider.MonitorableProviderFactory; /** * @author Stian Thorgersen */ -public interface JpaConnectionProviderFactory extends ProviderFactory { +public interface JpaConnectionProviderFactory extends MonitorableProviderFactory { + } diff --git a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java index 36be680a66..c8d1cbaed4 100755 --- a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java +++ b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java @@ -1,10 +1,11 @@ package org.keycloak.connections.mongo; -import com.mongodb.DB; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientOptions; -import com.mongodb.MongoCredential; -import com.mongodb.ServerAddress; +import java.lang.reflect.Method; +import java.net.UnknownHostException; +import java.util.Collections; + +import javax.net.ssl.SSLSocketFactory; + import org.jboss.logging.Logger; import org.keycloak.Config; import org.keycloak.connections.mongo.api.MongoStore; @@ -13,198 +14,225 @@ import org.keycloak.connections.mongo.impl.context.TransactionMongoStoreInvocati import org.keycloak.connections.mongo.updater.MongoUpdaterProvider; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; +import org.keycloak.provider.ProviderOperationalInfo; -import javax.net.ssl.SSLSocketFactory; -import java.lang.reflect.Method; -import java.net.UnknownHostException; -import java.util.Collections; +import com.mongodb.DB; +import com.mongodb.MongoClient; +import com.mongodb.MongoClientOptions; +import com.mongodb.MongoCredential; +import com.mongodb.ServerAddress; /** * @author Stian Thorgersen */ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionProviderFactory { - // TODO Make configurable - private String[] entities = new String[]{ - "org.keycloak.models.mongo.keycloak.entities.MongoRealmEntity", - "org.keycloak.models.mongo.keycloak.entities.MongoUserEntity", - "org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity", - "org.keycloak.models.entities.IdentityProviderEntity", - "org.keycloak.models.entities.ClientIdentityProviderMappingEntity", - "org.keycloak.models.entities.RequiredCredentialEntity", - "org.keycloak.models.entities.CredentialEntity", - "org.keycloak.models.entities.FederatedIdentityEntity", - "org.keycloak.models.mongo.keycloak.entities.MongoClientEntity", - "org.keycloak.models.sessions.mongo.entities.MongoUsernameLoginFailureEntity", - "org.keycloak.models.sessions.mongo.entities.MongoUserSessionEntity", - "org.keycloak.models.sessions.mongo.entities.MongoClientSessionEntity", - "org.keycloak.models.entities.UserFederationProviderEntity", - "org.keycloak.models.entities.UserFederationMapperEntity", - "org.keycloak.models.entities.ProtocolMapperEntity", - "org.keycloak.models.entities.IdentityProviderMapperEntity", - "org.keycloak.models.mongo.keycloak.entities.MongoUserConsentEntity", - "org.keycloak.models.mongo.keycloak.entities.MongoMigrationModelEntity", - "org.keycloak.models.entities.AuthenticationExecutionEntity", - "org.keycloak.models.entities.AuthenticationFlowEntity", - "org.keycloak.models.entities.AuthenticatorConfigEntity", - "org.keycloak.models.entities.RequiredActionProviderEntity", - }; + // TODO Make configurable + private String[] entities = new String[] { "org.keycloak.models.mongo.keycloak.entities.MongoRealmEntity", "org.keycloak.models.mongo.keycloak.entities.MongoUserEntity", "org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity", + "org.keycloak.models.entities.IdentityProviderEntity", "org.keycloak.models.entities.ClientIdentityProviderMappingEntity", "org.keycloak.models.entities.RequiredCredentialEntity", "org.keycloak.models.entities.CredentialEntity", + "org.keycloak.models.entities.FederatedIdentityEntity", "org.keycloak.models.mongo.keycloak.entities.MongoClientEntity", "org.keycloak.models.sessions.mongo.entities.MongoUsernameLoginFailureEntity", + "org.keycloak.models.sessions.mongo.entities.MongoUserSessionEntity", "org.keycloak.models.sessions.mongo.entities.MongoClientSessionEntity", "org.keycloak.models.entities.UserFederationProviderEntity", + "org.keycloak.models.entities.UserFederationMapperEntity", "org.keycloak.models.entities.ProtocolMapperEntity", "org.keycloak.models.entities.IdentityProviderMapperEntity", + "org.keycloak.models.mongo.keycloak.entities.MongoUserConsentEntity", "org.keycloak.models.mongo.keycloak.entities.MongoMigrationModelEntity", "org.keycloak.models.entities.AuthenticationExecutionEntity", + "org.keycloak.models.entities.AuthenticationFlowEntity", "org.keycloak.models.entities.AuthenticatorConfigEntity", "org.keycloak.models.entities.RequiredActionProviderEntity", }; - private static final Logger logger = Logger.getLogger(DefaultMongoConnectionFactoryProvider.class); + private static final Logger logger = Logger.getLogger(DefaultMongoConnectionFactoryProvider.class); - private volatile MongoClient client; + private volatile MongoClient client; - private MongoStore mongoStore; - private DB db; - protected Config.Scope config; + private MongoStore mongoStore; + private DB db; + protected Config.Scope config; - @Override - public MongoConnectionProvider create(KeycloakSession session) { - lazyInit(session); + private MongoDbInfo mongoDbInfo; - TransactionMongoStoreInvocationContext invocationContext = new TransactionMongoStoreInvocationContext(mongoStore); - session.getTransaction().enlist(new MongoKeycloakTransaction(invocationContext)); - return new DefaultMongoConnectionProvider(db, mongoStore, invocationContext); - } + @Override + public MongoConnectionProvider create(KeycloakSession session) { + lazyInit(session); - @Override - public void init(Config.Scope config) { - this.config = config; - } + TransactionMongoStoreInvocationContext invocationContext = new TransactionMongoStoreInvocationContext(mongoStore); + session.getTransaction().enlist(new MongoKeycloakTransaction(invocationContext)); + return new DefaultMongoConnectionProvider(db, mongoStore, invocationContext); + } - @Override - public void postInit(KeycloakSessionFactory factory) { + @Override + public void init(Config.Scope config) { + this.config = config; + } - } + @Override + public void postInit(KeycloakSessionFactory factory) { + } - private void lazyInit(KeycloakSession session) { - if (client == null) { - synchronized (this) { - if (client == null) { - try { - this.client = createMongoClient(); + private void lazyInit(KeycloakSession session) { + if (client == null) { + synchronized (this) { + if (client == null) { + try { + this.client = createMongoClient(); - String dbName = config.get("db", "keycloak"); - this.db = client.getDB(dbName); + String dbName = config.get("db", "keycloak"); + this.db = client.getDB(dbName); - String databaseSchema = config.get("databaseSchema"); - if (databaseSchema != null) { - if (databaseSchema.equals("update")) { - MongoUpdaterProvider mongoUpdater = session.getProvider(MongoUpdaterProvider.class); + String databaseSchema = config.get("databaseSchema"); + if (databaseSchema != null) { + if (databaseSchema.equals("update")) { + MongoUpdaterProvider mongoUpdater = session.getProvider(MongoUpdaterProvider.class); - if (mongoUpdater == null) { - throw new RuntimeException("Can't update database: Mongo updater provider not found"); - } + if (mongoUpdater == null) { + throw new RuntimeException("Can't update database: Mongo updater provider not found"); + } - mongoUpdater.update(session, db); - } else { - throw new RuntimeException("Invalid value for databaseSchema: " + databaseSchema); - } - } + mongoUpdater.update(session, db); + } else { + throw new RuntimeException("Invalid value for databaseSchema: " + databaseSchema); + } + } - this.mongoStore = new MongoStoreImpl(db, getManagedEntities()); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } - } - } + this.mongoStore = new MongoStoreImpl(db, getManagedEntities()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } + } + } - private Class[] getManagedEntities() throws ClassNotFoundException { - Class[] entityClasses = new Class[entities.length]; - for (int i = 0; i < entities.length; i++) { - entityClasses[i] = Thread.currentThread().getContextClassLoader().loadClass(entities[i]); - } - return entityClasses; - } + private Class[] getManagedEntities() throws ClassNotFoundException { + Class[] entityClasses = new Class[entities.length]; + for (int i = 0; i < entities.length; i++) { + entityClasses[i] = Thread.currentThread().getContextClassLoader().loadClass(entities[i]); + } + return entityClasses; + } - @Override - public void close() { - if (client != null) { - client.close(); - } - } + @Override + public void close() { + if (client != null) { + client.close(); + } + } - @Override - public String getId() { - return "default"; - } + @Override + public String getId() { + return "default"; + } - /** - * Override this method if you want more possibility to configure Mongo client. It can be also used to inject mongo client - * from different source. - * - * This method can assume that "config" is already set and can use it. - * - * @return mongoClient instance, which will be shared for whole Keycloak - * - * @throws UnknownHostException - */ - protected MongoClient createMongoClient() throws UnknownHostException { - String host = config.get("host", ServerAddress.defaultHost()); - int port = config.getInt("port", ServerAddress.defaultPort()); - String dbName = config.get("db", "keycloak"); + /** + * Override this method if you want more possibility to configure Mongo client. It can be also used to inject mongo + * client from different source. + * + * This method can assume that "config" is already set and can use it. + * + * @return mongoClient instance, which will be shared for whole Keycloak + * + * @throws UnknownHostException + */ + protected MongoClient createMongoClient() throws UnknownHostException { + String host = config.get("host", ServerAddress.defaultHost()); + int port = config.getInt("port", ServerAddress.defaultPort()); + String dbName = config.get("db", "keycloak"); - String user = config.get("user"); - String password = config.get("password"); + String user = config.get("user"); + String password = config.get("password"); - MongoClientOptions clientOptions = getClientOptions(); + MongoClientOptions clientOptions = getClientOptions(); - MongoClient client; - if (user != null && password != null) { - MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray()); - client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential), clientOptions); - } else { - client = new MongoClient(new ServerAddress(host, port), clientOptions); - } + MongoClient client; + if (user != null && password != null) { + MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray()); + client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential), clientOptions); + } else { + client = new MongoClient(new ServerAddress(host, port), clientOptions); + } - logger.debugv("Initialized mongo model. host: %s, port: %d, db: %s", host, port, dbName); - return client; - } + mongoDbInfo = new MongoDbInfo(); + mongoDbInfo.driverVersion = client.getVersion(); + mongoDbInfo.address = client.getAddress().toString(); + mongoDbInfo.database = dbName; + mongoDbInfo.user = user; - protected MongoClientOptions getClientOptions() { - MongoClientOptions.Builder builder = MongoClientOptions.builder(); - checkIntOption("connectionsPerHost", builder); - checkIntOption("threadsAllowedToBlockForConnectionMultiplier", builder); - checkIntOption("maxWaitTime", builder); - checkIntOption("connectTimeout", builder); - checkIntOption("socketTimeout", builder); - checkBooleanOption("socketKeepAlive", builder); - checkBooleanOption("autoConnectRetry", builder); - if (config.getLong("maxAutoConnectRetryTime") != null) { - builder.maxAutoConnectRetryTime(config.getLong("maxAutoConnectRetryTime")); - } - if(config.getBoolean("ssl", false)) { - builder.socketFactory(SSLSocketFactory.getDefault()); - } + logger.debugv("Initialized mongo model. host: %s, port: %d, db: %s", host, port, dbName); + return client; + } - return builder.build(); - } + protected MongoClientOptions getClientOptions() { + MongoClientOptions.Builder builder = MongoClientOptions.builder(); + checkIntOption("connectionsPerHost", builder); + checkIntOption("threadsAllowedToBlockForConnectionMultiplier", builder); + checkIntOption("maxWaitTime", builder); + checkIntOption("connectTimeout", builder); + checkIntOption("socketTimeout", builder); + checkBooleanOption("socketKeepAlive", builder); + checkBooleanOption("autoConnectRetry", builder); + if (config.getLong("maxAutoConnectRetryTime") != null) { + builder.maxAutoConnectRetryTime(config.getLong("maxAutoConnectRetryTime")); + } + if (config.getBoolean("ssl", false)) { + builder.socketFactory(SSLSocketFactory.getDefault()); + } - protected void checkBooleanOption(String optionName, MongoClientOptions.Builder builder) { - Boolean val = config.getBoolean(optionName); - if (val != null) { - try { - Method m = MongoClientOptions.Builder.class.getMethod(optionName, boolean.class); - m.invoke(builder, val); - } catch (Exception e) { - throw new IllegalStateException("Problem configuring boolean option " + optionName + " for mongo client. Ensure you used correct value true or false and if this option is supported by mongo driver", e); - } - } - } + return builder.build(); + } - protected void checkIntOption(String optionName, MongoClientOptions.Builder builder) { - Integer val = config.getInt(optionName); - if (val != null) { - try { - Method m = MongoClientOptions.Builder.class.getMethod(optionName, int.class); - m.invoke(builder, val); - } catch (Exception e) { - throw new IllegalStateException("Problem configuring int option " + optionName + " for mongo client. Ensure you used correct value (number) and if this option is supported by mongo driver", e); - } - } - } + protected void checkBooleanOption(String optionName, MongoClientOptions.Builder builder) { + Boolean val = config.getBoolean(optionName); + if (val != null) { + try { + Method m = MongoClientOptions.Builder.class.getMethod(optionName, boolean.class); + m.invoke(builder, val); + } catch (Exception e) { + throw new IllegalStateException("Problem configuring boolean option " + optionName + " for mongo client. Ensure you used correct value true or false and if this option is supported by mongo driver", e); + } + } + } + + protected void checkIntOption(String optionName, MongoClientOptions.Builder builder) { + Integer val = config.getInt(optionName); + if (val != null) { + try { + Method m = MongoClientOptions.Builder.class.getMethod(optionName, int.class); + m.invoke(builder, val); + } catch (Exception e) { + throw new IllegalStateException("Problem configuring int option " + optionName + " for mongo client. Ensure you used correct value (number) and if this option is supported by mongo driver", e); + } + } + } + + @Override + public ProviderOperationalInfo getOperationalInfo() { + return mongoDbInfo; + } + + public static class MongoDbInfo implements ProviderOperationalInfo { + + public String address; + public String database; + public String driverVersion; + public String user; + + @Override + public boolean isOk() { + // TODO KEYCLOAK-1578 - implement operational monitoring of Mongo DB connection + return true; + } + + public String getAddress() { + return address; + } + + public String getDatabase() { + return database; + } + + public String getDriverVersion() { + return driverVersion; + } + + public String getUser() { + return user; + } + } } diff --git a/connections/mongo/src/main/java/org/keycloak/connections/mongo/MongoConnectionProviderFactory.java b/connections/mongo/src/main/java/org/keycloak/connections/mongo/MongoConnectionProviderFactory.java index e787ce6382..3b3a00e560 100644 --- a/connections/mongo/src/main/java/org/keycloak/connections/mongo/MongoConnectionProviderFactory.java +++ b/connections/mongo/src/main/java/org/keycloak/connections/mongo/MongoConnectionProviderFactory.java @@ -1,9 +1,9 @@ package org.keycloak.connections.mongo; -import org.keycloak.provider.ProviderFactory; +import org.keycloak.provider.MonitorableProviderFactory; /** * @author Stian Thorgersen */ -public interface MongoConnectionProviderFactory extends ProviderFactory { +public interface MongoConnectionProviderFactory extends MonitorableProviderFactory { } diff --git a/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info.html b/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info.html index 5590f9576e..f4e4c6dcf2 100755 --- a/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info.html +++ b/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info.html @@ -1,21 +1,47 @@
-

Server Info

+

Server Info

- +
+ + + + + + + + + + + + +
Keycloak Version{{serverInfo.version}}
Server Time{{serverInfo.serverTime}} (update)
Server Uptime{{serverInfo.serverUptime}}
+ +
+ Java VM Memory Statistics +
+ + + + + + + + + + + + + +
Total Memory{{serverInfo.memoryInfo.totalFormated}}
Free Memory{{serverInfo.memoryInfo.freeFormated}} ({{serverInfo.memoryInfo.freePercentage}}%)
Used Memory{{serverInfo.memoryInfo.usedFormated}}
+
+
+ +
+ System Info +
+ - - - - - - - - - - - - - + @@ -66,23 +92,59 @@ -
Version{{serverInfo.version}}
Server Time{{serverInfo.serverTime}} (update)
Server Uptime{{serverInfo.serverUptime}}
Current Working DirectoryCurrent Working Directory {{serverInfo.systemInfo.userDir}}
OS Architecture {{serverInfo.systemInfo.osArchitecture}}
- -

Java VM Memory Statistics

- - - - - - - - - - - - - -
Total Memory{{serverInfo.memoryInfo.totalFormated}}
Free Memory{{serverInfo.memoryInfo.freeFormated}} ({{serverInfo.memoryInfo.freePercentage}}%)
Used Memory{{serverInfo.memoryInfo.usedFormated}}
+ +
+
+ + +
+ Database Info +
+ + + + + + + + + + + + + + + + + +
Database URL{{serverInfo.jpaInfo.jdbcUrl}}
Database User{{serverInfo.jpaInfo.databaseUser}}
Database Type{{serverInfo.jpaInfo.databaseProduct}}
Database Driver{{serverInfo.jpaInfo.databaseDriver}}
+
+
+ +
+ Mongo DB Info +
+ + + + + + + + + + + + + + + + + +
Address{{serverInfo.mongoDbInfo.address}}
Database{{serverInfo.mongoDbInfo.database}}
User{{serverInfo.mongoDbInfo.user}}
Driver Version{{serverInfo.mongoDbInfo.driverVersion}}
+
+
+
Providers @@ -93,7 +155,7 @@ - + @@ -117,7 +179,7 @@
SPISPI Providers
- + diff --git a/model/api/src/main/java/org/keycloak/provider/MonitorableProviderFactory.java b/model/api/src/main/java/org/keycloak/provider/MonitorableProviderFactory.java new file mode 100644 index 0000000000..7b9c95c921 --- /dev/null +++ b/model/api/src/main/java/org/keycloak/provider/MonitorableProviderFactory.java @@ -0,0 +1,18 @@ +package org.keycloak.provider; + +/** + * Provider factory for provider which is monitorable. It means some info about it can be shown on "Server Info" page or accessed over Operational monitoring endpoint. + * + * @author Vlastimil Elias (velias at redhat dot com) + */ +public interface MonitorableProviderFactory extends ProviderFactory { + + /** + * Get operational info about given provider. This info contains informations about providers configuration and operational conditions (eg. errors in connection to remote systems etc). + * Is used to be shown on "Server Info" page or in Operational monitoring endpoint. + * + * @return extendion of {@link ProviderOperationalInfo} + */ + public ProviderOperationalInfo getOperationalInfo(); + +} diff --git a/model/api/src/main/java/org/keycloak/provider/ProviderOperationalInfo.java b/model/api/src/main/java/org/keycloak/provider/ProviderOperationalInfo.java new file mode 100644 index 0000000000..ca3e8a6849 --- /dev/null +++ b/model/api/src/main/java/org/keycloak/provider/ProviderOperationalInfo.java @@ -0,0 +1,22 @@ +package org.keycloak.provider; + +import java.io.Serializable; + +/** + * Operational info about given Provider. + * Contains info about Provider that can be shown on "Server Info" page or accessed over Operational monitoring endpoint. + * + * @author Vlastimil Elias (velias at redhat dot com) + * @see MonitorableProviderFactory + */ +public interface ProviderOperationalInfo extends Serializable { + + /** + * Return true if provider is OK from operation point of view. It means it is able to perform necessary work. + * It can return false for example if remote DB of JPA provider is not available, or LDAP server of LDAP based user federation provider is not available. + * + * @return true if provider is OK to perform his operation. + */ + boolean isOk(); + +} diff --git a/services/pom.xml b/services/pom.xml index da71e9ae6f..16fef06d5c 100755 --- a/services/pom.xml +++ b/services/pom.xml @@ -39,6 +39,21 @@ keycloak-connections-http-client provided + + org.keycloak + keycloak-connections-jpa + provided + + + org.hibernate + hibernate-entitymanager + provided + + + org.keycloak + keycloak-connections-mongo + provided + org.keycloak keycloak-forms-common-freemarker diff --git a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java b/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java index c600b92e4b..91610db0c4 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java @@ -1,5 +1,6 @@ package org.keycloak.services.resources.admin; +import java.io.Serializable; import java.util.Collections; import java.util.Date; import java.util.HashMap; @@ -13,9 +14,13 @@ import java.util.Set; import javax.ws.rs.GET; import javax.ws.rs.core.Context; +import org.jboss.logging.Logger; import org.keycloak.Version; import org.keycloak.broker.provider.IdentityProvider; import org.keycloak.broker.provider.IdentityProviderFactory; +import org.keycloak.connections.jpa.JpaConnectionProvider; +import org.keycloak.connections.mongo.DefaultMongoConnectionFactoryProvider.MongoDbInfo; +import org.keycloak.connections.mongo.MongoConnectionProvider; import org.keycloak.events.EventListenerProvider; import org.keycloak.events.EventType; import org.keycloak.events.admin.OperationType; @@ -29,8 +34,10 @@ import org.keycloak.models.utils.ModelToRepresentation; import org.keycloak.protocol.LoginProtocol; import org.keycloak.protocol.LoginProtocolFactory; import org.keycloak.protocol.ProtocolMapper; +import org.keycloak.provider.MonitorableProviderFactory; import org.keycloak.provider.ProviderConfigProperty; import org.keycloak.provider.ProviderFactory; +import org.keycloak.provider.ProviderOperationalInfo; import org.keycloak.provider.Spi; import org.keycloak.representations.idm.ConfigPropertyRepresentation; import org.keycloak.representations.idm.ProtocolMapperRepresentation; @@ -41,6 +48,8 @@ import org.keycloak.social.SocialIdentityProvider; * @author Stian Thorgersen */ public class ServerInfoAdminResource { + + private static final Logger logger = Logger.getLogger(ServerInfoAdminResource.class); private static final Map> ENUMS = createEnumsMap(EventType.class, OperationType.class); @@ -58,6 +67,8 @@ public class ServerInfoAdminResource { info.version = Version.VERSION; info.serverTime = new Date().toString(); info.serverStartupTime = session.getKeycloakSessionFactory().getServerStartupTimestamp(); + info.memoryInfo = (new MemoryInfo()).init(Runtime.getRuntime()); + info.systemInfo = (new SystemInfo()).init(); setSocialProviders(info); setIdentityProviders(info); setThemes(info); @@ -68,6 +79,21 @@ public class ServerInfoAdminResource { setProtocolMapperTypes(info); setBuiltinProtocolMappers(info); info.setEnums(ENUMS); + + ProviderFactory jpf = session.getKeycloakSessionFactory().getProviderFactory(JpaConnectionProvider.class); + if(jpf!=null && jpf instanceof MonitorableProviderFactory){ + info.jpaInfo = ((MonitorableProviderFactory)jpf).getOperationalInfo(); + } else { + logger.debug("JPA provider not found or is not monitorable"); + } + + ProviderFactory mpf = session.getKeycloakSessionFactory().getProviderFactory(MongoConnectionProvider.class); + if(mpf!=null && mpf instanceof MonitorableProviderFactory){ + info.mongoDbInfo = ((MonitorableProviderFactory)mpf).getOperationalInfo(); + } else { + logger.debug("Mongo provider not found or is not monitorable"); + } + return info; } @@ -191,10 +217,28 @@ public class ServerInfoAdminResource { } } - public static class MemoryInfo{ + public static class MemoryInfo implements Serializable { + + protected long total; + + protected long used; + + public MemoryInfo(){ + } + + /** + * Fill object fwith info. + * @param runtime used to get memory info from. + * @return itself for chaining + */ + public MemoryInfo init(Runtime runtime){ + total = runtime.maxMemory(); + used = runtime.totalMemory() - runtime.freeMemory(); + return this; + } public long getTotal(){ - return Runtime.getRuntime().maxMemory(); + return total; } public String getTotalFormated(){ @@ -210,7 +254,7 @@ public class ServerInfoAdminResource { } public long getUsed(){ - return Runtime.getRuntime().totalMemory(); + return used; } public String getUsedFormated(){ @@ -218,7 +262,7 @@ public class ServerInfoAdminResource { } public long getFreePercentage(){ - return getFree()*100/getTotal(); + return getFree() * 100 / getTotal(); } private String formatMemory(long bytes){ @@ -233,71 +277,109 @@ public class ServerInfoAdminResource { } - public static class SystemInfo { + public static class SystemInfo implements Serializable { + + protected String javaVersion; + protected String javaVendor; + protected String javaVm; + protected String javaVmVersion; + protected String javaRuntime; + protected String javaHome; + protected String osName; + protected String osArchitecture; + protected String osVersion; + protected String fileEncoding; + protected String userName; + protected String userDir; + protected String userTimezone; + protected String userLocale; + + public SystemInfo() { + } + + /** + * Fill object with info about current system loaded from {@link System} properties. + * @return object itself for chaining + */ + protected SystemInfo init(){ + javaVersion = System.getProperty("java.version"); + javaVendor = System.getProperty("java.vendor"); + javaVm = System.getProperty("java.vm.name"); + javaVmVersion = System.getProperty("java.vm.version"); + javaRuntime = System.getProperty("java.runtime.name"); + javaHome = System.getProperty("java.home"); + osName = System.getProperty("os.name"); + osArchitecture = System.getProperty("os.arch"); + osVersion = System.getProperty("os.version"); + fileEncoding = System.getProperty("file.encoding"); + userName = System.getProperty("user.name"); + userDir = System.getProperty("user.dir"); + userTimezone = System.getProperty("user.timezone"); + userLocale = (new Locale(System.getProperty("user.country"),System.getProperty("user.language")).toString()); + return this; + } + public String getJavaVersion(){ - return System.getProperty("java.version"); + return javaVersion; } public String getJavaVendor(){ - return System.getProperty("java.vendor"); + return javaVendor; } public String getJavaVm(){ - return System.getProperty("java.vm.name"); + return javaVm; } public String getJavaVmVersion(){ - return System.getProperty("java.vm.version"); + return javaVmVersion; } public String getJavaRuntime(){ - return System.getProperty("java.runtime.name"); + return javaRuntime; } public String getJavaHome(){ - return System.getProperty("java.home"); + return javaHome; } public String getOsName(){ - return System.getProperty("os.name"); + return osName; } public String getOsArchitecture(){ - return System.getProperty("os.arch"); + return osArchitecture; } public String getOsVersion(){ - return System.getProperty("os.version"); + return osVersion; } public String getFileEncoding(){ - return System.getProperty("file.encoding"); + return fileEncoding; } public String getUserName(){ - return System.getProperty("user.name"); + return userName; } public String getUserDir(){ - return System.getProperty("user.dir"); + return userDir; } public String getUserTimezone(){ - return System.getProperty("user.timezone"); + return userTimezone; } public String getUserLocale(){ - return (new Locale(System.getProperty("user.country"),System.getProperty("user.language")).toString()); + return userLocale; } - } - public static class ServerInfoRepresentation { + public static class ServerInfoRepresentation implements Serializable { private String version; - private String serverTime; - private long serverStartupTime; private Map> themes; @@ -314,26 +396,44 @@ public class ServerInfoAdminResource { private Map> builtinProtocolMappers; private Map> enums; + + private MemoryInfo memoryInfo; + private SystemInfo systemInfo; + + private ProviderOperationalInfo jpaInfo; + private ProviderOperationalInfo mongoDbInfo; public ServerInfoRepresentation() { } public SystemInfo getSystemInfo(){ - return new SystemInfo(); + return systemInfo; } public MemoryInfo getMemoryInfo(){ - return new MemoryInfo(); + return memoryInfo; + } + + public ProviderOperationalInfo getJpaInfo() { + return jpaInfo; + } + + public ProviderOperationalInfo getMongoDbInfo() { + return mongoDbInfo; } public String getServerTime() { return serverTime; } + public long getServerStartupTime() { + return serverStartupTime; + } + /** * @return server startup time formatted */ - public String getServerStartupTime() { + public String getServerStartupTimeFormatted() { return (new Date(serverStartupTime)).toString(); } @@ -420,7 +520,7 @@ public class ServerInfoAdminResource { } } - public static class SpiInfoRepresentation { + public static class SpiInfoRepresentation implements Serializable { private String name; private boolean internal; private Set implementations; diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AdminAPITest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AdminAPITest.java index 3bde1cb740..85a81bbbeb 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AdminAPITest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AdminAPITest.java @@ -21,10 +21,27 @@ */ package org.keycloak.testsuite.admin; +import java.io.IOException; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; + import org.junit.Assert; import org.junit.ClassRule; import org.junit.Test; import org.keycloak.Config; +import org.keycloak.Version; import org.keycloak.models.ClientModel; import org.keycloak.models.ClientSessionModel; import org.keycloak.models.Constants; @@ -40,22 +57,8 @@ import org.keycloak.representations.idm.CredentialRepresentation; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.services.managers.RealmManager; import org.keycloak.services.resources.admin.AdminRoot; -import org.keycloak.testsuite.rule.AbstractKeycloakRule; import org.keycloak.testsuite.KeycloakServer; - -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.ClientRequestContext; -import javax.ws.rs.client.ClientRequestFilter; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.WebTarget; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; -import java.io.IOException; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import org.keycloak.testsuite.rule.AbstractKeycloakRule; /** * Tests Undertow Adapter @@ -295,4 +298,34 @@ public class AdminAPITest { testCreateRealm("/admin-test/testrealm.json"); } + @Test + public void testServerInfo() { + + String token = createToken(); + final String authHeader = "Bearer " + token; + ClientRequestFilter authFilter = new ClientRequestFilter() { + @Override + public void filter(ClientRequestContext requestContext) throws IOException { + requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, authHeader); + } + }; + Client client = ClientBuilder.newBuilder().register(authFilter).build(); + UriBuilder authBase = UriBuilder.fromUri("http://localhost:8081/auth"); + WebTarget target = client.target(AdminRoot.adminBaseUrl(authBase).path("serverinfo")); + + Map response = target.request().accept("application/json").get(Map.class); + + Assert.assertNotNull(response); + Assert.assertEquals(Version.VERSION, response.get("version")); + Assert.assertNotNull(response.get("serverTime")); + Assert.assertNotNull(response.get("serverStartupTime")); + + Assert.assertNotNull(response.get("memoryInfo")); + Assert.assertNotNull(response.get("jpaInfo")); + Assert.assertNull(response.get("mongoDbInfo")); + + System.out.println(response); + + } + } From 85816be96716b46fd6d622601764af8a9b7f7339 Mon Sep 17 00:00:00 2001 From: Vlastimil Elias Date: Wed, 22 Jul 2015 13:35:56 +0200 Subject: [PATCH 3/5] KEYCLOAK-1542 - code cleanup ahead of PR --- .../jpa/DefaultJpaConnectionProviderFactory.java | 6 ------ .../mongo/DefaultMongoConnectionFactoryProvider.java | 6 ------ .../org/keycloak/provider/ProviderOperationalInfo.java | 8 -------- .../services/resources/admin/ServerInfoAdminResource.java | 1 - 4 files changed, 21 deletions(-) diff --git a/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java b/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java index 36f3c09cce..1795cd13c5 100755 --- a/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java +++ b/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java @@ -231,12 +231,6 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide public String getDatabaseProduct() { return databaseProduct; } - - @Override - public boolean isOk() { - // TODO KEYCLOAK-1578 - implement operational monitoring of JPA DB connection - return true; - } } } diff --git a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java index c8d1cbaed4..248b880be6 100755 --- a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java +++ b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java @@ -212,12 +212,6 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro public String driverVersion; public String user; - @Override - public boolean isOk() { - // TODO KEYCLOAK-1578 - implement operational monitoring of Mongo DB connection - return true; - } - public String getAddress() { return address; } diff --git a/model/api/src/main/java/org/keycloak/provider/ProviderOperationalInfo.java b/model/api/src/main/java/org/keycloak/provider/ProviderOperationalInfo.java index ca3e8a6849..e37544d276 100644 --- a/model/api/src/main/java/org/keycloak/provider/ProviderOperationalInfo.java +++ b/model/api/src/main/java/org/keycloak/provider/ProviderOperationalInfo.java @@ -11,12 +11,4 @@ import java.io.Serializable; */ public interface ProviderOperationalInfo extends Serializable { - /** - * Return true if provider is OK from operation point of view. It means it is able to perform necessary work. - * It can return false for example if remote DB of JPA provider is not available, or LDAP server of LDAP based user federation provider is not available. - * - * @return true if provider is OK to perform his operation. - */ - boolean isOk(); - } diff --git a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java b/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java index 91610db0c4..dbb0ff0c8f 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java @@ -19,7 +19,6 @@ import org.keycloak.Version; import org.keycloak.broker.provider.IdentityProvider; import org.keycloak.broker.provider.IdentityProviderFactory; import org.keycloak.connections.jpa.JpaConnectionProvider; -import org.keycloak.connections.mongo.DefaultMongoConnectionFactoryProvider.MongoDbInfo; import org.keycloak.connections.mongo.MongoConnectionProvider; import org.keycloak.events.EventListenerProvider; import org.keycloak.events.EventType; From 1c142cb17074bf91f73e3f7ded7fa307da1e8afb Mon Sep 17 00:00:00 2001 From: Vlastimil Elias Date: Wed, 22 Jul 2015 14:28:08 +0200 Subject: [PATCH 4/5] KEYCLOAK-1542 - code formatting cleanup --- .../DefaultJpaConnectionProviderFactory.java | 385 +++++++++--------- ...DefaultMongoConnectionFactoryProvider.java | 377 +++++++++-------- .../admin/ServerInfoAdminResource.java | 28 +- .../testsuite/admin/AdminAPITest.java | 33 +- 4 files changed, 420 insertions(+), 403 deletions(-) diff --git a/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java b/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java index 1795cd13c5..aac1cdebd4 100755 --- a/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java +++ b/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java @@ -1,19 +1,5 @@ package org.keycloak.connections.jpa; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.HashMap; -import java.util.Map; - -import javax.naming.InitialContext; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; -import javax.sql.DataSource; - import org.hibernate.ejb.AvailableSettings; import org.jboss.logging.Logger; import org.keycloak.Config; @@ -22,215 +8,232 @@ import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.provider.ProviderOperationalInfo; +import javax.naming.InitialContext; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.sql.DataSource; + +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + /** * @author Stian Thorgersen */ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProviderFactory { - private static final Logger logger = Logger.getLogger(DefaultJpaConnectionProviderFactory.class); + private static final Logger logger = Logger.getLogger(DefaultJpaConnectionProviderFactory.class); - private volatile EntityManagerFactory emf; + private volatile EntityManagerFactory emf; - private Config.Scope config; + private Config.Scope config; + + private DatabaseInfo databaseInfo; - private DatabaseInfo databaseInfo; + @Override + public JpaConnectionProvider create(KeycloakSession session) { + lazyInit(session); - @Override - public JpaConnectionProvider create(KeycloakSession session) { - lazyInit(session); + EntityManager em = emf.createEntityManager(); + em = PersistenceExceptionConverter.create(em); + session.getTransaction().enlist(new JpaKeycloakTransaction(em)); + return new DefaultJpaConnectionProvider(em); + } - EntityManager em = emf.createEntityManager(); - em = PersistenceExceptionConverter.create(em); - session.getTransaction().enlist(new JpaKeycloakTransaction(em)); - return new DefaultJpaConnectionProvider(em); - } + @Override + public void close() { + if (emf != null) { + emf.close(); + } + } - @Override - public void close() { - if (emf != null) { - emf.close(); - } - } + @Override + public String getId() { + return "default"; + } - @Override - public String getId() { - return "default"; - } + @Override + public void init(Config.Scope config) { + this.config = config; + } - @Override - public void init(Config.Scope config) { - this.config = config; - } + @Override + public void postInit(KeycloakSessionFactory factory) { - @Override - public void postInit(KeycloakSessionFactory factory) { + } - } + private void lazyInit(KeycloakSession session) { + if (emf == null) { + synchronized (this) { + if (emf == null) { + logger.debug("Initializing JPA connections"); - private void lazyInit(KeycloakSession session) { - if (emf == null) { - synchronized (this) { - if (emf == null) { - logger.debug("Initializing JPA connections"); + Connection connection = null; - Connection connection = null; + String databaseSchema = config.get("databaseSchema"); - String databaseSchema = config.get("databaseSchema"); + Map properties = new HashMap(); - Map properties = new HashMap(); + String unitName = "keycloak-default"; - String unitName = "keycloak-default"; + String dataSource = config.get("dataSource"); + if (dataSource != null) { + if (config.getBoolean("jta", false)) { + properties.put(AvailableSettings.JTA_DATASOURCE, dataSource); + } else { + properties.put(AvailableSettings.NON_JTA_DATASOURCE, dataSource); + } + } else { + properties.put(AvailableSettings.JDBC_URL, config.get("url")); + properties.put(AvailableSettings.JDBC_DRIVER, config.get("driver")); - String dataSource = config.get("dataSource"); - if (dataSource != null) { - if (config.getBoolean("jta", false)) { - properties.put(AvailableSettings.JTA_DATASOURCE, dataSource); - } else { - properties.put(AvailableSettings.NON_JTA_DATASOURCE, dataSource); - } - } else { - properties.put(AvailableSettings.JDBC_URL, config.get("url")); - properties.put(AvailableSettings.JDBC_DRIVER, config.get("driver")); + String user = config.get("user"); + if (user != null) { + properties.put(AvailableSettings.JDBC_USER, user); + } + String password = config.get("password"); + if (password != null) { + properties.put(AvailableSettings.JDBC_PASSWORD, password); + } + } - String user = config.get("user"); - if (user != null) { - properties.put(AvailableSettings.JDBC_USER, user); - } - String password = config.get("password"); - if (password != null) { - properties.put(AvailableSettings.JDBC_PASSWORD, password); - } - } + String driverDialect = config.get("driverDialect"); + if (driverDialect != null && driverDialect.length() > 0) { + properties.put("hibernate.dialect", driverDialect); + } - String driverDialect = config.get("driverDialect"); - if (driverDialect != null && driverDialect.length() > 0) { - properties.put("hibernate.dialect", driverDialect); - } + String schema = config.get("schema"); + if (schema != null) { + properties.put("hibernate.default_schema", schema); + } - String schema = config.get("schema"); - if (schema != null) { - properties.put("hibernate.default_schema", schema); - } + if (databaseSchema != null) { + if (databaseSchema.equals("development-update")) { + properties.put("hibernate.hbm2ddl.auto", "update"); + databaseSchema = null; + } else if (databaseSchema.equals("development-validate")) { + properties.put("hibernate.hbm2ddl.auto", "validate"); + databaseSchema = null; + } + } - if (databaseSchema != null) { - if (databaseSchema.equals("development-update")) { - properties.put("hibernate.hbm2ddl.auto", "update"); - databaseSchema = null; - } else if (databaseSchema.equals("development-validate")) { - properties.put("hibernate.hbm2ddl.auto", "validate"); - databaseSchema = null; - } - } + properties.put("hibernate.show_sql", config.getBoolean("showSql", false)); + properties.put("hibernate.format_sql", config.getBoolean("formatSql", true)); - properties.put("hibernate.show_sql", config.getBoolean("showSql", false)); - properties.put("hibernate.format_sql", config.getBoolean("formatSql", true)); + connection = getConnection(); + try{ + prepareDatabaseInfo(connection); + + if (databaseSchema != null) { + logger.trace("Updating database"); + + JpaUpdaterProvider updater = session.getProvider(JpaUpdaterProvider.class); + if (updater == null) { + throw new RuntimeException("Can't update database: JPA updater provider not found"); + } + + if (databaseSchema.equals("update")) { + String currentVersion = null; + try { + ResultSet resultSet = connection.createStatement().executeQuery(updater.getCurrentVersionSql(schema)); + if (resultSet.next()) { + currentVersion = resultSet.getString(1); + } + } catch (SQLException e) { + } + + if (currentVersion == null || !JpaUpdaterProvider.LAST_VERSION.equals(currentVersion)) { + updater.update(session, connection, schema); + } else { + logger.debug("Database is up to date"); + } + } else if (databaseSchema.equals("validate")) { + updater.validate(connection, schema); + } else { + throw new RuntimeException("Invalid value for databaseSchema: " + databaseSchema); + } + + logger.trace("Database update completed"); + } + + logger.trace("Creating EntityManagerFactory"); + emf = Persistence.createEntityManagerFactory(unitName, properties); + logger.trace("EntityManagerFactory created"); - connection = getConnection(); - prepareDatabaseInfo(connection); + } finally { + // Close after creating EntityManagerFactory to prevent in-mem databases from closing + if (connection != null) { + try { + connection.close(); + } catch (SQLException e) { + logger.warn(e); + } + } + } + } + } + } + } + + protected void prepareDatabaseInfo(Connection connection) { + try { + databaseInfo = new DatabaseInfo(); + DatabaseMetaData md = connection.getMetaData(); + databaseInfo.databaseDriver = md.getDriverName() + " " + md.getDriverVersion(); + databaseInfo.databaseProduct = md.getDatabaseProductName() + " " + md.getDatabaseProductVersion(); + databaseInfo.databaseUser = md.getUserName(); + databaseInfo.jdbcUrl = md.getURL(); + } catch (SQLException e) { + logger.warn("Unable to get database info due " + e.getMessage()); + } + } - if (databaseSchema != null) { - logger.trace("Updating database"); + private Connection getConnection() { + try { + String dataSourceLookup = config.get("dataSource"); + if (dataSourceLookup != null) { + DataSource dataSource = (DataSource) new InitialContext().lookup(dataSourceLookup); + return dataSource.getConnection(); + } else { + Class.forName(config.get("driver")); + return DriverManager.getConnection(config.get("url"), config.get("user"), config.get("password")); + } + } catch (Exception e) { + throw new RuntimeException("Failed to connect to database", e); + } + } + + @Override + public DatabaseInfo getOperationalInfo() { + return databaseInfo; + } - JpaUpdaterProvider updater = session.getProvider(JpaUpdaterProvider.class); - if (updater == null) { - throw new RuntimeException("Can't update database: JPA updater provider not found"); - } + public static class DatabaseInfo implements ProviderOperationalInfo { + protected String jdbcUrl; + protected String databaseUser; + protected String databaseProduct; + protected String databaseDriver; - if (databaseSchema.equals("update")) { - String currentVersion = null; - try { - ResultSet resultSet = connection.createStatement().executeQuery(updater.getCurrentVersionSql(schema)); - if (resultSet.next()) { - currentVersion = resultSet.getString(1); - } - } catch (SQLException e) { - } + public String getJdbcUrl() { + return jdbcUrl; + } - if (currentVersion == null || !JpaUpdaterProvider.LAST_VERSION.equals(currentVersion)) { - updater.update(session, connection, schema); - } else { - logger.debug("Database is up to date"); - } - } else if (databaseSchema.equals("validate")) { - updater.validate(connection, schema); - } else { - throw new RuntimeException("Invalid value for databaseSchema: " + databaseSchema); - } + public String getDatabaseDriver() { + return databaseDriver; + } - logger.trace("Database update completed"); - } + public String getDatabaseUser() { + return databaseUser; + } - logger.trace("Creating EntityManagerFactory"); - emf = Persistence.createEntityManagerFactory(unitName, properties); - logger.trace("EntityManagerFactory created"); - - // Close after creating EntityManagerFactory to prevent in-mem databases from closing - if (connection != null) { - try { - connection.close(); - } catch (SQLException e) { - logger.warn(e); - } - } - } - } - } - } - - protected void prepareDatabaseInfo(Connection connection) { - try { - databaseInfo = new DatabaseInfo(); - DatabaseMetaData md = connection.getMetaData(); - databaseInfo.databaseDriver = md.getDriverName() + " " + md.getDriverVersion(); - databaseInfo.databaseProduct = md.getDatabaseProductName() + " " + md.getDatabaseProductVersion(); - databaseInfo.databaseUser = md.getUserName(); - databaseInfo.jdbcUrl = md.getURL(); - } catch (SQLException e) { - logger.warn("Unable to get database info due " + e.getMessage()); - } - } - - private Connection getConnection() { - try { - String dataSourceLookup = config.get("dataSource"); - if (dataSourceLookup != null) { - DataSource dataSource = (DataSource) new InitialContext().lookup(dataSourceLookup); - return dataSource.getConnection(); - } else { - Class.forName(config.get("driver")); - return DriverManager.getConnection(config.get("url"), config.get("user"), config.get("password")); - } - } catch (Exception e) { - throw new RuntimeException("Failed to connect to database", e); - } - } - - @Override - public DatabaseInfo getOperationalInfo() { - return databaseInfo; - } - - public static class DatabaseInfo implements ProviderOperationalInfo { - protected String jdbcUrl; - protected String databaseUser; - protected String databaseProduct; - protected String databaseDriver; - - public String getJdbcUrl() { - return jdbcUrl; - } - - public String getDatabaseDriver() { - return databaseDriver; - } - - public String getDatabaseUser() { - return databaseUser; - } - - public String getDatabaseProduct() { - return databaseProduct; - } - } + public String getDatabaseProduct() { + return databaseProduct; + } + } } diff --git a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java index 248b880be6..2df4b85363 100755 --- a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java +++ b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java @@ -1,10 +1,10 @@ package org.keycloak.connections.mongo; -import java.lang.reflect.Method; -import java.net.UnknownHostException; -import java.util.Collections; - -import javax.net.ssl.SSLSocketFactory; +import com.mongodb.DB; +import com.mongodb.MongoClient; +import com.mongodb.MongoClientOptions; +import com.mongodb.MongoCredential; +import com.mongodb.ServerAddress; import org.jboss.logging.Logger; import org.keycloak.Config; @@ -16,217 +16,234 @@ import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.provider.ProviderOperationalInfo; -import com.mongodb.DB; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientOptions; -import com.mongodb.MongoCredential; -import com.mongodb.ServerAddress; +import javax.net.ssl.SSLSocketFactory; + +import java.lang.reflect.Method; +import java.net.UnknownHostException; +import java.util.Collections; /** * @author Stian Thorgersen */ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionProviderFactory { - // TODO Make configurable - private String[] entities = new String[] { "org.keycloak.models.mongo.keycloak.entities.MongoRealmEntity", "org.keycloak.models.mongo.keycloak.entities.MongoUserEntity", "org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity", - "org.keycloak.models.entities.IdentityProviderEntity", "org.keycloak.models.entities.ClientIdentityProviderMappingEntity", "org.keycloak.models.entities.RequiredCredentialEntity", "org.keycloak.models.entities.CredentialEntity", - "org.keycloak.models.entities.FederatedIdentityEntity", "org.keycloak.models.mongo.keycloak.entities.MongoClientEntity", "org.keycloak.models.sessions.mongo.entities.MongoUsernameLoginFailureEntity", - "org.keycloak.models.sessions.mongo.entities.MongoUserSessionEntity", "org.keycloak.models.sessions.mongo.entities.MongoClientSessionEntity", "org.keycloak.models.entities.UserFederationProviderEntity", - "org.keycloak.models.entities.UserFederationMapperEntity", "org.keycloak.models.entities.ProtocolMapperEntity", "org.keycloak.models.entities.IdentityProviderMapperEntity", - "org.keycloak.models.mongo.keycloak.entities.MongoUserConsentEntity", "org.keycloak.models.mongo.keycloak.entities.MongoMigrationModelEntity", "org.keycloak.models.entities.AuthenticationExecutionEntity", - "org.keycloak.models.entities.AuthenticationFlowEntity", "org.keycloak.models.entities.AuthenticatorConfigEntity", "org.keycloak.models.entities.RequiredActionProviderEntity", }; + // TODO Make configurable + private String[] entities = new String[]{ + "org.keycloak.models.mongo.keycloak.entities.MongoRealmEntity", + "org.keycloak.models.mongo.keycloak.entities.MongoUserEntity", + "org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity", + "org.keycloak.models.entities.IdentityProviderEntity", + "org.keycloak.models.entities.ClientIdentityProviderMappingEntity", + "org.keycloak.models.entities.RequiredCredentialEntity", + "org.keycloak.models.entities.CredentialEntity", + "org.keycloak.models.entities.FederatedIdentityEntity", + "org.keycloak.models.mongo.keycloak.entities.MongoClientEntity", + "org.keycloak.models.sessions.mongo.entities.MongoUsernameLoginFailureEntity", + "org.keycloak.models.sessions.mongo.entities.MongoUserSessionEntity", + "org.keycloak.models.sessions.mongo.entities.MongoClientSessionEntity", + "org.keycloak.models.entities.UserFederationProviderEntity", + "org.keycloak.models.entities.UserFederationMapperEntity", + "org.keycloak.models.entities.ProtocolMapperEntity", + "org.keycloak.models.entities.IdentityProviderMapperEntity", + "org.keycloak.models.mongo.keycloak.entities.MongoUserConsentEntity", + "org.keycloak.models.mongo.keycloak.entities.MongoMigrationModelEntity", + "org.keycloak.models.entities.AuthenticationExecutionEntity", + "org.keycloak.models.entities.AuthenticationFlowEntity", + "org.keycloak.models.entities.AuthenticatorConfigEntity", + "org.keycloak.models.entities.RequiredActionProviderEntity", + }; - private static final Logger logger = Logger.getLogger(DefaultMongoConnectionFactoryProvider.class); + private static final Logger logger = Logger.getLogger(DefaultMongoConnectionFactoryProvider.class); - private volatile MongoClient client; + private volatile MongoClient client; - private MongoStore mongoStore; - private DB db; - protected Config.Scope config; + private MongoStore mongoStore; + private DB db; + protected Config.Scope config; + + private MongoDbInfo mongoDbInfo; - private MongoDbInfo mongoDbInfo; + @Override + public MongoConnectionProvider create(KeycloakSession session) { + lazyInit(session); - @Override - public MongoConnectionProvider create(KeycloakSession session) { - lazyInit(session); + TransactionMongoStoreInvocationContext invocationContext = new TransactionMongoStoreInvocationContext(mongoStore); + session.getTransaction().enlist(new MongoKeycloakTransaction(invocationContext)); + return new DefaultMongoConnectionProvider(db, mongoStore, invocationContext); + } - TransactionMongoStoreInvocationContext invocationContext = new TransactionMongoStoreInvocationContext(mongoStore); - session.getTransaction().enlist(new MongoKeycloakTransaction(invocationContext)); - return new DefaultMongoConnectionProvider(db, mongoStore, invocationContext); - } + @Override + public void init(Config.Scope config) { + this.config = config; + } - @Override - public void init(Config.Scope config) { - this.config = config; - } + @Override + public void postInit(KeycloakSessionFactory factory) { - @Override - public void postInit(KeycloakSessionFactory factory) { + } - } - private void lazyInit(KeycloakSession session) { - if (client == null) { - synchronized (this) { - if (client == null) { - try { - this.client = createMongoClient(); + private void lazyInit(KeycloakSession session) { + if (client == null) { + synchronized (this) { + if (client == null) { + try { + this.client = createMongoClient(); - String dbName = config.get("db", "keycloak"); - this.db = client.getDB(dbName); + String dbName = config.get("db", "keycloak"); + this.db = client.getDB(dbName); - String databaseSchema = config.get("databaseSchema"); - if (databaseSchema != null) { - if (databaseSchema.equals("update")) { - MongoUpdaterProvider mongoUpdater = session.getProvider(MongoUpdaterProvider.class); + String databaseSchema = config.get("databaseSchema"); + if (databaseSchema != null) { + if (databaseSchema.equals("update")) { + MongoUpdaterProvider mongoUpdater = session.getProvider(MongoUpdaterProvider.class); - if (mongoUpdater == null) { - throw new RuntimeException("Can't update database: Mongo updater provider not found"); - } + if (mongoUpdater == null) { + throw new RuntimeException("Can't update database: Mongo updater provider not found"); + } - mongoUpdater.update(session, db); - } else { - throw new RuntimeException("Invalid value for databaseSchema: " + databaseSchema); - } - } + mongoUpdater.update(session, db); + } else { + throw new RuntimeException("Invalid value for databaseSchema: " + databaseSchema); + } + } - this.mongoStore = new MongoStoreImpl(db, getManagedEntities()); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } - } - } + this.mongoStore = new MongoStoreImpl(db, getManagedEntities()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } + } + } - private Class[] getManagedEntities() throws ClassNotFoundException { - Class[] entityClasses = new Class[entities.length]; - for (int i = 0; i < entities.length; i++) { - entityClasses[i] = Thread.currentThread().getContextClassLoader().loadClass(entities[i]); - } - return entityClasses; - } + private Class[] getManagedEntities() throws ClassNotFoundException { + Class[] entityClasses = new Class[entities.length]; + for (int i = 0; i < entities.length; i++) { + entityClasses[i] = Thread.currentThread().getContextClassLoader().loadClass(entities[i]); + } + return entityClasses; + } - @Override - public void close() { - if (client != null) { - client.close(); - } - } + @Override + public void close() { + if (client != null) { + client.close(); + } + } - @Override - public String getId() { - return "default"; - } + @Override + public String getId() { + return "default"; + } - /** - * Override this method if you want more possibility to configure Mongo client. It can be also used to inject mongo - * client from different source. - * - * This method can assume that "config" is already set and can use it. - * - * @return mongoClient instance, which will be shared for whole Keycloak - * - * @throws UnknownHostException - */ - protected MongoClient createMongoClient() throws UnknownHostException { - String host = config.get("host", ServerAddress.defaultHost()); - int port = config.getInt("port", ServerAddress.defaultPort()); - String dbName = config.get("db", "keycloak"); + /** + * Override this method if you want more possibility to configure Mongo client. It can be also used to inject mongo client + * from different source. + * + * This method can assume that "config" is already set and can use it. + * + * @return mongoClient instance, which will be shared for whole Keycloak + * + * @throws UnknownHostException + */ + protected MongoClient createMongoClient() throws UnknownHostException { + String host = config.get("host", ServerAddress.defaultHost()); + int port = config.getInt("port", ServerAddress.defaultPort()); + String dbName = config.get("db", "keycloak"); - String user = config.get("user"); - String password = config.get("password"); + String user = config.get("user"); + String password = config.get("password"); - MongoClientOptions clientOptions = getClientOptions(); + MongoClientOptions clientOptions = getClientOptions(); - MongoClient client; - if (user != null && password != null) { - MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray()); - client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential), clientOptions); - } else { - client = new MongoClient(new ServerAddress(host, port), clientOptions); - } + MongoClient client; + if (user != null && password != null) { + MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray()); + client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential), clientOptions); + } else { + client = new MongoClient(new ServerAddress(host, port), clientOptions); + } + + mongoDbInfo = new MongoDbInfo(); + mongoDbInfo.driverVersion = client.getVersion(); + mongoDbInfo.address = client.getAddress().toString(); + mongoDbInfo.database = dbName; + mongoDbInfo.user = user; + + logger.debugv("Initialized mongo model. host: %s, port: %d, db: %s", host, port, dbName); + return client; + } - mongoDbInfo = new MongoDbInfo(); - mongoDbInfo.driverVersion = client.getVersion(); - mongoDbInfo.address = client.getAddress().toString(); - mongoDbInfo.database = dbName; - mongoDbInfo.user = user; + protected MongoClientOptions getClientOptions() { + MongoClientOptions.Builder builder = MongoClientOptions.builder(); + checkIntOption("connectionsPerHost", builder); + checkIntOption("threadsAllowedToBlockForConnectionMultiplier", builder); + checkIntOption("maxWaitTime", builder); + checkIntOption("connectTimeout", builder); + checkIntOption("socketTimeout", builder); + checkBooleanOption("socketKeepAlive", builder); + checkBooleanOption("autoConnectRetry", builder); + if (config.getLong("maxAutoConnectRetryTime") != null) { + builder.maxAutoConnectRetryTime(config.getLong("maxAutoConnectRetryTime")); + } + if(config.getBoolean("ssl", false)) { + builder.socketFactory(SSLSocketFactory.getDefault()); + } - logger.debugv("Initialized mongo model. host: %s, port: %d, db: %s", host, port, dbName); - return client; - } + return builder.build(); + } - protected MongoClientOptions getClientOptions() { - MongoClientOptions.Builder builder = MongoClientOptions.builder(); - checkIntOption("connectionsPerHost", builder); - checkIntOption("threadsAllowedToBlockForConnectionMultiplier", builder); - checkIntOption("maxWaitTime", builder); - checkIntOption("connectTimeout", builder); - checkIntOption("socketTimeout", builder); - checkBooleanOption("socketKeepAlive", builder); - checkBooleanOption("autoConnectRetry", builder); - if (config.getLong("maxAutoConnectRetryTime") != null) { - builder.maxAutoConnectRetryTime(config.getLong("maxAutoConnectRetryTime")); - } - if (config.getBoolean("ssl", false)) { - builder.socketFactory(SSLSocketFactory.getDefault()); - } + protected void checkBooleanOption(String optionName, MongoClientOptions.Builder builder) { + Boolean val = config.getBoolean(optionName); + if (val != null) { + try { + Method m = MongoClientOptions.Builder.class.getMethod(optionName, boolean.class); + m.invoke(builder, val); + } catch (Exception e) { + throw new IllegalStateException("Problem configuring boolean option " + optionName + " for mongo client. Ensure you used correct value true or false and if this option is supported by mongo driver", e); + } + } + } - return builder.build(); - } + protected void checkIntOption(String optionName, MongoClientOptions.Builder builder) { + Integer val = config.getInt(optionName); + if (val != null) { + try { + Method m = MongoClientOptions.Builder.class.getMethod(optionName, int.class); + m.invoke(builder, val); + } catch (Exception e) { + throw new IllegalStateException("Problem configuring int option " + optionName + " for mongo client. Ensure you used correct value (number) and if this option is supported by mongo driver", e); + } + } + } + + @Override + public ProviderOperationalInfo getOperationalInfo() { + return mongoDbInfo; + } - protected void checkBooleanOption(String optionName, MongoClientOptions.Builder builder) { - Boolean val = config.getBoolean(optionName); - if (val != null) { - try { - Method m = MongoClientOptions.Builder.class.getMethod(optionName, boolean.class); - m.invoke(builder, val); - } catch (Exception e) { - throw new IllegalStateException("Problem configuring boolean option " + optionName + " for mongo client. Ensure you used correct value true or false and if this option is supported by mongo driver", e); - } - } - } + public static class MongoDbInfo implements ProviderOperationalInfo { - protected void checkIntOption(String optionName, MongoClientOptions.Builder builder) { - Integer val = config.getInt(optionName); - if (val != null) { - try { - Method m = MongoClientOptions.Builder.class.getMethod(optionName, int.class); - m.invoke(builder, val); - } catch (Exception e) { - throw new IllegalStateException("Problem configuring int option " + optionName + " for mongo client. Ensure you used correct value (number) and if this option is supported by mongo driver", e); - } - } - } + public String address; + public String database; + public String driverVersion; + public String user; - @Override - public ProviderOperationalInfo getOperationalInfo() { - return mongoDbInfo; - } + public String getAddress() { + return address; + } - public static class MongoDbInfo implements ProviderOperationalInfo { + public String getDatabase() { + return database; + } - public String address; - public String database; - public String driverVersion; - public String user; - - public String getAddress() { - return address; - } - - public String getDatabase() { - return database; - } - - public String getDriverVersion() { - return driverVersion; - } - - public String getUser() { - return user; - } - } + public String getDriverVersion() { + return driverVersion; + } + public String getUser() { + return user; + } + } } diff --git a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java b/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java index dbb0ff0c8f..3b69304b06 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java @@ -1,19 +1,5 @@ package org.keycloak.services.resources.admin; -import java.io.Serializable; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.ServiceLoader; -import java.util.Set; - -import javax.ws.rs.GET; -import javax.ws.rs.core.Context; - import org.jboss.logging.Logger; import org.keycloak.Version; import org.keycloak.broker.provider.IdentityProvider; @@ -43,6 +29,19 @@ import org.keycloak.representations.idm.ProtocolMapperRepresentation; import org.keycloak.representations.idm.ProtocolMapperTypeRepresentation; import org.keycloak.social.SocialIdentityProvider; +import javax.ws.rs.GET; +import javax.ws.rs.core.Context; +import java.io.Serializable; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.ServiceLoader; +import java.util.Set; + /** * @author Stian Thorgersen */ @@ -219,7 +218,6 @@ public class ServerInfoAdminResource { public static class MemoryInfo implements Serializable { protected long total; - protected long used; public MemoryInfo(){ diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AdminAPITest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AdminAPITest.java index 85a81bbbeb..2cca19a0c9 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AdminAPITest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AdminAPITest.java @@ -21,22 +21,6 @@ */ package org.keycloak.testsuite.admin; -import java.io.IOException; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.ClientRequestContext; -import javax.ws.rs.client.ClientRequestFilter; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.WebTarget; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; - import org.junit.Assert; import org.junit.ClassRule; import org.junit.Test; @@ -57,8 +41,23 @@ import org.keycloak.representations.idm.CredentialRepresentation; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.services.managers.RealmManager; import org.keycloak.services.resources.admin.AdminRoot; -import org.keycloak.testsuite.KeycloakServer; import org.keycloak.testsuite.rule.AbstractKeycloakRule; +import org.keycloak.testsuite.KeycloakServer; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.io.IOException; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; /** * Tests Undertow Adapter From 99f74acb94f1761417be3000d8615287866e2807 Mon Sep 17 00:00:00 2001 From: Vlastimil Elias Date: Thu, 23 Jul 2015 16:39:27 +0200 Subject: [PATCH 5/5] KEYCLOAK-1542 - rewrote showing info from providers to be generic --- .../DefaultJpaConnectionProviderFactory.java | 48 +-- .../jpa/JpaConnectionProviderFactory.java | 4 +- ...DefaultMongoConnectionFactoryProvider.java | 43 +- .../mongo/MongoConnectionProviderFactory.java | 4 +- .../theme/base/admin/resources/js/app.js | 8 +- .../admin/resources/js/controllers/realm.js | 7 + .../theme/base/admin/resources/js/loaders.js | 4 + .../theme/base/admin/resources/js/services.js | 3 + .../admin/resources/partials/server-info.html | 118 +++--- .../provider/MonitorableProviderFactory.java | 18 - .../provider/ProviderOperationalInfo.java | 14 - .../ServerInfoAwareProviderFactory.java | 20 + services/pom.xml | 15 - .../services/resources/admin/AdminRoot.java | 26 ++ .../admin/ServerInfoAdminResource.java | 254 +----------- .../admin/ServerInfoPageAdminResource.java | 375 ++++++++++++++++++ .../testsuite/admin/AdminAPITest.java | 34 +- 17 files changed, 555 insertions(+), 440 deletions(-) delete mode 100644 model/api/src/main/java/org/keycloak/provider/MonitorableProviderFactory.java delete mode 100644 model/api/src/main/java/org/keycloak/provider/ProviderOperationalInfo.java create mode 100644 model/api/src/main/java/org/keycloak/provider/ServerInfoAwareProviderFactory.java create mode 100644 services/src/main/java/org/keycloak/services/resources/admin/ServerInfoPageAdminResource.java diff --git a/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java b/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java index aac1cdebd4..a79c82b228 100755 --- a/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java +++ b/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java @@ -6,20 +6,19 @@ import org.keycloak.Config; import org.keycloak.connections.jpa.updater.JpaUpdaterProvider; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; -import org.keycloak.provider.ProviderOperationalInfo; import javax.naming.InitialContext; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.sql.DataSource; - import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; /** @@ -33,7 +32,7 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide private Config.Scope config; - private DatabaseInfo databaseInfo; + private Map operationalInfo; @Override public JpaConnectionProvider create(KeycloakSession session) { @@ -127,7 +126,7 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide connection = getConnection(); try{ - prepareDatabaseInfo(connection); + prepareOperationalInfo(connection); if (databaseSchema != null) { logger.trace("Updating database"); @@ -180,16 +179,16 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide } } - protected void prepareDatabaseInfo(Connection connection) { + protected void prepareOperationalInfo(Connection connection) { try { - databaseInfo = new DatabaseInfo(); + operationalInfo = new LinkedHashMap<>(); DatabaseMetaData md = connection.getMetaData(); - databaseInfo.databaseDriver = md.getDriverName() + " " + md.getDriverVersion(); - databaseInfo.databaseProduct = md.getDatabaseProductName() + " " + md.getDatabaseProductVersion(); - databaseInfo.databaseUser = md.getUserName(); - databaseInfo.jdbcUrl = md.getURL(); + operationalInfo.put("databaseUrl",md.getURL()); + operationalInfo.put("databaseUser", md.getUserName()); + operationalInfo.put("databaseProduct", md.getDatabaseProductName() + " " + md.getDatabaseProductVersion()); + operationalInfo.put("databaseDriver", md.getDriverName() + " " + md.getDriverVersion()); } catch (SQLException e) { - logger.warn("Unable to get database info due " + e.getMessage()); + logger.warn("Unable to prepare operational info due database exception: " + e.getMessage()); } } @@ -209,31 +208,8 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide } @Override - public DatabaseInfo getOperationalInfo() { - return databaseInfo; - } - - public static class DatabaseInfo implements ProviderOperationalInfo { - protected String jdbcUrl; - protected String databaseUser; - protected String databaseProduct; - protected String databaseDriver; - - public String getJdbcUrl() { - return jdbcUrl; - } - - public String getDatabaseDriver() { - return databaseDriver; - } - - public String getDatabaseUser() { - return databaseUser; - } - - public String getDatabaseProduct() { - return databaseProduct; - } + public Map getOperationalInfo() { + return operationalInfo; } } diff --git a/connections/jpa/src/main/java/org/keycloak/connections/jpa/JpaConnectionProviderFactory.java b/connections/jpa/src/main/java/org/keycloak/connections/jpa/JpaConnectionProviderFactory.java index ab2b119c29..288e403335 100644 --- a/connections/jpa/src/main/java/org/keycloak/connections/jpa/JpaConnectionProviderFactory.java +++ b/connections/jpa/src/main/java/org/keycloak/connections/jpa/JpaConnectionProviderFactory.java @@ -1,10 +1,10 @@ package org.keycloak.connections.jpa; -import org.keycloak.provider.MonitorableProviderFactory; +import org.keycloak.provider.ServerInfoAwareProviderFactory; /** * @author Stian Thorgersen */ -public interface JpaConnectionProviderFactory extends MonitorableProviderFactory { +public interface JpaConnectionProviderFactory extends ServerInfoAwareProviderFactory { } diff --git a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java index 2df4b85363..8da15b69c1 100755 --- a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java +++ b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java @@ -14,13 +14,13 @@ import org.keycloak.connections.mongo.impl.context.TransactionMongoStoreInvocati import org.keycloak.connections.mongo.updater.MongoUpdaterProvider; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; -import org.keycloak.provider.ProviderOperationalInfo; import javax.net.ssl.SSLSocketFactory; - import java.lang.reflect.Method; import java.net.UnknownHostException; import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; /** * @author Stian Thorgersen @@ -61,7 +61,7 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro private DB db; protected Config.Scope config; - private MongoDbInfo mongoDbInfo; + private Map operationalInfo; @Override public MongoConnectionProvider create(KeycloakSession session) { @@ -165,11 +165,11 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro client = new MongoClient(new ServerAddress(host, port), clientOptions); } - mongoDbInfo = new MongoDbInfo(); - mongoDbInfo.driverVersion = client.getVersion(); - mongoDbInfo.address = client.getAddress().toString(); - mongoDbInfo.database = dbName; - mongoDbInfo.user = user; + operationalInfo = new LinkedHashMap<>(); + operationalInfo.put("mongoServerAddress", client.getAddress().toString()); + operationalInfo.put("mongoDatabaseName", dbName); + operationalInfo.put("mongoUser", user); + operationalInfo.put("mongoDriverVersion", client.getVersion()); logger.debugv("Initialized mongo model. host: %s, port: %d, db: %s", host, port, dbName); return client; @@ -219,31 +219,8 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro } @Override - public ProviderOperationalInfo getOperationalInfo() { - return mongoDbInfo; + public Map getOperationalInfo() { + return operationalInfo; } - public static class MongoDbInfo implements ProviderOperationalInfo { - - public String address; - public String database; - public String driverVersion; - public String user; - - public String getAddress() { - return address; - } - - public String getDatabase() { - return database; - } - - public String getDriverVersion() { - return driverVersion; - } - - public String getUser() { - return user; - } - } } diff --git a/connections/mongo/src/main/java/org/keycloak/connections/mongo/MongoConnectionProviderFactory.java b/connections/mongo/src/main/java/org/keycloak/connections/mongo/MongoConnectionProviderFactory.java index 3b3a00e560..bce5fe4b9c 100644 --- a/connections/mongo/src/main/java/org/keycloak/connections/mongo/MongoConnectionProviderFactory.java +++ b/connections/mongo/src/main/java/org/keycloak/connections/mongo/MongoConnectionProviderFactory.java @@ -1,9 +1,9 @@ package org.keycloak.connections.mongo; -import org.keycloak.provider.MonitorableProviderFactory; +import org.keycloak.provider.ServerInfoAwareProviderFactory; /** * @author Stian Thorgersen */ -public interface MongoConnectionProviderFactory extends MonitorableProviderFactory { +public interface MongoConnectionProviderFactory extends ServerInfoAwareProviderFactory { } diff --git a/forms/common-themes/src/main/resources/theme/base/admin/resources/js/app.js b/forms/common-themes/src/main/resources/theme/base/admin/resources/js/app.js index 3f8618bf76..8797f5ef4b 100755 --- a/forms/common-themes/src/main/resources/theme/base/admin/resources/js/app.js +++ b/forms/common-themes/src/main/resources/theme/base/admin/resources/js/app.js @@ -1112,7 +1112,13 @@ module.config([ '$routeProvider', function($routeProvider) { controller : 'AuthenticationConfigCreateCtrl' }) .when('/server-info', { - templateUrl : resourceUrl + '/partials/server-info.html' + templateUrl : resourceUrl + '/partials/server-info.html', + resolve : { + serverInfoPage : function(ServerInfoPageLoader) { + return ServerInfoPageLoader(); + } + }, + controller : 'ServerInfoPageCtrl' }) .when('/logout', { templateUrl : resourceUrl + '/partials/home.html', diff --git a/forms/common-themes/src/main/resources/theme/base/admin/resources/js/controllers/realm.js b/forms/common-themes/src/main/resources/theme/base/admin/resources/js/controllers/realm.js index 59a3f8518b..243fff73a7 100755 --- a/forms/common-themes/src/main/resources/theme/base/admin/resources/js/controllers/realm.js +++ b/forms/common-themes/src/main/resources/theme/base/admin/resources/js/controllers/realm.js @@ -113,6 +113,13 @@ module.controller('HomeCtrl', function(Realm, Auth, $location) { }); }); +module.controller('ServerInfoPageCtrl', function($scope, ServerInfoPage) { + $scope.serverInfoPage = ServerInfoPage.get(); + $scope.serverInfoPageUpdate = function() { + $scope.serverInfoPage = ServerInfoPage.get(); + }; +}); + module.controller('RealmListCtrl', function($scope, Realm, Current) { $scope.realms = Realm.query(); Current.realms = $scope.realms; diff --git a/forms/common-themes/src/main/resources/theme/base/admin/resources/js/loaders.js b/forms/common-themes/src/main/resources/theme/base/admin/resources/js/loaders.js index 773f6f0271..329802bc3c 100755 --- a/forms/common-themes/src/main/resources/theme/base/admin/resources/js/loaders.js +++ b/forms/common-themes/src/main/resources/theme/base/admin/resources/js/loaders.js @@ -39,6 +39,10 @@ module.factory('ServerInfoLoader', function(Loader, ServerInfo, $q) { return Loader.get(ServerInfo); }); +module.factory('ServerInfoPageLoader', function(Loader, ServerInfoPage, $q) { + return Loader.get(ServerInfoPage); +}); + module.factory('RealmLoader', function(Loader, Realm, $route, $q) { return Loader.get(Realm, function() { return { diff --git a/forms/common-themes/src/main/resources/theme/base/admin/resources/js/services.js b/forms/common-themes/src/main/resources/theme/base/admin/resources/js/services.js index 5e9fefda63..7bb14804f8 100755 --- a/forms/common-themes/src/main/resources/theme/base/admin/resources/js/services.js +++ b/forms/common-themes/src/main/resources/theme/base/admin/resources/js/services.js @@ -205,6 +205,9 @@ module.factory('ServerInfo', function($resource) { return $resource(authUrl + '/admin/serverinfo'); }); +module.factory('ServerInfoPage', function($resource) { + return $resource(authUrl + '/admin/serverinfopage'); +}); module.factory('ClientProtocolMapper', function($resource) { diff --git a/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info.html b/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info.html index f4e4c6dcf2..0dfac4cb9a 100755 --- a/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info.html +++ b/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info.html @@ -4,15 +4,15 @@
SPISPI Providers
- + - + - +
Keycloak Version{{serverInfo.version}}{{serverInfoPage.version}}
Server Time{{serverInfo.serverTime}} (update){{serverInfoPage.serverTime}} (update)
Server Uptime{{serverInfo.serverUptime}}{{serverInfoPage.serverUptime}}
@@ -22,15 +22,15 @@ - + - + - +
Total Memory{{serverInfo.memoryInfo.totalFormated}}{{serverInfoPage.memoryInfo.totalFormated}}
Free Memory{{serverInfo.memoryInfo.freeFormated}} ({{serverInfo.memoryInfo.freePercentage}}%){{serverInfoPage.memoryInfo.freeFormated}} ({{serverInfoPage.memoryInfo.freePercentage}}%)
Used Memory{{serverInfo.memoryInfo.usedFormated}}{{serverInfoPage.memoryInfo.usedFormated}}
@@ -42,109 +42,73 @@ - + - + - + - + - + - + - + - + - + - + - + - + - +
Current Working Directory{{serverInfo.systemInfo.userDir}}{{serverInfoPage.systemInfo.userDir}}
Java Version{{serverInfo.systemInfo.javaVersion}}{{serverInfoPage.systemInfo.javaVersion}}
Java Vendor{{serverInfo.systemInfo.javaVendor}}{{serverInfoPage.systemInfo.javaVendor}}
Java Runtime{{serverInfo.systemInfo.javaRuntime}}{{serverInfoPage.systemInfo.javaRuntime}}
Java VM{{serverInfo.systemInfo.javaVm}}{{serverInfoPage.systemInfo.javaVm}}
Java VM Version{{serverInfo.systemInfo.javaVmVersion}}{{serverInfoPage.systemInfo.javaVmVersion}}
Java Home{{serverInfo.systemInfo.javaHome}}{{serverInfoPage.systemInfo.javaHome}}
User Name{{serverInfo.systemInfo.userName}}{{serverInfoPage.systemInfo.userName}}
User Timezone{{serverInfo.systemInfo.userTimezone}}{{serverInfoPage.systemInfo.userTimezone}}
User Locale{{serverInfo.systemInfo.userLocale}}{{serverInfoPage.systemInfo.userLocale}}
System Encoding{{serverInfo.systemInfo.fileEncoding}}{{serverInfoPage.systemInfo.fileEncoding}}
Operating System{{serverInfo.systemInfo.osName}} {{serverInfo.systemInfo.osVersion}}{{serverInfoPage.systemInfo.osName}} {{serverInfoPage.systemInfo.osVersion}}
OS Architecture{{serverInfo.systemInfo.osArchitecture}}{{serverInfoPage.systemInfo.osArchitecture}}
-
+
Database Info
- - - - - - - - - - - - - - - + + +
Database URL{{serverInfo.jpaInfo.jdbcUrl}}
Database User{{serverInfo.jpaInfo.databaseUser}}
Database Type{{serverInfo.jpaInfo.databaseProduct}}
Database Driver{{serverInfo.jpaInfo.databaseDriver}}
{{key}}{{value}}
-
- Mongo DB Info -
- - - - - - - - - - - - - - - - - -
Address{{serverInfo.mongoDbInfo.address}}
Database{{serverInfo.mongoDbInfo.database}}
User{{serverInfo.mongoDbInfo.user}}
Driver Version{{serverInfo.mongoDbInfo.driverVersion}}
-
-
-
Providers @@ -160,11 +124,23 @@ - + {{spi.name}} -
- {{provider}} +
+ {{provider.name}} + + + + + + + +
{{key}}{{value}}
+
@@ -184,11 +160,23 @@ - + {{spi.name}} -
- {{provider}} +
+ {{provider.name}} + + + + + + + +
{{key}}{{value}}
+
diff --git a/model/api/src/main/java/org/keycloak/provider/MonitorableProviderFactory.java b/model/api/src/main/java/org/keycloak/provider/MonitorableProviderFactory.java deleted file mode 100644 index 7b9c95c921..0000000000 --- a/model/api/src/main/java/org/keycloak/provider/MonitorableProviderFactory.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.keycloak.provider; - -/** - * Provider factory for provider which is monitorable. It means some info about it can be shown on "Server Info" page or accessed over Operational monitoring endpoint. - * - * @author Vlastimil Elias (velias at redhat dot com) - */ -public interface MonitorableProviderFactory extends ProviderFactory { - - /** - * Get operational info about given provider. This info contains informations about providers configuration and operational conditions (eg. errors in connection to remote systems etc). - * Is used to be shown on "Server Info" page or in Operational monitoring endpoint. - * - * @return extendion of {@link ProviderOperationalInfo} - */ - public ProviderOperationalInfo getOperationalInfo(); - -} diff --git a/model/api/src/main/java/org/keycloak/provider/ProviderOperationalInfo.java b/model/api/src/main/java/org/keycloak/provider/ProviderOperationalInfo.java deleted file mode 100644 index e37544d276..0000000000 --- a/model/api/src/main/java/org/keycloak/provider/ProviderOperationalInfo.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.keycloak.provider; - -import java.io.Serializable; - -/** - * Operational info about given Provider. - * Contains info about Provider that can be shown on "Server Info" page or accessed over Operational monitoring endpoint. - * - * @author Vlastimil Elias (velias at redhat dot com) - * @see MonitorableProviderFactory - */ -public interface ProviderOperationalInfo extends Serializable { - -} diff --git a/model/api/src/main/java/org/keycloak/provider/ServerInfoAwareProviderFactory.java b/model/api/src/main/java/org/keycloak/provider/ServerInfoAwareProviderFactory.java new file mode 100644 index 0000000000..97d17b1dcc --- /dev/null +++ b/model/api/src/main/java/org/keycloak/provider/ServerInfoAwareProviderFactory.java @@ -0,0 +1,20 @@ +package org.keycloak.provider; + +import java.util.Map; + +/** + * Marker interface for ProviderFactory of Provider which wants to show some info on "Server Info" page in Admin console. + * + * @author Vlastimil Elias (velias at redhat dot com) + */ +public interface ServerInfoAwareProviderFactory extends ProviderFactory { + + /** + * Get operational info about given provider. This info contains informations about providers configuration and operational conditions (eg. errors in connection to remote systems etc) which is + * shown on "Server Info" page. + * + * @return Map with keys describing value and relevant values itself + */ + public Map getOperationalInfo(); + +} diff --git a/services/pom.xml b/services/pom.xml index 16fef06d5c..da71e9ae6f 100755 --- a/services/pom.xml +++ b/services/pom.xml @@ -39,21 +39,6 @@ keycloak-connections-http-client provided - - org.keycloak - keycloak-connections-jpa - provided - - - org.hibernate - hibernate-entitymanager - provided - - - org.keycloak - keycloak-connections-mongo - provided - org.keycloak keycloak-forms-common-freemarker diff --git a/services/src/main/java/org/keycloak/services/resources/admin/AdminRoot.java b/services/src/main/java/org/keycloak/services/resources/admin/AdminRoot.java index e1d1fa3c48..6b3ca3eafa 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/AdminRoot.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/AdminRoot.java @@ -218,6 +218,32 @@ public class AdminRoot { return adminResource; } + /** + * Operational information about the server for "Server Info" page + * + * @param headers + * @return + */ + @Path("serverinfopage") + public ServerInfoPageAdminResource getServerInfoPage(@Context final HttpHeaders headers) { + handlePreflightRequest(); + + AdminAuth auth = authenticateRealmAdminRequest(headers); + if (!isAdmin(auth)) { + throw new ForbiddenException(); + } + + if (auth != null) { + logger.debug("authenticated admin access for: " + auth.getUser().getUsername()); + } + + Cors.add(request).allowedOrigins(auth.getToken()).allowedMethods("GET", "PUT", "POST", "DELETE").auth().build(response); + + ServerInfoPageAdminResource adminResource = new ServerInfoPageAdminResource(); + ResteasyProviderFactory.getInstance().injectProperties(adminResource); + return adminResource; + } + protected boolean isAdmin(AdminAuth auth) { RealmManager realmManager = new RealmManager(session); diff --git a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java b/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java index 3b69304b06..4f1c22575b 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java @@ -1,11 +1,8 @@ package org.keycloak.services.resources.admin; -import org.jboss.logging.Logger; import org.keycloak.Version; import org.keycloak.broker.provider.IdentityProvider; import org.keycloak.broker.provider.IdentityProviderFactory; -import org.keycloak.connections.jpa.JpaConnectionProvider; -import org.keycloak.connections.mongo.MongoConnectionProvider; import org.keycloak.events.EventListenerProvider; import org.keycloak.events.EventType; import org.keycloak.events.admin.OperationType; @@ -19,10 +16,8 @@ import org.keycloak.models.utils.ModelToRepresentation; import org.keycloak.protocol.LoginProtocol; import org.keycloak.protocol.LoginProtocolFactory; import org.keycloak.protocol.ProtocolMapper; -import org.keycloak.provider.MonitorableProviderFactory; import org.keycloak.provider.ProviderConfigProperty; import org.keycloak.provider.ProviderFactory; -import org.keycloak.provider.ProviderOperationalInfo; import org.keycloak.provider.Spi; import org.keycloak.representations.idm.ConfigPropertyRepresentation; import org.keycloak.representations.idm.ProtocolMapperRepresentation; @@ -31,13 +26,11 @@ import org.keycloak.social.SocialIdentityProvider; import javax.ws.rs.GET; import javax.ws.rs.core.Context; -import java.io.Serializable; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.LinkedList; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.ServiceLoader; import java.util.Set; @@ -46,8 +39,6 @@ import java.util.Set; * @author Stian Thorgersen */ public class ServerInfoAdminResource { - - private static final Logger logger = Logger.getLogger(ServerInfoAdminResource.class); private static final Map> ENUMS = createEnumsMap(EventType.class, OperationType.class); @@ -64,9 +55,6 @@ public class ServerInfoAdminResource { ServerInfoRepresentation info = new ServerInfoRepresentation(); info.version = Version.VERSION; info.serverTime = new Date().toString(); - info.serverStartupTime = session.getKeycloakSessionFactory().getServerStartupTimestamp(); - info.memoryInfo = (new MemoryInfo()).init(Runtime.getRuntime()); - info.systemInfo = (new SystemInfo()).init(); setSocialProviders(info); setIdentityProviders(info); setThemes(info); @@ -77,21 +65,6 @@ public class ServerInfoAdminResource { setProtocolMapperTypes(info); setBuiltinProtocolMappers(info); info.setEnums(ENUMS); - - ProviderFactory jpf = session.getKeycloakSessionFactory().getProviderFactory(JpaConnectionProvider.class); - if(jpf!=null && jpf instanceof MonitorableProviderFactory){ - info.jpaInfo = ((MonitorableProviderFactory)jpf).getOperationalInfo(); - } else { - logger.debug("JPA provider not found or is not monitorable"); - } - - ProviderFactory mpf = session.getKeycloakSessionFactory().getProviderFactory(MongoConnectionProvider.class); - if(mpf!=null && mpf instanceof MonitorableProviderFactory){ - info.mongoDbInfo = ((MonitorableProviderFactory)mpf).getOperationalInfo(); - } else { - logger.debug("Mongo provider not found or is not monitorable"); - } - return info; } @@ -214,170 +187,12 @@ public class ServerInfoAdminResource { info.clientImporters.add(data); } } - - public static class MemoryInfo implements Serializable { - - protected long total; - protected long used; - - public MemoryInfo(){ - } - - /** - * Fill object fwith info. - * @param runtime used to get memory info from. - * @return itself for chaining - */ - public MemoryInfo init(Runtime runtime){ - total = runtime.maxMemory(); - used = runtime.totalMemory() - runtime.freeMemory(); - return this; - } - - public long getTotal(){ - return total; - } - - public String getTotalFormated(){ - return formatMemory(getTotal()); - } - - public long getFree(){ - return getTotal() - getUsed(); - } - public String getFreeFormated(){ - return formatMemory(getFree()); - } - - public long getUsed(){ - return used; - } - - public String getUsedFormated(){ - return formatMemory(getUsed()); - } - - public long getFreePercentage(){ - return getFree() * 100 / getTotal(); - } - - private String formatMemory(long bytes){ - if(bytes > 1024L*1024L){ - return bytes/(1024L *1024L) + " MB"; - } else if(bytes > 1024L){ - return bytes/(1024L) + " kB"; - } else { - return bytes + " B"; - } - } - - } - - public static class SystemInfo implements Serializable { - - protected String javaVersion; - protected String javaVendor; - protected String javaVm; - protected String javaVmVersion; - protected String javaRuntime; - protected String javaHome; - protected String osName; - protected String osArchitecture; - protected String osVersion; - protected String fileEncoding; - protected String userName; - protected String userDir; - protected String userTimezone; - protected String userLocale; - - public SystemInfo() { - } - - /** - * Fill object with info about current system loaded from {@link System} properties. - * @return object itself for chaining - */ - protected SystemInfo init(){ - javaVersion = System.getProperty("java.version"); - javaVendor = System.getProperty("java.vendor"); - javaVm = System.getProperty("java.vm.name"); - javaVmVersion = System.getProperty("java.vm.version"); - javaRuntime = System.getProperty("java.runtime.name"); - javaHome = System.getProperty("java.home"); - osName = System.getProperty("os.name"); - osArchitecture = System.getProperty("os.arch"); - osVersion = System.getProperty("os.version"); - fileEncoding = System.getProperty("file.encoding"); - userName = System.getProperty("user.name"); - userDir = System.getProperty("user.dir"); - userTimezone = System.getProperty("user.timezone"); - userLocale = (new Locale(System.getProperty("user.country"),System.getProperty("user.language")).toString()); - return this; - } - - public String getJavaVersion(){ - return javaVersion; - } - - public String getJavaVendor(){ - return javaVendor; - } - - public String getJavaVm(){ - return javaVm; - } - - public String getJavaVmVersion(){ - return javaVmVersion; - } - - public String getJavaRuntime(){ - return javaRuntime; - } - - public String getJavaHome(){ - return javaHome; - } - - public String getOsName(){ - return osName; - } - - public String getOsArchitecture(){ - return osArchitecture; - } - - public String getOsVersion(){ - return osVersion; - } - - public String getFileEncoding(){ - return fileEncoding; - } - - public String getUserName(){ - return userName; - } - - public String getUserDir(){ - return userDir; - } - - public String getUserTimezone(){ - return userTimezone; - } - - public String getUserLocale(){ - return userLocale; - } - } - - public static class ServerInfoRepresentation implements Serializable { + public static class ServerInfoRepresentation { private String version; + private String serverTime; - private long serverStartupTime; private Map> themes; @@ -393,76 +208,13 @@ public class ServerInfoAdminResource { private Map> builtinProtocolMappers; private Map> enums; - - private MemoryInfo memoryInfo; - private SystemInfo systemInfo; - - private ProviderOperationalInfo jpaInfo; - private ProviderOperationalInfo mongoDbInfo; public ServerInfoRepresentation() { } - - public SystemInfo getSystemInfo(){ - return systemInfo; - } - - public MemoryInfo getMemoryInfo(){ - return memoryInfo; - } - - public ProviderOperationalInfo getJpaInfo() { - return jpaInfo; - } - - public ProviderOperationalInfo getMongoDbInfo() { - return mongoDbInfo; - } public String getServerTime() { return serverTime; } - - public long getServerStartupTime() { - return serverStartupTime; - } - - /** - * @return server startup time formatted - */ - public String getServerStartupTimeFormatted() { - return (new Date(serverStartupTime)).toString(); - } - - /** - * @return server uptime in millis - */ - public long getServerUptimeMillis(){ - return System.currentTimeMillis() - serverStartupTime; - } - - /** - * @return server uptime formatted like "0 days, 10 hours, 24 minutes, 55 seconds" - */ - public String getServerUptime(){ - long diffInSeconds = getServerUptimeMillis()/1000; - long diff[] = new long[] { 0, 0, 0, 0 }; - /* sec */diff[3] = (diffInSeconds >= 60 ? diffInSeconds % 60 : diffInSeconds); - /* min */diff[2] = (diffInSeconds = (diffInSeconds / 60)) >= 60 ? diffInSeconds % 60 : diffInSeconds; - /* hours */diff[1] = (diffInSeconds = (diffInSeconds / 60)) >= 24 ? diffInSeconds % 24 : diffInSeconds; - /* days */diff[0] = (diffInSeconds = (diffInSeconds / 24)); - - return String.format( - "%d day%s, %d hour%s, %d minute%s, %d second%s", - diff[0], - diff[0] != 1 ? "s" : "", - diff[1], - diff[1] != 1 ? "s" : "", - diff[2], - diff[2] != 1 ? "s" : "", - diff[3], - diff[3] != 1 ? "s" : ""); - } public String getVersion() { return version; @@ -517,7 +269,7 @@ public class ServerInfoAdminResource { } } - public static class SpiInfoRepresentation implements Serializable { + public static class SpiInfoRepresentation { private String name; private boolean internal; private Set implementations; diff --git a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoPageAdminResource.java b/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoPageAdminResource.java new file mode 100644 index 0000000000..c6a720816c --- /dev/null +++ b/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoPageAdminResource.java @@ -0,0 +1,375 @@ +package org.keycloak.services.resources.admin; + +import java.io.Serializable; +import java.util.Date; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.ServiceLoader; +import java.util.Set; + +import javax.ws.rs.GET; +import javax.ws.rs.core.Context; + +import org.jboss.logging.Logger; +import org.keycloak.Version; +import org.keycloak.models.KeycloakSession; +import org.keycloak.provider.ServerInfoAwareProviderFactory; +import org.keycloak.provider.Spi; + +/** + * REST endpoint which return info for "Server Info" page. + * + * @author Vlastimil Elias (velias at redhat dot com) + */ +public class ServerInfoPageAdminResource { + + private static final Logger logger = Logger.getLogger(ServerInfoPageAdminResource.class); + + @Context + private KeycloakSession session; + + /** + * Returns a list of providers and other operational info about the page. + * + * @return + */ + @GET + public ServerInfoRepresentation getInfo() { + ServerInfoRepresentation info = new ServerInfoRepresentation(); + info.version = Version.VERSION; + info.serverTime = new Date().toString(); + info.serverStartupTime = session.getKeycloakSessionFactory().getServerStartupTimestamp(); + info.memoryInfo = (new MemoryInfo()).init(Runtime.getRuntime()); + info.systemInfo = (new SystemInfo()).init(); + setProviders(info); + return info; + } + + private void setProviders(ServerInfoRepresentation info) { + List providers = new LinkedList<>(); + for (Spi spi : ServiceLoader.load(Spi.class)) { + SpiInfoRepresentation spiRep = new SpiInfoRepresentation(); + spiRep.setName(spi.getName()); + spiRep.setInternal(spi.isInternal()); + spiRep.setSystemInfo(ServerInfoAwareProviderFactory.class.isAssignableFrom(spi.getProviderFactoryClass())); + Set s = session.listProviderIds(spi.getProviderClass()); + Set srs = new HashSet<>(); + + if(s!=null){ + for(String name: s){ + SpiImplementationRepresentation sr = new SpiImplementationRepresentation(name); + if(spiRep.isSystemInfo()){ + sr.setOperationalInfo(((ServerInfoAwareProviderFactory)session.getKeycloakSessionFactory().getProviderFactory(spi.getProviderClass(), name)).getOperationalInfo()); + } + srs.add(sr); + } + } + spiRep.setImplementations(srs); + providers.add(spiRep); + } + info.providers = providers; + } + + + public static class MemoryInfo implements Serializable { + + protected long total; + protected long used; + + public MemoryInfo(){ + } + + /** + * Fill object fwith info. + * @param runtime used to get memory info from. + * @return itself for chaining + */ + public MemoryInfo init(Runtime runtime){ + total = runtime.maxMemory(); + used = runtime.totalMemory() - runtime.freeMemory(); + return this; + } + + public long getTotal(){ + return total; + } + + public String getTotalFormated(){ + return formatMemory(getTotal()); + } + + public long getFree(){ + return getTotal() - getUsed(); + } + + public String getFreeFormated(){ + return formatMemory(getFree()); + } + + public long getUsed(){ + return used; + } + + public String getUsedFormated(){ + return formatMemory(getUsed()); + } + + public long getFreePercentage(){ + return getFree() * 100 / getTotal(); + } + + private String formatMemory(long bytes){ + if(bytes > 1024L*1024L){ + return bytes/(1024L *1024L) + " MB"; + } else if(bytes > 1024L){ + return bytes/(1024L) + " kB"; + } else { + return bytes + " B"; + } + } + + } + + public static class SystemInfo implements Serializable { + + protected String javaVersion; + protected String javaVendor; + protected String javaVm; + protected String javaVmVersion; + protected String javaRuntime; + protected String javaHome; + protected String osName; + protected String osArchitecture; + protected String osVersion; + protected String fileEncoding; + protected String userName; + protected String userDir; + protected String userTimezone; + protected String userLocale; + + public SystemInfo() { + } + + /** + * Fill object with info about current system loaded from {@link System} properties. + * @return object itself for chaining + */ + protected SystemInfo init(){ + javaVersion = System.getProperty("java.version"); + javaVendor = System.getProperty("java.vendor"); + javaVm = System.getProperty("java.vm.name"); + javaVmVersion = System.getProperty("java.vm.version"); + javaRuntime = System.getProperty("java.runtime.name"); + javaHome = System.getProperty("java.home"); + osName = System.getProperty("os.name"); + osArchitecture = System.getProperty("os.arch"); + osVersion = System.getProperty("os.version"); + fileEncoding = System.getProperty("file.encoding"); + userName = System.getProperty("user.name"); + userDir = System.getProperty("user.dir"); + userTimezone = System.getProperty("user.timezone"); + userLocale = (new Locale(System.getProperty("user.country"),System.getProperty("user.language")).toString()); + return this; + } + + public String getJavaVersion(){ + return javaVersion; + } + + public String getJavaVendor(){ + return javaVendor; + } + + public String getJavaVm(){ + return javaVm; + } + + public String getJavaVmVersion(){ + return javaVmVersion; + } + + public String getJavaRuntime(){ + return javaRuntime; + } + + public String getJavaHome(){ + return javaHome; + } + + public String getOsName(){ + return osName; + } + + public String getOsArchitecture(){ + return osArchitecture; + } + + public String getOsVersion(){ + return osVersion; + } + + public String getFileEncoding(){ + return fileEncoding; + } + + public String getUserName(){ + return userName; + } + + public String getUserDir(){ + return userDir; + } + + public String getUserTimezone(){ + return userTimezone; + } + + public String getUserLocale(){ + return userLocale; + } + } + + public static class ServerInfoRepresentation implements Serializable { + + private String version; + private String serverTime; + private long serverStartupTime; + + + private List providers; + + private MemoryInfo memoryInfo; + private SystemInfo systemInfo; + + public ServerInfoRepresentation() { + } + + public SystemInfo getSystemInfo(){ + return systemInfo; + } + + public MemoryInfo getMemoryInfo(){ + return memoryInfo; + } + + public String getServerTime() { + return serverTime; + } + + public long getServerStartupTime() { + return serverStartupTime; + } + + /** + * @return server startup time formatted + */ + public String getServerStartupTimeFormatted() { + return (new Date(serverStartupTime)).toString(); + } + + /** + * @return server uptime in millis + */ + public long getServerUptimeMillis(){ + return System.currentTimeMillis() - serverStartupTime; + } + + /** + * @return server uptime formatted like "0 days, 10 hours, 24 minutes, 55 seconds" + */ + public String getServerUptime(){ + long diffInSeconds = getServerUptimeMillis()/1000; + long diff[] = new long[] { 0, 0, 0, 0 }; + /* sec */diff[3] = (diffInSeconds >= 60 ? diffInSeconds % 60 : diffInSeconds); + /* min */diff[2] = (diffInSeconds = (diffInSeconds / 60)) >= 60 ? diffInSeconds % 60 : diffInSeconds; + /* hours */diff[1] = (diffInSeconds = (diffInSeconds / 60)) >= 24 ? diffInSeconds % 24 : diffInSeconds; + /* days */diff[0] = (diffInSeconds = (diffInSeconds / 24)); + + return String.format( + "%d day%s, %d hour%s, %d minute%s, %d second%s", + diff[0], + diff[0] != 1 ? "s" : "", + diff[1], + diff[1] != 1 ? "s" : "", + diff[2], + diff[2] != 1 ? "s" : "", + diff[3], + diff[3] != 1 ? "s" : ""); + } + + public String getVersion() { + return version; + } + + + public List getProviders() { + return providers; + } + } + + public static class SpiInfoRepresentation implements Serializable { + private String name; + private boolean internal; + private boolean systemInfo; + private Set implementations; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean isInternal() { + return internal; + } + + public void setInternal(boolean internal) { + this.internal = internal; + } + + public Set getImplementations() { + return implementations; + } + + public boolean isSystemInfo() { + return systemInfo; + } + + public void setSystemInfo(boolean systemInfo) { + this.systemInfo = systemInfo; + } + + public void setImplementations(Set implementations) { + this.implementations = implementations; + } + } + + public static class SpiImplementationRepresentation implements Serializable { + + private String name; + private Map operationalInfo; + + public SpiImplementationRepresentation(String name) { + super(); + this.name = name; + } + + public Map getOperationalInfo() { + return operationalInfo; + } + + public void setOperationalInfo(Map operationalInfo) { + this.operationalInfo = operationalInfo; + } + + public String getName() { + return name; + } + + } +} diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AdminAPITest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AdminAPITest.java index 2cca19a0c9..33ead317e4 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AdminAPITest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AdminAPITest.java @@ -317,13 +317,41 @@ public class AdminAPITest { Assert.assertNotNull(response); Assert.assertEquals(Version.VERSION, response.get("version")); Assert.assertNotNull(response.get("serverTime")); + Assert.assertNotNull(response.get("providers")); + Assert.assertNotNull(response.get("themes")); + Assert.assertNotNull(response.get("enums")); + + // System.out.println(response); + + } + + @Test + public void testServerInfoPage() { + + String token = createToken(); + final String authHeader = "Bearer " + token; + ClientRequestFilter authFilter = new ClientRequestFilter() { + @Override + public void filter(ClientRequestContext requestContext) throws IOException { + requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, authHeader); + } + }; + Client client = ClientBuilder.newBuilder().register(authFilter).build(); + UriBuilder authBase = UriBuilder.fromUri("http://localhost:8081/auth"); + WebTarget target = client.target(AdminRoot.adminBaseUrl(authBase).path("serverinfopage")); + + Map response = target.request().accept("application/json").get(Map.class); + + Assert.assertNotNull(response); + Assert.assertEquals(Version.VERSION, response.get("version")); + Assert.assertNotNull(response.get("serverTime")); + Assert.assertNotNull(response.get("providers")); Assert.assertNotNull(response.get("serverStartupTime")); Assert.assertNotNull(response.get("memoryInfo")); - Assert.assertNotNull(response.get("jpaInfo")); - Assert.assertNull(response.get("mongoDbInfo")); + Assert.assertNotNull(response.get("systemInfo")); - System.out.println(response); + // System.out.println(response); }