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=========================================================
21 package org.onap.aai.restclient;
23 import java.util.LinkedHashMap;
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.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 implements ApplicationContextInitializer<ConfigurableApplicationContext> {
37 private static final Pattern decodePasswordPattern = Pattern.compile("password\\((.*?)\\)");
39 private PasswordDecoder passwordDecoder = new JettyPasswordDecoder();
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);
55 private void decodePasswords(PropertySource<?> source, Map<String, Object> propertyOverrides) {
56 if (source instanceof EnumerablePropertySource) {
57 EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) source;
58 for (String key : enumerablePropertySource.getPropertyNames()) {
59 Object rawValue = source.getProperty(key);
60 if (rawValue instanceof String) {
61 String decodedValue = decodePasswordsInString((String) rawValue);
62 propertyOverrides.put(key, decodedValue);
68 private String decodePasswordsInString(String input) {
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();