2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.config;
22 import java.util.LinkedHashMap;
24 import java.util.Optional;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
28 import org.springframework.context.ApplicationContextInitializer;
29 import org.springframework.context.ConfigurableApplicationContext;
30 import org.springframework.core.env.CompositePropertySource;
31 import org.springframework.core.env.ConfigurableEnvironment;
32 import org.springframework.core.env.EnumerablePropertySource;
33 import org.springframework.core.env.MapPropertySource;
34 import org.springframework.core.env.PropertySource;
35 import org.springframework.stereotype.Component;
37 public class PropertyPasswordConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> {
39 private static final Pattern decodePasswordPattern = Pattern.compile("password\\((.*?)\\)");
41 private PasswordDecoder passwordDecoder = new JettyPasswordDecoder();
44 public void initialize(ConfigurableApplicationContext applicationContext) {
45 ConfigurableEnvironment environment = applicationContext.getEnvironment();
46 for (PropertySource<?> propertySource : environment.getPropertySources()) {
47 Map<String, Object> propertyOverrides = new LinkedHashMap<>();
48 decodePasswords(propertySource, propertyOverrides);
49 if (!propertyOverrides.isEmpty()) {
50 PropertySource<?> decodedProperties = new MapPropertySource("decoded "+ propertySource.getName(), propertyOverrides);
51 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 decodedValue = decodePasswordsInString((String) rawValue);
63 propertyOverrides.put(key, decodedValue);
69 private String decodePasswordsInString(String input) {
70 if (input == null) return null;
71 StringBuffer output = new StringBuffer();
72 Matcher matcher = decodePasswordPattern.matcher(input);
73 while (matcher.find()) {
74 String replacement = passwordDecoder.decode(matcher.group(1));
75 matcher.appendReplacement(output, replacement);
77 matcher.appendTail(output);
78 return output.toString();