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