db67ac66df8d96fc519971d6af44040872293ee3
[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  *
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 package org.onap.aai.schemaservice.config;
21
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;
28
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.LinkedHashMap;
34 import java.util.Map;
35 import java.util.Properties;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38
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.*;
45
46 public class PropertyPasswordConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> {
47
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());
51
52     @Override
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<>();
61
62         // Override the passwords from application.properties if we find AAF certman files
63         if (certPath != null) {
64             try {
65                 passwordFile = new File(certPath + ".password");
66                 passwordStream = new FileInputStream(passwordFile);
67
68                 if (passwordStream != null) {
69                     String keystorePassword = null;
70
71                     keystorePassword = IOUtils.toString(passwordStream);
72                     if (keystorePassword != null) {
73                         keystorePassword = keystorePassword.trim();
74                         sslProps.put("server.ssl.key-store-password", keystorePassword);
75                     }
76                     else{
77                         logger.warn("Keystore password is null in AAF Certman password file");
78                     }
79                 } else {
80                     logger.info("Not using AAF Certman password file");
81                 }
82             } catch (IOException e) {
83                 logger.warn("Not using AAF Certman password file " + passwordFile.getName() + " e=" + e.getMessage());
84             } finally {
85                 if (passwordStream != null) {
86                     try {
87                         passwordStream.close();
88                     } catch (Exception e) {
89                     }
90                 }
91             }
92             try {
93                 passphrasesFile = new File(certPath + ".passphrases");
94                 passphrasesStream = new FileInputStream(passphrasesFile);
95
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);
104                     }
105                     else {
106                         logger.warn("Truststore password is null in AAF Certman passphrases file");
107                     }
108                 } else {
109                     logger.info("Not using AAF Certman passphrases file");
110                 }
111             } catch (IOException e) {
112                 logger.warn("Not using AAF Certman passphrases file " +  passphrasesFile.getName() + " e=" + e.getMessage());
113             } finally {
114                 if (passphrasesStream != null) {
115                     try {
116                         passphrasesStream.close();
117                     } catch (Exception e) {
118                     }
119                 }
120             }
121         }
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);
128             }
129
130         }
131         if (!sslProps.isEmpty()) {
132             logger.info("Using AAF Certman files");
133             PropertySource<?> additionalProperties = new MapPropertySource("additionalProperties", sslProps);
134             environment.getPropertySources().addFirst(additionalProperties);
135         }
136
137     }
138
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);
147                 }
148             }
149         }
150     }
151
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);
159         }
160         matcher.appendTail(output);
161         return output.toString();
162     }
163
164 }