Fix sonar issues
[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 java.nio.charset.StandardCharsets;
23 import org.eclipse.jetty.util.security.Password;
24 import org.springframework.context.ApplicationContextInitializer;
25 import org.springframework.context.ConfigurableApplicationContext;
26 import org.springframework.core.env.ConfigurableEnvironment;
27 import org.springframework.core.env.MapPropertySource;
28 import org.springframework.core.env.PropertySource;
29
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
37 public class PropertyPasswordConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> {
38
39     private static final String PROP_KEY_STORE_PASS = "server.ssl.key-store-password";
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         String keystorePassword = null;
48         String truststorePassword = null;
49
50         if (certPath != null) {
51             try (InputStream passwordStream = new FileInputStream(certPath + ".password")) {
52                 keystorePassword = new String(passwordStream.readAllBytes(), StandardCharsets.UTF_8);
53                 keystorePassword = keystorePassword.trim();
54                 sslProps.put(PROP_KEY_STORE_PASS, keystorePassword);
55             } catch (IOException e) {
56                 keystorePassword = null;
57             }
58             try (InputStream passphrasesStream = new FileInputStream(certPath + ".passphrases");) {
59                 Properties passphrasesProps = new Properties();
60                 passphrasesProps.load(passphrasesStream);
61                 truststorePassword = passphrasesProps.getProperty("cadi_truststore_password");
62                 if (truststorePassword != null) {
63                     truststorePassword = truststorePassword.trim();
64                 }
65                 sslProps.put("server.ssl.trust-store-password", truststorePassword);
66             } catch (IOException e) {
67                 truststorePassword = null;
68             }
69         }
70         if (keystorePassword == null || keystorePassword.isEmpty()) {
71             keystorePassword = System.getProperty("KEY_STORE_PASSWORD");
72             if (keystorePassword != null && (!keystorePassword.isEmpty()) ) {
73                 System.setProperty(PROP_KEY_STORE_PASS, new Password(keystorePassword).toString());
74             }
75             if (keystorePassword == null || keystorePassword.isEmpty()) {
76                 throw new IllegalArgumentException("Mandatory property KEY_STORE_PASSWORD not set");
77             }
78         }
79         else {
80             sslProps.put(PROP_KEY_STORE_PASS, keystorePassword);
81         }
82         if (truststorePassword != null && !truststorePassword.isEmpty()) {
83             sslProps.put("server.ssl.trust-store-password", truststorePassword);
84         }
85         if (!sslProps.isEmpty()) {
86             PropertySource<?> additionalProperties = new MapPropertySource("additionalProperties", sslProps);
87             environment.getPropertySources().addFirst(additionalProperties);
88         }
89     }
90 }