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;
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;
32 public class JettySSLUtils {
34 private JettySSLUtils() {
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);
44 return new JettySslConfig(sslProperties);
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());
53 KeyStore keystore = KeyStore.getInstance(sslProperties.getKeystoreType());
54 try (FileInputStream instream = new FileInputStream(new File(sslProperties.getKeystorePath()));) {
55 keystore.load(instream, sslProperties.getKeystorePass().toCharArray());
57 // Trust own CA and all self-signed certs
58 return SSLContexts.custom().loadKeyMaterial(keystore, sslProperties.getKeystorePass().toCharArray())
59 .loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
62 public static class JettySslConfig {
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;
69 JettySslConfig(Properties sslProperties) {
70 this.sslProperties = sslProperties;
73 public String getJettyBase() {
74 return JettySslConfig.JETTY_BASE;
77 public String getKeystorePath() {
78 return sslProperties.getProperty("jetty.sslContext.keyStorePath");
81 public String getKeystorePass() {
82 return sslProperties.getProperty("jetty.sslContext.keyStorePassword");
85 public String getKeystoreType() {
86 return sslProperties.getProperty(KEY_STORE_TYPE_PROPERTY_NAME, KeyStore.getDefaultType());
89 public String getTruststorePath() {
90 return sslProperties.getProperty("jetty.sslContext.trustStorePath");
93 public String getTruststorePass() {
94 return sslProperties.getProperty("jetty.sslContext.trustStorePassword");
97 public String getTruststoreType() {
98 return sslProperties.getProperty(TRUST_STORE_TYPE_PROPERTY_NAME, KeyStore.getDefaultType());
101 public String getKeyStoreManager() {
102 return sslProperties.getProperty("jetty.sslContext.keyManagerPassword");
105 public Boolean getNeedClientAuth() {
106 if (sslProperties.containsKey("jetty.sslContext.needClientAuth")) {
107 return Boolean.valueOf(sslProperties.getProperty("jetty.sslContext.needClientAuth"));
109 return Boolean.FALSE;
113 public String getProperty(String key) {
114 return sslProperties.getProperty(key);