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.schemaservice.config;
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;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.LinkedHashMap;
35 import java.util.Properties;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.apache.commons.io.IOUtils;
42 import org.springframework.context.ApplicationContextInitializer;
43 import org.springframework.context.ConfigurableApplicationContext;
44 import org.springframework.core.env.*;
46 public class PropertyPasswordConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> {
48 private static final Pattern decodePasswordPattern = Pattern.compile("password\\((.*?)\\)");
49 private PasswordDecoder passwordDecoder = new JettyPasswordDecoder();
50 private static final Logger logger = LoggerFactory.getLogger(PropertyPasswordConfiguration.class.getName());
53 public void initialize(ConfigurableApplicationContext applicationContext) {
54 ConfigurableEnvironment environment = applicationContext.getEnvironment();
55 String certPath = environment.getProperty("server.certs.location");
56 File passwordFile = null;
57 File passphrasesFile = null;
58 InputStream passwordStream = null;
59 InputStream passphrasesStream = null;
60 Map<String, Object> sslProps = new LinkedHashMap<>();
62 // Override the passwords from application.properties if we find AAF certman files
63 if (certPath != null) {
65 passwordFile = new File(certPath + ".password");
66 passwordStream = new FileInputStream(passwordFile);
68 if (passwordStream != null) {
69 String keystorePassword = null;
71 keystorePassword = IOUtils.toString(passwordStream);
72 if (keystorePassword != null) {
73 keystorePassword = keystorePassword.trim();
74 sslProps.put("server.ssl.key-store-password", keystorePassword);
77 logger.warn("Keystore password is null in AAF Certman password file");
80 logger.info("Not using AAF Certman password file");
82 } catch (IOException e) {
83 logger.warn("Not using AAF Certman password file " + passwordFile.getName() + " e=" + e.getMessage());
85 if (passwordStream != null) {
87 passwordStream.close();
88 } catch (Exception e) {
93 passphrasesFile = new File(certPath + ".passphrases");
94 passphrasesStream = new FileInputStream(passphrasesFile);
96 if (passphrasesStream != null) {
97 String truststorePassword = null;
98 Properties passphrasesProps = new Properties();
99 passphrasesProps.load(passphrasesStream);
100 truststorePassword = passphrasesProps.getProperty("cadi_truststore_password");
101 if (truststorePassword != null) {
102 truststorePassword = truststorePassword.trim();
103 sslProps.put("server.ssl.trust-store-password", truststorePassword);
106 logger.warn("Truststore password is null in AAF Certman passphrases file");
109 logger.info("Not using AAF Certman passphrases file");
111 } catch (IOException e) {
112 logger.warn("Not using AAF Certman passphrases file " + passphrasesFile.getName() + " e=" + e.getMessage());
114 if (passphrasesStream != null) {
116 passphrasesStream.close();
117 } catch (Exception e) {
122 for (PropertySource<?> propertySource : environment.getPropertySources()) {
123 Map<String, Object> propertyOverrides = new LinkedHashMap<>();
124 decodePasswords(propertySource, propertyOverrides);
125 if (!propertyOverrides.isEmpty()) {
126 PropertySource<?> decodedProperties = new MapPropertySource("decoded "+ propertySource.getName(), propertyOverrides);
127 environment.getPropertySources().addBefore(propertySource.getName(), decodedProperties);
131 if (!sslProps.isEmpty()) {
132 logger.info("Using AAF Certman files");
133 PropertySource<?> additionalProperties = new MapPropertySource("additionalProperties", sslProps);
134 environment.getPropertySources().addFirst(additionalProperties);
139 private void decodePasswords(PropertySource<?> source, Map<String, Object> propertyOverrides) {
140 if (source instanceof EnumerablePropertySource) {
141 EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) source;
142 for (String key : enumerablePropertySource.getPropertyNames()) {
143 Object rawValue = source.getProperty(key);
144 if (rawValue instanceof String) {
145 String decodedValue = decodePasswordsInString((String) rawValue);
146 propertyOverrides.put(key, decodedValue);
152 private String decodePasswordsInString(String input) {
153 if (input == null) return null;
154 StringBuffer output = new StringBuffer();
155 Matcher matcher = decodePasswordPattern.matcher(input);
156 while (matcher.find()) {
157 String replacement = passwordDecoder.decode(matcher.group(1));
158 matcher.appendReplacement(output, replacement);
160 matcher.appendTail(output);
161 return output.toString();