1 package com.mapd.common;
3 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
4 import org.apache.http.conn.ssl.SSLSocketFactory;
5 import org.apache.http.conn.ssl.X509HostnameVerifier;
6 import org.apache.http.impl.client.CloseableHttpClient;
7 import org.apache.http.impl.client.HttpClients;
8 import org.apache.http.ssl.SSLContextBuilder;
9 import org.apache.http.ssl.SSLContexts;
10 import org.apache.thrift.transport.THttpClient;
11 import org.apache.thrift.transport.TSSLTransportFactory;
12 import org.apache.thrift.transport.TServerSocket;
13 import org.apache.thrift.transport.TServerTransport;
14 import org.apache.thrift.transport.TSocket;
15 import org.apache.thrift.transport.TTransport;
16 import org.slf4j.LoggerFactory;
19 import java.nio.file.Path;
20 import java.nio.file.Paths;
21 import java.security.GeneralSecurityException;
22 import java.security.KeyStore;
23 import java.security.cert.CertificateException;
24 import java.security.cert.X509Certificate;
25 import java.util.Arrays;
27 import javax.net.ssl.KeyManager;
28 import javax.net.ssl.SSLContext;
29 import javax.net.ssl.SSLSocket;
30 import javax.net.ssl.TrustManager;
31 import javax.net.ssl.TrustManagerFactory;
32 import javax.net.ssl.X509TrustManager;
36 LoggerFactory.getLogger(SockTransportProperties.class);
47 boolean validate_server_name =
false;
53 boolean validate_server_name)
throws Exception {
60 String trustStoreName, String trustStorePassword)
throws Exception {
62 trustStoreName, trustStorePassword,
true);
65 String trustStoreName, String trustStorePassword,
boolean validate_server_name)
70 validate_server_name);
75 String keyStoreName, String keyStorePassword)
throws Exception {
76 boolean validate_server_name =
false;
77 if (keyStoreName == null || keyStorePassword == null) {
78 String errStr =
new String(
79 "Invalid null parameter(s) used for getEncryptedServer. Both keyStoreName and keyStorePassword must be specified");
80 RuntimeException rE =
new RuntimeException(errStr);
81 HEAVYDBLOGGER.error(errStr, rE);
87 validate_server_name);
91 boolean validate_server_name =
false;
103 encryptedClientSpecifiedTrustStore
119 boolean validate_server_name)
throws Exception {
121 ? SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER
122 : SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
125 char[] store_password =
"".toCharArray();
126 if (passwd != null && !passwd.isEmpty()) {
127 store_password = passwd.toCharArray();
130 case encryptedServer: {
135 case encryptedClientSpecifiedTrustStore: {
136 if (store_name == null) {
139 KeyStore trust_store = KeyStore.getInstance(KeyStore.getDefaultType());
141 java.io.FileInputStream fis =
new java.io.FileInputStream(store_name);
142 trust_store.load(fis, store_password);
143 }
catch (Exception eX) {
145 new String(
"Error loading key/trust store [" + store_name +
"]");
146 HEAVYDBLOGGER.error(err_str, eX);
154 String errStr =
new String(
155 "Invalid transportType [" +
transportType +
"] used in constructor");
156 RuntimeException rE =
new RuntimeException(errStr);
157 HEAVYDBLOGGER.error(errStr, rE);
166 ? SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER
167 : SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
170 case encryptedClientDefaultTrustStore:
174 case unencryptedClient:
175 case unencryptedServer:
178 String errStr =
new String(
179 "Invalid transportType [" +
transportType +
"] used in constructor");
180 RuntimeException rE =
new RuntimeException(errStr);
181 HEAVYDBLOGGER.error(errStr, rE);
194 TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
"PKIX");
196 trustManagerFactory.init(trust_store);
208 throws org.apache.thrift.TException {
209 TTransport tTransport = null;
211 case encryptedClientDefaultTrustStore:
212 case encryptedClientSpecifiedTrustStore:
215 case unencryptedClient:
216 tTransport =
new TSocket(server_host, port);
219 String errStr =
new String(
"Invalid transportType [" +
transportType
220 +
"] used in openClientTransport");
221 RuntimeException rE =
new RuntimeException(errStr);
222 HEAVYDBLOGGER.error(errStr, rE);
229 throws org.apache.thrift.TException {
231 int socket_so_timeout_ms = 0;
232 TSocket tsocket = null;
234 SSLContext sc = SSLContext.getInstance(
"TLS");
235 sc.init(null,
trustManagers,
new java.security.SecureRandom());
237 SSLSocket sx = (SSLSocket) sc.getSocketFactory().createSocket(server_host, port);
238 sx.setSoTimeout(socket_so_timeout_ms);
239 tsocket =
new TSocket(sx);
240 }
catch (Exception ex) {
241 String errStr =
new String(
"Error openBinaryEncrypted [" + server_host +
":" + port
242 +
"] used in openClientTransport - ");
243 errStr += ex.toString();
244 RuntimeException rE =
new RuntimeException(errStr);
245 HEAVYDBLOGGER.error(errStr, rE);
255 String errStr =
new String(
"Invalid transportType [" +
transportType
256 +
"] used in openHttpsClientTransport");
257 RuntimeException rE =
new RuntimeException(errStr);
258 HEAVYDBLOGGER.error(errStr, rE);
261 TTransport transport = null;
264 SSLContext sc = SSLContext.getInstance(
"TLS");
265 sc.init(null,
trustManagers,
new java.security.SecureRandom());
266 SSLConnectionSocketFactory sslConnectionSocketFactory = null;
267 sslConnectionSocketFactory =
270 CloseableHttpClient closeableHttpClient =
272 .setSSLSocketFactory(sslConnectionSocketFactory)
275 new THttpClient(
"https://" + server_host +
":" + port, closeableHttpClient);
277 }
catch (Exception ex) {
278 String err_str =
new String(
"Exception:" + ex.getClass().getCanonicalName()
279 +
" thrown. Unable to create Secure socket for the HTTPS connection");
280 HEAVYDBLOGGER.error(err_str, ex);
288 throws org.apache.thrift.TException {
290 String errStr =
new String(
"Invalid transportType [" +
transportType
291 +
"] used in openHttpClientTransport");
292 RuntimeException rE =
new RuntimeException(errStr);
293 HEAVYDBLOGGER.error(errStr, rE);
297 String url =
"http://" + server_host +
":" + port;
298 return (
new THttpClient(url));
305 throws org.apache.thrift.TException {
309 return (
new TServerSocket(port));
311 String errStr =
new String(
"Invalid transportType [" +
transportType
312 +
"] used in openServerTransport");
313 RuntimeException rE =
new RuntimeException(errStr);
314 HEAVYDBLOGGER.error(errStr, rE);
320 throws org.apache.thrift.TException {
322 int socket_so_timeout_ms = 0;
323 TSSLTransportFactory.TSSLTransportParameters
params =
324 new TSSLTransportFactory.TSSLTransportParameters();
327 params.requireClientAuth(
false);
331 TServerTransport t = TSSLTransportFactory.getServerSocket(
332 port, socket_so_timeout_ms, null,
params);
342 SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
static SockTransportProperties getEncryptedClientSpecifiedTrustStore(String trustStoreName, String trustStorePassword)
TTransport openHttpClientTransport(String server_host, int port)
encryptedClientDefaultTrustStore
TTransport openHttpsClientTransport(String server_host, int port)
static final org.slf4j.Logger HEAVYDBLOGGER
static SockTransportProperties getUnecryptedServer()
static SockTransportProperties getEncryptedClientSpecifiedTrustStore(String trustStoreName, String trustStorePassword, boolean validate_server_name)
SockTransportProperties(String truststore_name, String truststore_passwd)
TServerTransport openServerTransportEncrypted(int port)
TTransport openClientTransport(String server_host, int port)
TrustManager[] trustManagers
static SockTransportProperties getEncryptedServer(String keyStoreName, String keyStorePassword)
TransportType transportType
SockTransportProperties(TransportType tT, String store_name, String passwd, boolean validate_server_name)
X509HostnameVerifier x509HostnameVerifier_
TTransport openBinaryEncrypted(String server_host, int port)
encryptedClientSpecifiedTrustStore
static SockTransportProperties getUnencryptedClient()
TServerTransport openServerTransport(int port)
char[] key_store_password
static SockTransportProperties getEncryptedClientDefaultTrustStore(boolean validate_server_name)
SockTransportProperties(TransportType transportType, boolean validate_server_name)
void initializeAcceptedIssuers(KeyStore trust_store)