mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(elbv2): preserve TLS handshakes during certificate reloads (#6473)
* fix(elbv2): preserve TLS handshakes during certificate reloads Reuse the generation-aware TLS key manager for ELBv2 and publish fully loaded SNI snapshots without replacing the active SSL context. Preserve the previous snapshot on load failure. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> * fix(elbv2): cover TLS resumption and wildcard selection Verify TLS 1.2 resumption with unchanged material and fresh authentication after certificate replacement or removal. Separate the wildcard certificate from the default and document the full-handshake boundary. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> * fix(elbv2): document retained TLS sessions on reload Exercise stateful and stateless TLS 1.2 resumption with shared client contexts. Document the retained-session certificate boundary while preserving full-handshake refresh behavior. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> --------- Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
c517e4eb76
commit
2525e38179
11 files changed
+532
-102
No files matched your search
+1
-1
@@ -1,4 +1,4 @@
|
||||
package io.github.hectorvent.floci.services.iot;
|
||||
package io.github.hectorvent.floci.core.common.tls;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.Pem;
|
||||
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
|
||||
+29
-36
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.elbv2;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.CertificateReferenceResolver;
|
||||
import io.github.hectorvent.floci.core.common.CertificateReferenceResolver.CertificateMaterial;
|
||||
import io.github.hectorvent.floci.services.acm.AcmService;
|
||||
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
|
||||
import io.github.hectorvent.floci.services.acm.model.Certificate;
|
||||
@@ -11,6 +12,7 @@ import io.github.hectorvent.floci.services.acm.model.CertificateStatus;
|
||||
import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
|
||||
import io.github.hectorvent.floci.services.acm.model.ValidationMethod;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.testing.InlineMocks;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.arc.ClientProxy;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
@@ -18,12 +20,6 @@ import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.Response;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.http.HttpServerOptions;
|
||||
import io.vertx.core.net.PemKeyCertOptions;
|
||||
import io.vertx.core.net.SSLOptions;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
@@ -1310,40 +1306,39 @@ class ElbV2HttpsDataPlaneIntegrationTest {
|
||||
@Order(Integer.MAX_VALUE - 1)
|
||||
void identicalCertificateUpdateRecoversAfterAnActualSslRefreshFailure() throws Exception {
|
||||
ElbV2DataPlane runtime = ClientProxy.unwrap(dataPlane);
|
||||
Vertx originalVertx = runtime.vertx;
|
||||
Vertx delegatingVertx = mock(Vertx.class, delegatesTo(originalVertx));
|
||||
AtomicBoolean failOnce = new AtomicBoolean(true);
|
||||
CertificateReferenceResolver originalResolver = runtime.certificateResolver;
|
||||
CertificateReferenceResolver wrapped = mock(CertificateReferenceResolver.class, delegatesTo(originalResolver));
|
||||
AtomicInteger updates = new AtomicInteger();
|
||||
AtomicReference<Future<Boolean>> failedUpdate = new AtomicReference<>();
|
||||
CountDownLatch failureCompleted = new CountDownLatch(1);
|
||||
doAnswer(creation -> {
|
||||
// Qualify Vert.x HttpServer to distinguish it from the fixture's JDK HttpServer.
|
||||
io.vertx.core.http.HttpServer actual = originalVertx.createHttpServer(
|
||||
(HttpServerOptions) creation.getArgument(0));
|
||||
io.vertx.core.http.HttpServer wrapped = mock(io.vertx.core.http.HttpServer.class, delegatesTo(actual));
|
||||
doAnswer(update -> {
|
||||
updates.incrementAndGet();
|
||||
if (failOnce.compareAndSet(true, false)) {
|
||||
SSLOptions invalid = new SSLOptions().setKeyCertOptions(new PemKeyCertOptions()
|
||||
.addKeyValue(Buffer.buffer("not a PEM private key"))
|
||||
.addCertValue(Buffer.buffer("not a PEM certificate")));
|
||||
Future<Boolean> failure = actual.updateSSLOptions(invalid, true);
|
||||
failedUpdate.set(failure);
|
||||
failure.onComplete(ignored -> failureCompleted.countDown());
|
||||
return failure;
|
||||
AtomicReference<String> replacementArn = new AtomicReference<>();
|
||||
AtomicReference<CertificateMaterial> failedMaterial = new AtomicReference<>();
|
||||
doAnswer(call -> {
|
||||
CertificateMaterial material = originalResolver.publishedMaterial(
|
||||
call.getArgument(0), call.getArgument(1), call.getArgument(2));
|
||||
if (material.arn().equals(replacementArn.get())) {
|
||||
int read = updates.incrementAndGet();
|
||||
if (read == 1) {
|
||||
CertificateMaterial invalid = InlineMocks.inlineSpy(material);
|
||||
// Let pair validation succeed, then fail the key-manager load.
|
||||
AtomicInteger keyReads = new AtomicInteger();
|
||||
doAnswer(ignored -> keyReads.incrementAndGet() == 1 ? material.privateKey() : "not a PEM private key")
|
||||
.when(invalid).privateKey();
|
||||
failedMaterial.set(invalid);
|
||||
}
|
||||
return actual.updateSSLOptions((SSLOptions) update.getArgument(0), true);
|
||||
}).when(wrapped).updateSSLOptions(any(SSLOptions.class), eq(true));
|
||||
return wrapped;
|
||||
}).when(delegatingVertx).createHttpServer(any(HttpServerOptions.class));
|
||||
if (read <= 2) {
|
||||
return failedMaterial.get();
|
||||
}
|
||||
}
|
||||
return material;
|
||||
}).when(wrapped).publishedMaterial(anyString(), anyString(), anyString());
|
||||
LoadBalancerRef loadBalancer = createLoadBalancer("https-refresh-retry");
|
||||
Certificate original = null;
|
||||
Certificate replacement = null;
|
||||
String listener = null;
|
||||
try {
|
||||
runtime.vertx = delegatingVertx;
|
||||
runtime.certificateResolver = wrapped;
|
||||
original = requestCertificate(loadBalancer.dnsName());
|
||||
replacement = requestCertificate(loadBalancer.dnsName());
|
||||
replacementArn.set(replacement.getArn());
|
||||
assertNotEquals(fingerprint(parseCertificate(original.getCertificateBody())),
|
||||
fingerprint(parseCertificate(replacement.getCertificateBody())));
|
||||
listener = createFixedResponseListener(loadBalancer.arn(), "HTTPS", HTTPS_PORT,
|
||||
@@ -1355,17 +1350,15 @@ class ElbV2HttpsDataPlaneIntegrationTest {
|
||||
.formParam("Certificates.member.1.CertificateArn", replacement.getArn())
|
||||
.when().post("/").then().statusCode(200);
|
||||
if (attempt == 0) {
|
||||
assertTrue(failureCompleted.await(5, TimeUnit.SECONDS));
|
||||
assertNotNull(failedUpdate.get());
|
||||
assertTrue(failedUpdate.get().failed(), "malformed PEM must fail the real SSL update");
|
||||
assertEquals(2, updates.get(), "the failed snapshot must pass the publication recheck");
|
||||
awaitHttpsMaterial(loadBalancer.dnsName(), 200, "retry", original, original, replacement);
|
||||
} else {
|
||||
awaitHttpsMaterial(loadBalancer.dnsName(), 200, "retry", replacement, original, replacement);
|
||||
}
|
||||
}
|
||||
assertEquals(2, updates.get(), "identical API retry must retry the real server update");
|
||||
assertEquals(4, updates.get(), "both API attempts must resolve and recheck their certificate snapshot");
|
||||
} finally {
|
||||
runtime.vertx = originalVertx;
|
||||
runtime.certificateResolver = originalResolver;
|
||||
String ownedListener = listener;
|
||||
Certificate ownedOriginal = original;
|
||||
Certificate ownedReplacement = replacement;
|
||||
|
||||
+20
@@ -1,9 +1,16 @@
|
||||
package io.github.hectorvent.floci.services.elbv2;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.tls.ReloadingKeyManager;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.Listener;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.Rule;
|
||||
import io.vertx.core.http.HttpServerOptions;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
|
||||
import javax.net.ssl.ExtendedSSLSession;
|
||||
import javax.net.ssl.SNIHostName;
|
||||
import javax.net.ssl.SSLEngine;
|
||||
import javax.net.ssl.X509ExtendedKeyManager;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.List;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.Function;
|
||||
@@ -12,10 +19,23 @@ import java.util.function.Supplier;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
final class ElbV2PublicationTestSupport {
|
||||
private ElbV2PublicationTestSupport() { }
|
||||
|
||||
static X509Certificate servedCertificate(HttpServerOptions options, String host) {
|
||||
X509ExtendedKeyManager manager = (X509ExtendedKeyManager)
|
||||
ReloadingKeyManager.keyManagerOf(null, options.getKeyCertOptions());
|
||||
SSLEngine engine = mock(SSLEngine.class);
|
||||
ExtendedSSLSession session = mock(ExtendedSSLSession.class);
|
||||
when(engine.getHandshakeSession()).thenReturn(session);
|
||||
when(session.getRequestedServerNames()).thenReturn(List.of(new SNIHostName(host)));
|
||||
String alias = manager.chooseEngineServerAlias("RSA", null, engine);
|
||||
return manager.getCertificateChain(alias)[0];
|
||||
}
|
||||
|
||||
static void publishRuntime(ElbV2DataPlane plane, Listener listener, String region,
|
||||
Integer previousPort, List<Rule> rules) {
|
||||
while (!plane.publishListener(listener, region, previousPort, () -> {
|
||||
|
||||
+30
-33
@@ -4,12 +4,14 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.TlsProxyServer;
|
||||
import io.github.hectorvent.floci.core.common.CertificateReferenceResolver;
|
||||
import io.github.hectorvent.floci.core.common.CertificateReferenceResolver.CertificateMaterial;
|
||||
import io.github.hectorvent.floci.core.common.Pem;
|
||||
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
|
||||
import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.Action;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.Listener;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.LoadBalancer;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.Rule;
|
||||
import io.github.hectorvent.floci.testing.InlineMocks;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.core.Promise;
|
||||
@@ -18,7 +20,6 @@ import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.core.http.HttpServerOptions;
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import io.vertx.core.http.HttpServerResponse;
|
||||
import io.vertx.core.net.PemKeyCertOptions;
|
||||
import io.vertx.core.net.SSLOptions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -26,14 +27,15 @@ import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.IntConsumer;
|
||||
|
||||
import static io.github.hectorvent.floci.services.elbv2.ElbV2PublicationTestSupport.publishRuntime;
|
||||
import static io.github.hectorvent.floci.services.elbv2.ElbV2PublicationTestSupport.removeRuntime;
|
||||
import static io.github.hectorvent.floci.services.elbv2.ElbV2PublicationTestSupport.servedCertificate;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -161,7 +163,7 @@ class ElbV2ReservedPortTest {
|
||||
assertFalse(dataPlane.isReservedByFloci(8080));
|
||||
}
|
||||
@Test
|
||||
void identicalRestartRetriesAFailedTlsRefresh() {
|
||||
void identicalRestartRetriesAFailedTlsRefresh() throws Exception {
|
||||
ElbV2DataPlane dataPlane = dataPlaneWith(4566, false, 0);
|
||||
EmulatorConfig.ElbV2ServiceConfig elbv2 = mock(EmulatorConfig.ElbV2ServiceConfig.class);
|
||||
when(elbv2.mock()).thenReturn(false);
|
||||
@@ -175,9 +177,6 @@ class ElbV2ReservedPortTest {
|
||||
when(server.requestHandler(any())).thenReturn(server);
|
||||
when(server.listen()).thenReturn(Future.succeededFuture(server));
|
||||
when(server.close()).thenReturn(Future.succeededFuture());
|
||||
when(server.updateSSLOptions(any(SSLOptions.class), eq(true)))
|
||||
.thenReturn(Future.failedFuture(new IllegalStateException("controlled TLS refresh failure")),
|
||||
Future.succeededFuture(true));
|
||||
when(dataPlane.vertx.createHttpServer(any(HttpServerOptions.class))).thenReturn(server);
|
||||
String region = "us-west-2";
|
||||
String owner = "arn:aws:elasticloadbalancing:us-west-2:000000000000:listener/app/lb/1/2";
|
||||
@@ -199,29 +198,30 @@ class ElbV2ReservedPortTest {
|
||||
CertificateMaterial first = new CertificateMaterial(certificateArn, old.certificatePem(), old.privateKeyPem(), null);
|
||||
CertificateMaterial next = new CertificateMaterial(certificateArn,
|
||||
replacement.certificatePem(), replacement.privateKeyPem(), null);
|
||||
when(dataPlane.certificateResolver.publishedMaterial(owner, region, certificateArn)).thenReturn(first, next, next);
|
||||
CertificateMaterial invalid = InlineMocks.inlineSpy(next);
|
||||
// Let pair validation succeed, then fail the key-manager load.
|
||||
AtomicInteger keyReads = new AtomicInteger();
|
||||
doAnswer(ignored -> keyReads.incrementAndGet() == 1 ? replacement.privateKeyPem() : "not a PEM private key")
|
||||
.when(invalid).privateKey();
|
||||
when(dataPlane.certificateResolver.publishedMaterial(owner, region, certificateArn)).thenReturn(first, first, invalid, invalid, next, next);
|
||||
try {
|
||||
publishRuntime(dataPlane, listener, region, listener.getPort(), List.of());
|
||||
ArgumentCaptor<HttpServerOptions> options = ArgumentCaptor.forClass(HttpServerOptions.class);
|
||||
verify(dataPlane.vertx).createHttpServer(options.capture());
|
||||
publishRuntime(dataPlane, listener, region, listener.getPort(), List.of());
|
||||
verify(server).updateSSLOptions(any(SSLOptions.class), eq(true));
|
||||
assertEquals(Pem.parseCertificate(old.certificatePem()),
|
||||
servedCertificate(options.getValue(), "tls.example.test"));
|
||||
publishRuntime(dataPlane, listener, region, listener.getPort(), List.of());
|
||||
ArgumentCaptor<SSLOptions> options = ArgumentCaptor.forClass(SSLOptions.class);
|
||||
verify(server, times(2)).updateSSLOptions(options.capture(), eq(true));
|
||||
for (SSLOptions attempt : options.getAllValues()) {
|
||||
PemKeyCertOptions material = assertInstanceOf(PemKeyCertOptions.class, attempt.getKeyCertOptions());
|
||||
assertEquals(1, material.getCertValues().size());
|
||||
assertEquals(replacement.certificatePem().stripTrailing() + "\n",
|
||||
material.getCertValues().getFirst().toString());
|
||||
assertEquals(1, material.getKeyValues().size());
|
||||
assertEquals(replacement.privateKeyPem(), material.getKeyValues().getFirst().toString());
|
||||
}
|
||||
assertEquals(Pem.parseCertificate(replacement.certificatePem()),
|
||||
servedCertificate(options.getValue(), "tls.example.test"));
|
||||
verify(server, never()).updateSSLOptions(any(SSLOptions.class), eq(true));
|
||||
} finally {
|
||||
dataPlane.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void movingSecureListenerRefreshesTheSurvivingOldPort() {
|
||||
void movingSecureListenerRefreshesTheSurvivingOldPort() throws Exception {
|
||||
ElbV2DataPlane dataPlane = dataPlaneWith(4566, false, 0);
|
||||
EmulatorConfig.ElbV2ServiceConfig elbv2 = mock(EmulatorConfig.ElbV2ServiceConfig.class);
|
||||
when(elbv2.mock()).thenReturn(false);
|
||||
@@ -238,8 +238,6 @@ class ElbV2ReservedPortTest {
|
||||
when(server.listen()).thenReturn(Future.succeededFuture(server));
|
||||
when(server.close()).thenReturn(Future.succeededFuture());
|
||||
}
|
||||
when(oldServer.updateSSLOptions(any(SSLOptions.class), eq(true)))
|
||||
.thenReturn(Future.succeededFuture(true));
|
||||
when(dataPlane.vertx.createHttpServer(any(HttpServerOptions.class)))
|
||||
.thenAnswer(invocation -> ((HttpServerOptions) invocation.getArgument(0)).getPort() == 8443
|
||||
? oldServer : newServer);
|
||||
@@ -271,22 +269,21 @@ class ElbV2ReservedPortTest {
|
||||
try {
|
||||
publishRuntime(dataPlane, survivor, "us-west-2", survivor.getPort(), List.of());
|
||||
publishRuntime(dataPlane, moved, "us-west-2", moved.getPort(), List.of());
|
||||
ArgumentCaptor<SSLOptions> initial = ArgumentCaptor.forClass(SSLOptions.class);
|
||||
verify(oldServer).updateSSLOptions(initial.capture(), eq(true));
|
||||
assertEquals(2, assertInstanceOf(PemKeyCertOptions.class,
|
||||
initial.getValue().getKeyCertOptions()).getCertValues().size());
|
||||
ArgumentCaptor<HttpServerOptions> initial = ArgumentCaptor.forClass(HttpServerOptions.class);
|
||||
verify(dataPlane.vertx).createHttpServer(initial.capture());
|
||||
assertEquals(Pem.parseCertificate(survivorCertificate.certificatePem()),
|
||||
servedCertificate(initial.getValue(), "survivor.example.test"));
|
||||
assertFalse(servedCertificate(initial.getValue(), "moved.example.test")
|
||||
.equals(Pem.parseCertificate(survivorCertificate.certificatePem())));
|
||||
|
||||
moved.setPort(9443);
|
||||
publishRuntime(dataPlane, moved, "us-west-2", moved.getPort(), List.of());
|
||||
|
||||
ArgumentCaptor<SSLOptions> refreshed = ArgumentCaptor.forClass(SSLOptions.class);
|
||||
verify(oldServer, times(2)).updateSSLOptions(refreshed.capture(), eq(true));
|
||||
PemKeyCertOptions retained = assertInstanceOf(PemKeyCertOptions.class,
|
||||
refreshed.getAllValues().getLast().getKeyCertOptions());
|
||||
assertEquals(List.of(survivorCertificate.certificatePem().stripTrailing() + "\n"),
|
||||
retained.getCertValues().stream().map(value -> value.toString()).toList());
|
||||
assertEquals(List.of(survivorCertificate.privateKeyPem()),
|
||||
retained.getKeyValues().stream().map(value -> value.toString()).toList());
|
||||
assertEquals(Pem.parseCertificate(survivorCertificate.certificatePem()),
|
||||
servedCertificate(initial.getValue(), "survivor.example.test"));
|
||||
assertEquals(Pem.parseCertificate(survivorCertificate.certificatePem()),
|
||||
servedCertificate(initial.getValue(), "moved.example.test"),
|
||||
"the old port must fall back to its survivor after the other certificate moves away");
|
||||
verify(newServer).listen();
|
||||
verify(oldServer, never()).close();
|
||||
} finally {
|
||||
|
||||
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.TlsProxyServer;
|
||||
import io.github.hectorvent.floci.core.common.CertificateReferenceResolver;
|
||||
import io.github.hectorvent.floci.core.common.CertificateReferenceResolver.CertificateMaterial;
|
||||
import io.github.hectorvent.floci.core.common.Pem;
|
||||
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
|
||||
import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.Action;
|
||||
@@ -18,8 +19,6 @@ import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.core.http.HttpServerOptions;
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import io.vertx.core.http.HttpServerResponse;
|
||||
import io.vertx.core.net.PemKeyCertOptions;
|
||||
import io.vertx.core.net.SSLOptions;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@@ -37,13 +36,11 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static io.github.hectorvent.floci.services.elbv2.ElbV2PublicationTestSupport.publishRuntime;
|
||||
import static io.github.hectorvent.floci.services.elbv2.ElbV2PublicationTestSupport.removeRuntime;
|
||||
import static io.github.hectorvent.floci.services.elbv2.ElbV2PublicationTestSupport.servedCertificate;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -80,7 +77,6 @@ class ElbV2TlsCohortTest {
|
||||
when(server.requestHandler(any())).thenReturn(server);
|
||||
when(server.listen()).thenReturn(Future.succeededFuture(server));
|
||||
when(server.close()).thenReturn(Future.succeededFuture());
|
||||
when(server.updateSSLOptions(any(SSLOptions.class), eq(true))).thenReturn(Future.succeededFuture(true));
|
||||
CertificateGenerator generator = new CertificateGenerator();
|
||||
CertificateMaterial seed = material(generator, certificateArn, "seed.example.test");
|
||||
CertificateMaterial first = InlineMocks.inlineSpy(material(generator, certificateArn, "old.example.test"));
|
||||
@@ -133,6 +129,8 @@ class ElbV2TlsCohortTest {
|
||||
try {
|
||||
publishRuntime(dataPlane, left, region, left.getPort(), List.of(fixedRule("left")));
|
||||
publishRuntime(dataPlane, right, region, right.getPort(), List.of(fixedRule("right")));
|
||||
ArgumentCaptor<HttpServerOptions> options = ArgumentCaptor.forClass(HttpServerOptions.class);
|
||||
verify(dataPlane.vertx).createHttpServer(options.capture());
|
||||
ArgumentCaptor<Handler<HttpServerRequest>> handler = ArgumentCaptor.captor();
|
||||
verify(server).requestHandler(handler.capture());
|
||||
leftMaterial.set(first);
|
||||
@@ -171,19 +169,8 @@ class ElbV2TlsCohortTest {
|
||||
handler.getValue().handle(request);
|
||||
verify(response).setStatusCode(502);
|
||||
verify(response).end("No listener for host");
|
||||
ArgumentCaptor<SSLOptions> updates = ArgumentCaptor.forClass(SSLOptions.class);
|
||||
verify(server, atLeastOnce()).updateSSLOptions(updates.capture(), eq(true));
|
||||
List<SSLOptions> orderedUpdates = updates.getAllValues();
|
||||
PemKeyCertOptions preceding = assertInstanceOf(PemKeyCertOptions.class,
|
||||
orderedUpdates.get(orderedUpdates.size() - 2).getKeyCertOptions());
|
||||
assertEquals(List.of(first.privateKey()),
|
||||
preceding.getKeyValues().stream().map(value -> value.toString()).toList());
|
||||
PemKeyCertOptions finalOptions = assertInstanceOf(PemKeyCertOptions.class,
|
||||
orderedUpdates.getLast().getKeyCertOptions());
|
||||
assertEquals(List.of(replacement.privateKey()),
|
||||
finalOptions.getKeyValues().stream().map(value -> value.toString()).toList());
|
||||
assertEquals(List.of(replacement.certificateBody().stripTrailing() + "\n"),
|
||||
finalOptions.getCertValues().stream().map(value -> value.toString()).toList());
|
||||
assertEquals(Pem.parseCertificate(replacement.certificateBody()),
|
||||
servedCertificate(options.getValue(), alias));
|
||||
removeRuntime(dataPlane, right.getListenerArn(), right.getPort());
|
||||
HttpServerRequest survivingRequest = mock(HttpServerRequest.class);
|
||||
HttpServerResponse survivingResponse = mock(HttpServerResponse.class);
|
||||
|
||||
@@ -0,0 +1,304 @@
|
||||
package io.github.hectorvent.floci.services.elbv2;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.Pem;
|
||||
import io.github.hectorvent.floci.core.common.tls.ReloadingKeyManager;
|
||||
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
|
||||
import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.core.http.HttpServerOptions;
|
||||
import io.vertx.core.net.JdkSSLEngineOptions;
|
||||
import io.vertx.core.net.KeyCertOptions;
|
||||
import io.vertx.core.net.PemKeyCertOptions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.parallel.Isolated;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import javax.net.ssl.KeyManagerFactory;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
import javax.net.ssl.SSLParameters;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyStore;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@Isolated("Temporarily configures JSSE client session tickets")
|
||||
class ElbV2TlsKeyManagerTest {
|
||||
|
||||
private static final String HOST = "refresh.example.test";
|
||||
|
||||
@Test
|
||||
void verifiedHandshakesContinueWhileReplacementKeysAreLoading() throws Exception {
|
||||
CertificateGenerator.GeneratedCertificate first = certificate(HOST);
|
||||
CertificateGenerator.GeneratedCertificate second = certificate(HOST);
|
||||
Vertx vertx = Vertx.vertx();
|
||||
CountDownLatch entered = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
HttpServer server = null;
|
||||
try (ExecutorService executor = Executors.newSingleThreadExecutor()) {
|
||||
ReloadingKeyManager manager = new ReloadingKeyManager(new ElbV2TlsKeyManager(vertx, pem(first)));
|
||||
server = server(vertx, manager);
|
||||
assertEquals(Pem.parseCertificate(first.certificatePem()), handshake(server.actualPort(), HOST, first, second));
|
||||
|
||||
PemKeyCertOptions replacement = pem(second);
|
||||
KeyCertOptions paused = new KeyCertOptions() {
|
||||
@Override
|
||||
public KeyCertOptions copy() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyManagerFactory getKeyManagerFactory(Vertx runtime) throws Exception {
|
||||
entered.countDown();
|
||||
assertTrue(release.await(10, TimeUnit.SECONDS), "replacement key loading was not released");
|
||||
return replacement.getKeyManagerFactory(runtime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<String, KeyManagerFactory> keyManagerFactoryMapper(Vertx runtime) throws Exception {
|
||||
return replacement.keyManagerFactoryMapper(runtime);
|
||||
}
|
||||
};
|
||||
Future<?> update = executor.submit(() -> manager.reload(new ElbV2TlsKeyManager(vertx, paused)));
|
||||
try {
|
||||
assertTrue(entered.await(5, TimeUnit.SECONDS), "replacement key loading did not start");
|
||||
assertFalse(update.isDone());
|
||||
for (int i = 0; i < 3; i++) {
|
||||
assertEquals(Pem.parseCertificate(first.certificatePem()),
|
||||
handshake(server.actualPort(), HOST, first, second));
|
||||
}
|
||||
} finally {
|
||||
release.countDown();
|
||||
}
|
||||
update.get(10, TimeUnit.SECONDS);
|
||||
assertEquals(Pem.parseCertificate(second.certificatePem()), handshake(server.actualPort(), HOST, first, second));
|
||||
} finally {
|
||||
release.countDown();
|
||||
close(server, vertx);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedReplacementLeavesVerifiedHandshakesOnThePreviousCertificate() throws Exception {
|
||||
CertificateGenerator.GeneratedCertificate first = certificate(HOST);
|
||||
Vertx vertx = Vertx.vertx();
|
||||
HttpServer server = null;
|
||||
try {
|
||||
ReloadingKeyManager manager = new ReloadingKeyManager(new ElbV2TlsKeyManager(vertx, pem(first)));
|
||||
server = server(vertx, manager);
|
||||
PemKeyCertOptions invalid = new PemKeyCertOptions().addCertValue(Buffer.buffer("invalid"))
|
||||
.addKeyValue(Buffer.buffer("invalid"));
|
||||
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> manager.reload(new ElbV2TlsKeyManager(vertx, invalid)));
|
||||
|
||||
assertEquals(Pem.parseCertificate(first.certificatePem()), handshake(server.actualPort(), HOST, first));
|
||||
} finally {
|
||||
close(server, vertx);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void sniSelectsAdditionalCertificateAndReloadRemovesIt() throws Exception {
|
||||
CertificateGenerator.GeneratedCertificate first = certificate(HOST);
|
||||
CertificateGenerator.GeneratedCertificate additional = certificate("additional.example.test");
|
||||
Vertx vertx = Vertx.vertx();
|
||||
HttpServer server = null;
|
||||
try {
|
||||
ReloadingKeyManager manager = new ReloadingKeyManager(new ElbV2TlsKeyManager(vertx, pem(first)));
|
||||
server = server(vertx, manager);
|
||||
manager.reload(new ElbV2TlsKeyManager(vertx, pem(first, additional)));
|
||||
assertEquals(Pem.parseCertificate(additional.certificatePem()),
|
||||
handshake(server.actualPort(), "additional.example.test", first, additional));
|
||||
assertEquals(Pem.parseCertificate(first.certificatePem()), handshake(server.actualPort(), HOST, first, additional));
|
||||
|
||||
manager.reload(new ElbV2TlsKeyManager(vertx, pem(first)));
|
||||
int port = server.actualPort();
|
||||
assertThrows(SSLHandshakeException.class,
|
||||
() -> handshake(port, "additional.example.test", first, additional));
|
||||
assertEquals(Pem.parseCertificate(first.certificatePem()), handshake(port, HOST, first));
|
||||
} finally {
|
||||
close(server, vertx);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardAndMissingSniPreserveCertificateSelection() throws Exception {
|
||||
CertificateGenerator.GeneratedCertificate defaultCertificate = certificate("default.other.test");
|
||||
CertificateGenerator.GeneratedCertificate wildcard = certificate("*.example.test");
|
||||
Vertx vertx = Vertx.vertx();
|
||||
HttpServer server = null;
|
||||
try {
|
||||
ReloadingKeyManager manager = new ReloadingKeyManager(new ElbV2TlsKeyManager(vertx, pem(defaultCertificate, wildcard)));
|
||||
server = server(vertx, manager);
|
||||
assertEquals(Pem.parseCertificate(wildcard.certificatePem()),
|
||||
handshake(server.actualPort(), HOST, true, defaultCertificate, wildcard));
|
||||
assertEquals(Pem.parseCertificate(defaultCertificate.certificatePem()),
|
||||
handshake(server.actualPort(), "default.other.test", false, defaultCertificate, wildcard));
|
||||
} finally {
|
||||
close(server, vertx);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void reusedClientContextTracksCertificateReplacementAndRemoval(boolean sessionTickets) throws Exception {
|
||||
CertificateGenerator.GeneratedCertificate defaultCertificate = certificate("default.other.test");
|
||||
CertificateGenerator.GeneratedCertificate first = certificate(HOST);
|
||||
CertificateGenerator.GeneratedCertificate replacement = certificate(HOST);
|
||||
Vertx vertx = Vertx.vertx();
|
||||
HttpServer server = null;
|
||||
try {
|
||||
ReloadingKeyManager manager = new ReloadingKeyManager(
|
||||
new ElbV2TlsKeyManager(vertx, pem(first)));
|
||||
server = server(vertx, manager);
|
||||
int port = server.actualPort();
|
||||
SSLContext client = clientContextWithSessionTickets(sessionTickets, defaultCertificate, first, replacement);
|
||||
SSLSession initial = handshake(port, HOST, true, client);
|
||||
assertEquals("TLSv1.2", initial.getProtocol());
|
||||
assertTrue(initial.getId().length > 0);
|
||||
assertEquals(Pem.parseCertificate(first.certificatePem()), initial.getPeerCertificates()[0]);
|
||||
|
||||
SSLSession beforeReplacement = handshake(port, HOST, true, client);
|
||||
assertArrayEquals(initial.getId(), beforeReplacement.getId());
|
||||
|
||||
manager.reload(new ElbV2TlsKeyManager(vertx, pem(replacement)));
|
||||
SSLSession afterReplacement = handshake(port, HOST, true, client);
|
||||
assertEquals(!sessionTickets, Arrays.equals(initial.getId(), afterReplacement.getId()));
|
||||
CertificateGenerator.GeneratedCertificate expected = sessionTickets ? replacement : first;
|
||||
assertEquals(Pem.parseCertificate(expected.certificatePem()), afterReplacement.getPeerCertificates()[0]);
|
||||
SSLSession resumedReplacement = handshake(port, HOST, true, client);
|
||||
assertArrayEquals(afterReplacement.getId(), resumedReplacement.getId());
|
||||
assertEquals(Pem.parseCertificate(replacement.certificatePem()),
|
||||
handshake(port, HOST, defaultCertificate, first, replacement));
|
||||
|
||||
manager.reload(new ElbV2TlsKeyManager(vertx, pem(defaultCertificate)));
|
||||
if (!sessionTickets) {
|
||||
SSLSession afterRemoval = handshake(port, HOST, true, client);
|
||||
assertArrayEquals(initial.getId(), afterRemoval.getId());
|
||||
assertEquals(Pem.parseCertificate(first.certificatePem()), afterRemoval.getPeerCertificates()[0]);
|
||||
afterRemoval.invalidate();
|
||||
}
|
||||
assertThrows(SSLHandshakeException.class, () -> handshake(port, HOST, true, client));
|
||||
assertEquals(Pem.parseCertificate(defaultCertificate.certificatePem()),
|
||||
handshake(port, "default.other.test", defaultCertificate));
|
||||
} finally {
|
||||
close(server, vertx);
|
||||
}
|
||||
}
|
||||
|
||||
private static CertificateGenerator.GeneratedCertificate certificate(String host) {
|
||||
return new CertificateGenerator().generateSelfSignedCertificate(host, List.of(), KeyAlgorithm.RSA_2048);
|
||||
}
|
||||
|
||||
private static PemKeyCertOptions pem(CertificateGenerator.GeneratedCertificate... certificates) {
|
||||
PemKeyCertOptions options = new PemKeyCertOptions();
|
||||
for (CertificateGenerator.GeneratedCertificate certificate : certificates) {
|
||||
options.addKeyValue(Buffer.buffer(certificate.privateKeyPem()));
|
||||
options.addCertValue(Buffer.buffer(certificate.certificatePem()));
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
private static HttpServer server(Vertx vertx, ReloadingKeyManager manager) throws Exception {
|
||||
return vertx.createHttpServer(new HttpServerOptions().setHost("127.0.0.1").setPort(0)
|
||||
.setSsl(true).setSslEngineOptions(new JdkSSLEngineOptions())
|
||||
.setKeyCertOptions(KeyCertOptions.wrap(manager)))
|
||||
.requestHandler(request -> request.response().end("ok"))
|
||||
.listen().toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private static X509Certificate handshake(int port, String host,
|
||||
CertificateGenerator.GeneratedCertificate... trusted) throws Exception {
|
||||
return handshake(port, host, true, trusted);
|
||||
}
|
||||
|
||||
private static X509Certificate handshake(int port, String host, boolean sni,
|
||||
CertificateGenerator.GeneratedCertificate... trusted) throws Exception {
|
||||
SSLSession session = handshake(port, host, sni, clientContext("TLS", trusted));
|
||||
return (X509Certificate) session.getPeerCertificates()[0];
|
||||
}
|
||||
|
||||
private static SSLContext clientContextWithSessionTickets(boolean enabled,
|
||||
CertificateGenerator.GeneratedCertificate... trusted) throws Exception {
|
||||
String property = "jdk.tls.client.enableSessionTicketExtension";
|
||||
String previous = System.getProperty(property);
|
||||
try {
|
||||
System.setProperty(property, Boolean.toString(enabled));
|
||||
return clientContext("TLSv1.2", trusted);
|
||||
} finally {
|
||||
if (previous == null) {
|
||||
System.clearProperty(property);
|
||||
} else {
|
||||
System.setProperty(property, previous);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static SSLContext clientContext(String protocol,
|
||||
CertificateGenerator.GeneratedCertificate... trusted) throws Exception {
|
||||
KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||
store.load(null, null);
|
||||
for (int i = 0; i < trusted.length; i++) {
|
||||
store.setCertificateEntry(Integer.toString(i), Pem.parseCertificate(trusted[i].certificatePem()));
|
||||
}
|
||||
TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
||||
factory.init(store);
|
||||
SSLContext context = SSLContext.getInstance(protocol);
|
||||
context.init(null, factory.getTrustManagers(), null);
|
||||
return context;
|
||||
}
|
||||
|
||||
private static SSLSession handshake(int port, String host, boolean sni, SSLContext context) throws Exception {
|
||||
try (Socket transport = new Socket()) {
|
||||
transport.connect(new InetSocketAddress("127.0.0.1", port), 2000);
|
||||
try (SSLSocket socket = (SSLSocket) context.getSocketFactory().createSocket(transport, host, port, true)) {
|
||||
socket.setSoTimeout(2000);
|
||||
SSLParameters parameters = socket.getSSLParameters();
|
||||
parameters.setEndpointIdentificationAlgorithm("HTTPS");
|
||||
if (!sni) {
|
||||
parameters.setServerNames(List.of());
|
||||
}
|
||||
socket.setSSLParameters(parameters);
|
||||
socket.startHandshake();
|
||||
socket.getOutputStream().write(("GET / HTTP/1.1\r\nHost: " + host
|
||||
+ "\r\nConnection: close\r\n\r\n").getBytes(StandardCharsets.US_ASCII));
|
||||
String response = new String(socket.getInputStream().readAllBytes(), StandardCharsets.US_ASCII);
|
||||
assertTrue(response.startsWith("HTTP/1.1 200"));
|
||||
assertTrue(response.endsWith("ok"));
|
||||
return socket.getSession();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void close(HttpServer server, Vertx vertx) throws Exception {
|
||||
try {
|
||||
if (server != null) {
|
||||
server.close().toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
}
|
||||
} finally {
|
||||
vertx.close().toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user