Mask sensitive placeholders in env endpoint
Closes gh-8282
This commit is contained in:
@@ -39,6 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Christian Dupuis
|
||||
* @author Nicolas Lejeune
|
||||
* @author Stephane Nicoll
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentEndpoint> {
|
||||
|
||||
@@ -194,6 +195,66 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
|
||||
assertThat(systemProperties.get("apiKey")).isEqualTo("******");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void propertyWithPlaceholderResolved() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"my.foo: ${bar.blah}", "bar.blah: hello");
|
||||
this.context.register(Config.class);
|
||||
this.context.refresh();
|
||||
EnvironmentEndpoint report = getEndpointBean();
|
||||
Map<String, Object> env = report.invoke();
|
||||
Map<String, Object> testProperties = (Map<String, Object>) env
|
||||
.get("test");
|
||||
assertThat(testProperties.get("my.foo")).isEqualTo("hello");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void propertyWithPlaceholderNotResolved() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"my.foo: ${bar.blah}");
|
||||
this.context.register(Config.class);
|
||||
this.context.refresh();
|
||||
EnvironmentEndpoint report = getEndpointBean();
|
||||
Map<String, Object> env = report.invoke();
|
||||
Map<String, Object> testProperties = (Map<String, Object>) env
|
||||
.get("test");
|
||||
assertThat(testProperties.get("my.foo")).isEqualTo("${bar.blah}");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void propertyWithSensitivePlaceholderResolved() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"my.foo: http://${bar.password}://hello", "bar.password: hello");
|
||||
this.context.register(Config.class);
|
||||
this.context.refresh();
|
||||
EnvironmentEndpoint report = getEndpointBean();
|
||||
Map<String, Object> env = report.invoke();
|
||||
Map<String, Object> testProperties = (Map<String, Object>) env
|
||||
.get("test");
|
||||
assertThat(testProperties.get("my.foo")).isEqualTo("http://******://hello");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void propertyWithSensitivePlaceholderNotResolved() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"my.foo: http://${bar.password}://hello");
|
||||
this.context.register(Config.class);
|
||||
this.context.refresh();
|
||||
EnvironmentEndpoint report = getEndpointBean();
|
||||
Map<String, Object> env = report.invoke();
|
||||
Map<String, Object> testProperties = (Map<String, Object>) env
|
||||
.get("test");
|
||||
assertThat(testProperties.get("my.foo")).isEqualTo("http://${bar.password}://hello");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
public static class Config {
|
||||
|
||||
@@ -148,6 +148,17 @@ public class EnvironmentMvcEndpointTests {
|
||||
.andExpect(content().string(containsString("\"my.foo\":\"${my.bar}\"")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedPathWithSensitivePlaceholderShouldSanitize() throws Exception {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("my.foo", "${my.password}");
|
||||
map.put("my.password", "hello");
|
||||
((ConfigurableEnvironment) this.context.getEnvironment()).getPropertySources()
|
||||
.addFirst(new MapPropertySource("placeholder", map));
|
||||
this.mvc.perform(get("/env/my.*")).andExpect(status().isOk())
|
||||
.andExpect(content().string(containsString("\"my.foo\":\"******\"")));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import({ JacksonAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class, WebMvcAutoConfiguration.class,
|
||||
|
||||
Reference in New Issue
Block a user