TLS support in sdc-fe
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / servlets / SSLProxyServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.sdc.fe.servlets;
21
22 import javax.servlet.ServletException;
23
24 import org.eclipse.jetty.client.HttpClient;
25 import org.eclipse.jetty.proxy.ProxyServlet;
26 import org.eclipse.jetty.util.ssl.SslContextFactory;
27 import org.onap.config.api.JettySSLUtils;
28 import org.openecomp.sdc.common.api.Constants;
29 import org.openecomp.sdc.fe.config.Configuration;
30 import org.openecomp.sdc.fe.config.ConfigurationManager;
31 import org.openecomp.sdc.fe.utils.BeProtocol;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public abstract class SSLProxyServlet extends ProxyServlet {
36
37     private static final long serialVersionUID = 1L;
38     private static final Logger log = LoggerFactory.getLogger(SSLProxyServlet.class);
39
40     @Override
41     protected HttpClient createHttpClient() throws ServletException {
42         Configuration config = ((ConfigurationManager) getServletConfig().getServletContext().getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
43             .getConfiguration();
44         boolean isSecureClient = !config.getBeProtocol().equals(BeProtocol.HTTP.getProtocolName());
45         HttpClient client = (isSecureClient) ? getSecureHttpClient() : super.createHttpClient();
46         int requestTimeout = config.getRequestTimeout() * 1000;
47         if (requestTimeout == 0) {
48             requestTimeout = 1200_000;
49         }
50         setTimeout(requestTimeout);
51         client.setIdleTimeout(requestTimeout);
52         client.setStopTimeout(requestTimeout);
53         return client;
54     }
55
56     private HttpClient getSecureHttpClient() throws ServletException {
57         final JettySSLUtils.JettySslConfig sslConfig = JettySSLUtils.getSSLConfig();
58         SslContextFactory sslContextFactory = new SslContextFactory.Client();
59         sslContextFactory.setKeyStorePath(sslConfig.getKeystorePath());
60         sslContextFactory.setKeyStorePassword(sslConfig.getKeystorePass());
61         sslContextFactory.setKeyManagerPassword(sslConfig.getKeystorePass());
62         sslContextFactory.setTrustStorePath(sslConfig.getTruststorePath());
63         sslContextFactory.setTrustStorePassword(sslConfig.getTruststorePass());
64         sslContextFactory.setKeyStorePath(sslConfig.getKeystorePath());
65         
66         // Instantiate HttpClient with the SslContextFactory
67         final var httpClient = new HttpClient(sslContextFactory);
68         // Configure HttpClient, for example:
69         httpClient.setFollowRedirects(false);
70         // Start HttpClient
71         try {
72             httpClient.start();
73         } catch (Exception x) {
74             log.error("Exception thrown while starting httpClient", x);
75             throw new ServletException(x);
76         }
77         return httpClient;
78     }
79 }