26
26
import org .springframework .core .env .EnumerablePropertySource ;
27
27
import org .springframework .core .env .Environment ;
28
28
import org .springframework .core .env .MutablePropertySources ;
29
+ import org .springframework .core .env .PropertyResolver ;
29
30
import org .springframework .core .env .PropertySource ;
31
+ import org .springframework .core .env .PropertySources ;
32
+ import org .springframework .core .env .PropertySourcesPropertyResolver ;
30
33
import org .springframework .core .env .StandardEnvironment ;
31
34
32
35
/**
35
38
* @author Dave Syer
36
39
* @author Phillip Webb
37
40
* @author Christian Dupuis
41
+ * @author Madhura Bhave
38
42
*/
39
43
@ ConfigurationProperties (prefix = "endpoints.env" )
40
44
public class EnvironmentEndpoint extends AbstractEndpoint <Map <String , Object >> {
@@ -56,14 +60,15 @@ public void setKeysToSanitize(String... keysToSanitize) {
56
60
public Map <String , Object > invoke () {
57
61
Map <String , Object > result = new LinkedHashMap <String , Object >();
58
62
result .put ("profiles" , getEnvironment ().getActiveProfiles ());
59
- for (Entry <String , PropertySource <?>> entry : getPropertySources ().entrySet ()) {
63
+ PropertyResolver resolver = getResolver ();
64
+ for (Entry <String , PropertySource <?>> entry : getPropertySourcesAsMap ().entrySet ()) {
60
65
PropertySource <?> source = entry .getValue ();
61
66
String sourceName = entry .getKey ();
62
67
if (source instanceof EnumerablePropertySource ) {
63
68
EnumerablePropertySource <?> enumerable = (EnumerablePropertySource <?>) source ;
64
69
Map <String , Object > properties = new LinkedHashMap <String , Object >();
65
70
for (String name : enumerable .getPropertyNames ()) {
66
- properties .put (name , sanitize (name , enumerable .getProperty (name )));
71
+ properties .put (name , sanitize (name , resolver .getProperty (name )));
67
72
}
68
73
properties = postProcessSourceProperties (sourceName , properties );
69
74
if (properties != null ) {
@@ -74,20 +79,32 @@ public Map<String, Object> invoke() {
74
79
return result ;
75
80
}
76
81
77
- private Map <String , PropertySource <?>> getPropertySources () {
82
+ public PropertyResolver getResolver () {
83
+ PlaceholderSanitizingPropertyResolver resolver = new PlaceholderSanitizingPropertyResolver (
84
+ getPropertySources (), this .sanitizer );
85
+ resolver .setIgnoreUnresolvableNestedPlaceholders (true );
86
+ return resolver ;
87
+ }
88
+
89
+ private Map <String , PropertySource <?>> getPropertySourcesAsMap () {
78
90
Map <String , PropertySource <?>> map = new LinkedHashMap <String , PropertySource <?>>();
79
- MutablePropertySources sources = null ;
91
+ MutablePropertySources sources = getPropertySources ();
92
+ for (PropertySource <?> source : sources ) {
93
+ extract ("" , map , source );
94
+ }
95
+ return map ;
96
+ }
97
+
98
+ private MutablePropertySources getPropertySources () {
99
+ MutablePropertySources sources ;
80
100
Environment environment = getEnvironment ();
81
101
if (environment != null && environment instanceof ConfigurableEnvironment ) {
82
102
sources = ((ConfigurableEnvironment ) environment ).getPropertySources ();
83
103
}
84
104
else {
85
105
sources = new StandardEnvironment ().getPropertySources ();
86
106
}
87
- for (PropertySource <?> source : sources ) {
88
- extract ("" , map , source );
89
- }
90
- return map ;
107
+ return sources ;
91
108
}
92
109
93
110
private void extract (String root , Map <String , PropertySource <?>> map ,
@@ -120,4 +137,32 @@ protected Map<String, Object> postProcessSourceProperties(String sourceName,
120
137
return properties ;
121
138
}
122
139
140
+ /**
141
+ * {@link PropertySourcesPropertyResolver} that sanitizes sensitive placeholders
142
+ * if present.
143
+ *
144
+ * @author Madhura Bhave
145
+ */
146
+ private class PlaceholderSanitizingPropertyResolver extends PropertySourcesPropertyResolver {
147
+
148
+ private final Sanitizer sanitizer ;
149
+
150
+ /**
151
+ * Create a new resolver against the given property sources.
152
+ * @param propertySources the set of {@link PropertySource} objects to use
153
+ * @param sanitizer the sanitizer used to sanitize sensitive values
154
+ */
155
+ PlaceholderSanitizingPropertyResolver (PropertySources
156
+ propertySources , Sanitizer sanitizer ) {
157
+ super (propertySources );
158
+ this .sanitizer = sanitizer ;
159
+ }
160
+
161
+ @ Override
162
+ protected String getPropertyAsRawString (String key ) {
163
+ String value = super .getPropertyAsRawString (key );
164
+ return (String ) this .sanitizer .sanitize (key , value );
165
+ }
166
+ }
167
+
123
168
}
0 commit comments