[AAI] Update babel to fix security vulnerabilities
[aai/babel.git] / src / main / java / org / onap / aai / babel / config / 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 package org.onap.aai.babel.config;
21
22 import org.apache.commons.io.IOUtils;
23 import org.eclipse.jetty.util.security.Password;
24 import org.onap.aai.babel.logging.LogHelper;
25 import org.springframework.context.ApplicationContextInitializer;
26 import org.springframework.context.ConfigurableApplicationContext;
27 import org.springframework.core.env.ConfigurableEnvironment;
28 import org.springframework.core.env.MapPropertySource;
29 import org.springframework.core.env.PropertySource;
30
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.LinkedHashMap;
36 import java.util.Map;
37 import java.util.Properties;
38
39 public class PropertyPasswordConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> {
40
41     @Override
42     public void initialize(ConfigurableApplicationContext applicationContext) {
43
44         Map<String, Object> sslProps = new LinkedHashMap<>();
45         ConfigurableEnvironment environment = applicationContext.getEnvironment();
46         String certPath = environment.getProperty("server.certs.location");
47         File passwordFile = null;
48         File passphrasesFile = null;
49         InputStream passwordStream = null;
50         InputStream passphrasesStream = null;
51         String keystorePassword = null;
52         String truststorePassword = null;
53
54         if (certPath != null) {
55             try {
56                 passwordFile = new File(certPath + ".password");
57                 passwordStream = new FileInputStream(passwordFile);
58
59                 if (passwordStream != null) {
60                     keystorePassword = IOUtils.toString(passwordStream);
61                     if (keystorePassword != null) {
62                         keystorePassword = keystorePassword.trim();
63                     }
64                     sslProps.put("server.ssl.key-store-password", keystorePassword);
65                 }
66             } catch (IOException e) {
67             } finally {
68                 if (passwordStream != null) {
69                     try {
70                         passwordStream.close();
71                     } catch (Exception e) {
72                     }
73                 }
74             }
75             try {
76                 passphrasesFile = new File(certPath + ".passphrases");
77                 passphrasesStream = new FileInputStream(passphrasesFile);
78
79                 if (passphrasesStream != null) {
80                     Properties passphrasesProps = new Properties();
81                     passphrasesProps.load(passphrasesStream);
82                     truststorePassword = passphrasesProps.getProperty("cadi_truststore_password");
83                     if (truststorePassword != null) {
84                         truststorePassword = truststorePassword.trim();
85                     }
86                     sslProps.put("server.ssl.trust-store-password", truststorePassword);
87                 } else {
88                 }
89             } catch (IOException e) {
90             } finally {
91                 if (passphrasesStream != null) {
92                     try {
93                         passphrasesStream.close();
94                     } catch (Exception e) {
95                     }
96                 }
97             }
98         }
99         if (keystorePassword == null || keystorePassword.isEmpty()) {
100             keystorePassword = System.getProperty("KEY_STORE_PASSWORD");
101             if (keystorePassword != null && (!keystorePassword.isEmpty()) ) {
102                 System.setProperty("server.ssl.key-store-password", new Password(keystorePassword).toString());
103             }
104             if (keystorePassword == null || keystorePassword.isEmpty()) {
105                 throw new IllegalArgumentException("Mandatory property KEY_STORE_PASSWORD not set");
106             }
107         }
108         else {
109             sslProps.put("server.ssl.key-store-password", keystorePassword);
110         }
111         if (truststorePassword == null || truststorePassword.isEmpty()) {
112         }
113         else {
114             sslProps.put("server.ssl.trust-store-password", truststorePassword);
115         }
116         if (!sslProps.isEmpty()) {
117             PropertySource<?> additionalProperties = new MapPropertySource("additionalProperties", sslProps);
118             environment.getPropertySources().addFirst(additionalProperties);
119         }
120     }
121 }