Light, fluffy, and always free - The AWS Local Emulator alternative
fix(elbv2): preserve TLS handshakes during certificate reloads (#6473)
Andre Kurait2525e38179b3
diff --git a/docs/services/elb.md b/docs/services/elb.mdindex 9cb7b50dd9..b1fd66a786 100644--- a/docs/services/elb.md+++ b/docs/services/elb.md@@ -89,7 +89,8 @@ Gateway Load Balancer listeners require omitted `Port` and `Protocol` fields on## Behavior Notes- HTTPS listeners terminate TLS using the configured ACM or IAM server certificates, with SNI certificate selection. When several load balancers share one local HTTPS port, clients must send SNI to identify the certificate they need; a connection without SNI cannot identify a particular load balancer before the TLS handshake.-- ACM certificates must be issued before attaching them. Reimporting an attached ACM certificate refreshes subsequent TLS connections; existing connections keep their negotiated certificate. ACM deletion and revocation are refused while a listener still references the certificate.+- ACM certificates must be issued before attaching them. Reimporting an attached ACM certificate refreshes subsequent full TLS handshakes; existing connections keep their negotiated certificate. Reload does not invalidate TLS session caches: a resumed session can retain its previously authenticated certificate after replacement or removal until the session expires or is invalidated. ACM deletion and revocation are refused while a listener still references the certificate.+- Certificate replacement keeps the previous key material available to new handshakes while the replacement loads. A failed reload leaves the previous certificate active.- Unusable saved listeners are retained for correction and logged individually so other saved listeners can start.- Listeners sharing a local port must use the same protocol. Requests select a load balancer by its DNS name, a hostname declared by its rules, or a certificate hostname; ambiguous hostnames return 502.- Load balancer, target group, listener, rule, and tag state is persisted through Floci storage and rebuilt on service startup.diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/ReloadingKeyManager.java b/src/main/java/io/github/hectorvent/floci/core/common/tls/ReloadingKeyManager.javasimilarity index 93%rename from src/main/java/io/github/hectorvent/floci/services/iot/ReloadingKeyManager.javarename to src/main/java/io/github/hectorvent/floci/core/common/tls/ReloadingKeyManager.javaindex 26911a31cd..629f598ada 100644--- a/src/main/java/io/github/hectorvent/floci/services/iot/ReloadingKeyManager.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/tls/ReloadingKeyManager.java@@ -1,4 +1,4 @@-package io.github.hectorvent.floci.services.iot;+package io.github.hectorvent.floci.core.common.tls;import io.vertx.core.Vertx;import io.vertx.core.net.KeyCertOptions;@@ -18,7 +18,7 @@ import java.util.List;import java.util.function.LongSupplier;/**- * Server key manager for the MQTT TLS listener that answers every handshake from the key material+ * Server key manager for TLS listeners that answers every handshake from the key material* it was last given, so a reissued server certificate is served without rebuilding the listener's* SSL context: Vert.x drops the connections it accepts while a context is being rebuilt, whereas* JSSE asks the key manager for an alias, then that alias's key and chain, on every handshake.@@ -29,7 +29,7 @@ import java.util.function.LongSupplier;* a handshake that began before any number of reloads still reads a matching key and chain.* Retired generations are dropped on the next reload after that window.*/-final class ReloadingKeyManager extends X509ExtendedKeyManager {+public final class ReloadingKeyManager extends X509ExtendedKeyManager {static final Duration RETIRED_GENERATION_RETENTION = Duration.ofSeconds(60);@@ -41,7 +41,7 @@ final class ReloadingKeyManager extends X509ExtendedKeyManager {/** Superseded generations, newest first, each within the retention window at its last reload. */private volatile List<Generation> retired = List.of();- ReloadingKeyManager(X509KeyManager delegate) {+ public ReloadingKeyManager(X509KeyManager delegate) {this(delegate, System::nanoTime);}@@ -51,7 +51,7 @@ final class ReloadingKeyManager extends X509ExtendedKeyManager {}/** The first X.509 key manager of {@code options}, the TLS registry's default key store. */- static X509KeyManager keyManagerOf(Vertx vertx, KeyCertOptions options) {+ public static X509KeyManager keyManagerOf(Vertx vertx, KeyCertOptions options) {if (options == null) {throw new IllegalStateException("the default TLS configuration has no key store");}@@ -72,7 +72,7 @@ final class ReloadingKeyManager extends X509ExtendedKeyManager {}/** Serves {@code delegate} from the next handshake on; the generations before it stay readable for a while. */- synchronized void reload(X509KeyManager delegate) {+ public synchronized void reload(X509KeyManager delegate) {long now = nanoTime.getAsLong();List<Generation> kept = new ArrayList<>();kept.add(new Generation(current.number(), current.delegate(), now));diff --git a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2DataPlane.java b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2DataPlane.javaindex 21847518fa..03bfec22a1 100644--- a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2DataPlane.java+++ b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2DataPlane.java@@ -7,6 +7,7 @@ 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.core.common.tls.ReloadingKeyManager;import io.github.hectorvent.floci.services.ec2.Ec2Service;import io.github.hectorvent.floci.services.elbv2.model.Action;import io.github.hectorvent.floci.services.elbv2.model.Listener;@@ -31,9 +32,10 @@ import io.vertx.core.http.HttpServerRequest;import io.vertx.core.http.HttpServerResponse;import io.vertx.core.http.HttpVersion;import io.vertx.core.http.RequestOptions;+import io.vertx.core.net.JdkSSLEngineOptions;+import io.vertx.core.net.KeyCertOptions;import io.vertx.core.net.NetSocket;import io.vertx.core.net.PemKeyCertOptions;-import io.vertx.core.net.SSLOptions;import jakarta.annotation.PostConstruct;import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;@@ -123,6 +125,7 @@ public class ElbV2DataPlane {private int depth;private int closing;private long generation;+ private ReloadingKeyManager tlsKeyManager;private volatile RoutingView routing = new RoutingView(Map.of(), Map.of());}@@ -476,9 +479,10 @@ public class ElbV2DataPlane {.setHost("0.0.0.0").setPort(port);if (secure) {+ state.tlsKeyManager = new ReloadingKeyManager(new ElbV2TlsKeyManager(vertx, tlsKeyCertOptions(port)));options.setSsl(true)- .setSni(true)- .setPemKeyCertOptions(tlsKeyCertOptions(port));+ .setSslEngineOptions(new JdkSSLEngineOptions())+ .setKeyCertOptions(KeyCertOptions.wrap(state.tlsKeyManager));}HttpServer server = vertx.createHttpServer(options);long generation = ++state.generation;@@ -655,9 +659,15 @@ public class ElbV2DataPlane {}private void refreshTlsOptions(int port, HttpServer server) {- server.updateSSLOptions(new SSLOptions().setKeyCertOptions(tlsKeyCertOptions(port)), true)- .onFailure(err -> LOG.warnv("ELBv2 listener TLS certificates failed to refresh on {0}: {1}",- String.valueOf(port), err.getMessage()));+ PortState state = portStates.get(port);+ if (state != null && servers.get(port) == server && state.tlsKeyManager != null) {+ try {+ state.tlsKeyManager.reload(new ElbV2TlsKeyManager(vertx, tlsKeyCertOptions(port)));+ } catch (RuntimeException failure) {+ LOG.warnv(failure, "ELBv2 listener TLS certificates failed to refresh on {0}: {1}",+ String.valueOf(port), failure.getMessage());+ }+ }}private void restartPortServer(int port) {diff --git a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsKeyManager.java b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsKeyManager.javanew file mode 100644index 0000000000..0ef97d3753--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsKeyManager.java@@ -0,0 +1,117 @@+package io.github.hectorvent.floci.services.elbv2;++import io.github.hectorvent.floci.core.common.tls.ReloadingKeyManager;+import io.vertx.core.Vertx;+import io.vertx.core.net.KeyCertOptions;++import javax.net.ssl.ExtendedSSLSession;+import javax.net.ssl.KeyManager;+import javax.net.ssl.KeyManagerFactory;+import javax.net.ssl.SNIHostName;+import javax.net.ssl.SNIServerName;+import javax.net.ssl.SSLEngine;+import javax.net.ssl.X509ExtendedKeyManager;+import javax.net.ssl.X509KeyManager;+import java.net.Socket;+import java.security.Principal;+import java.security.PrivateKey;+import java.security.cert.X509Certificate;+import java.util.Locale;+import java.util.function.Function;++/**+ * An immutable SNI selection snapshot. Host-qualified aliases keep key and chain lookups on the+ * selected manager; the enclosing ReloadingKeyManager also qualifies them by certificate generation.+ */+final class ElbV2TlsKeyManager extends X509ExtendedKeyManager {++ private final X509KeyManager defaultManager;+ private final Function<String, KeyManagerFactory> managers;++ ElbV2TlsKeyManager(Vertx vertx, KeyCertOptions options) {+ defaultManager = ReloadingKeyManager.keyManagerOf(vertx, options);+ try {+ managers = options.keyManagerFactoryMapper(vertx);+ } catch (Exception failure) {+ throw new IllegalStateException("Cannot load ELBv2 SNI certificates", failure);+ }+ }++ @Override+ public String chooseEngineServerAlias(String keyType, Principal[] issuers, SSLEngine engine) {+ String host = "";+ if (engine.getHandshakeSession() instanceof ExtendedSSLSession session) {+ for (SNIServerName name : session.getRequestedServerNames()) {+ if (name instanceof SNIHostName hostname) {+ host = hostname.getAsciiName().toLowerCase(Locale.ROOT);+ break;+ }+ }+ }+ X509KeyManager manager = managerFor(host);+ String alias = manager instanceof X509ExtendedKeyManager extended+ ? extended.chooseEngineServerAlias(keyType, issuers, engine)+ : manager.chooseServerAlias(keyType, issuers, null);+ return alias == null ? null : host + ":" + alias;+ }++ @Override+ public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) {+ String alias = defaultManager.chooseServerAlias(keyType, issuers, socket);+ return alias == null ? null : ":" + alias;+ }++ @Override+ public String[] getServerAliases(String keyType, Principal[] issuers) {+ String[] aliases = defaultManager.getServerAliases(keyType, issuers);+ if (aliases == null) {+ return new String[0];+ }+ String[] qualified = new String[aliases.length];+ for (int i = 0; i < aliases.length; i++) {+ qualified[i] = ":" + aliases[i];+ }+ return qualified;+ }++ @Override+ public X509Certificate[] getCertificateChain(String alias) {+ int separator = alias == null ? -1 : alias.indexOf(':');+ return separator < 0 ? null : managerFor(alias.substring(0, separator))+ .getCertificateChain(alias.substring(separator + 1));+ }++ @Override+ public PrivateKey getPrivateKey(String alias) {+ int separator = alias == null ? -1 : alias.indexOf(':');+ return separator < 0 ? null : managerFor(alias.substring(0, separator))+ .getPrivateKey(alias.substring(separator + 1));+ }++ @Override+ public String[] getClientAliases(String keyType, Principal[] issuers) {+ return new String[0];+ }++ @Override+ public String chooseClientAlias(String[] keyTypes, Principal[] issuers, Socket socket) {+ return null;+ }++ @Override+ public String chooseEngineClientAlias(String[] keyTypes, Principal[] issuers, SSLEngine engine) {+ return null;+ }++ private X509KeyManager managerFor(String host) {+ KeyManagerFactory factory = host.isEmpty() ? null : managers.apply(host);+ if (factory != null) {+ for (KeyManager manager : factory.getKeyManagers()) {+ if (manager instanceof X509KeyManager x509) {+ return x509;+ }+ }+ }+ return defaultManager;+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.javaindex c5cbb0360b..e822a5051f 100644--- a/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java+++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.Resettable;+import io.github.hectorvent.floci.core.common.tls.ReloadingKeyManager;import io.github.hectorvent.floci.services.iot.model.IotRetainedMessage;import io.netty.handler.codec.TooLongFrameException;import io.netty.handler.codec.mqtt.MqttConnectReturnCode;diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/ReloadingKeyManagerTest.java b/src/test/java/io/github/hectorvent/floci/core/common/tls/ReloadingKeyManagerTest.javasimilarity index 99%rename from src/test/java/io/github/hectorvent/floci/services/iot/ReloadingKeyManagerTest.javarename to src/test/java/io/github/hectorvent/floci/core/common/tls/ReloadingKeyManagerTest.javaindex 5e042028e7..751e6523c0 100644--- a/src/test/java/io/github/hectorvent/floci/services/iot/ReloadingKeyManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/tls/ReloadingKeyManagerTest.java@@ -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;diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2HttpsDataPlaneIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2HttpsDataPlaneIntegrationTest.javaindex 827f35313d..2c2b8643d7 100644--- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2HttpsDataPlaneIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2HttpsDataPlaneIntegrationTest.java@@ -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;diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2PublicationTestSupport.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2PublicationTestSupport.javaindex dbb747b712..56ff81b393 100644--- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2PublicationTestSupport.java+++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2PublicationTestSupport.java@@ -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, () -> {diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ReservedPortTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ReservedPortTest.javaindex 3e0f8293e2..67515615f2 100644--- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ReservedPortTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ReservedPortTest.java@@ -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 {diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsCohortTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsCohortTest.javaindex 4bd81bc1b4..3a6e900f95 100644--- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsCohortTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsCohortTest.java@@ -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);diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsKeyManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsKeyManagerTest.javanew file mode 100644index 0000000000..936c93b4dc--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsKeyManagerTest.java@@ -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);+ }+ }+}