Setting default store types when not defined in configuration for init of https client
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / utils / JettySSLUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.openecomp.sdc.fe.utils;
22
23 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
24 import org.apache.http.ssl.SSLContexts;
25
26 import javax.net.ssl.SSLContext;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.security.GeneralSecurityException;
31 import java.security.KeyStore;
32 import java.util.Properties;
33
34 public class JettySSLUtils {
35     private JettySSLUtils() {
36     }
37
38     public static JettySslConfig getSSLConfig() throws IOException {
39         Properties sslProperties = new Properties();
40
41         String sslPropsPath = System.getenv("JETTY_BASE") + File.separator + "/start.d/ssl.ini";
42         File sslPropsFile = new File(sslPropsPath);
43         try (FileInputStream fis = new FileInputStream(sslPropsFile)) {
44             sslProperties.load(fis);
45         }
46
47         return new JettySslConfig(sslProperties);
48     }
49
50
51     public static SSLContext getSslContext() throws GeneralSecurityException, IOException {
52         JettySSLUtils.JettySslConfig sslProperties = JettySSLUtils.getSSLConfig();
53
54         KeyStore trustStore  = KeyStore.getInstance(sslProperties.getTruststoreType());
55         try (FileInputStream instream = new FileInputStream(new File(sslProperties.getTruststorePath()));
56         ){
57             trustStore.load(instream, (sslProperties.getTruststorePass()).toCharArray());
58         }
59
60         KeyStore keystore  = KeyStore.getInstance(sslProperties.getKeystoreType());
61         try (FileInputStream instream = new FileInputStream(new File(sslProperties.getKeystorePath()));
62         ){
63             keystore.load(instream, sslProperties.getKeystorePass().toCharArray());
64         }
65
66         // Trust own CA and all self-signed certs
67         return SSLContexts.custom()
68                 .loadKeyMaterial(keystore, sslProperties.getKeystorePass().toCharArray())
69                 .loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
70                 .build();
71     }
72
73
74     public static class JettySslConfig {
75         Properties sslProperties;
76         static final String JETTY_BASE = System.getenv("JETTY_BASE");
77         static final String KEY_STORE_TYPE_PROPERTY_NAME="jetty.sslContext.keyStoreType";
78         static final String TRUST_STORE_TYPE_PROPERTY_NAME="jetty.sslContext.trustStoreType";
79         JettySslConfig(Properties sslProperties) {
80             this.sslProperties = sslProperties;
81         }
82
83         public String getJettyBase() {
84             return JettySslConfig.JETTY_BASE;
85         }
86
87         public String getKeystorePath() {
88             return sslProperties.getProperty("jetty.sslContext.keyStorePath");
89         }
90
91         public String getKeystorePass() {
92             return sslProperties.getProperty("jetty.sslContext.keyStorePassword");
93         }
94
95         public String getKeystoreType() {
96             return sslProperties.getProperty(KEY_STORE_TYPE_PROPERTY_NAME, KeyStore.getDefaultType());
97         }
98
99         public String getTruststorePath() {
100             return sslProperties.getProperty("jetty.sslContext.trustStorePath");
101         }
102
103         public String getTruststorePass() {
104             return sslProperties.getProperty("jetty.sslContext.trustStorePassword");
105         }
106
107         public String getTruststoreType() {
108             return sslProperties.getProperty(TRUST_STORE_TYPE_PROPERTY_NAME, KeyStore.getDefaultType());
109         }
110
111         public String getKeyStoreManager() {
112             return sslProperties.getProperty("jetty.sslContext.keyManagerPassword");
113         }
114
115         public Boolean getNeedClientAuth() {
116             if (sslProperties.containsKey("jetty.sslContext.needClientAuth")) {
117                 return Boolean.valueOf(sslProperties.getProperty("jetty.sslContext.needClientAuth"));
118             } else {
119                 return Boolean.FALSE;
120             }
121         }
122
123         public String getProperty(String key) {
124             return sslProperties.getProperty(key);
125         }
126
127     }
128
129 }