Skip to content

Commit 5c6cdb2

Browse files
authored
Replace deprecated SSLConnectionSocketFactory with recommended API (#6281)
* Replace deprecated SSLConnectionSocketFactory with recommended API * Fixed checkstyle issues * Changed name tlsSocketStrategy on builder * Removed warning log * added more test cases * updated after review * Added ConnectionSocketFactory to Apache5Client builder same as Apache4 * handled PR comments * Removed unused classes after moving to SSL sockets
1 parent e88d20d commit 5c6cdb2

File tree

10 files changed

+256
-405
lines changed

10 files changed

+256
-405
lines changed

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

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
import org.apache.hc.client5.http.routing.RoutingSupport;
5959
import org.apache.hc.client5.http.ssl.DefaultHostnameVerifier;
6060
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
61-
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
61+
import org.apache.hc.client5.http.ssl.TlsSocketStrategy;
6262
import org.apache.hc.core5.http.ClassicHttpRequest;
6363
import org.apache.hc.core5.http.ClassicHttpResponse;
6464
import org.apache.hc.core5.http.Header;
@@ -467,12 +467,15 @@ public interface Builder extends SdkHttpClient.Builder<Apache5HttpClient.Builder
467467
Builder dnsResolver(DnsResolver dnsResolver);
468468

469469
/**
470-
* Configuration that defines a custom Socket factory. If set to a null value, a default factory is used.
471-
* <p>
472-
* When set to a non-null value, the use of a custom factory implies the configuration options TRUST_ALL_CERTIFICATES,
473-
* TLS_TRUST_MANAGERS_PROVIDER, and TLS_KEY_MANAGERS_PROVIDER are ignored.
470+
* Configure a custom TLS strategy for SSL/TLS connections.
471+
* This is the preferred method over the ConnectionSocketFactory.
472+
*
473+
* @param tlsSocketStrategy The TLS strategy to use for upgrading connections to TLS.
474+
* If null, default TLS configuration will be used.
475+
* @return This builder for method chaining
476+
474477
*/
475-
Builder socketFactory(SSLConnectionSocketFactory socketFactory);
478+
Builder tlsSocketStrategy(TlsSocketStrategy tlsSocketStrategy);
476479

477480
/**
478481
* Configuration that defines an HTTP route planner that computes the route an HTTP request should take.
@@ -530,7 +533,7 @@ private static final class DefaultBuilder implements Builder {
530533
private HttpRoutePlanner httpRoutePlanner;
531534
private CredentialsProvider credentialsProvider;
532535
private DnsResolver dnsResolver;
533-
private SSLConnectionSocketFactory socketFactory;
536+
private TlsSocketStrategy tlsStrategy;
534537

535538
private DefaultBuilder() {
536539
}
@@ -652,15 +655,11 @@ public void setDnsResolver(DnsResolver dnsResolver) {
652655
}
653656

654657
@Override
655-
public Builder socketFactory(SSLConnectionSocketFactory socketFactory) {
656-
this.socketFactory = socketFactory;
658+
public Builder tlsSocketStrategy(TlsSocketStrategy tlsSocketStrategy) {
659+
this.tlsStrategy = tlsSocketStrategy;
657660
return this;
658661
}
659662

660-
public void setSocketFactory(SSLConnectionSocketFactory socketFactory) {
661-
socketFactory(socketFactory);
662-
}
663-
664663
@Override
665664
public Builder httpRoutePlanner(HttpRoutePlanner httpRoutePlanner) {
666665
this.httpRoutePlanner = httpRoutePlanner;
@@ -734,13 +733,13 @@ public SdkHttpClient buildWithDefaults(AttributeMap serviceDefaults) {
734733
private static class ApacheConnectionManagerFactory {
735734

736735
public PoolingHttpClientConnectionManager create(Apache5HttpClient.DefaultBuilder configuration,
737-
AttributeMap standardOptions) {
738-
// TODO : Deprecated method needs to be removed with new replacements
739-
SSLConnectionSocketFactory sslsf = getPreferredSocketFactory(configuration, standardOptions);
736+
AttributeMap standardOptions) {
737+
738+
TlsSocketStrategy tlsStrategy = getPreferredTlsStrategy(configuration, standardOptions);
740739

741740
PoolingHttpClientConnectionManagerBuilder builder =
742741
PoolingHttpClientConnectionManagerBuilder.create()
743-
.setSSLSocketFactory(sslsf)
742+
.setTlsSocketStrategy(tlsStrategy)
744743
.setSchemePortResolver(DefaultSchemePortResolver.INSTANCE)
745744
.setDnsResolver(configuration.dnsResolver);
746745
builder.setMaxConnPerRoute(standardOptions.get(SdkHttpConfigurationOption.MAX_CONNECTIONS));
@@ -765,11 +764,13 @@ private static ConnectionConfig getConnectionConfig(AttributeMap standardOptions
765764
return connectionConfigBuilder.build();
766765
}
767766

768-
private SSLConnectionSocketFactory getPreferredSocketFactory(Apache5HttpClient.DefaultBuilder configuration,
769-
AttributeMap standardOptions) {
770-
return Optional.ofNullable(configuration.socketFactory)
771-
.orElseGet(() -> new SdkTlsSocketFactory(getSslContext(standardOptions),
772-
getHostNameVerifier(standardOptions)));
767+
private TlsSocketStrategy getPreferredTlsStrategy(Apache5HttpClient.DefaultBuilder configuration,
768+
AttributeMap standardOptions) {
769+
if (configuration.tlsStrategy != null) {
770+
return configuration.tlsStrategy;
771+
}
772+
return new SdkTlsSocketFactory(getSslContext(standardOptions),
773+
getHostNameVerifier(standardOptions));
773774
}
774775

775776

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 & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,50 @@
1616
package software.amazon.awssdk.http.apache5.internal.conn;
1717

1818
import java.io.IOException;
19-
import java.net.InetSocketAddress;
2019
import java.net.Socket;
2120
import java.util.Arrays;
2221
import javax.net.ssl.HostnameVerifier;
2322
import javax.net.ssl.SSLContext;
2423
import javax.net.ssl.SSLSocket;
25-
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
26-
import org.apache.hc.core5.http.HttpHost;
24+
import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
2725
import org.apache.hc.core5.http.protocol.HttpContext;
28-
import org.apache.hc.core5.util.TimeValue;
2926
import software.amazon.awssdk.annotations.SdkInternalApi;
30-
import software.amazon.awssdk.http.apache5.internal.net.SdkSocket;
27+
import software.amazon.awssdk.http.apache5.internal.net.SdkSslSocket;
3128
import software.amazon.awssdk.utils.Logger;
3229

3330
@SdkInternalApi
34-
public class SdkTlsSocketFactory extends SSLConnectionSocketFactory {
31+
public class SdkTlsSocketFactory extends DefaultClientTlsStrategy {
3532

3633
private static final Logger log = Logger.loggerFor(SdkTlsSocketFactory.class);
3734

3835
public SdkTlsSocketFactory(SSLContext sslContext, HostnameVerifier hostnameVerifier) {
3936
super(sslContext, hostnameVerifier);
4037
if (sslContext == null) {
4138
throw new IllegalArgumentException(
42-
"sslContext must not be null. " + "Use SSLContext.getDefault() if you are unsure.");
39+
"sslContext must not be null. Use SSLContext.getDefault() if you are unsure.");
4340
}
4441
}
4542

4643
@Override
47-
protected final void prepareSocket(SSLSocket socket) {
44+
protected void initializeSocket(SSLSocket socket) {
45+
super.initializeSocket(socket);
4846
log.debug(() -> String.format("socket.getSupportedProtocols(): %s, socket.getEnabledProtocols(): %s",
4947
Arrays.toString(socket.getSupportedProtocols()),
5048
Arrays.toString(socket.getEnabledProtocols())));
5149
}
5250

5351
@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()));
52+
public SSLSocket upgrade(Socket socket,
53+
String target,
54+
int port,
55+
Object attachment,
56+
HttpContext context) throws IOException {
57+
log.trace(() -> String.format("Upgrading socket to TLS for %s:%s", target, port));
6158

62-
Socket connectSocket = super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
63-
return new SdkSocket(connectSocket);
59+
SSLSocket upgradedSocket = super.upgrade(socket, target, port, attachment, context);
60+
61+
// Wrap the upgraded SSLSocket in SdkSSLSocket for logging
62+
return new SdkSslSocket(upgradedSocket);
6463
}
64+
6565
}

0 commit comments

Comments
 (0)