Commit bf839e57 authored by Mathieu Bernatet's avatar Mathieu Bernatet Committed by Phillip Webb

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
parent 491a61d5
...@@ -84,6 +84,10 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAwa ...@@ -84,6 +84,10 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAwa
this.enabled = enabled; this.enabled = enabled;
} }
protected final Environment getEnvironment() {
return this.environment;
}
@Override @Override
public void setEnvironment(Environment environment) { public void setEnvironment(Environment environment) {
this.environment = environment; this.environment = environment;
...@@ -104,7 +108,8 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAwa ...@@ -104,7 +108,8 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAwa
return this.enabled; return this.enabled;
} }
if (this.environment != null) { 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; return true;
} }
......
...@@ -21,7 +21,6 @@ import java.util.Map; ...@@ -21,7 +21,6 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.EnumerablePropertySource;
...@@ -38,10 +37,7 @@ import org.springframework.core.env.StandardEnvironment; ...@@ -38,10 +37,7 @@ import org.springframework.core.env.StandardEnvironment;
* @author Christian Dupuis * @author Christian Dupuis
*/ */
@ConfigurationProperties(prefix = "endpoints.env", ignoreUnknownFields = false) @ConfigurationProperties(prefix = "endpoints.env", ignoreUnknownFields = false)
public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> implements public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> {
EnvironmentAware {
private Environment environment;
private final Sanitizer sanitizer = new Sanitizer(); private final Sanitizer sanitizer = new Sanitizer();
...@@ -59,7 +55,7 @@ public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> i ...@@ -59,7 +55,7 @@ public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> i
@Override @Override
public Map<String, Object> invoke() { public Map<String, Object> invoke() {
Map<String, Object> result = new LinkedHashMap<String, Object>(); Map<String, Object> result = new LinkedHashMap<String, Object>();
result.put("profiles", this.environment.getActiveProfiles()); result.put("profiles", getEnvironment().getActiveProfiles());
for (Entry<String, PropertySource<?>> entry : getPropertySources().entrySet()) { for (Entry<String, PropertySource<?>> entry : getPropertySources().entrySet()) {
PropertySource<?> source = entry.getValue(); PropertySource<?> source = entry.getValue();
String sourceName = entry.getKey(); String sourceName = entry.getKey();
...@@ -78,9 +74,9 @@ public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> i ...@@ -78,9 +74,9 @@ public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> i
private Map<String, PropertySource<?>> getPropertySources() { private Map<String, PropertySource<?>> getPropertySources() {
Map<String, PropertySource<?>> map = new LinkedHashMap<String, PropertySource<?>>(); Map<String, PropertySource<?>> map = new LinkedHashMap<String, PropertySource<?>>();
MutablePropertySources sources = null; MutablePropertySources sources = null;
if (this.environment != null Environment environment = getEnvironment();
&& this.environment instanceof ConfigurableEnvironment) { if (environment != null && environment instanceof ConfigurableEnvironment) {
sources = ((ConfigurableEnvironment) this.environment).getPropertySources(); sources = ((ConfigurableEnvironment) environment).getPropertySources();
} }
else { else {
sources = new StandardEnvironment().getPropertySources(); sources = new StandardEnvironment().getPropertySources();
...@@ -108,9 +104,4 @@ public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> i ...@@ -108,9 +104,4 @@ public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> i
return this.sanitizer.sanitize(name, object); return this.sanitizer.sanitize(name, object);
} }
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
} }
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
package org.springframework.boot.actuate.endpoint; package org.springframework.boot.actuate.endpoint;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
...@@ -133,6 +135,30 @@ public abstract class AbstractEndpointTests<T extends Endpoint<?>> { ...@@ -133,6 +135,30 @@ public abstract class AbstractEndpointTests<T extends Endpoint<?>> {
assertThat(getEndpointBean().isEnabled(), equalTo(true)); assertThat(getEndpointBean().isEnabled(), equalTo(true));
} }
@Test
public void isAllEndpointsDisabled() throws Exception {
this.context = new AnnotationConfigApplicationContext();
PropertySource<?> propertySource = new MapPropertySource("test",
Collections.<String, Object> 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<String, Object> source = new HashMap<String, Object>();
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") @SuppressWarnings("unchecked")
protected T getEndpointBean() { protected T getEndpointBean() {
return (T) this.context.getBean(this.type); return (T) this.context.getBean(this.type);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment