Skip to content

Commit bf839e5

Browse files
metcoxphilwebb
authored andcommitted
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 spring-projectsgh-2264 Closes spring-projectsgh-2265
1 parent 491a61d commit bf839e5

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/AbstractEndpoint.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public AbstractEndpoint(String id, boolean sensitive, boolean enabled) {
8484
this.enabled = enabled;
8585
}
8686

87+
protected final Environment getEnvironment() {
88+
return this.environment;
89+
}
90+
8791
@Override
8892
public void setEnvironment(Environment environment) {
8993
this.environment = environment;
@@ -104,7 +108,8 @@ public boolean isEnabled() {
104108
return this.enabled;
105109
}
106110
if (this.environment != null) {
107-
this.environment.getProperty(ENDPOINTS_ENABLED_PROPERTY, Boolean.class, true);
111+
return this.environment.getProperty(ENDPOINTS_ENABLED_PROPERTY,
112+
Boolean.class, true);
108113
}
109114
return true;
110115
}

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Map.Entry;
2222

2323
import org.springframework.boot.context.properties.ConfigurationProperties;
24-
import org.springframework.context.EnvironmentAware;
2524
import org.springframework.core.env.CompositePropertySource;
2625
import org.springframework.core.env.ConfigurableEnvironment;
2726
import org.springframework.core.env.EnumerablePropertySource;
@@ -38,10 +37,7 @@
3837
* @author Christian Dupuis
3938
*/
4039
@ConfigurationProperties(prefix = "endpoints.env", ignoreUnknownFields = false)
41-
public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> implements
42-
EnvironmentAware {
43-
44-
private Environment environment;
40+
public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> {
4541

4642
private final Sanitizer sanitizer = new Sanitizer();
4743

@@ -59,7 +55,7 @@ public void setKeysToSanitize(String... keysToSanitize) {
5955
@Override
6056
public Map<String, Object> invoke() {
6157
Map<String, Object> result = new LinkedHashMap<String, Object>();
62-
result.put("profiles", this.environment.getActiveProfiles());
58+
result.put("profiles", getEnvironment().getActiveProfiles());
6359
for (Entry<String, PropertySource<?>> entry : getPropertySources().entrySet()) {
6460
PropertySource<?> source = entry.getValue();
6561
String sourceName = entry.getKey();
@@ -78,9 +74,9 @@ public Map<String, Object> invoke() {
7874
private Map<String, PropertySource<?>> getPropertySources() {
7975
Map<String, PropertySource<?>> map = new LinkedHashMap<String, PropertySource<?>>();
8076
MutablePropertySources sources = null;
81-
if (this.environment != null
82-
&& this.environment instanceof ConfigurableEnvironment) {
83-
sources = ((ConfigurableEnvironment) this.environment).getPropertySources();
77+
Environment environment = getEnvironment();
78+
if (environment != null && environment instanceof ConfigurableEnvironment) {
79+
sources = ((ConfigurableEnvironment) environment).getPropertySources();
8480
}
8581
else {
8682
sources = new StandardEnvironment().getPropertySources();
@@ -108,9 +104,4 @@ public Object sanitize(String name, Object object) {
108104
return this.sanitizer.sanitize(name, object);
109105
}
110106

111-
@Override
112-
public void setEnvironment(Environment environment) {
113-
this.environment = environment;
114-
}
115-
116107
}

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/AbstractEndpointTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.boot.actuate.endpoint;
1818

1919
import java.util.Collections;
20+
import java.util.HashMap;
21+
import java.util.Map;
2022

2123
import org.junit.After;
2224
import org.junit.Before;
@@ -133,6 +135,30 @@ Collections.<String, Object> singletonMap(this.property + ".enabled",
133135
assertThat(getEndpointBean().isEnabled(), equalTo(true));
134136
}
135137

138+
@Test
139+
public void isAllEndpointsDisabled() throws Exception {
140+
this.context = new AnnotationConfigApplicationContext();
141+
PropertySource<?> propertySource = new MapPropertySource("test",
142+
Collections.<String, Object> singletonMap("endpoints.enabled", false));
143+
this.context.getEnvironment().getPropertySources().addFirst(propertySource);
144+
this.context.register(this.configClass);
145+
this.context.refresh();
146+
assertThat(getEndpointBean().isEnabled(), equalTo(false));
147+
}
148+
149+
@Test
150+
public void isOptIn() throws Exception {
151+
this.context = new AnnotationConfigApplicationContext();
152+
Map<String, Object> source = new HashMap<String, Object>();
153+
source.put("endpoints.enabled", false);
154+
source.put(this.property + ".enabled", true);
155+
PropertySource<?> propertySource = new MapPropertySource("test", source);
156+
this.context.getEnvironment().getPropertySources().addFirst(propertySource);
157+
this.context.register(this.configClass);
158+
this.context.refresh();
159+
assertThat(getEndpointBean().isEnabled(), equalTo(true));
160+
}
161+
136162
@SuppressWarnings("unchecked")
137163
protected T getEndpointBean() {
138164
return (T) this.context.getBean(this.type);

0 commit comments

Comments
 (0)