Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-rest / src / main / java / org / onap / aai / restclient / PropertyPasswordConfiguration.java
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
21 package org.onap.aai.restclient;
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 implements ApplicationContextInitializer<ConfigurableApplicationContext> {
44
45     private static final Pattern decodePasswordPattern = Pattern.compile("password\\((.*?)\\)");
46     private PasswordDecoder passwordDecoder = new JettyPasswordDecoder();
47     private static final Logger logger = LoggerFactory.getLogger(PropertyPasswordConfiguration.class.getName());
48
49     @Override
50     public void initialize(ConfigurableApplicationContext applicationContext) {
51         ConfigurableEnvironment environment = applicationContext.getEnvironment();
52         String certPath = environment.getProperty("server.certs.location");
53         File passwordFile = null;
54         File passphrasesFile = null;
55         InputStream passwordStream = null;
56         InputStream passphrasesStream = null;
57         Map<String, Object> sslProps = new LinkedHashMap<>();
58
59         // Override the passwords from application.properties if we find AAF certman files
60         if (certPath != null) {
61             try {
62                 passwordFile = new File(certPath + ".password");
63                 passwordStream = new FileInputStream(passwordFile);
64
65                 if (passwordStream != null) {
66                     String keystorePassword = null;
67
68                     keystorePassword = IOUtils.toString(passwordStream);
69                     if (keystorePassword != null) {
70                         keystorePassword = keystorePassword.trim();
71                     }
72                     sslProps.put("server.ssl.key-store-password", keystorePassword);
73                     sslProps.put("schema.service.ssl.key-store-password", keystorePassword);
74                     sslProps.put("validation.service.ssl.key-store-password", keystorePassword);
75                 } else {
76                     logger.info("Not using AAF Certman password file");
77                 }
78             } catch (IOException e) {
79                 logger.warn("Not using AAF Certman password file, e=" + e.getMessage());
80             } finally {
81                 if (passwordStream != null) {
82                     try {
83                         passwordStream.close();
84                     } catch (Exception e) {
85                     }
86                 }
87             }
88             try {
89                 passphrasesFile = new File(certPath + ".passphrases");
90                 passphrasesStream = new FileInputStream(passphrasesFile);
91
92                 if (passphrasesStream != null) {
93                     String truststorePassword = null;
94                     Properties passphrasesProps = new Properties();
95                     passphrasesProps.load(passphrasesStream);
96                     truststorePassword = passphrasesProps.getProperty("cadi_truststore_password");
97                     if (truststorePassword != null) {
98                         truststorePassword = truststorePassword.trim();
99                     }
100                     sslProps.put("server.ssl.trust-store-password", truststorePassword);
101                     sslProps.put("schema.service.ssl.trust-store-password", truststorePassword);
102                     sslProps.put("validation.service.ssl.trust-store-password", truststorePassword);
103                 } else {
104                     logger.info("Not using AAF Certman passphrases file");
105                 }
106             } catch (IOException e) {
107                 logger.warn("Not using AAF Certman passphrases file, e=" + e.getMessage());
108             } finally {
109                 if (passphrasesStream != null) {
110                     try {
111                         passphrasesStream.close();
112                     } catch (Exception e) {
113                     }
114                 }
115             }
116         }
117         for (PropertySource<?> propertySource : environment.getPropertySources()) {
118             Map<String, Object> propertyOverrides = new LinkedHashMap<>();
119             decodePasswords(propertySource, propertyOverrides);
120             if (!propertyOverrides.isEmpty()) {
121                 PropertySource<?> decodedProperties =
122                         new MapPropertySource("decoded " + propertySource.getName(), propertyOverrides);
123                 environment.getPropertySources().addBefore(propertySource.getName(), decodedProperties);
124             }
125
126         }
127         if (!sslProps.isEmpty()) {
128             logger.info("Using AAF Certman files");
129             PropertySource<?> additionalProperties = new MapPropertySource("additionalProperties", sslProps);
130             environment.getPropertySources().addFirst(additionalProperties);
131         }
132     }
133
134     private void decodePasswords(PropertySource<?> source, Map<String, Object> propertyOverrides) {
135         if (source instanceof EnumerablePropertySource) {
136             EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) source;
137             for (String key : enumerablePropertySource.getPropertyNames()) {
138                 Object rawValue = source.getProperty(key);
139                 if (rawValue instanceof String) {
140                     String decodedValue = decodePasswordsInString((String) rawValue);
141                     propertyOverrides.put(key, decodedValue);
142                 }
143             }
144         }
145     }
146
147     private String decodePasswordsInString(String input) {
148         if (input == null)
149             return null;
150         StringBuffer output = new StringBuffer();
151         Matcher matcher = decodePasswordPattern.matcher(input);
152         while (matcher.find()) {
153             String replacement = passwordDecoder.decode(matcher.group(1));
154             matcher.appendReplacement(output, replacement);
155         }
156         matcher.appendTail(output);
157         return output.toString();
158     }
159
160 }