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