blob: b5543755a31bff04ebf71a2114aaafce15577b09 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package org.onap.aai.sparky.config;
import java.util.LinkedHashMap;
import java.util.Map;
import org.eclipse.jetty.util.security.Password;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
public class PropertyPasswordConfiguration
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static final String JETTY_OBFUSCATION_PATTERN = "OBF:";
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
for (PropertySource<?> propertySource : environment.getPropertySources()) {
Map<String, Object> propertyOverrides = new LinkedHashMap<>();
decodePasswords(propertySource, propertyOverrides);
if (!propertyOverrides.isEmpty()) {
PropertySource<?> decodedProperties =
new MapPropertySource("decoded " + propertySource.getName(), propertyOverrides);
environment.getPropertySources().addBefore(propertySource.getName(), decodedProperties);
}
}
}
private void decodePasswords(PropertySource<?> source, Map<String, Object> propertyOverrides) {
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) source;
for (String key : enumerablePropertySource.getPropertyNames()) {
Object rawValue = source.getProperty(key);
if (rawValue instanceof String) {
String rawValueString = (String) rawValue;
if (rawValueString.startsWith(JETTY_OBFUSCATION_PATTERN)) {
String decodedValue = Password.deobfuscate(rawValueString);
propertyOverrides.put(key, decodedValue);
}
}
}
}
}
}
|