From d1964c20abbf93731781e230747b0155224c597d Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Fri, 24 Jul 2015 14:47:37 +0200 Subject: [PATCH] Refactored server-info and updated admin console --- .../theme/base/admin/resources/js/app.js | 19 +- .../admin/resources/js/controllers/clients.js | 2 +- .../admin/resources/js/controllers/realm.js | 32 +- .../theme/base/admin/resources/js/loaders.js | 10 +- .../theme/base/admin/resources/js/services.js | 24 +- .../partials/server-info-providers.html | 55 +++ .../admin/resources/partials/server-info.html | 264 +++++------- .../services/resources/admin/AdminRoot.java | 27 +- .../admin/ServerInfoPageAdminResource.java | 375 ------------------ .../admin/info/MemoryInfoRepresentation.java | 54 +++ .../admin/info/ProviderRepresentation.java | 17 + .../{ => info}/ServerInfoAdminResource.java | 200 ++-------- .../admin/info/ServerInfoRepresentation.java | 108 +++++ .../admin/info/SpiInfoRepresentation.java | 39 ++ .../admin/info/SystemInfoRepresentation.java | 217 ++++++++++ 15 files changed, 681 insertions(+), 762 deletions(-) create mode 100755 forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info-providers.html delete mode 100644 services/src/main/java/org/keycloak/services/resources/admin/ServerInfoPageAdminResource.java create mode 100644 services/src/main/java/org/keycloak/services/resources/admin/info/MemoryInfoRepresentation.java create mode 100644 services/src/main/java/org/keycloak/services/resources/admin/info/ProviderRepresentation.java rename services/src/main/java/org/keycloak/services/resources/admin/{ => info}/ServerInfoAdminResource.java (54%) create mode 100644 services/src/main/java/org/keycloak/services/resources/admin/info/ServerInfoRepresentation.java create mode 100644 services/src/main/java/org/keycloak/services/resources/admin/info/SpiInfoRepresentation.java create mode 100644 services/src/main/java/org/keycloak/services/resources/admin/info/SystemInfoRepresentation.java 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 4f02ce6b48..5e01598199 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 @@ -82,8 +82,8 @@ module.config([ '$routeProvider', function($routeProvider) { realm : function(RealmLoader) { return RealmLoader(); }, - serverInfo : function(ServerInfoLoader) { - return ServerInfoLoader(); + serverInfo : function(ServerInfo) { + return ServerInfo.delay; } }, controller : 'RealmLoginSettingsCtrl' @@ -1135,11 +1135,20 @@ module.config([ '$routeProvider', function($routeProvider) { .when('/server-info', { templateUrl : resourceUrl + '/partials/server-info.html', resolve : { - serverInfoPage : function(ServerInfoPageLoader) { - return ServerInfoPageLoader(); + serverInfo : function(ServerInfoLoader) { + return ServerInfoLoader(); } }, - controller : 'ServerInfoPageCtrl' + controller : 'ServerInfoCtrl' + }) + .when('/server-info/providers', { + templateUrl : resourceUrl + '/partials/server-info-providers.html', + resolve : { + serverInfo : function(ServerInfoLoader) { + return ServerInfoLoader(); + } + }, + controller : 'ServerInfoCtrl' }) .when('/logout', { templateUrl : resourceUrl + '/partials/home.html', diff --git a/forms/common-themes/src/main/resources/theme/base/admin/resources/js/controllers/clients.js b/forms/common-themes/src/main/resources/theme/base/admin/resources/js/controllers/clients.js index 6331c9d39a..8ad8ae646e 100755 --- a/forms/common-themes/src/main/resources/theme/base/admin/resources/js/controllers/clients.js +++ b/forms/common-themes/src/main/resources/theme/base/admin/resources/js/controllers/clients.js @@ -549,7 +549,7 @@ module.controller('ClientDetailCtrl', function($scope, realm, client, $route, se "bearer-only" ]; - $scope.protocols = serverInfo.protocols; + $scope.protocols = Object.keys(serverInfo.providers['login-protocol'].providers); $scope.signatureAlgorithms = [ "RSA_SHA1", 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 0b5745a081..15a8fa9223 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 @@ -7,9 +7,6 @@ module.controller('GlobalCtrl', function($scope, $http, Auth, WhoAmI, Current, $ $scope.resourceUrl = resourceUrl; $scope.auth = Auth; $scope.serverInfo = ServerInfo.get(); - $scope.serverInfoUpdate = function() { - $scope.serverInfo = ServerInfo.get(); - }; function hasAnyAccess() { var realmAccess = Auth.user && Auth.user['realm_access']; @@ -125,18 +122,23 @@ module.controller('RealmTabCtrl', function(Dialog, $scope, Current, Realm, Notif }; }); -module.controller('ServerInfoPageCtrl', function($scope, ServerInfoPage) { - $scope.serverInfoPage = ServerInfoPage.get(); - $scope.serverInfoPageUpdate = function() { - $scope.serverInfoPage = ServerInfoPage.get(); - }; -}); +module.controller('ServerInfoCtrl', function($scope, ServerInfo) { + ServerInfo.reload(); -module.controller('ServerInfoPageCtrl', function($scope, ServerInfoPage) { - $scope.serverInfoPage = ServerInfoPage.get(); - $scope.serverInfoPageUpdate = function() { - $scope.serverInfoPage = ServerInfoPage.get(); - }; + $scope.serverInfo = ServerInfo.get(); + + $scope.$watch($scope.serverInfo, function() { + $scope.providers = []; + for(var spi in $scope.serverInfo.providers) { + var p = angular.copy($scope.serverInfo.providers[spi]); + p.name = spi; + $scope.providers.push(p) + } + }); + + $scope.serverInfoReload = function() { + ServerInfo.reload(); + } }); module.controller('RealmListCtrl', function($scope, Realm, Current) { @@ -1231,7 +1233,7 @@ module.controller('RealmEventsConfigCtrl', function($scope, eventsConfig, RealmE } }); - $scope.eventListeners = serverInfo.eventListeners; + $scope.eventListeners = Object.keys(serverInfo.providers.eventsListener.providers); $scope.eventSelectOptions = { 'multiple': true, 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 2e65b3c712..c347759413 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 @@ -35,12 +35,10 @@ module.factory('RealmListLoader', function(Loader, Realm, $q) { return Loader.get(Realm); }); -module.factory('ServerInfoLoader', function(Loader, ServerInfo, $q) { - return Loader.get(ServerInfo); -}); - -module.factory('ServerInfoPageLoader', function(Loader, ServerInfoPage, $q) { - return Loader.get(ServerInfoPage); +module.factory('ServerInfoLoader', function(Loader, ServerInfo) { + return function() { + return ServerInfo.promise; + }; }); module.factory('RealmLoader', function(Loader, Realm, $route, $q) { 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 b5d8c330f3..04929ef7f1 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 @@ -215,12 +215,26 @@ module.factory('RealmLDAPConnectionTester', function($resource) { return $resource(authUrl + '/admin/realms/:realm/testLDAPConnection'); }); -module.factory('ServerInfo', function($resource) { - return $resource(authUrl + '/admin/serverinfo'); -}); +module.service('ServerInfo', function($resource, $q, $http) { + var info = {}; + var delay = $q.defer(); -module.factory('ServerInfoPage', function($resource) { - return $resource(authUrl + '/admin/serverinfopage'); + $http.get(authUrl + '/admin/serverinfo').success(function(data) { + info = data; + delay.resolve(info); + }); + + return { + get: function() { + return info; + }, + reload: function() { + $http.get(authUrl + '/admin/serverinfo').success(function(data) { + angular.copy(data, info); + }); + }, + promise: delay.promise + } }); diff --git a/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info-providers.html b/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info-providers.html new file mode 100755 index 0000000000..6f8bbb49d7 --- /dev/null +++ b/forms/common-themes/src/main/resources/theme/base/admin/resources/partials/server-info-providers.html @@ -0,0 +1,55 @@ +
+

+ Server Info + +

+ + + + + + + + + + + + + + + + + + + +
+
+
+
+ +
+
+
+
SPIProviders
{{spi.name}} +
+ {{providerName}} + + + + + + + +
{{key}}{{value}}
+
+
+
+
+ + \ No newline at end of file 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 991f208401..a92c774b25 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,174 +1,104 @@ -
-

Server Info

+
+

+ Server Info + +

- - - - - - - - - - - - - -
Keycloak Version{{serverInfoPage.version}}
Server Time{{serverInfoPage.serverTime}} (update)
Server Uptime{{serverInfoPage.serverUptime}}
+ + + + + + + + + + + + + + + +
Keycloak Version{{serverInfo.systemInfo.version}}
Server Time{{serverInfo.systemInfo.serverTime}}
Server Uptime{{serverInfo.systemInfo.uptime}}
-
- Java VM Memory Statistics -
- - - - - - - - - - - - - -
Total Memory{{serverInfoPage.memoryInfo.totalFormated}}
Free Memory{{serverInfoPage.memoryInfo.freeFormated}} ({{serverInfoPage.memoryInfo.freePercentage}}%)
Used Memory{{serverInfoPage.memoryInfo.usedFormated}}
-
-
-
- System Info -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + Memory +
Current Working Directory{{serverInfoPage.systemInfo.userDir}}
Java Version{{serverInfoPage.systemInfo.javaVersion}}
Java Vendor{{serverInfoPage.systemInfo.javaVendor}}
Java Runtime{{serverInfoPage.systemInfo.javaRuntime}}
Java VM{{serverInfoPage.systemInfo.javaVm}}
Java VM Version{{serverInfoPage.systemInfo.javaVmVersion}}
Java Home{{serverInfoPage.systemInfo.javaHome}}
User Name{{serverInfoPage.systemInfo.userName}}
User Timezone{{serverInfoPage.systemInfo.userTimezone}}
User Locale{{serverInfoPage.systemInfo.userLocale}}
System Encoding{{serverInfoPage.systemInfo.fileEncoding}}
Operating System{{serverInfoPage.systemInfo.osName}} {{serverInfoPage.systemInfo.osVersion}}
OS Architecture{{serverInfoPage.systemInfo.osArchitecture}}
+ + + + + + + + + + + +
Total Memory{{serverInfo.memoryInfo.totalFormated}}
Free Memory{{serverInfo.memoryInfo.freeFormated}} ({{serverInfo.memoryInfo.freePercentage}}%)
Used Memory{{serverInfo.memoryInfo.usedFormated}}
-
-
+
- Providers - -
-

Public SPIs

- For public SPIs there are built-in providers, but it's also supported to write your own custom providers. - - - - - - - - - - - - - - -
SPIProviders
{{spi.name}} -
- {{provider.name}} - - - - - - - -
{{key}}{{value}}
-
-
-
-
- -
-

Internal SPIs

- For internal SPIs there are only built-in providers. It's not recommended to write your own custom providers as internal SPIs may change or be removed without notice. - - - - - - - - - - - - - - -
SPIProviders
{{spi.name}} -
- {{provider.name}} - - - - - - - -
{{key}}{{value}}
-
-
-
-
+ System + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
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}}
-
\ No newline at end of file +
+ + \ No newline at end of file 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 6b3ca3eafa..97c147247c 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 @@ -20,6 +20,7 @@ import org.keycloak.services.managers.AppAuthManager; import org.keycloak.services.managers.AuthenticationManager; import org.keycloak.services.managers.RealmManager; import org.keycloak.services.resources.Cors; +import org.keycloak.services.resources.admin.info.ServerInfoAdminResource; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -218,32 +219,6 @@ 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/ServerInfoPageAdminResource.java b/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoPageAdminResource.java deleted file mode 100644 index c6a720816c..0000000000 --- a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoPageAdminResource.java +++ /dev/null @@ -1,375 +0,0 @@ -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/services/src/main/java/org/keycloak/services/resources/admin/info/MemoryInfoRepresentation.java b/services/src/main/java/org/keycloak/services/resources/admin/info/MemoryInfoRepresentation.java new file mode 100644 index 0000000000..56a1110d72 --- /dev/null +++ b/services/src/main/java/org/keycloak/services/resources/admin/info/MemoryInfoRepresentation.java @@ -0,0 +1,54 @@ +package org.keycloak.services.resources.admin.info; + +public class MemoryInfoRepresentation { + + protected long total; + protected long used; + + public static MemoryInfoRepresentation create() { + MemoryInfoRepresentation rep = new MemoryInfoRepresentation(); + Runtime runtime = Runtime.getRuntime(); + rep.total = runtime.maxMemory(); + rep.used = runtime.totalMemory() - runtime.freeMemory(); + return rep; + } + + 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"; + } + } + +} diff --git a/services/src/main/java/org/keycloak/services/resources/admin/info/ProviderRepresentation.java b/services/src/main/java/org/keycloak/services/resources/admin/info/ProviderRepresentation.java new file mode 100644 index 0000000000..ffcaeb1651 --- /dev/null +++ b/services/src/main/java/org/keycloak/services/resources/admin/info/ProviderRepresentation.java @@ -0,0 +1,17 @@ +package org.keycloak.services.resources.admin.info; + +import java.util.Map; + +public class ProviderRepresentation { + + private Map operationalInfo; + + public Map getOperationalInfo() { + return operationalInfo; + } + + public void setOperationalInfo(Map operationalInfo) { + this.operationalInfo = operationalInfo; + } + +} diff --git a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java b/services/src/main/java/org/keycloak/services/resources/admin/info/ServerInfoAdminResource.java similarity index 54% rename from services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java rename to services/src/main/java/org/keycloak/services/resources/admin/info/ServerInfoAdminResource.java index 4f1c22575b..8223556b18 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/ServerInfoAdminResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/info/ServerInfoAdminResource.java @@ -1,9 +1,7 @@ -package org.keycloak.services.resources.admin; +package org.keycloak.services.resources.admin.info; -import org.keycloak.Version; import org.keycloak.broker.provider.IdentityProvider; import org.keycloak.broker.provider.IdentityProviderFactory; -import org.keycloak.events.EventListenerProvider; import org.keycloak.events.EventType; import org.keycloak.events.admin.OperationType; import org.keycloak.exportimport.ClientImporter; @@ -18,6 +16,7 @@ import org.keycloak.protocol.LoginProtocolFactory; import org.keycloak.protocol.ProtocolMapper; import org.keycloak.provider.ProviderConfigProperty; import org.keycloak.provider.ProviderFactory; +import org.keycloak.provider.ServerInfoAwareProviderFactory; import org.keycloak.provider.Spi; import org.keycloak.representations.idm.ConfigPropertyRepresentation; import org.keycloak.representations.idm.ProtocolMapperRepresentation; @@ -26,14 +25,7 @@ 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; +import java.util.*; /** * @author Stian Thorgersen @@ -53,13 +45,12 @@ public class ServerInfoAdminResource { @GET public ServerInfoRepresentation getInfo() { ServerInfoRepresentation info = new ServerInfoRepresentation(); - info.version = Version.VERSION; - info.serverTime = new Date().toString(); + info.setSystemInfo(SystemInfoRepresentation.create(session)); + info.setMemoryInfo(MemoryInfoRepresentation.create()); + setSocialProviders(info); setIdentityProviders(info); setThemes(info); - setEventListeners(info); - setProtocols(info); setClientImporters(info); setProviders(info); setProtocolMapperTypes(info); @@ -69,42 +60,55 @@ public class ServerInfoAdminResource { } private void setProviders(ServerInfoRepresentation info) { - List providers = new LinkedList<>(); + Map spis = new HashMap<>(); for (Spi spi : ServiceLoader.load(Spi.class)) { SpiInfoRepresentation spiRep = new SpiInfoRepresentation(); - spiRep.setName(spi.getName()); spiRep.setInternal(spi.isInternal()); - spiRep.setImplementations(session.listProviderIds(spi.getProviderClass())); - providers.add(spiRep); + spiRep.setSystemInfo(ServerInfoAwareProviderFactory.class.isAssignableFrom(spi.getProviderFactoryClass())); + Set providerIds = session.listProviderIds(spi.getProviderClass()); + Map providers = new HashMap<>(); + + if (providerIds != null) { + for (String name : providerIds) { + ProviderRepresentation provider = new ProviderRepresentation(); + if (spiRep.isSystemInfo()) { + provider.setOperationalInfo(((ServerInfoAwareProviderFactory) session.getKeycloakSessionFactory().getProviderFactory(spi.getProviderClass(), name)).getOperationalInfo()); + } + providers.put(name, provider); + } + } + spiRep.setProviders(providers); + + spis.put(spi.getName(), spiRep); } - info.providers = providers; + info.setProviders(spis); } private void setThemes(ServerInfoRepresentation info) { ThemeProvider themeProvider = session.getProvider(ThemeProvider.class, "extending"); - info.themes = new HashMap>(); + info.setThemes(new HashMap>()); for (Theme.Type type : Theme.Type.values()) { List themes = new LinkedList(themeProvider.nameSet(type)); Collections.sort(themes); - info.themes.put(type.toString().toLowerCase(), themes); + info.getThemes().put(type.toString().toLowerCase(), themes); } } private void setSocialProviders(ServerInfoRepresentation info) { - info.socialProviders = new LinkedList<>(); + info.setSocialProviders(new LinkedList>()); List providerFactories = session.getKeycloakSessionFactory().getProviderFactories(SocialIdentityProvider.class); - setIdentityProviders(providerFactories, info.socialProviders, "Social"); + setIdentityProviders(providerFactories, info.getSocialProviders(), "Social"); } private void setIdentityProviders(ServerInfoRepresentation info) { - info.identityProviders = new LinkedList<>(); + info.setIdentityProviders(new LinkedList>()); List providerFactories = session.getKeycloakSessionFactory().getProviderFactories(IdentityProvider.class); - setIdentityProviders(providerFactories, info.identityProviders, "User-defined"); + setIdentityProviders(providerFactories, info.getIdentityProviders(), "User-defined"); providerFactories = session.getKeycloakSessionFactory().getProviderFactories(SocialIdentityProvider.class); - setIdentityProviders(providerFactories, info.identityProviders, "Social"); + setIdentityProviders(providerFactories, info.getIdentityProviders(), "Social"); } public void setIdentityProviders(List factories, List> providers, String groupName) { @@ -119,31 +123,14 @@ public class ServerInfoAdminResource { } } - private void setEventListeners(ServerInfoRepresentation info) { - info.eventListeners = new LinkedList(); - - Set providers = session.listProviderIds(EventListenerProvider.class); - if (providers != null) { - info.eventListeners.addAll(providers); - } - } - - private void setProtocols(ServerInfoRepresentation info) { - info.protocols = new LinkedList(); - for (ProviderFactory p : session.getKeycloakSessionFactory().getProviderFactories(LoginProtocol.class)) { - info.protocols.add(p.getId()); - } - Collections.sort(info.protocols); - } - private void setProtocolMapperTypes(ServerInfoRepresentation info) { - info.protocolMapperTypes = new HashMap>(); + info.setProtocolMapperTypes(new HashMap>()); for (ProviderFactory p : session.getKeycloakSessionFactory().getProviderFactories(ProtocolMapper.class)) { ProtocolMapper mapper = (ProtocolMapper)p; - List types = info.protocolMapperTypes.get(mapper.getProtocol()); + List types = info.getProtocolMapperTypes().get(mapper.getProtocol()); if (types == null) { types = new LinkedList(); - info.protocolMapperTypes.put(mapper.getProtocol(), types); + info.getProtocolMapperTypes().put(mapper.getProtocol(), types); } ProtocolMapperTypeRepresentation rep = new ProtocolMapperTypeRepresentation(); rep.setId(mapper.getId()); @@ -166,136 +153,25 @@ public class ServerInfoAdminResource { } private void setBuiltinProtocolMappers(ServerInfoRepresentation info) { - info.builtinProtocolMappers = new HashMap<>(); + info.setBuiltinProtocolMappers(new HashMap>()); for (ProviderFactory p : session.getKeycloakSessionFactory().getProviderFactories(LoginProtocol.class)) { LoginProtocolFactory factory = (LoginProtocolFactory)p; List mappers = new LinkedList<>(); for (ProtocolMapperModel mapper : factory.getBuiltinMappers()) { mappers.add(ModelToRepresentation.toRepresentation(mapper)); } - info.builtinProtocolMappers.put(p.getId(), mappers); + info.getBuiltinProtocolMappers().put(p.getId(), mappers); } } private void setClientImporters(ServerInfoRepresentation info) { - info.clientImporters = new LinkedList>(); + info.setClientImporters(new LinkedList>()); for (ProviderFactory p : session.getKeycloakSessionFactory().getProviderFactories(ClientImporter.class)) { ClientImporterFactory factory = (ClientImporterFactory)p; Map data = new HashMap(); data.put("id", factory.getId()); data.put("name", factory.getDisplayName()); - info.clientImporters.add(data); - } - } - - public static class ServerInfoRepresentation { - - private String version; - - private String serverTime; - - private Map> themes; - - private List> socialProviders; - public List> identityProviders; - private List protocols; - private List> clientImporters; - - private List providers; - - private List eventListeners; - private Map> protocolMapperTypes; - private Map> builtinProtocolMappers; - - private Map> enums; - - public ServerInfoRepresentation() { - } - - public String getServerTime() { - return serverTime; - } - - public String getVersion() { - return version; - } - - public Map> getThemes() { - return themes; - } - - public List> getSocialProviders() { - return socialProviders; - } - - public List> getIdentityProviders() { - return this.identityProviders; - } - - public List getEventListeners() { - return eventListeners; - } - - public List getProtocols() { - return protocols; - } - - public List> getClientImporters() { - return clientImporters; - } - - public List getProviders() { - return providers; - } - - public Map> getProtocolMapperTypes() { - return protocolMapperTypes; - } - - public Map> getBuiltinProtocolMappers() { - return builtinProtocolMappers; - } - - public void setBuiltinProtocolMappers(Map> builtinProtocolMappers) { - this.builtinProtocolMappers = builtinProtocolMappers; - } - - public Map> getEnums() { - return enums; - } - - public void setEnums(Map> enums) { - this.enums = enums; - } - } - - public static class SpiInfoRepresentation { - private String name; - private boolean internal; - 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 void setImplementations(Set implementations) { - this.implementations = implementations; + info.getClientImporters().add(data); } } diff --git a/services/src/main/java/org/keycloak/services/resources/admin/info/ServerInfoRepresentation.java b/services/src/main/java/org/keycloak/services/resources/admin/info/ServerInfoRepresentation.java new file mode 100644 index 0000000000..9b7f4f5bbb --- /dev/null +++ b/services/src/main/java/org/keycloak/services/resources/admin/info/ServerInfoRepresentation.java @@ -0,0 +1,108 @@ +package org.keycloak.services.resources.admin.info; + +import org.keycloak.representations.idm.ProtocolMapperRepresentation; +import org.keycloak.representations.idm.ProtocolMapperTypeRepresentation; + +import java.util.List; +import java.util.Map; + +/** + * @author Stian Thorgersen + */ +public class ServerInfoRepresentation { + + private SystemInfoRepresentation systemInfo; + private MemoryInfoRepresentation memoryInfo; + + private Map> themes; + + private List> socialProviders; + private List> identityProviders; + private List> clientImporters; + + private Map providers; + + private Map> protocolMapperTypes; + private Map> builtinProtocolMappers; + + private Map> enums; + + public SystemInfoRepresentation getSystemInfo() { + return systemInfo; + } + + public void setSystemInfo(SystemInfoRepresentation systemInfo) { + this.systemInfo = systemInfo; + } + + public MemoryInfoRepresentation getMemoryInfo() { + return memoryInfo; + } + + public void setMemoryInfo(MemoryInfoRepresentation memoryInfo) { + this.memoryInfo = memoryInfo; + } + public Map> getThemes() { + return themes; + } + + public void setThemes(Map> themes) { + this.themes = themes; + } + + public List> getSocialProviders() { + return socialProviders; + } + + public void setSocialProviders(List> socialProviders) { + this.socialProviders = socialProviders; + } + + public List> getIdentityProviders() { + return identityProviders; + } + + public void setIdentityProviders(List> identityProviders) { + this.identityProviders = identityProviders; + } + + public List> getClientImporters() { + return clientImporters; + } + + public void setClientImporters(List> clientImporters) { + this.clientImporters = clientImporters; + } + + public Map getProviders() { + return providers; + } + + public void setProviders(Map providers) { + this.providers = providers; + } + + public Map> getProtocolMapperTypes() { + return protocolMapperTypes; + } + + public void setProtocolMapperTypes(Map> protocolMapperTypes) { + this.protocolMapperTypes = protocolMapperTypes; + } + + public Map> getBuiltinProtocolMappers() { + return builtinProtocolMappers; + } + + public void setBuiltinProtocolMappers(Map> builtinProtocolMappers) { + this.builtinProtocolMappers = builtinProtocolMappers; + } + + public Map> getEnums() { + return enums; + } + + public void setEnums(Map> enums) { + this.enums = enums; + } +} diff --git a/services/src/main/java/org/keycloak/services/resources/admin/info/SpiInfoRepresentation.java b/services/src/main/java/org/keycloak/services/resources/admin/info/SpiInfoRepresentation.java new file mode 100644 index 0000000000..3f94a5a9f3 --- /dev/null +++ b/services/src/main/java/org/keycloak/services/resources/admin/info/SpiInfoRepresentation.java @@ -0,0 +1,39 @@ +package org.keycloak.services.resources.admin.info; + +import java.util.Map; + +/** + * @author Stian Thorgersen + */ +public class SpiInfoRepresentation { + + private boolean internal; + private boolean systemInfo; + + private Map providers; + + public boolean isInternal() { + return internal; + } + + public void setInternal(boolean internal) { + this.internal = internal; + } + + public boolean isSystemInfo() { + return systemInfo; + } + + public void setSystemInfo(boolean systemInfo) { + this.systemInfo = systemInfo; + } + + public Map getProviders() { + return providers; + } + + public void setProviders(Map providers) { + this.providers = providers; + } + +} diff --git a/services/src/main/java/org/keycloak/services/resources/admin/info/SystemInfoRepresentation.java b/services/src/main/java/org/keycloak/services/resources/admin/info/SystemInfoRepresentation.java new file mode 100644 index 0000000000..bc0329a785 --- /dev/null +++ b/services/src/main/java/org/keycloak/services/resources/admin/info/SystemInfoRepresentation.java @@ -0,0 +1,217 @@ +package org.keycloak.services.resources.admin.info; + +import org.keycloak.Version; +import org.keycloak.models.KeycloakSession; + +import java.util.Date; +import java.util.Locale; + +public class SystemInfoRepresentation { + + private String version; + private String serverTime; + private String uptime; + private long uptimeMillis; + private String javaVersion; + private String javaVendor; + private String javaVm; + private String javaVmVersion; + private String javaRuntime; + private String javaHome; + private String osName; + private String osArchitecture; + private String osVersion; + private String fileEncoding; + private String userName; + private String userDir; + private String userTimezone; + private String userLocale; + + public static SystemInfoRepresentation create(KeycloakSession session) { + SystemInfoRepresentation rep = new SystemInfoRepresentation(); + rep.version = Version.VERSION; + rep.serverTime = new Date().toString(); + rep.uptimeMillis = System.currentTimeMillis() - session.getKeycloakSessionFactory().getServerStartupTimestamp(); + rep.uptime = formatUptime(rep.uptimeMillis); + rep.javaVersion = System.getProperty("java.version"); + rep.javaVendor = System.getProperty("java.vendor"); + rep.javaVm = System.getProperty("java.vm.name"); + rep.javaVmVersion = System.getProperty("java.vm.version"); + rep.javaRuntime = System.getProperty("java.runtime.name"); + rep.javaHome = System.getProperty("java.home"); + rep.osName = System.getProperty("os.name"); + rep.osArchitecture = System.getProperty("os.arch"); + rep.osVersion = System.getProperty("os.version"); + rep.fileEncoding = System.getProperty("file.encoding"); + rep.userName = System.getProperty("user.name"); + rep.userDir = System.getProperty("user.dir"); + rep.userTimezone = System.getProperty("user.timezone"); + rep.userLocale = (new Locale(System.getProperty("user.country"), System.getProperty("user.language")).toString()); + return rep; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getServerTime() { + return serverTime; + } + + public void setServerTime(String serverTime) { + this.serverTime = serverTime; + } + + public String getUptime() { + return uptime; + } + + public void setUptime(String uptime) { + this.uptime = uptime; + } + + public long getUptimeMillis() { + return uptimeMillis; + } + + public void setUptimeMillis(long uptimeMillis) { + this.uptimeMillis = uptimeMillis; + } + + public String getJavaVersion() { + return javaVersion; + } + + public void setJavaVersion(String javaVersion) { + this.javaVersion = javaVersion; + } + + public String getJavaVendor() { + return javaVendor; + } + + public void setJavaVendor(String javaVendor) { + this.javaVendor = javaVendor; + } + + public String getJavaVm() { + return javaVm; + } + + public void setJavaVm(String javaVm) { + this.javaVm = javaVm; + } + + public String getJavaVmVersion() { + return javaVmVersion; + } + + public void setJavaVmVersion(String javaVmVersion) { + this.javaVmVersion = javaVmVersion; + } + + public String getJavaRuntime() { + return javaRuntime; + } + + public void setJavaRuntime(String javaRuntime) { + this.javaRuntime = javaRuntime; + } + + public String getJavaHome() { + return javaHome; + } + + public void setJavaHome(String javaHome) { + this.javaHome = javaHome; + } + + public String getOsName() { + return osName; + } + + public void setOsName(String osName) { + this.osName = osName; + } + + public String getOsArchitecture() { + return osArchitecture; + } + + public void setOsArchitecture(String osArchitecture) { + this.osArchitecture = osArchitecture; + } + + public String getOsVersion() { + return osVersion; + } + + public void setOsVersion(String osVersion) { + this.osVersion = osVersion; + } + + public String getFileEncoding() { + return fileEncoding; + } + + public void setFileEncoding(String fileEncoding) { + this.fileEncoding = fileEncoding; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getUserDir() { + return userDir; + } + + public void setUserDir(String userDir) { + this.userDir = userDir; + } + + public String getUserTimezone() { + return userTimezone; + } + + public void setUserTimezone(String userTimezone) { + this.userTimezone = userTimezone; + } + + public String getUserLocale() { + return userLocale; + } + + public void setUserLocale(String userLocale) { + this.userLocale = userLocale; + } + + private static String formatUptime(long uptime) { + long diffInSeconds = uptime / 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" : ""); + } + +}