29416341dac1013003102dfbde49f47e3e3b27b6
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. 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
21 package org.onap.pnfsimulator.simulator.client.utils.ssl;
22
23 import org.apache.http.client.HttpClient;
24 import org.apache.http.client.config.RequestConfig;
25 import org.apache.http.conn.ssl.NoopHostnameVerifier;
26 import org.apache.http.conn.ssl.TrustAllStrategy;
27 import org.apache.http.conn.ssl.TrustStrategy;
28 import org.apache.http.impl.client.HttpClientBuilder;
29 import org.apache.http.impl.client.HttpClients;
30 import org.apache.http.ssl.SSLContextBuilder;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import javax.net.ssl.SSLContext;
35 import java.net.MalformedURLException;
36 import java.net.URL;
37 import java.security.KeyManagementException;
38 import java.security.KeyStoreException;
39 import java.security.NoSuchAlgorithmException;
40
41 public enum SslSupportLevel {
42
43     NONE {
44         public HttpClient getClient(RequestConfig requestConfig) {
45             LOGGER.info("<!-----IN SslSupportLevel.NONE, Creating BasicHttpClient for http protocol----!>");
46             return HttpClientBuilder
47                     .create()
48                     .setDefaultRequestConfig(requestConfig)
49                     .build();
50         }
51     },
52     ALWAYS_TRUST {
53         public HttpClient getClient(RequestConfig requestConfig) {
54             LoggerFactory.getLogger(SslSupportLevel.class).info("<!-----IN SslSupportLevel.ALWAYS_TRUST, Creating client with SSL support for https protocol----!>");
55             HttpClient client;
56             try {
57                 SSLContext alwaysTrustSslContext = SSLContextBuilder.create().loadTrustMaterial(TRUST_STRATEGY_ALWAYS).build();
58                 client = HttpClients.custom()
59                         .setSSLContext(alwaysTrustSslContext)
60                         .setSSLHostnameVerifier(new NoopHostnameVerifier())
61                         .setDefaultRequestConfig(requestConfig)
62                         .build();
63
64             } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
65                 LOGGER.error("Could not initialize client due to SSL exception: {}. Default client without SSL support will be used instead.\nCause: {}", e.getMessage(), e.getCause());
66                 client = NONE.getClient(requestConfig);
67             }
68             return client;
69         }
70     };
71
72     private static final Logger LOGGER = LoggerFactory.getLogger(SslSupportLevel.class);
73     private static final TrustStrategy TRUST_STRATEGY_ALWAYS = new TrustAllStrategy();
74
75     public static SslSupportLevel getSupportLevelBasedOnProtocol(String url) throws MalformedURLException {
76             return "https".equals(new URL(url).getProtocol()) ? SslSupportLevel.ALWAYS_TRUST : SslSupportLevel.NONE;
77     }
78
79     public abstract HttpClient getClient(RequestConfig requestConfig);
80
81 }