31
31
import java .net .URISyntaxException ;
32
32
import java .time .Duration ;
33
33
import java .util .Collection ;
34
+ import java .util .List ;
34
35
import java .util .Map ;
35
36
import java .util .concurrent .CompletableFuture ;
36
37
import java .util .concurrent .ConcurrentHashMap ;
@@ -90,6 +91,7 @@ public void channelCreated(Channel ch) throws Exception {
90
91
private final SslProvider sslProvider ;
91
92
private final ProxyConfiguration proxyConfiguration ;
92
93
private final BootstrapProvider bootstrapProvider ;
94
+ private final SslContextProvider sslContextProvider ;
93
95
94
96
private AwaitCloseChannelPoolMap (Builder builder , Function <Builder , BootstrapProvider > createBootStrapProvider ) {
95
97
this .configuration = builder .configuration ;
@@ -100,6 +102,7 @@ private AwaitCloseChannelPoolMap(Builder builder, Function<Builder, BootstrapPro
100
102
this .sslProvider = builder .sslProvider ;
101
103
this .proxyConfiguration = builder .proxyConfiguration ;
102
104
this .bootstrapProvider = createBootStrapProvider .apply (builder );
105
+ this .sslContextProvider = new SslContextProvider (configuration , protocol , sslProvider );
103
106
}
104
107
105
108
private AwaitCloseChannelPoolMap (Builder builder ) {
@@ -123,8 +126,11 @@ public static Builder builder() {
123
126
124
127
@ Override
125
128
protected SimpleChannelPoolAwareChannelPool newPool (URI key ) {
126
- SslContext sslContext = sslContext (key );
127
-
129
+ SslContext sslContext = null ;
130
+ if (needSslContext (key )) {
131
+ sslContext = sslContextProvider .sslContext ();
132
+ }
133
+
128
134
Bootstrap bootstrap = createBootstrap (key );
129
135
130
136
AtomicReference <ChannelPool > channelPoolRef = new AtomicReference <>();
@@ -259,53 +265,12 @@ private SdkChannelPool wrapBaseChannelPool(Bootstrap bootstrap, ChannelPool chan
259
265
return sdkChannelPool ;
260
266
}
261
267
262
- private SslContext sslContext (URI targetAddress ) {
268
+ private boolean needSslContext (URI targetAddress ) {
263
269
URI proxyAddress = proxyAddress (targetAddress );
264
-
265
270
boolean needContext = targetAddress .getScheme ().equalsIgnoreCase ("https" )
266
- || proxyAddress != null && proxyAddress .getScheme ().equalsIgnoreCase ("https" );
267
-
268
- if (!needContext ) {
269
- return null ;
270
- }
271
+ || proxyAddress != null && proxyAddress .getScheme ().equalsIgnoreCase ("https" );
271
272
272
- try {
273
- return SslContextBuilder .forClient ()
274
- .sslProvider (sslProvider )
275
- .ciphers (Http2SecurityUtil .CIPHERS , SupportedCipherSuiteFilter .INSTANCE )
276
- .trustManager (getTrustManager ())
277
- .keyManager (getKeyManager ())
278
- .build ();
279
- } catch (SSLException e ) {
280
- throw new RuntimeException (e );
281
- }
282
- }
283
-
284
- private TrustManagerFactory getTrustManager () {
285
- Validate .isTrue (configuration .tlsTrustManagersProvider () == null || !configuration .trustAllCertificates (),
286
- "A TlsTrustManagerProvider can't be provided if TrustAllCertificates is also set" );
287
-
288
- if (configuration .tlsTrustManagersProvider () != null ) {
289
- return StaticTrustManagerFactory .create (configuration .tlsTrustManagersProvider ().trustManagers ());
290
- }
291
-
292
- if (configuration .trustAllCertificates ()) {
293
- log .warn (() -> "SSL Certificate verification is disabled. This is not a safe setting and should only be "
294
- + "used for testing." );
295
- return InsecureTrustManagerFactory .INSTANCE ;
296
- }
297
-
298
- return null ;
299
- }
300
-
301
- private KeyManagerFactory getKeyManager () {
302
- if (configuration .tlsKeyManagersProvider () != null ) {
303
- KeyManager [] keyManagers = configuration .tlsKeyManagersProvider ().keyManagers ();
304
- if (keyManagers != null ) {
305
- return StaticKeyManagerFactory .create (keyManagers );
306
- }
307
- }
308
- return null ;
273
+ return needContext ;
309
274
}
310
275
311
276
public static class Builder {
0 commit comments