6232c326a5977deede8c2635f0d6ce0a253f822f
[sdnc/apps.git] /
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
21 package org.onap.sdnc.apps.pomba.servicedecomposition;
22
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
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;
34 import org.springframework.stereotype.Component;
35
36 @Component
37 public class PropertyPasswordConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext>
38 {
39
40     private static final Pattern decodePasswordPattern = Pattern.compile("password\\((.*?)\\)");
41
42     private PasswordDecoder passwordDecoder = new JettyPasswordDecoder();
43
44     @Override
45     public void initialize(ConfigurableApplicationContext applicationContext) {
46         ConfigurableEnvironment environment = applicationContext.getEnvironment();
47         for (PropertySource<?> propertySource : environment.getPropertySources()) {
48             Map<String, Object> propertyOverrides = new LinkedHashMap<>();
49             decodePasswords(propertySource, propertyOverrides);
50             if (!propertyOverrides.isEmpty()) {
51                 PropertySource<?> decodedProperties = new MapPropertySource("decoded "+ propertySource.getName(), propertyOverrides);
52                 environment.getPropertySources().addBefore(propertySource.getName(), decodedProperties);
53             }
54         }
55     }
56
57     private void decodePasswords(PropertySource<?> source, Map<String, Object> propertyOverrides) {
58         if (source instanceof EnumerablePropertySource) {
59             EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) source;
60             for (String key : enumerablePropertySource.getPropertyNames()) {
61                 Object rawValue = source.getProperty(key);
62                 if (rawValue instanceof String) {
63                     String decodedValue = decodePasswordsInString((String) rawValue);
64                     propertyOverrides.put(key, decodedValue);
65                 }
66             }
67         }
68     }
69
70     private String decodePasswordsInString(String input) {
71         if (input == null) return null;
72         StringBuffer output = new StringBuffer();
73         Matcher matcher = decodePasswordPattern.matcher(input);
74         while (matcher.find()) {
75             String replacement = passwordDecoder.decode(matcher.group(1));
76             matcher.appendReplacement(output, replacement);
77         }
78         matcher.appendTail(output);
79         return output.toString();
80     }
81 }