Skip to content

Commit 724e1b2

Browse files
committed
Replace deprecated SSLConnectionSocketFactory with recommended API
1 parent dd3571f commit 724e1b2

File tree

9 files changed

+215
-48
lines changed

9 files changed

+215
-48
lines changed

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/Apache5HttpClient.java

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.apache.hc.client5.http.ssl.DefaultHostnameVerifier;
5757
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
5858
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
59+
import org.apache.hc.client5.http.ssl.TlsSocketStrategy;
5960
import org.apache.hc.core5.http.ClassicHttpResponse;
6061
import org.apache.hc.core5.http.Header;
6162
import org.apache.hc.core5.http.HttpEntity;
@@ -88,6 +89,7 @@
8889
import software.amazon.awssdk.http.apache5.internal.SdkProxyRoutePlanner;
8990
import software.amazon.awssdk.http.apache5.internal.conn.ClientConnectionManagerFactory;
9091
import software.amazon.awssdk.http.apache5.internal.conn.IdleConnectionReaper;
92+
import software.amazon.awssdk.http.apache5.internal.conn.SslSocketFactoryToTlsStrategyAdapter;
9193
import software.amazon.awssdk.http.apache5.internal.conn.SdkConnectionKeepAliveStrategy;
9294
import software.amazon.awssdk.http.apache5.internal.conn.SdkTlsSocketFactory;
9395
import software.amazon.awssdk.http.apache5.internal.impl.Apache5HttpRequestFactory;
@@ -457,8 +459,12 @@ public interface Builder extends SdkHttpClient.Builder<Apache5HttpClient.Builder
457459
* When set to a non-null value, the use of a custom factory implies the configuration options TRUST_ALL_CERTIFICATES,
458460
* TLS_TRUST_MANAGERS_PROVIDER, and TLS_KEY_MANAGERS_PROVIDER are ignored.
459461
*/
462+
@Deprecated
460463
Builder socketFactory(SSLConnectionSocketFactory socketFactory);
461464

465+
466+
Builder tlsStrategy(TlsSocketStrategy tlsStrategy);
467+
462468
/**
463469
* Configuration that defines an HTTP route planner that computes the route an HTTP request should take.
464470
* May not be used in conjunction with {@link #proxyConfiguration(ProxyConfiguration)}.
@@ -515,7 +521,8 @@ private static final class DefaultBuilder implements Builder {
515521
private HttpRoutePlanner httpRoutePlanner;
516522
private CredentialsProvider credentialsProvider;
517523
private DnsResolver dnsResolver;
518-
private SSLConnectionSocketFactory socketFactory;
524+
private SSLConnectionSocketFactory legacySocketFactory;
525+
private TlsSocketStrategy tlsStrategy;
519526

520527
private DefaultBuilder() {
521528
}
@@ -638,12 +645,23 @@ public void setDnsResolver(DnsResolver dnsResolver) {
638645

639646
@Override
640647
public Builder socketFactory(SSLConnectionSocketFactory socketFactory) {
641-
this.socketFactory = socketFactory;
648+
log.warn(() -> "SSLConnectionSocketFactory is deprecated. Consider migrating to tlsStrategy().");
649+
this.legacySocketFactory = socketFactory;
650+
this.tlsStrategy = null; // Clear any previously set strategy
642651
return this;
643652
}
644653

645-
public void setSocketFactory(SSLConnectionSocketFactory socketFactory) {
646-
socketFactory(socketFactory);
654+
@Override
655+
public Builder tlsStrategy(TlsSocketStrategy tlsStrategy) {
656+
this.tlsStrategy = tlsStrategy;
657+
this.legacySocketFactory = null; // Clear any legacy factory
658+
return this;
659+
}
660+
661+
662+
663+
public void setLegacySocketFactory(SSLConnectionSocketFactory legacySocketFactory) {
664+
socketFactory(legacySocketFactory);
647665
}
648666

649667
@Override
@@ -714,20 +732,36 @@ public SdkHttpClient buildWithDefaults(AttributeMap serviceDefaults) {
714732
SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS);
715733
return new Apache5HttpClient(this, resolvedOptions);
716734
}
735+
736+
// Internal method to get the effective TLS strategy
737+
738+
TlsSocketStrategy getEffectiveTlsStrategy() {
739+
if (tlsStrategy != null) {
740+
return tlsStrategy;
741+
}
742+
if (legacySocketFactory != null) {
743+
return new SslSocketFactoryToTlsStrategyAdapter(legacySocketFactory);
744+
}
745+
return null;
746+
}
747+
748+
717749
}
718750

719751
private static class ApacheConnectionManagerFactory {
720752

721753
public PoolingHttpClientConnectionManager create(Apache5HttpClient.DefaultBuilder configuration,
722-
AttributeMap standardOptions) {
723-
// TODO : Deprecated method needs to be removed with new replacements
724-
SSLConnectionSocketFactory sslsf = getPreferredSocketFactory(configuration, standardOptions);
754+
AttributeMap standardOptions) {
755+
756+
TlsSocketStrategy tlsStrategy = getPreferredTlsStrategy(configuration, standardOptions);
725757

726758
PoolingHttpClientConnectionManagerBuilder builder =
727759
PoolingHttpClientConnectionManagerBuilder.create()
728-
.setSSLSocketFactory(sslsf)
760+
.setTlsSocketStrategy(tlsStrategy)
729761
.setSchemePortResolver(DefaultSchemePortResolver.INSTANCE)
730762
.setDnsResolver(configuration.dnsResolver);
763+
764+
731765
Duration connectionTtl = standardOptions.get(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE);
732766
if (!connectionTtl.isZero()) {
733767
// Skip TTL=0 to maintain backward compatibility (infinite in 4.x vs immediate expiration in 5.x)
@@ -739,11 +773,15 @@ public PoolingHttpClientConnectionManager create(Apache5HttpClient.DefaultBuilde
739773
return builder.build();
740774
}
741775

742-
private SSLConnectionSocketFactory getPreferredSocketFactory(Apache5HttpClient.DefaultBuilder configuration,
743-
AttributeMap standardOptions) {
744-
return Optional.ofNullable(configuration.socketFactory)
745-
.orElseGet(() -> new SdkTlsSocketFactory(getSslContext(standardOptions),
746-
getHostNameVerifier(standardOptions)));
776+
private TlsSocketStrategy getPreferredTlsStrategy(Apache5HttpClient.DefaultBuilder configuration,
777+
AttributeMap standardOptions) {
778+
// Use the effective strategy which handles both legacy and new approaches
779+
TlsSocketStrategy configuredStrategy = configuration.getEffectiveTlsStrategy();
780+
if (configuredStrategy != null) {
781+
return configuredStrategy;
782+
}
783+
return new SdkTlsSocketFactory(getSslContext(standardOptions),
784+
getHostNameVerifier(standardOptions));
747785
}
748786

749787

@@ -815,6 +853,7 @@ private SocketConfig buildSocketConfig(AttributeMap standardOptions) {
815853
.build();
816854
}
817855

856+
818857
}
819858

820859
private static class LocalAddressRoutePlanner extends DefaultRoutePlanner {

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/internal/conn/ClientConnectionManagerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static HttpClientConnectionManager wrap(HttpClientConnectionManager orig)
4949
/**
5050
* Further wraps {@link LeaseRequest} to capture performance metrics.
5151
*/
52-
private static class InstrumentedHttpClientConnectionManager extends DelegatingHttpClientConnectionManager {
52+
private static final class InstrumentedHttpClientConnectionManager extends DelegatingHttpClientConnectionManager {
5353

5454
private InstrumentedHttpClientConnectionManager(HttpClientConnectionManager delegate) {
5555
super(delegate);

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/internal/conn/ClientConnectionRequestFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static LeaseRequest wrap(LeaseRequest orig) {
5555
/**
5656
* Measures the latency of {@link LeaseRequest#get(Timeout)}.
5757
*/
58-
private static class InstrumentedConnectionRequest extends DelegatingConnectionRequest {
58+
private static final class InstrumentedConnectionRequest extends DelegatingConnectionRequest {
5959

6060
private InstrumentedConnectionRequest(LeaseRequest delegate) {
6161
super(delegate);

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/internal/conn/SdkTlsSocketFactory.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,49 @@
2222
import javax.net.ssl.HostnameVerifier;
2323
import javax.net.ssl.SSLContext;
2424
import javax.net.ssl.SSLSocket;
25+
import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
2526
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
2627
import org.apache.hc.core5.http.HttpHost;
2728
import org.apache.hc.core5.http.protocol.HttpContext;
2829
import org.apache.hc.core5.util.TimeValue;
2930
import software.amazon.awssdk.annotations.SdkInternalApi;
3031
import software.amazon.awssdk.http.apache5.internal.net.SdkSocket;
32+
import software.amazon.awssdk.http.apache5.internal.net.SdkSslSocket;
3133
import software.amazon.awssdk.utils.Logger;
3234

3335
@SdkInternalApi
34-
public class SdkTlsSocketFactory extends SSLConnectionSocketFactory {
36+
public class SdkTlsSocketFactory extends DefaultClientTlsStrategy {
3537

3638
private static final Logger log = Logger.loggerFor(SdkTlsSocketFactory.class);
3739

3840
public SdkTlsSocketFactory(SSLContext sslContext, HostnameVerifier hostnameVerifier) {
3941
super(sslContext, hostnameVerifier);
4042
if (sslContext == null) {
4143
throw new IllegalArgumentException(
42-
"sslContext must not be null. " + "Use SSLContext.getDefault() if you are unsure.");
44+
"sslContext must not be null. Use SSLContext.getDefault() if you are unsure.");
4345
}
4446
}
4547

4648
@Override
47-
protected final void prepareSocket(SSLSocket socket) {
49+
protected void initializeSocket(SSLSocket socket) {
50+
super.initializeSocket(socket);
4851
log.debug(() -> String.format("socket.getSupportedProtocols(): %s, socket.getEnabledProtocols(): %s",
4952
Arrays.toString(socket.getSupportedProtocols()),
5053
Arrays.toString(socket.getEnabledProtocols())));
5154
}
5255

5356
@Override
54-
public Socket connectSocket(TimeValue connectTimeout,
55-
Socket socket,
56-
HttpHost host,
57-
InetSocketAddress remoteAddress,
58-
InetSocketAddress localAddress,
59-
HttpContext context) throws IOException {
60-
log.trace(() -> String.format("Connecting to %s:%s", remoteAddress.getAddress(), remoteAddress.getPort()));
57+
public SSLSocket upgrade(Socket socket,
58+
String target,
59+
int port,
60+
Object attachment,
61+
HttpContext context) throws IOException {
62+
log.trace(() -> String.format("Upgrading socket to TLS for %s:%s", target, port));
6163

62-
Socket connectSocket = super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
63-
return new SdkSocket(connectSocket);
64+
SSLSocket upgradedSocket = super.upgrade(socket, target, port, attachment, context);
65+
66+
// Wrap the upgraded SSLSocket in SdkSSLSocket for logging
67+
return new SdkSslSocket(upgradedSocket);
6468
}
69+
6570
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.apache5.internal.conn;
17+
18+
import java.io.IOException;
19+
import java.net.Socket;
20+
import javax.net.ssl.SSLSocket;
21+
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
22+
import org.apache.hc.client5.http.ssl.TlsSocketStrategy;
23+
import org.apache.hc.core5.http.protocol.HttpContext;
24+
import software.amazon.awssdk.annotations.SdkInternalApi;
25+
26+
/**
27+
* Adapter to wrap legacy SSLConnectionSocketFactory as TlsSocketStrategy
28+
*/
29+
@SdkInternalApi
30+
public class SslSocketFactoryToTlsStrategyAdapter implements TlsSocketStrategy {
31+
32+
private final SSLConnectionSocketFactory legacySocketFactory;
33+
34+
public SslSocketFactoryToTlsStrategyAdapter(SSLConnectionSocketFactory legacySocketFactory) {
35+
this.legacySocketFactory = legacySocketFactory;
36+
}
37+
38+
@Override
39+
public SSLSocket upgrade(Socket socket,
40+
String target,
41+
int port,
42+
Object attachment,
43+
HttpContext context) throws IOException {
44+
return (SSLSocket) legacySocketFactory.createLayeredSocket(socket, target, port, context);
45+
}
46+
}

0 commit comments

Comments
 (0)