From bf839e57a56f90f552a838568ea02b706b572dce Mon Sep 17 00:00:00 2001 From: Mathieu Bernatet Date: Wed, 31 Dec 2014 17:15:16 +0100 Subject: [PATCH] Fix global `endpoints.enabled` property support Update AbstractEndpoint to correctly support the `endpoints.enabled` property. Also fix EnvironmentEnpoint which would previously prevent the Environment from being set. Fixes gh-2264 Closes gh-2265 --- .../actuate/endpoint/AbstractEndpoint.java | 7 ++++- .../actuate/endpoint/EnvironmentEndpoint.java | 19 ++++---------- .../endpoint/AbstractEndpointTests.java | 26 +++++++++++++++++++ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/AbstractEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/AbstractEndpoint.java index 55317fd9266..0cdf6ecff55 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/AbstractEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/AbstractEndpoint.java @@ -84,6 +84,10 @@ public abstract class AbstractEndpoint implements Endpoint, EnvironmentAwa this.enabled = enabled; } + protected final Environment getEnvironment() { + return this.environment; + } + @Override public void setEnvironment(Environment environment) { this.environment = environment; @@ -104,7 +108,8 @@ public abstract class AbstractEndpoint implements Endpoint, EnvironmentAwa return this.enabled; } if (this.environment != null) { - this.environment.getProperty(ENDPOINTS_ENABLED_PROPERTY, Boolean.class, true); + return this.environment.getProperty(ENDPOINTS_ENABLED_PROPERTY, + Boolean.class, true); } return true; } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java index e0c922bce5d..ed45809a77d 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java @@ -21,7 +21,6 @@ import java.util.Map; import java.util.Map.Entry; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.EnvironmentAware; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.EnumerablePropertySource; @@ -38,10 +37,7 @@ import org.springframework.core.env.StandardEnvironment; * @author Christian Dupuis */ @ConfigurationProperties(prefix = "endpoints.env", ignoreUnknownFields = false) -public class EnvironmentEndpoint extends AbstractEndpoint> implements - EnvironmentAware { - - private Environment environment; +public class EnvironmentEndpoint extends AbstractEndpoint> { private final Sanitizer sanitizer = new Sanitizer(); @@ -59,7 +55,7 @@ public class EnvironmentEndpoint extends AbstractEndpoint> i @Override public Map invoke() { Map result = new LinkedHashMap(); - result.put("profiles", this.environment.getActiveProfiles()); + result.put("profiles", getEnvironment().getActiveProfiles()); for (Entry> entry : getPropertySources().entrySet()) { PropertySource source = entry.getValue(); String sourceName = entry.getKey(); @@ -78,9 +74,9 @@ public class EnvironmentEndpoint extends AbstractEndpoint> i private Map> getPropertySources() { Map> map = new LinkedHashMap>(); MutablePropertySources sources = null; - if (this.environment != null - && this.environment instanceof ConfigurableEnvironment) { - sources = ((ConfigurableEnvironment) this.environment).getPropertySources(); + Environment environment = getEnvironment(); + if (environment != null && environment instanceof ConfigurableEnvironment) { + sources = ((ConfigurableEnvironment) environment).getPropertySources(); } else { sources = new StandardEnvironment().getPropertySources(); @@ -108,9 +104,4 @@ public class EnvironmentEndpoint extends AbstractEndpoint> i return this.sanitizer.sanitize(name, object); } - @Override - public void setEnvironment(Environment environment) { - this.environment = environment; - } - } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/AbstractEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/AbstractEndpointTests.java index c3263f779ba..d90b3b83ce4 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/AbstractEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/AbstractEndpointTests.java @@ -17,6 +17,8 @@ package org.springframework.boot.actuate.endpoint; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import org.junit.After; import org.junit.Before; @@ -133,6 +135,30 @@ public abstract class AbstractEndpointTests> { assertThat(getEndpointBean().isEnabled(), equalTo(true)); } + @Test + public void isAllEndpointsDisabled() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + PropertySource propertySource = new MapPropertySource("test", + Collections. singletonMap("endpoints.enabled", false)); + this.context.getEnvironment().getPropertySources().addFirst(propertySource); + this.context.register(this.configClass); + this.context.refresh(); + assertThat(getEndpointBean().isEnabled(), equalTo(false)); + } + + @Test + public void isOptIn() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + Map source = new HashMap(); + source.put("endpoints.enabled", false); + source.put(this.property + ".enabled", true); + PropertySource propertySource = new MapPropertySource("test", source); + this.context.getEnvironment().getPropertySources().addFirst(propertySource); + this.context.register(this.configClass); + this.context.refresh(); + assertThat(getEndpointBean().isEnabled(), equalTo(true)); + } + @SuppressWarnings("unchecked") protected T getEndpointBean() { return (T) this.context.getBean(this.type);