2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.config.api;
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;
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;
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");
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);
49 return new JettySslConfig(sslProperties);
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());
59 final KeyStore keystore = KeyStore.getInstance(sslProperties.getKeystoreType());
60 try (final InputStream fis = new FileInputStream(sslProperties.getKeystorePath())) {
61 keystore.load(fis, sslProperties.getKeystorePass().toCharArray());
63 // Trust own CA and all self-signed certs
64 return SSLContexts.custom()
65 .loadKeyMaterial(keystore, sslProperties.getKeystorePass().toCharArray())
66 .loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
71 public static class JettySslConfig {
73 private final Properties sslProperties;
75 public String getJettyBase(){
79 public String getKeystorePath() {
80 return sslProperties.getProperty("jetty.sslContext.keyStorePath");
83 public String getKeystorePass() {
84 return sslProperties.getProperty("jetty.sslContext.keyStorePassword");
87 public String getKeystoreType() {
88 return sslProperties.getProperty("jetty.sslContext.keyStoreType", KeyStore.getDefaultType());
91 public String getTruststorePath() {
92 return sslProperties.getProperty("jetty.sslContext.trustStorePath");
95 public String getTruststorePass() {
96 return sslProperties.getProperty("jetty.sslContext.trustStorePassword");
99 public String getTruststoreType() {
100 return sslProperties.getProperty("jetty.sslContext.trustStoreType", KeyStore.getDefaultType());
103 public String getKeyManagerPassword() {
104 return sslProperties.getProperty("jetty.sslContext.keyManagerPassword");
107 public Boolean getNeedClientAuth() {
108 if (sslProperties.containsKey("jetty.sslContext.needClientAuth")) {
109 return Boolean.valueOf(sslProperties.getProperty("jetty.sslContext.needClientAuth"));
111 return Boolean.FALSE;