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
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<T> implements Endpoint<T>, 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;
}
......
......@@ -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<Map<String, Object>> implements
EnvironmentAware {
private Environment environment;
public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> {
private final Sanitizer sanitizer = new Sanitizer();
......@@ -59,7 +55,7 @@ public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> i
@Override
public Map<String, Object> invoke() {
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()) {
PropertySource<?> source = entry.getValue();
String sourceName = entry.getKey();
......@@ -78,9 +74,9 @@ public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> i
private Map<String, PropertySource<?>> getPropertySources() {
Map<String, PropertySource<?>> map = new LinkedHashMap<String, PropertySource<?>>();
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<Map<String, Object>> i
return this.sanitizer.sanitize(name, object);
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}
......@@ -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<T extends Endpoint<?>> {
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")
protected T getEndpointBean() {
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