1 package org.onap.aai.sparky.config;
 
   3 import java.util.LinkedHashMap;
 
   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;
 
  14 public class PropertyPasswordConfiguration
 
  15     implements ApplicationContextInitializer<ConfigurableApplicationContext> {
 
  17   private static final String JETTY_OBFUSCATION_PATTERN = "OBF:";
 
  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);
 
  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);