2292577c6d4cdabcce10223f270ef023eee76f3d
[aai/schema-service.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  * <p>
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * <p>
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.aai.schemaservice.config;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29 import java.util.Properties;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 import org.apache.commons.io.IOUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.context.ApplicationContextInitializer;
37 import org.springframework.context.ConfigurableApplicationContext;
38 import org.springframework.core.env.ConfigurableEnvironment;
39 import org.springframework.core.env.EnumerablePropertySource;
40 import org.springframework.core.env.MapPropertySource;
41 import org.springframework.core.env.PropertySource;
42
43 public class PropertyPasswordConfiguration
44     implements ApplicationContextInitializer<ConfigurableApplicationContext> {
45
46     private static final Pattern decodePasswordPattern = Pattern.compile("password\\((.*?)\\)");
47     private PasswordDecoder passwordDecoder = new JettyPasswordDecoder();
48     private static final Logger logger =
49         LoggerFactory.getLogger(PropertyPasswordConfiguration.class.getName());
50
51     @Override
52     public void initialize(ConfigurableApplicationContext applicationContext) {
53
54         ConfigurableEnvironment environment = applicationContext.getEnvironment();
55         String certPath = environment.getProperty("server.certs.location");
56         Map<String, Object> sslProps = new LinkedHashMap<>();
57
58         // Override the passwords from application.properties if we find AAF certman files
59         if (certPath != null) {
60             File passwordFile = new File(certPath + ".password");
61             try (InputStream passwordStream = new FileInputStream(passwordFile)) {
62                 String keystorePassword = null;
63
64                 keystorePassword = IOUtils.toString(passwordStream);
65                 if (keystorePassword != null) {
66                     keystorePassword = keystorePassword.trim();
67                     sslProps.put("server.ssl.key-store-password", keystorePassword);
68                 } else {
69                     logger.warn("Keystore password is null in AAF Certman password file");
70                 }
71             } catch (IOException e) {
72                 logger.warn("Not using AAF Certman password file " + passwordFile.getName() + " e="
73                     + e.getMessage());
74             }
75
76             File passphrasesFile = new File(certPath + ".passphrases");
77             try (InputStream passphrasesStream = new FileInputStream(passphrasesFile)) {
78                 String truststorePassword = null;
79                 Properties passphrasesProps = new Properties();
80                 passphrasesProps.load(passphrasesStream);
81                 truststorePassword = passphrasesProps.getProperty("cadi_truststore_password");
82                 if (truststorePassword != null) {
83                     truststorePassword = truststorePassword.trim();
84                     sslProps.put("server.ssl.trust-store-password", truststorePassword);
85                 } else {
86                     logger.warn("Truststore password is null in AAF Certman passphrases file");
87                 }
88             } catch (IOException e) {
89                 logger.warn("Not using AAF Certman passphrases file " + passphrasesFile.getName()
90                     + " e=" + e.getMessage());
91             }
92         }
93         for (PropertySource<?> propertySource : environment.getPropertySources()) {
94             Map<String, Object> propertyOverrides = new LinkedHashMap<>();
95             decodePasswords(propertySource, propertyOverrides);
96             if (!propertyOverrides.isEmpty()) {
97                 PropertySource<?> decodedProperties =
98                     new MapPropertySource("decoded " + propertySource.getName(), propertyOverrides);
99                 environment.getPropertySources().addBefore(propertySource.getName(),
100                     decodedProperties);
101             }
102
103         }
104         if (!sslProps.isEmpty()) {
105             logger.info("Using AAF Certman files");
106             PropertySource<?> additionalProperties =
107                 new MapPropertySource("additionalProperties", sslProps);
108             environment.getPropertySources().addFirst(additionalProperties);
109         }
110
111     }
112
113     private void decodePasswords(PropertySource<?> source, Map<String, Object> propertyOverrides) {
114         if (source instanceof EnumerablePropertySource) {
115             EnumerablePropertySource<?> enumerablePropertySource =
116                 (EnumerablePropertySource<?>) source;
117             for (String key : enumerablePropertySource.getPropertyNames()) {
118                 Object rawValue = source.getProperty(key);
119                 if (rawValue instanceof String) {
120                     String decodedValue = decodePasswordsInString((String) rawValue);
121                     propertyOverrides.put(key, decodedValue);
122                 }
123             }
124         }
125     }
126
127     private String decodePasswordsInString(String input) {
128         if (input == null) {
129             return null;
130         }
131         StringBuffer output = new StringBuffer();
132         Matcher matcher = decodePasswordPattern.matcher(input);
133         while (matcher.find()) {
134             String replacement = passwordDecoder.decode(matcher.group(1));
135             matcher.appendReplacement(output, replacement);
136         }
137         matcher.appendTail(output);
138         return output.toString();
139     }
140
141 }