32
32
33
33
package org .opensearch .client ;
34
34
35
+ import com .carrotsearch .randomizedtesting .annotations .ThreadLeakFilters ;
36
+
35
37
import com .sun .net .httpserver .HttpExchange ;
36
38
import com .sun .net .httpserver .HttpHandler ;
37
39
import com .sun .net .httpserver .HttpsConfigurator ;
38
40
import com .sun .net .httpserver .HttpsServer ;
39
41
40
42
import org .apache .hc .core5 .http .HttpHost ;
43
+ import org .apache .hc .core5 .ssl .SSLContextBuilder ;
41
44
import org .junit .AfterClass ;
42
45
import org .junit .BeforeClass ;
43
46
44
47
import javax .net .ssl .KeyManagerFactory ;
45
48
import javax .net .ssl .SSLContext ;
46
- import javax .net .ssl .SSLHandshakeException ;
49
+ import javax .net .ssl .SSLException ;
47
50
import javax .net .ssl .TrustManagerFactory ;
48
51
49
52
import java .io .IOException ;
50
53
import java .io .InputStream ;
51
54
import java .net .InetAddress ;
52
55
import java .net .InetSocketAddress ;
53
- import java .nio .file .Files ;
54
- import java .nio .file .Paths ;
55
56
import java .security .AccessController ;
56
- import java .security .KeyFactory ;
57
57
import java .security .KeyStore ;
58
58
import java .security .PrivilegedAction ;
59
- import java .security .cert .Certificate ;
60
- import java .security .cert .CertificateFactory ;
61
- import java .security .spec .PKCS8EncodedKeySpec ;
59
+ import java .security .SecureRandom ;
62
60
63
61
import static org .hamcrest .Matchers .instanceOf ;
64
62
import static org .junit .Assert .assertEquals ;
68
66
/**
69
67
* Integration test to validate the builder builds a client with the correct configuration
70
68
*/
69
+ @ ThreadLeakFilters (filters = { BCDisposalDaemonFilter .class })
71
70
public class RestClientBuilderIntegTests extends RestClientTestCase {
72
71
73
72
private static HttpsServer httpsServer ;
74
73
75
74
@ BeforeClass
76
75
public static void startHttpServer () throws Exception {
77
76
httpsServer = HttpsServer .create (new InetSocketAddress (InetAddress .getLoopbackAddress (), 0 ), 0 );
78
- httpsServer .setHttpsConfigurator (new HttpsConfigurator (getSslContext ()));
77
+ httpsServer .setHttpsConfigurator (new HttpsConfigurator (getSslContext (true )));
79
78
httpsServer .createContext ("/" , new ResponseHandler ());
80
79
httpsServer .start ();
81
80
}
@@ -103,11 +102,11 @@ public void testBuilderUsesDefaultSSLContext() throws Exception {
103
102
client .performRequest (new Request ("GET" , "/" ));
104
103
fail ("connection should have been rejected due to SSL handshake" );
105
104
} catch (Exception e ) {
106
- assertThat (e , instanceOf (SSLHandshakeException .class ));
105
+ assertThat (e . getCause () , instanceOf (SSLException .class ));
107
106
}
108
107
}
109
108
110
- SSLContext .setDefault (getSslContext ());
109
+ SSLContext .setDefault (getSslContext (false ));
111
110
try (RestClient client = buildRestClient ()) {
112
111
Response response = client .performRequest (new Request ("GET" , "/" ));
113
112
assertEquals (200 , response .getStatusLine ().getStatusCode ());
@@ -122,34 +121,37 @@ private RestClient buildRestClient() {
122
121
return RestClient .builder (new HttpHost ("https" , address .getHostString (), address .getPort ())).build ();
123
122
}
124
123
125
- private static SSLContext getSslContext () throws Exception {
126
- SSLContext sslContext = SSLContext .getInstance (getProtocol ());
124
+ private static SSLContext getSslContext (boolean server ) throws Exception {
125
+ SSLContext sslContext ;
126
+ char [] password = "password" .toCharArray ();
127
+ SecureRandom secureRandom = SecureRandom .getInstance ("DEFAULT" , "BCFIPS" );
128
+ String fileExtension = ".jks" ;
129
+
127
130
try (
128
- InputStream certFile = RestClientBuilderIntegTests .class .getResourceAsStream ("/test.crt" );
129
- InputStream keyStoreFile = RestClientBuilderIntegTests .class .getResourceAsStream ("/test_truststore.jks" )
131
+ InputStream trustStoreFile = RestClientBuilderIntegTests .class .getResourceAsStream ("/test_truststore" + fileExtension );
132
+ InputStream keyStoreFile = RestClientBuilderIntegTests .class .getResourceAsStream ("/testks" + fileExtension )
130
133
) {
131
- // Build a keystore of default type programmatically since we can't use JKS keystores to
132
- // init a KeyManagerFactory in FIPS 140 JVMs.
133
- KeyStore keyStore = KeyStore .getInstance (KeyStore .getDefaultType ());
134
- keyStore .load (null , "password" .toCharArray ());
135
- CertificateFactory certFactory = CertificateFactory .getInstance ("X.509" );
136
- PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec (
137
- Files .readAllBytes (Paths .get (RestClientBuilderIntegTests .class .getResource ("/test.der" ).toURI ()))
138
- );
139
- KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
140
- keyStore .setKeyEntry (
141
- "mykey" ,
142
- keyFactory .generatePrivate (privateKeySpec ),
143
- "password" .toCharArray (),
144
- new Certificate [] { certFactory .generateCertificate (certFile ) }
145
- );
146
- KeyManagerFactory kmf = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
147
- kmf .init (keyStore , "password" .toCharArray ());
134
+ KeyStore keyStore = KeyStore .getInstance ("JKS" );
135
+ keyStore .load (keyStoreFile , password );
136
+ KeyManagerFactory kmf = KeyManagerFactory .getInstance ("PKIX" , "BCJSSE" );
137
+ kmf .init (keyStore , password );
138
+
148
139
KeyStore trustStore = KeyStore .getInstance ("JKS" );
149
- trustStore .load (keyStoreFile , " password" . toCharArray () );
150
- TrustManagerFactory tmf = TrustManagerFactory .getInstance (TrustManagerFactory . getDefaultAlgorithm () );
140
+ trustStore .load (trustStoreFile , password );
141
+ TrustManagerFactory tmf = TrustManagerFactory .getInstance ("PKIX" , "BCJSSE" );
151
142
tmf .init (trustStore );
152
- sslContext .init (kmf .getKeyManagers (), tmf .getTrustManagers (), null );
143
+
144
+ SSLContextBuilder sslContextBuilder = SSLContextBuilder .create ()
145
+ .setProvider ("BCJSSE" )
146
+ .setProtocol (getProtocol ())
147
+ .setSecureRandom (secureRandom );
148
+
149
+ if (server ) {
150
+ sslContextBuilder .loadKeyMaterial (keyStore , password );
151
+ }
152
+ sslContextBuilder .loadTrustMaterial (trustStore , null );
153
+ sslContext = sslContextBuilder .build ();
154
+
153
155
}
154
156
return sslContext ;
155
157
}
0 commit comments