2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 
   6  * Copyright © 2017-2018 Amdocs
 
   7  * ================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License");
 
   9  * you may not use this file except in compliance with the License.
 
  10  * You may obtain a copy of the License at
 
  12  *       http://www.apache.org/licenses/LICENSE-2.0
 
  14  * Unless required by applicable law or agreed to in writing, software
 
  15  * distributed under the License is distributed on an "AS IS" BASIS,
 
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  17  * See the License for the specific language governing permissions and
 
  18  * limitations under the License.
 
  19  * ============LICENSE_END=========================================================
 
  21 package org.onap.aai.sparky.config;
 
  23 import java.util.LinkedHashMap;
 
  26 import org.apache.commons.lang3.StringUtils;
 
  27 import org.eclipse.jetty.util.security.Password;
 
  28 import org.springframework.context.ApplicationContextInitializer;
 
  29 import org.springframework.context.ConfigurableApplicationContext;
 
  30 import org.springframework.core.env.ConfigurableEnvironment;
 
  31 import org.springframework.core.env.EnumerablePropertySource;
 
  32 import org.springframework.core.env.MapPropertySource;
 
  33 import org.springframework.core.env.PropertySource;
 
  35 public class PropertyPasswordConfiguration
 
  36         implements ApplicationContextInitializer<ConfigurableApplicationContext> {
 
  38   private static final String JETTY_OBFUSCATION_PATTERN = "OBF:";
 
  39   private static final String ENV = "ENV:";
 
  42   public void initialize(ConfigurableApplicationContext applicationContext) {
 
  43     ConfigurableEnvironment environment = applicationContext.getEnvironment();
 
  44     for (PropertySource<?> propertySource : environment.getPropertySources()) {
 
  45       Map<String, Object> propertyOverrides = new LinkedHashMap<>();
 
  46       decodePasswords(propertySource, propertyOverrides);
 
  47       if (!propertyOverrides.isEmpty()) {
 
  48         PropertySource<?> decodedProperties =
 
  49                 new MapPropertySource("decoded " + propertySource.getName(), propertyOverrides);
 
  50         environment.getPropertySources().addBefore(propertySource.getName(), decodedProperties);
 
  56   private void decodePasswords(PropertySource<?> source, Map<String, Object> propertyOverrides) {
 
  57     if (source instanceof EnumerablePropertySource) {
 
  58       EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) source;
 
  59       for (String key : enumerablePropertySource.getPropertyNames()) {
 
  60         Object rawValue = source.getProperty(key);
 
  61         if (rawValue instanceof String) {
 
  62           String rawValueString = (String) rawValue;
 
  63           if (rawValueString.startsWith(JETTY_OBFUSCATION_PATTERN)) {
 
  64             String decodedValue = Password.deobfuscate(rawValueString);
 
  65             propertyOverrides.put(key, decodedValue);
 
  66           } else if(rawValueString.startsWith(ENV)){
 
  67             String decodedValue = System.getProperty(StringUtils.removeStart(rawValueString, ENV));
 
  68             propertyOverrides.put(key, decodedValue);