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