Skip to content

Commit 5d25588

Browse files
committed
Make properties to sanitize configurable in EnvironmentEndpoint.
Add "key" to default keys that will be sanitized. fixes #1027
1 parent 1a32a6a commit 5d25588

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class ConfigurationPropertiesReportEndpoint extends
6666

6767
private static final String CGLIB_FILTER_ID = "cglibFilter";
6868

69-
private String[] keysToSanitize = new String[] { "password", "secret" };
69+
private String[] keysToSanitize = new String[] { "password", "secret", "key" };
7070

7171
private ApplicationContext context;
7272

@@ -76,10 +76,6 @@ public ConfigurationPropertiesReportEndpoint() {
7676
super("configprops");
7777
}
7878

79-
public String[] getKeysToSanitize() {
80-
return this.keysToSanitize;
81-
}
82-
8379
@Override
8480
public void setApplicationContext(ApplicationContext context) throws BeansException {
8581
this.context = context;

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,35 @@
2626
import org.springframework.core.env.Environment;
2727
import org.springframework.core.env.PropertySource;
2828
import org.springframework.core.env.StandardEnvironment;
29+
import org.springframework.util.Assert;
2930

3031
/**
3132
* {@link Endpoint} to expose {@link ConfigurableEnvironment environment} information.
3233
*
3334
* @author Dave Syer
3435
* @author Phillip Webb
36+
* @author Christian Dupuis
3537
*/
3638
@ConfigurationProperties(prefix = "endpoints.env", ignoreUnknownFields = false)
3739
public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> implements
3840
EnvironmentAware {
3941

4042
private Environment environment;
4143

44+
private String[] keysToSanitize = new String[] { "password", "secret", "key" };
45+
4246
/**
4347
* Create a new {@link EnvironmentEndpoint} instance.
4448
*/
4549
public EnvironmentEndpoint() {
4650
super("env");
4751
}
4852

53+
public void setKeysToSanitize(String... keysToSanitize) {
54+
Assert.notNull(keysToSanitize, "KeysToSanitize must not be null");
55+
this.keysToSanitize = keysToSanitize;
56+
}
57+
4958
@Override
5059
public Map<String, Object> invoke() {
5160
Map<String, Object> result = new LinkedHashMap<String, Object>();
@@ -71,10 +80,11 @@ private Iterable<PropertySource<?>> getPropertySources() {
7180
return new StandardEnvironment().getPropertySources();
7281
}
7382

74-
public static Object sanitize(String name, Object object) {
75-
if (name.toLowerCase().endsWith("password")
76-
|| name.toLowerCase().endsWith("secret")) {
77-
return object == null ? null : "******";
83+
public Object sanitize(String name, Object object) {
84+
for (String keyToSanitize : this.keysToSanitize) {
85+
if (name.toLowerCase().endsWith(keyToSanitize)) {
86+
return (object == null ? null : "******");
87+
}
7888
}
7989
return object;
8090
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* Adapter to expose {@link EnvironmentEndpoint} as an {@link MvcEndpoint}.
3131
*
3232
* @author Dave Syer
33+
* @author Christian Dupuis
3334
*/
3435
public class EnvironmentMvcEndpoint extends EndpointMvcAdapter implements
3536
EnvironmentAware {
@@ -47,7 +48,7 @@ public Object value(@PathVariable String name) {
4748
if (result == null) {
4849
throw new NoSuchPropertyException("No such property: " + name);
4950
}
50-
return EnvironmentEndpoint.sanitize(name, result);
51+
return ((EnvironmentEndpoint) getDelegate()).sanitize(name, result);
5152
}
5253

5354
@Override

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,18 +16,22 @@
1616

1717
package org.springframework.boot.actuate.endpoint;
1818

19+
import java.util.Map;
20+
1921
import org.junit.Test;
2022
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2123
import org.springframework.context.annotation.Bean;
2224
import org.springframework.context.annotation.Configuration;
2325

2426
import static org.hamcrest.Matchers.greaterThan;
27+
import static org.junit.Assert.assertEquals;
2528
import static org.junit.Assert.assertThat;
2629

2730
/**
2831
* Tests for {@link EnvironmentEndpoint}.
2932
*
3033
* @author Phillip Webb
34+
* @author Christian Dupuis
3135
*/
3236
public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentEndpoint> {
3337

@@ -40,6 +44,19 @@ public void invoke() throws Exception {
4044
assertThat(getEndpointBean().invoke().size(), greaterThan(0));
4145
}
4246

47+
@SuppressWarnings("unchecked")
48+
@Test
49+
public void testKeySanitization() throws Exception {
50+
System.setProperty("dbPassword", "123456");
51+
System.setProperty("apiKey", "123456");
52+
EnvironmentEndpoint report = getEndpointBean();
53+
Map<String, Object> env = report.invoke();
54+
assertEquals("******",
55+
((Map<String, Object>) env.get("systemProperties")).get("dbPassword"));
56+
assertEquals("******",
57+
((Map<String, Object>) env.get("systemProperties")).get("apiKey"));
58+
}
59+
4360
@Configuration
4461
@EnableConfigurationProperties
4562
public static class Config {

0 commit comments

Comments
 (0)