Skip to content

Commit 34bcb56

Browse files
committed
Refactor: extract sslContext logic to a separate class
1 parent 11abeb8 commit 34bcb56

File tree

2 files changed

+98
-46
lines changed

2 files changed

+98
-46
lines changed

http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/internal/AwaitCloseChannelPoolMap.java

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.URISyntaxException;
3232
import java.time.Duration;
3333
import java.util.Collection;
34+
import java.util.List;
3435
import java.util.Map;
3536
import java.util.concurrent.CompletableFuture;
3637
import java.util.concurrent.ConcurrentHashMap;
@@ -90,6 +91,7 @@ public void channelCreated(Channel ch) throws Exception {
9091
private final SslProvider sslProvider;
9192
private final ProxyConfiguration proxyConfiguration;
9293
private final BootstrapProvider bootstrapProvider;
94+
private final SslContextProvider sslContextProvider;
9395

9496
private AwaitCloseChannelPoolMap(Builder builder, Function<Builder, BootstrapProvider> createBootStrapProvider) {
9597
this.configuration = builder.configuration;
@@ -100,6 +102,7 @@ private AwaitCloseChannelPoolMap(Builder builder, Function<Builder, BootstrapPro
100102
this.sslProvider = builder.sslProvider;
101103
this.proxyConfiguration = builder.proxyConfiguration;
102104
this.bootstrapProvider = createBootStrapProvider.apply(builder);
105+
this.sslContextProvider = new SslContextProvider(configuration, protocol, sslProvider);
103106
}
104107

105108
private AwaitCloseChannelPoolMap(Builder builder) {
@@ -123,8 +126,11 @@ public static Builder builder() {
123126

124127
@Override
125128
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+
128134
Bootstrap bootstrap = createBootstrap(key);
129135

130136
AtomicReference<ChannelPool> channelPoolRef = new AtomicReference<>();
@@ -259,53 +265,12 @@ private SdkChannelPool wrapBaseChannelPool(Bootstrap bootstrap, ChannelPool chan
259265
return sdkChannelPool;
260266
}
261267

262-
private SslContext sslContext(URI targetAddress) {
268+
private boolean needSslContext(URI targetAddress) {
263269
URI proxyAddress = proxyAddress(targetAddress);
264-
265270
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");
271272

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;
309274
}
310275

311276
public static class Builder {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.nio.netty.internal;
17+
18+
import io.netty.handler.codec.http2.Http2SecurityUtil;
19+
import io.netty.handler.ssl.SslContext;
20+
import io.netty.handler.ssl.SslContextBuilder;
21+
import io.netty.handler.ssl.SslProvider;
22+
import io.netty.handler.ssl.SupportedCipherSuiteFilter;
23+
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
24+
import javax.net.ssl.KeyManager;
25+
import javax.net.ssl.KeyManagerFactory;
26+
import javax.net.ssl.SSLException;
27+
import javax.net.ssl.TrustManagerFactory;
28+
import software.amazon.awssdk.annotations.SdkInternalApi;
29+
import software.amazon.awssdk.http.Protocol;
30+
import software.amazon.awssdk.utils.Logger;
31+
import software.amazon.awssdk.utils.Validate;
32+
33+
@SdkInternalApi
34+
public final class SslContextProvider {
35+
private static final Logger log = Logger.loggerFor(SslContextProvider.class);
36+
private final Protocol protocol;
37+
private final SslProvider sslProvider;
38+
private final TrustManagerFactory trustManagerFactory;
39+
private final KeyManagerFactory keyManagerFactory;
40+
41+
public SslContextProvider(NettyConfiguration configuration, Protocol protocol, SslProvider sslProvider) {
42+
this.protocol = protocol;
43+
this.sslProvider = sslProvider;
44+
this.trustManagerFactory = getTrustManager(configuration);
45+
this.keyManagerFactory = getKeyManager(configuration);
46+
}
47+
48+
public SslContext sslContext() {
49+
try {
50+
return SslContextBuilder.forClient()
51+
.sslProvider(sslProvider)
52+
.ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
53+
.trustManager(trustManagerFactory)
54+
.keyManager(keyManagerFactory)
55+
.build();
56+
} catch (SSLException e) {
57+
throw new RuntimeException(e);
58+
}
59+
}
60+
61+
private TrustManagerFactory getTrustManager(NettyConfiguration configuration) {
62+
Validate.isTrue(configuration.tlsTrustManagersProvider() == null || !configuration.trustAllCertificates(),
63+
"A TlsTrustManagerProvider can't be provided if TrustAllCertificates is also set");
64+
65+
if (configuration.tlsTrustManagersProvider() != null) {
66+
return StaticTrustManagerFactory.create(configuration.tlsTrustManagersProvider().trustManagers());
67+
}
68+
69+
if (configuration.trustAllCertificates()) {
70+
log.warn(() -> "SSL Certificate verification is disabled. This is not a safe setting and should only be "
71+
+ "used for testing.");
72+
return InsecureTrustManagerFactory.INSTANCE;
73+
}
74+
75+
return null;
76+
}
77+
78+
private KeyManagerFactory getKeyManager(NettyConfiguration configuration) {
79+
if (configuration.tlsKeyManagersProvider() != null) {
80+
KeyManager[] keyManagers = configuration.tlsKeyManagersProvider().keyManagers();
81+
if (keyManagers != null) {
82+
return StaticKeyManagerFactory.create(keyManagers);
83+
}
84+
}
85+
return null;
86+
}
87+
}

0 commit comments

Comments
 (0)