Skip to content

Commit

Permalink
streamline banner settings and include them in info endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
SavvasMisaghMoayyed committed Dec 4, 2017
1 parent 7838806 commit a4bbf02
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 121 deletions.
1 change: 1 addition & 0 deletions core/cas-server-core-util-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ dependencies {
implementation libraries.bouncycastle
implementation libraries.groovy
implementation libraries.semver
implementation libraries.oshi
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
Expand Down Expand Up @@ -403,4 +404,18 @@ public static String decryptJwtValue(final Key secretKeyEncryptionKey,
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* Is jce installed ?
*
* @return the boolean
*/
public static boolean isJceInstalled() {
try {
final int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
return maxKeyLen == Integer.MAX_VALUE;
} catch (final Exception e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.apereo.cas.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.vdurmont.semver4j.Semver;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringBootVersion;
import org.springframework.core.SpringVersion;

import java.net.URL;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
* This is {@link SystemUtils}.
*
* @author Misagh Moayyed
* @since 5.3.0
*/
public final class SystemUtils {
private static final ObjectMapper MAPPER = new ObjectMapper().findAndRegisterModules();

private static final String UPDATE_CHECK_MAVEN_URL = "https://search.maven.org/solrsearch/select?q=g:%22org.apereo.cas%22%20AND%20a:%22cas-server%22";

private SystemUtils() {
}

/**
* Gets system info.
*
* @return the system info
*/
public static Map<String, Object> getSystemInfo() {
final Properties properties = System.getProperties();

final Map<String, Object> info = new LinkedHashMap<>();
info.put("CAS Version", StringUtils.defaultString(CasVersion.getVersion(), "Not Available"));
info.put("CAS Commit Id", StringUtils.defaultString(CasVersion.getSpecificationVersion(), "Not Available"));
info.put("CAS Build Date/Time", CasVersion.getDateTime());
info.put("Spring Boot Version", SpringBootVersion.getVersion());
info.put("Spring Version", SpringVersion.getVersion());

info.put("Java Home", properties.get("java.home"));
info.put("Java Vendor", properties.get("java.vendor"));
info.put("Java Version", properties.get("java.version"));

final Runtime runtime = Runtime.getRuntime();
info.put("JVM Free Memory", FileUtils.byteCountToDisplaySize(runtime.freeMemory()));
info.put("JVM Maximum Memory", FileUtils.byteCountToDisplaySize(runtime.maxMemory()));
info.put("JVM Total Memory", FileUtils.byteCountToDisplaySize(runtime.totalMemory()));

info.put("JCE Installed", StringUtils.capitalize(BooleanUtils.toStringYesNo(EncodingUtils.isJceInstalled())));
info.put("OS Architecture", properties.get("os.arch"));
info.put("OS Name", properties.get("os.name"));
info.put("OS Version", properties.get("os.version"));
info.put("OS Date/Time", LocalDateTime.now());
info.put("OS Temp Directory", FileUtils.getTempDirectoryPath());

injectUpdateInfoIntoBannerIfNeeded(info);

return info;
}

private static void injectUpdateInfoIntoBannerIfNeeded(final Map<String, Object> info) {
try {
final Properties properties = System.getProperties();
if (!properties.containsKey("CAS_UPDATE_CHECK_ENABLED")) {
return;
}

final URL url = new URL(UPDATE_CHECK_MAVEN_URL);
final Map results = MAPPER.readValue(url, Map.class);
if (!results.containsKey("response")) {
return;
}
final Map response = (Map) results.get("response");
if (!response.containsKey("numFound") && (int) response.get("numFound") != 1) {
return;
}

final List docs = (List) response.get("docs");
if (docs.isEmpty()) {
return;
}

final Map entry = (Map) docs.get(0);
final String latestVersion = (String) entry.get("latestVersion");
if (StringUtils.isNotBlank(latestVersion)) {
final String currentVersion = CasVersion.getVersion();
final Semver latestSem = new Semver(latestVersion);
final Semver currentSem = new Semver(currentVersion);

if (currentSem.isLowerThan(latestSem)) {
final String updateString = String.format("[Latest Version: %s / Stable: %s]", latestVersion,
StringUtils.capitalize(BooleanUtils.toStringYesNo(latestSem.isStable())));
info.put("Update Availability", updateString);
}
}

} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
}

}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
package org.apereo.cas.util.spring.boot;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.vdurmont.semver4j.Semver;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apereo.cas.util.AsciiArtUtils;
import org.apereo.cas.util.CasVersion;
import org.apereo.cas.util.SystemUtils;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringBootVersion;
import org.springframework.core.SpringVersion;
import org.springframework.core.env.Environment;

import javax.crypto.Cipher;
import java.io.PrintStream;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Formatter;
import java.util.List;
import java.util.Map;
import java.util.Properties;

Expand All @@ -29,18 +20,12 @@
* @since 5.0.0
*/
public abstract class AbstractCasBanner implements Banner {
/**
* Line separator length.
*/
private static final int SEPARATOR_REPEAT_COUNT = 60;
private static final String UPDATE_CHECK_MAVEN_URL = "https://search.maven.org/solrsearch/select?q=g:%22org.apereo.cas%22%20AND%20a:%22cas-server%22";

private static final int SEPARATOR_REPEAT_COUNT = 60;
/**
* A line separator.
* Line separator string.
*/
public static final String LINE_SEPARATOR = String.join(StringUtils.EMPTY, Collections.nCopies(SEPARATOR_REPEAT_COUNT, "-"));

private static final ObjectMapper MAPPER = new ObjectMapper().findAndRegisterModules();
protected static final String LINE_SEPARATOR = String.join(StringUtils.EMPTY, Collections.nCopies(SEPARATOR_REPEAT_COUNT, "-"));

@Override
public void printBanner(final Environment environment, final Class<?> sourceClass, final PrintStream out) {
Expand Down Expand Up @@ -70,80 +55,15 @@ private String collectEnvironmentInfo(final Environment environment, final Class
}

try (Formatter formatter = new Formatter()) {
formatter.format("CAS Version: %s%n", StringUtils.defaultString(CasVersion.getVersion(), "Not Available"));
formatter.format("CAS Commit Id: %s%n", StringUtils.defaultString(CasVersion.getSpecificationVersion(), "Not Available"));
formatter.format("CAS Build Date/Time: %s%n", CasVersion.getDateTime());
formatter.format("Spring Boot Version: %s%n", SpringBootVersion.getVersion());
formatter.format("Spring Version: %s%n", SpringVersion.getVersion());
formatter.format("%s%n", LINE_SEPARATOR);

formatter.format("Java Home: %s%n", properties.get("java.home"));
formatter.format("Java Vendor: %s%n", properties.get("java.vendor"));
formatter.format("Java Version: %s%n", properties.get("java.version"));
final Runtime runtime = Runtime.getRuntime();
formatter.format("JVM Free Memory: %s%n", FileUtils.byteCountToDisplaySize(runtime.freeMemory()));
formatter.format("JVM Maximum Memory: %s%n", FileUtils.byteCountToDisplaySize(runtime.maxMemory()));
formatter.format("JVM Total Memory: %s%n", FileUtils.byteCountToDisplaySize(runtime.totalMemory()));
formatter.format("JCE Installed: %s%n", StringUtils.capitalize(BooleanUtils.toStringYesNo(isJceInstalled())));
final Map<String, Object> sysInfo = SystemUtils.getSystemInfo();
sysInfo.forEach((k, v) -> formatter.format("%s: %s%n", k, v));
formatter.format("%s%n", LINE_SEPARATOR);

formatter.format("OS Architecture: %s%n", properties.get("os.arch"));
formatter.format("OS Name: %s%n", properties.get("os.name"));
formatter.format("OS Version: %s%n", properties.get("os.version"));
formatter.format("OS Date/Time: %s%n", LocalDateTime.now());
formatter.format("OS Temp Directory: %s%n", FileUtils.getTempDirectoryPath());

formatter.format("%s%n", LINE_SEPARATOR);

injectUpdateInfoIntoBannerIfNeeded(formatter);

injectEnvironmentInfoIntoBanner(formatter, environment, sourceClass);

return formatter.toString();
}
}

private static void injectUpdateInfoIntoBannerIfNeeded(final Formatter formatter) {
try {
final Properties properties = System.getProperties();
if (!properties.containsKey("CAS_UPDATE_CHECK_ENABLED")) {
return;
}

final URL url = new URL(UPDATE_CHECK_MAVEN_URL);
final Map results = MAPPER.readValue(url, Map.class);
if (!results.containsKey("response")) {
return;
}
final Map response = (Map) results.get("response");
if (!response.containsKey("numFound") && (int) response.get("numFound") != 1) {
return;
}

final List docs = (List) response.get("docs");
if (docs.isEmpty()) {
return;
}

final Map entry = (Map) docs.get(0);
final String latestVersion = (String) entry.get("latestVersion");
if (StringUtils.isNotBlank(latestVersion)) {
final String currentVersion = CasVersion.getVersion();
final Semver latestSem = new Semver(latestVersion);
final Semver currentSem = new Semver(currentVersion);
formatter.format("Update Available: %s [Latest Version: %s / Stable: %s]%n",
StringUtils.capitalize(BooleanUtils.toStringYesNo(currentSem.isLowerThan(latestSem))),
latestVersion,
StringUtils.capitalize(BooleanUtils.toStringYesNo(latestSem.isStable())));
formatter.format("%s%n", LINE_SEPARATOR);
}

} catch (final Exception e) {
throw new RuntimeException(e.getMessage(), e);
}

}

/**
* Inject environment info into banner.
*
Expand All @@ -155,13 +75,4 @@ protected void injectEnvironmentInfoIntoBanner(final Formatter formatter,
final Environment environment,
final Class<?> sourceClass) {
}

private static boolean isJceInstalled() {
try {
final int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
return maxKeyLen == Integer.MAX_VALUE;
} catch (final Exception e) {
return false;
}
}
}
1 change: 1 addition & 0 deletions core/cas-server-core-web-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ dependencies {

implementation libraries.pac4j
implementation libraries.thymeleaf
implementation libraries.oshi
}
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ javassistVersion=3.22.0-GA
javaxJstlVersion=1.2

semverVersion=2.1.0
oshiVersion=3.4.4

swaggerVersion=1.5.16
swaggerSpringFoxVersion=2.7.0
Expand Down
9 changes: 8 additions & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ ext.libraries = [
],
semver : [
dependencies.create("com.vdurmont:semver4j:$semverVersion") {
exclude(group: "org.slf4j", module: "slf4j-api")
force = true
}
],
oshi : [
dependencies.create("com.github.oshi:oshi-core:$oshiVersion") {
exclude(group: "org.slf4j", module: "slf4j-api")
force = true
}
],
Expand Down Expand Up @@ -2181,7 +2188,7 @@ ext.libraries = [
}
],
springcloudeureka : [
dependencies.create("org.springframework.cloud:spring-cloud-starter-eureka:$springCloudEurekaVersion") {
dependencies.create("org.springframework.cloud:spring-cloud-starter-netflic-eureka:$springCloudEurekaVersion") {
exclude(group: "org.slf4j", module: "slf4j-api")
exclude(group: "com.google.code.findbugs", module: "jsr305")
exclude(group: "javax.servlet", module: "servlet-api")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.apereo.cas.web.report;

import org.apereo.cas.util.SystemUtils;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;

/**
* This is {@link CasInfoEndpointContributor}.
*
* @author Misagh Moayyed
* @since 5.3.0
*/
public class CasInfoEndpointContributor implements InfoContributor {
@Override
public void contribute(final Info.Builder builder) {
builder.withDetails(SystemUtils.getSystemInfo());
}
}
Loading

0 comments on commit a4bbf02

Please sign in to comment.