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