Skip to content

Commit d7f3008

Browse files
committed
Move configuration of EnvironmentEndpoint
See spring-projectsgh-10263
1 parent bb62229 commit d7f3008

File tree

6 files changed

+102
-28
lines changed

6 files changed

+102
-28
lines changed

spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/env/EnvironmentEndpointAutoConfiguration.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.boot.actuate.env.EnvironmentEndpoint;
2121
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
23+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2324
import org.springframework.context.annotation.Bean;
2425
import org.springframework.context.annotation.Configuration;
2526
import org.springframework.core.env.Environment;
@@ -28,16 +29,30 @@
2829
* {@link EnableAutoConfiguration Auto-configuration} for the {@link EnvironmentEndpoint}.
2930
*
3031
* @author Phillip Webb
32+
* @author Stephane Nicoll
3133
* @since 2.0.0
3234
*/
3335
@Configuration
36+
@EnableConfigurationProperties(EnvironmentEndpointProperties.class)
3437
public class EnvironmentEndpointAutoConfiguration {
3538

39+
private final EnvironmentEndpointProperties properties;
40+
41+
public EnvironmentEndpointAutoConfiguration(
42+
EnvironmentEndpointProperties properties) {
43+
this.properties = properties;
44+
}
45+
3646
@Bean
3747
@ConditionalOnMissingBean
3848
@ConditionalOnEnabledEndpoint
3949
public EnvironmentEndpoint environmentEndpoint(Environment environment) {
40-
return new EnvironmentEndpoint(environment);
50+
EnvironmentEndpoint endpoint = new EnvironmentEndpoint(environment);
51+
String[] keysToSanitize = this.properties.getKeysToSanitize();
52+
if (keysToSanitize != null) {
53+
endpoint.setKeysToSanitize(keysToSanitize);
54+
}
55+
return endpoint;
4156
}
4257

4358
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.env;
18+
19+
import org.springframework.boot.actuate.env.EnvironmentEndpoint;
20+
import org.springframework.boot.context.properties.ConfigurationProperties;
21+
22+
/**
23+
* Configuration properties for {@link EnvironmentEndpoint}.
24+
*
25+
* @author Stephane Nicoll
26+
* @since 2.0.0
27+
*/
28+
@ConfigurationProperties("endpoints.env")
29+
public class EnvironmentEndpointProperties {
30+
31+
/**
32+
* Keys that should be sanitized. Keys can be simple strings that the property ends
33+
* with or regex expressions.
34+
*/
35+
private String[] keysToSanitize;
36+
37+
public String[] getKeysToSanitize() {
38+
return this.keysToSanitize;
39+
}
40+
41+
public void setKeysToSanitize(String[] keysToSanitize) {
42+
this.keysToSanitize = keysToSanitize;
43+
}
44+
45+
}

spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
},
3131
{
3232
"name": "endpoints.env.keys-to-sanitize",
33-
"type": "java.lang.String[]",
34-
"sourceType": "org.springframework.boot.actuate.env.EnvironmentEndpoint",
35-
"description": "Keys that should be sanitized. Keys can be simple strings that the property ends with or regex expressions.",
3633
"defaultValue": [
3734
"password",
3835
"secret",

spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/env/EnvironmentEndpointAutoConfigurationTests.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.env;
1818

19+
import java.util.Map;
20+
1921
import org.junit.Test;
2022

2123
import org.springframework.boot.actuate.env.EnvironmentEndpoint;
24+
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor;
25+
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor.PropertySourceDescriptor;
26+
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor.PropertySourceDescriptor.PropertyValueDescriptor;
2227
import org.springframework.boot.autoconfigure.AutoConfigurations;
28+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
2329
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
30+
import org.springframework.boot.test.context.runner.ContextConsumer;
2431

2532
import static org.assertj.core.api.Assertions.assertThat;
2633

@@ -37,8 +44,8 @@ public class EnvironmentEndpointAutoConfigurationTests {
3744

3845
@Test
3946
public void runShouldHaveEndpointBean() {
40-
this.contextRunner.run((context) -> assertThat(context)
41-
.hasSingleBean(EnvironmentEndpoint.class));
47+
this.contextRunner.withSystemProperties("dbPassword=123456", "apiKey=123456")
48+
.run(validateSystemProperties("******", "******"));
4249
}
4350

4451
@Test
@@ -49,4 +56,33 @@ public void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointBean()
4956
.doesNotHaveBean(EnvironmentEndpoint.class));
5057
}
5158

59+
60+
@Test
61+
public void keysToSanitizeCanBeConfiguredViaTheEnvironment() throws Exception {
62+
this.contextRunner.withSystemProperties("dbPassword=123456", "apiKey=123456")
63+
.withPropertyValues("endpoints.env.keys-to-sanitize=.*pass.*")
64+
.run(validateSystemProperties("******", "123456"));
65+
}
66+
67+
private ContextConsumer<AssertableApplicationContext> validateSystemProperties(
68+
String dbPassword, String apiKey) {
69+
return context -> {
70+
assertThat(context).hasSingleBean(EnvironmentEndpoint.class);
71+
EnvironmentEndpoint endpoint = context.getBean(EnvironmentEndpoint.class);
72+
EnvironmentDescriptor env = endpoint.environment(null);
73+
Map<String, PropertyValueDescriptor> systemProperties = getSource(
74+
"systemProperties", env).getProperties();
75+
assertThat(systemProperties.get("dbPassword").getValue())
76+
.isEqualTo(dbPassword);
77+
assertThat(systemProperties.get("apiKey").getValue()).isEqualTo(apiKey);
78+
};
79+
}
80+
81+
private PropertySourceDescriptor getSource(String name,
82+
EnvironmentDescriptor descriptor) {
83+
return descriptor.getPropertySources().stream()
84+
.filter((source) -> name.equals(source.getName())).findFirst().get();
85+
}
86+
87+
5288
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.springframework.boot.actuate.endpoint.annotation.Selector;
3232
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor.PropertySourceDescriptor;
3333
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor.PropertySourceDescriptor.PropertyValueDescriptor;
34-
import org.springframework.boot.context.properties.ConfigurationProperties;
3534
import org.springframework.boot.origin.OriginLookup;
3635
import org.springframework.core.env.CompositePropertySource;
3736
import org.springframework.core.env.ConfigurableEnvironment;
@@ -57,7 +56,6 @@
5756
* @since 2.0.0
5857
*/
5958
@Endpoint(id = "env")
60-
@ConfigurationProperties("endpoints.env")
6159
public class EnvironmentEndpoint {
6260

6361
private final Sanitizer sanitizer = new Sanitizer();
@@ -192,7 +190,7 @@ protected String getPropertyAsRawString(String key) {
192190
/**
193191
* A description of an {@link Environment}.
194192
*/
195-
static final class EnvironmentDescriptor {
193+
public static final class EnvironmentDescriptor {
196194

197195
private final List<String> activeProfiles;
198196

@@ -215,7 +213,7 @@ public List<PropertySourceDescriptor> getPropertySources() {
215213
/**
216214
* A description of a {@link PropertySource}.
217215
*/
218-
static final class PropertySourceDescriptor {
216+
public static final class PropertySourceDescriptor {
219217

220218
private final String name;
221219

@@ -238,7 +236,7 @@ public Map<String, PropertyValueDescriptor> getProperties() {
238236
/**
239237
* A description of a property's value, including its origin if available.
240238
*/
241-
static final class PropertyValueDescriptor {
239+
public static final class PropertyValueDescriptor {
242240

243241
private final Object value;
244242

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/env/EnvironmentEndpointTests.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor.PropertySourceDescriptor;
2828
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor.PropertySourceDescriptor.PropertyValueDescriptor;
2929
import org.springframework.boot.context.properties.EnableConfigurationProperties;
30-
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3130
import org.springframework.boot.test.util.TestPropertyValues;
3231
import org.springframework.context.annotation.Bean;
3332
import org.springframework.context.annotation.Configuration;
@@ -152,22 +151,6 @@ public void sensitiveKeysMatchingCustomPatternHaveTheirValuesSanitized() {
152151
clearSystemProperties("dbPassword", "apiKey");
153152
}
154153

155-
@Test
156-
public void keysToSanitizeCanBeConfiguredViaTheEnvironment() throws Exception {
157-
ApplicationContextRunner tester = new ApplicationContextRunner()
158-
.withSystemProperties("dbPassword=123456", "apiKey=123456")
159-
.withPropertyValues("endpoints.env.keys-to-sanitize=.*pass.*")
160-
.withUserConfiguration(Config.class);
161-
tester.run((context) -> {
162-
EnvironmentEndpoint endpoint = context.getBean(EnvironmentEndpoint.class);
163-
EnvironmentDescriptor env = endpoint.environment(null);
164-
Map<String, PropertyValueDescriptor> systemProperties = getSource(
165-
"systemProperties", env).getProperties();
166-
assertThat(systemProperties.get("dbPassword").getValue()).isEqualTo("******");
167-
assertThat(systemProperties.get("apiKey").getValue()).isEqualTo("123456");
168-
});
169-
}
170-
171154
@Test
172155
public void propertyWithPlaceholderResolved() {
173156
StandardEnvironment environment = new StandardEnvironment();

0 commit comments

Comments
 (0)