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