fix(elasticache): terminate required TLS at cache proxies (#6227)

This commit is contained in:
jwoa95 authored and GitHub committed 2026-10-24 09:31:10 -04:00
1 parent b4c71b9e25
commit 346a7f4526
25 files changed
+979 -100

No files matched your search

@@ -170,13 +170,21 @@ import software.amazon.awssdk.services.wafv2.Wafv2Client;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.ArrayList;
@@ -388,6 +396,28 @@ public final class TestFixtures {
return ENDPOINT.getHost();
}
/**
* A TLS context that trusts only the CA Floci serves at {@code /_floci/ca.pem}, for a client
* that verifies an endpoint Floci signs, such as a DSQL cluster or an encrypted cache.
*/
public static SSLContext flociCaTrust() throws IOException, InterruptedException, GeneralSecurityException {
HttpResponse<String> ca = emulatorHttpClient().send(
HttpRequest.newBuilder(ENDPOINT.resolve("/_floci/ca.pem")).build(),
HttpResponse.BodyHandlers.ofString());
KeyStore trusted = KeyStore.getInstance(KeyStore.getDefaultType());
trusted.load(null, null);
int index = 0;
for (Certificate certificate : CertificateFactory.getInstance("X.509").generateCertificates(
new ByteArrayInputStream(ca.body().getBytes(StandardCharsets.US_ASCII)))) {
trusted.setCertificateEntry("floci-" + index++, certificate);
}
TrustManagerFactory trust = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trust.init(trusted);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, trust.getTrustManagers(), null);
return context;
}
// ============================================
// Lambda dispatch availability probe
// ============================================
@@ -16,21 +16,12 @@ import software.amazon.awssdk.services.dsql.model.GetClusterResponse;
import software.amazon.awssdk.services.dsql.model.ResourceNotFoundException;
import software.amazon.awssdk.services.dsql.model.ValidationException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
@@ -229,21 +220,7 @@ class DsqlCompatTest {
public ServerNameSocketFactory(Properties info) throws Exception {
this.serverName = info.getProperty(SERVER_NAME_PROPERTY);
HttpResponse<String> ca = TestFixtures.emulatorHttpClient().send(
HttpRequest.newBuilder(TestFixtures.endpoint().resolve("/_floci/ca.pem")).build(),
HttpResponse.BodyHandlers.ofString());
KeyStore trusted = KeyStore.getInstance(KeyStore.getDefaultType());
trusted.load(null, null);
int index = 0;
for (Certificate certificate : CertificateFactory.getInstance("X.509").generateCertificates(
new ByteArrayInputStream(ca.body().getBytes(StandardCharsets.US_ASCII)))) {
trusted.setCertificateEntry("floci-" + index++, certificate);
}
TrustManagerFactory trust = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trust.init(trusted);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, trust.getTrustManagers(), null);
this.delegate = context.getSocketFactory();
this.delegate = TestFixtures.flociCaTrust().getSocketFactory();
}
@Override
@@ -35,6 +35,8 @@ import software.amazon.awssdk.services.elasticache.model.UserAlreadyExistsExcept
import software.amazon.awssdk.services.elasticache.model.UserGroupNotFoundException;
import software.amazon.awssdk.services.elasticache.model.UserNotFoundException;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -447,9 +449,15 @@ class ElastiCacheTest {
Assumptions.assumeTrue(userCreated && userId != null, "User must exist from earlier ordered test");
}
private static Socket openSocket(int port) throws IOException {
Socket socket = new Socket(TestFixtures.proxyHost(), port);
/** The group has an auth token, so it is encrypted in transit and its endpoint takes TLS only. */
private static Socket openSocket(int port) throws Exception {
SSLSocket socket = (SSLSocket) TestFixtures.flociCaTrust().getSocketFactory()
.createSocket(TestFixtures.proxyHost(), port);
SSLParameters parameters = socket.getSSLParameters();
parameters.setEndpointIdentificationAlgorithm("HTTPS");
socket.setSSLParameters(parameters);
socket.setSoTimeout(5000);
socket.startHandshake();
return socket;
}