32
32
import com .rabbitmq .stream .sasl .DefaultSaslConfiguration ;
33
33
import io .netty .handler .ssl .SslContext ;
34
34
import io .netty .handler .ssl .SslContextBuilder ;
35
+ import io .netty .handler .ssl .SslProvider ;
35
36
import java .io .File ;
36
37
import java .io .FileInputStream ;
37
38
import java .net .InetAddress ;
54
55
import javax .net .ssl .SSLHandshakeException ;
55
56
import org .junit .jupiter .api .Test ;
56
57
import org .junit .jupiter .api .extension .ExtendWith ;
58
+ import org .junit .jupiter .params .Parameter ;
59
+ import org .junit .jupiter .params .ParameterizedClass ;
60
+ import org .junit .jupiter .params .provider .EnumSource ;
57
61
58
62
@ DisabledIfTlsNotEnabled
59
63
@ ExtendWith (TestUtils .StreamTestInfrastructureExtension .class )
64
+ @ ParameterizedClass
65
+ @ EnumSource (names = {"JDK" , "OPENSSL" })
60
66
public class TlsTest {
61
67
68
+ @ Parameter SslProvider sslProvider ;
69
+
62
70
String stream ;
63
71
64
72
TestUtils .ClientFactory cf ;
65
73
int credit = 10 ;
66
74
67
- static SslContext alwaysTrustSslContext () {
75
+ SslContext alwaysTrustSslContext () {
68
76
try {
69
- return SslContextBuilder . forClient ().trustManager (TRUST_EVERYTHING_TRUST_MANAGER ).build ();
77
+ return builder ().trustManager (TRUST_EVERYTHING_TRUST_MANAGER ).build ();
70
78
} catch (SSLException e ) {
71
79
throw new RuntimeException (e );
72
80
}
@@ -191,7 +199,7 @@ void unverifiedConnection() {
191
199
void verifiedConnectionWithCorrectServerCertificate () throws Exception {
192
200
// in server certificate SAN
193
201
String hostname = "localhost" ;
194
- SslContext context = SslContextBuilder . forClient ().trustManager (caCertificate ()).build ();
202
+ SslContext context = builder ().trustManager (caCertificate ()).build ();
195
203
cf .get (new ClientParameters ().host (hostname ).sslContext (context ));
196
204
}
197
205
@@ -200,25 +208,22 @@ void verifiedConnectionWithCorrectServerCertificateWithSni() throws Exception {
200
208
// not in server certificate SAN, but setting SNI makes it work
201
209
String hostname = "127.0.0.1" ;
202
210
SslContext context =
203
- SslContextBuilder .forClient ()
204
- .trustManager (caCertificate ())
205
- .serverName (new SNIHostName ("localhost" ))
206
- .build ();
211
+ builder ().trustManager (caCertificate ()).serverName (new SNIHostName ("localhost" )).build ();
207
212
cf .get (new ClientParameters ().host (hostname ).sslContext (context ));
208
213
}
209
214
210
215
@ Test
211
216
void verifiedConnectionWithCorrectServerCertificateFailsIfHostnameNotInSan () throws Exception {
212
217
// not in server certificate SAN
213
218
String hostname = "127.0.0.1" ;
214
- SslContext context = SslContextBuilder . forClient ().trustManager (caCertificate ()).build ();
219
+ SslContext context = builder ().trustManager (caCertificate ()).build ();
215
220
assertThatThrownBy (() -> cf .get (new ClientParameters ().host (hostname ).sslContext (context )))
216
221
.hasCauseInstanceOf (SSLHandshakeException .class );
217
222
}
218
223
219
224
@ Test
220
225
void verifiedConnectionWithWrongServerCertificate () throws Exception {
221
- SslContext context = SslContextBuilder . forClient ().trustManager (clientCertificate ()).build ();
226
+ SslContext context = builder ().trustManager (clientCertificate ()).build ();
222
227
assertThatThrownBy (() -> cf .get (new ClientParameters ().sslContext (context )))
223
228
.isInstanceOf (StreamException .class )
224
229
.hasCauseInstanceOf (SSLHandshakeException .class );
@@ -227,7 +232,7 @@ void verifiedConnectionWithWrongServerCertificate() throws Exception {
227
232
@ Test
228
233
void verifiedConnectionWithCorrectClientPrivateKey () throws Exception {
229
234
SslContext context =
230
- SslContextBuilder . forClient ()
235
+ builder ()
231
236
.trustManager (caCertificate ())
232
237
.keyManager (clientKey (), clientCertificate ())
233
238
.build ();
@@ -241,10 +246,7 @@ void verifiedConnectionWithCorrectClientPrivateKey() throws Exception {
241
246
void saslExternalShouldSucceedWithUserForClientCertificate () throws Exception {
242
247
X509Certificate clientCertificate = clientCertificate ();
243
248
SslContext context =
244
- SslContextBuilder .forClient ()
245
- .trustManager (caCertificate ())
246
- .keyManager (clientKey (), clientCertificate )
247
- .build ();
249
+ builder ().trustManager (caCertificate ()).keyManager (clientKey (), clientCertificate ).build ();
248
250
249
251
String username = clientCertificate .getSubjectX500Principal ().getName ();
250
252
Cli .rabbitmqctlIgnoreError (format ("delete_user %s" , username ));
@@ -268,10 +270,7 @@ void saslExternalShouldSucceedWithUserForClientCertificate() throws Exception {
268
270
void saslExternalShouldFailIfNoUserForClientCertificate () throws Exception {
269
271
X509Certificate clientCertificate = clientCertificate ();
270
272
SslContext context =
271
- SslContextBuilder .forClient ()
272
- .trustManager (caCertificate ())
273
- .keyManager (clientKey (), clientCertificate )
274
- .build ();
273
+ builder ().trustManager (caCertificate ()).keyManager (clientKey (), clientCertificate ).build ();
275
274
276
275
String username = clientCertificate .getSubjectX500Principal ().getName ();
277
276
Cli .rabbitmqctlIgnoreError (format ("delete_user %s" , username ));
@@ -288,7 +287,7 @@ void saslExternalShouldFailIfNoUserForClientCertificate() throws Exception {
288
287
289
288
@ Test
290
289
void hostnameVerificationShouldFailWhenSettingHostToLoopbackInterface () throws Exception {
291
- SslContext context = SslContextBuilder . forClient ().trustManager (caCertificate ()).build ();
290
+ SslContext context = builder ().trustManager (caCertificate ()).build ();
292
291
assertThatThrownBy (() -> cf .get (new ClientParameters ().sslContext (context ).host ("127.0.0.1" )))
293
292
.isInstanceOf (StreamException .class )
294
293
.hasCauseInstanceOf (SSLHandshakeException .class );
@@ -298,10 +297,7 @@ void hostnameVerificationShouldFailWhenSettingHostToLoopbackInterface() throws E
298
297
void shouldConnectWhenSettingHostToLoopbackInterfaceAndDisablingHostnameVerification ()
299
298
throws Exception {
300
299
SslContext context =
301
- SslContextBuilder .forClient ()
302
- .endpointIdentificationAlgorithm (null )
303
- .trustManager (caCertificate ())
304
- .build ();
300
+ builder ().endpointIdentificationAlgorithm (null ).trustManager (caCertificate ()).build ();
305
301
cf .get (new ClientParameters ().sslContext (context ).host ("127.0.0.1" ));
306
302
}
307
303
@@ -325,7 +321,7 @@ void environmentPublisherConsumer() throws Exception {
325
321
.uri ("rabbitmq-stream+tls://localhost" )
326
322
.addressResolver (addr -> new Address ("localhost" , Client .DEFAULT_TLS_PORT ))
327
323
.tls ()
328
- .sslContext (SslContextBuilder . forClient ().trustManager (caCertificate ()).build ())
324
+ .sslContext (builder ().trustManager (caCertificate ()).build ())
329
325
.environmentBuilder ()
330
326
.build ()) {
331
327
@@ -371,4 +367,8 @@ private static String hostname() {
371
367
private static String tlsArtefactPath (String in ) {
372
368
return in .replace ("$(hostname)" , hostname ()).replace ("$(hostname -s)" , hostname ());
373
369
}
370
+
371
+ private SslContextBuilder builder () {
372
+ return SslContextBuilder .forClient ().sslProvider (sslProvider );
373
+ }
374
374
}
0 commit comments