97ae7bc6471eb369b8ba51cec55edb12d34aa114
[aai/aai-common.git] / aai-rest / src / main / java / org / onap / aai / restclient / PropertyPasswordConfiguration.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.restclient;
21
22 import org.springframework.context.ApplicationContextInitializer;
23 import org.springframework.context.ConfigurableApplicationContext;
24 import org.springframework.core.env.ConfigurableEnvironment;
25 import org.springframework.core.env.EnumerablePropertySource;
26 import org.springframework.core.env.MapPropertySource;
27 import org.springframework.core.env.PropertySource;
28
29 import java.util.LinkedHashMap;
30 import java.util.Map;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34 public class PropertyPasswordConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> {
35
36     private static final Pattern decodePasswordPattern = Pattern.compile("password\\((.*?)\\)");
37
38     private PasswordDecoder passwordDecoder = new JettyPasswordDecoder();
39
40     @Override
41     public void initialize(ConfigurableApplicationContext applicationContext) {
42         ConfigurableEnvironment environment = applicationContext.getEnvironment();
43         for (PropertySource<?> propertySource : environment.getPropertySources()) {
44             Map<String, Object> propertyOverrides = new LinkedHashMap<>();
45             decodePasswords(propertySource, propertyOverrides);
46             if (!propertyOverrides.isEmpty()) {
47                 PropertySource<?> decodedProperties = new MapPropertySource("decoded " + propertySource.getName(), propertyOverrides);
48                 environment.getPropertySources().addBefore(propertySource.getName(), decodedProperties);
49             }
50         }
51     }
52
53     private void decodePasswords(PropertySource<?> source, Map<String, Object> propertyOverrides) {
54         if (source instanceof EnumerablePropertySource) {
55             EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) source;
56             for (String key : enumerablePropertySource.getPropertyNames()) {
57                 Object rawValue = source.getProperty(key);
58                 if (rawValue instanceof String) {
59                     String decodedValue = decodePasswordsInString((String) rawValue);
60                     propertyOverrides.put(key, decodedValue);
61                 }
62             }
63         }
64     }
65
66     private String decodePasswordsInString(String input) {
67         if (input == null) return null;
68         StringBuffer output = new StringBuffer();
69         Matcher matcher = decodePasswordPattern.matcher(input);
70         while (matcher.find()) {
71             String replacement = passwordDecoder.decode(matcher.group(1));
72             matcher.appendReplacement(output, replacement);
73         }
74         matcher.appendTail(output);
75         return output.toString();
76     }
77
78 }