mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(elbv2): do not let a listener bind a port Floci reserves for itself (#3034)
* fix(elbv2): do not let a listener bind a port Floci reserves for itself The ELBv2 data plane binds each listener's port for real, and the TLS proxy binds the emulator port plus floci.tls.aws-https-port (443 by default) because CDK's custom-resource cfn-response callback hardcodes https:// and ignores the port in the ResponseURL. Nothing arbitrated between them, so the two raced and whichever started first won. That made the outcome depend on start order. On a fresh start the proxy took 443 and the load balancer logged a bind failure; on a start that restored a persisted ALB with an HTTPS listener, the listener took 443 and answered CDK's callback in plain HTTP, so every Custom:: resource hung until CloudFormation timed out - with only a warning to explain it. startPortServer now yields a reserved port, registering the listener on the control plane but leaving it without a data plane, and says so. Load balancers can still have 443 by setting floci.tls.aws-https-port=0 or disabling TLS. * fix(elbv2): yield 443 only while the TLS proxy actually holds it The reserved-port check read configuration, not state. Binding the AWS-HTTPS port is privileged and its failure is deliberately non-fatal, so a proxy that lost the bind still had the port marked reserved and the ELBv2 listener also declined it. The port then went unserved by either side, which is worse than the race the reservation exists to settle. TlsProxyServer now records which binds failed and exposes reservesPort(), and ElbV2DataPlane asks it instead of inferring from config. An unresolved bind still reserves, so a listener cannot win the start-up race by arriving first. Adds TlsProxyServerReservedPortTest for the bind-outcome half of the contract: reserved while unresolved, released once failed, and nothing reserved when TLS is off or aws-https-port is 0. * fix(elbv2): tell the operator the right way to free a reserved port The warning always suggested floci.tls.aws-https-port=0, which is not a remedy when the reserved port is the emulator's own: that one cannot be handed over at all. Pick the advice from which port was asked for. * fix(elbv2): rebind a listener when the proxy gives up its port Yielding a port whose bind had not resolved yet is the safe default, but if that bind then failed nothing retried the listener: startPortServer had returned null, so no server was stored and the listener stayed registered with no data plane while the port sat free. TlsProxyServer now notifies on a failed bind, and the data plane binds any listener already registered on that port. Handlers registered after the fact are replayed the ports already given up, since the data plane only wires itself in once a listener exists. Pinned by notifiesHandlersWhenABindFails, which resolves listen() to a failure and asserts the port is both released and reported. * fix(elbv2): subscribe to released ports outside the server map's mapping function startPortServer registered the proxy's release handler on first use, from inside servers.computeIfAbsent. onPortReleased replays the ports already given up as it subscribes, so when the AWS-HTTPS bind had already failed before any listener existed, that replay ran bindListenersWaitingOn for the same port, which called servers.computeIfAbsent again for the key the outer call was still computing. ConcurrentHashMap rejects that outright: IllegalStateException: Recursive update, straight out of startListener. Registration moves to the callers, after the listener is in listenersByHostAndPort so the replay still finds it, and before the server map is touched so nothing re-enters it. The mapping function now only builds a server. Pinned by aListenerStartsOnAPortTheProxyGaveUpBeforeItSubscribed, which throws Recursive update when the registration is moved back inside.
This commit is contained in:
1 parent
0fce2d2407
commit
bf517ac952
4 files changed
+367
No files matched your search
@@ -0,0 +1,122 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.Promise;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.net.NetClient;
|
||||
import io.vertx.core.net.NetServer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* The proxy only reserves a port it holds or still expects to hold. Binding the AWS-HTTPS port is
|
||||
* privileged and its failure is non-fatal, so a port the proxy never acquired has to stay
|
||||
* available: anything that yields to the proxy on configuration alone would leave it unserved.
|
||||
*/
|
||||
class TlsProxyServerReservedPortTest {
|
||||
|
||||
private static TlsProxyServer proxyWith(int flociPort, boolean tlsEnabled, int awsHttpsPort) {
|
||||
EmulatorConfig.TlsConfig tls = mock(EmulatorConfig.TlsConfig.class);
|
||||
when(tls.enabled()).thenReturn(tlsEnabled);
|
||||
when(tls.awsHttpsPort()).thenReturn(awsHttpsPort);
|
||||
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.port()).thenReturn(flociPort);
|
||||
when(config.tls()).thenReturn(tls);
|
||||
|
||||
// The listen() future is left uncompleted on purpose: that is the "bind outcome not yet
|
||||
// known" state, which must still reserve the port. Tests that need a resolved outcome
|
||||
// stage it directly on failedPorts.
|
||||
NetServer server = mock(NetServer.class);
|
||||
when(server.connectHandler(any())).thenReturn(server);
|
||||
when(server.listen()).thenReturn(Promise.<NetServer>promise().future());
|
||||
|
||||
Vertx vertx = mock(Vertx.class);
|
||||
when(vertx.createNetClient()).thenReturn(mock(NetClient.class));
|
||||
when(vertx.createNetServer(any())).thenReturn(server);
|
||||
|
||||
return new TlsProxyServer(vertx, config, 4510, 4511);
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant that resolves listen() to {@code outcome} and lets the caller register a handler
|
||||
* before construction, since the bind callback fires during the constructor.
|
||||
*/
|
||||
private static TlsProxyServer proxyWith(int flociPort, boolean tlsEnabled, int awsHttpsPort,
|
||||
Future<NetServer> outcome,
|
||||
java.util.function.Consumer<TlsProxyServer> beforeStart) {
|
||||
EmulatorConfig.TlsConfig tls = mock(EmulatorConfig.TlsConfig.class);
|
||||
when(tls.enabled()).thenReturn(tlsEnabled);
|
||||
when(tls.awsHttpsPort()).thenReturn(awsHttpsPort);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.port()).thenReturn(flociPort);
|
||||
when(config.tls()).thenReturn(tls);
|
||||
|
||||
NetServer server = mock(NetServer.class);
|
||||
when(server.connectHandler(any())).thenReturn(server);
|
||||
when(server.listen()).thenReturn(outcome);
|
||||
|
||||
Vertx vertx = mock(Vertx.class);
|
||||
when(vertx.createNetClient()).thenReturn(mock(NetClient.class));
|
||||
when(vertx.createNetServer(any())).thenReturn(server);
|
||||
|
||||
TlsProxyServer proxy = new TlsProxyServer(vertx, config, 4510, 4511);
|
||||
beforeStart.accept(proxy);
|
||||
return proxy;
|
||||
}
|
||||
|
||||
@Test
|
||||
void reservesTheAwsHttpsPortBeforeTheBindOutcomeIsKnown() {
|
||||
TlsProxyServer proxy = proxyWith(4566, true, 443);
|
||||
assertTrue(proxy.reservesPort(443),
|
||||
"an unresolved bind must still hold the port, or a listener races the proxy for it");
|
||||
}
|
||||
|
||||
@Test
|
||||
void releasesTheAwsHttpsPortOnceItsBindHasFailed() {
|
||||
TlsProxyServer proxy = proxyWith(4566, true, 443);
|
||||
proxy.failedPorts.add(443);
|
||||
assertFalse(proxy.reservesPort(443),
|
||||
"a port the proxy failed to bind must stay available to load balancer listeners");
|
||||
}
|
||||
|
||||
@Test
|
||||
void reservesNothingWhenTlsIsDisabled() {
|
||||
TlsProxyServer proxy = proxyWith(4566, false, 443);
|
||||
assertFalse(proxy.reservesPort(443));
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotReserveTheAwsHttpsPortWhenItIsDisabled() {
|
||||
TlsProxyServer proxy = proxyWith(4566, true, 0);
|
||||
assertFalse(proxy.reservesPort(443));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notifiesHandlersWhenABindFails() {
|
||||
List<Integer> released = new ArrayList<>();
|
||||
// listen() resolving to a failure is what happens when the privileged port is refused.
|
||||
TlsProxyServer proxy = proxyWith(4566, true, 443,
|
||||
Future.failedFuture(new RuntimeException("permission denied")),
|
||||
p -> p.onPortReleased(released::add));
|
||||
|
||||
assertTrue(released.contains(443),
|
||||
"a listener that yielded 443 has no other way to learn the bind failed");
|
||||
assertFalse(proxy.reservesPort(443));
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotReserveAnUnrelatedPort() {
|
||||
TlsProxyServer proxy = proxyWith(4566, true, 443);
|
||||
assertFalse(proxy.reservesPort(8080));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package io.github.hectorvent.floci.services.elbv2;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.TlsProxyServer;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.Listener;
|
||||
import io.vertx.core.Promise;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.core.http.HttpServerOptions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.IntConsumer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* A load balancer listener must not bind a port Floci needs for itself, or the two race and
|
||||
* whichever starts first wins - see {@link ElbV2DataPlane#isReservedByFloci(int)}.
|
||||
*/
|
||||
class ElbV2ReservedPortTest {
|
||||
|
||||
private static ElbV2DataPlane dataPlaneWith(int flociPort, boolean tlsEnabled, int awsHttpsPort) {
|
||||
EmulatorConfig.TlsConfig tls = mock(EmulatorConfig.TlsConfig.class);
|
||||
when(tls.enabled()).thenReturn(tlsEnabled);
|
||||
when(tls.awsHttpsPort()).thenReturn(awsHttpsPort);
|
||||
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.port()).thenReturn(flociPort);
|
||||
when(config.tls()).thenReturn(tls);
|
||||
|
||||
// Mirrors TlsProxyServer.reservesPort for the configured case; the bind-outcome half of
|
||||
// that contract is covered by TlsProxyServerReservedPortTest.
|
||||
TlsProxyServer proxy = mock(TlsProxyServer.class);
|
||||
when(proxy.reservesPort(anyInt())).thenAnswer(inv ->
|
||||
tlsEnabled && awsHttpsPort > 0 && (int) inv.getArgument(0) == awsHttpsPort);
|
||||
|
||||
ElbV2DataPlane dataPlane = new ElbV2DataPlane();
|
||||
dataPlane.config = config;
|
||||
dataPlane.tlsProxyServer = proxy;
|
||||
return dataPlane;
|
||||
}
|
||||
|
||||
@Test
|
||||
void reservesTheEmulatorsOwnPortEvenWithTlsOff() {
|
||||
ElbV2DataPlane dataPlane = dataPlaneWith(4566, false, 443);
|
||||
assertTrue(dataPlane.isReservedByFloci(4566));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reservesTheAwsHttpsPortWhenTlsIsOn() {
|
||||
ElbV2DataPlane dataPlane = dataPlaneWith(4566, true, 443);
|
||||
assertTrue(dataPlane.isReservedByFloci(443),
|
||||
"with TLS on, 443 carries CDK's cfn-response callback");
|
||||
}
|
||||
|
||||
@Test
|
||||
void leaves443ToLoadBalancersWhenTlsIsOff() {
|
||||
ElbV2DataPlane dataPlane = dataPlaneWith(4566, false, 443);
|
||||
assertFalse(dataPlane.isReservedByFloci(443));
|
||||
}
|
||||
|
||||
@Test
|
||||
void leaves443ToLoadBalancersWhenTheAwsHttpsPortIsDisabled() {
|
||||
ElbV2DataPlane dataPlane = dataPlaneWith(4566, true, 0);
|
||||
assertFalse(dataPlane.isReservedByFloci(443),
|
||||
"aws-https-port=0 is the documented escape hatch");
|
||||
}
|
||||
|
||||
/**
|
||||
* The proxy gave up the port before any listener existed, so the data plane subscribes and is
|
||||
* replayed that port in the same breath. The replay rebinds listeners waiting on the port,
|
||||
* which reaches back into the same server map: doing the subscribing from inside the map's
|
||||
* own mapping function re-entered computeIfAbsent for the key it was still computing, and
|
||||
* ConcurrentHashMap answers that with IllegalStateException: Recursive update.
|
||||
*/
|
||||
@Test
|
||||
void aListenerStartsOnAPortTheProxyGaveUpBeforeItSubscribed() {
|
||||
EmulatorConfig.TlsConfig tls = mock(EmulatorConfig.TlsConfig.class);
|
||||
when(tls.enabled()).thenReturn(true);
|
||||
when(tls.awsHttpsPort()).thenReturn(443);
|
||||
EmulatorConfig.ElbV2ServiceConfig elbv2 = mock(EmulatorConfig.ElbV2ServiceConfig.class);
|
||||
when(elbv2.mock()).thenReturn(false);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
when(services.elbv2()).thenReturn(elbv2);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.port()).thenReturn(4566);
|
||||
when(config.tls()).thenReturn(tls);
|
||||
when(config.services()).thenReturn(services);
|
||||
|
||||
// 443 is already in failedPorts, so subscribing replays it synchronously, exactly as
|
||||
// TlsProxyServer.onPortReleased does.
|
||||
TlsProxyServer proxy = mock(TlsProxyServer.class);
|
||||
when(proxy.reservesPort(anyInt())).thenReturn(false);
|
||||
doAnswer(inv -> {
|
||||
inv.getArgument(0, IntConsumer.class).accept(443);
|
||||
return null;
|
||||
}).when(proxy).onPortReleased(any());
|
||||
|
||||
HttpServer server = mock(HttpServer.class);
|
||||
when(server.requestHandler(any())).thenReturn(server);
|
||||
// Left uncompleted: the listen outcome is not what this test is about.
|
||||
when(server.listen()).thenReturn(Promise.<HttpServer>promise().future());
|
||||
Vertx vertx = mock(Vertx.class);
|
||||
when(vertx.createHttpServer(any(HttpServerOptions.class))).thenReturn(server);
|
||||
|
||||
ElbV2DataPlane dataPlane = new ElbV2DataPlane();
|
||||
dataPlane.config = config;
|
||||
dataPlane.tlsProxyServer = proxy;
|
||||
dataPlane.vertx = vertx;
|
||||
dataPlane.elbV2Service = mock(ElbV2Service.class);
|
||||
|
||||
Listener listener = new Listener();
|
||||
listener.setListenerArn("arn:aws:elasticloadbalancing:us-west-2:000000000000:listener/app/lb/1/2");
|
||||
listener.setLoadBalancerArn("arn:aws:elasticloadbalancing:us-west-2:000000000000:loadbalancer/app/lb/1");
|
||||
listener.setPort(443);
|
||||
|
||||
assertDoesNotThrow(() -> dataPlane.startListener(listener, "us-west-2", List.of()),
|
||||
"the released-port replay re-entered the server map for the key it was computing");
|
||||
verify(vertx, times(1)).createHttpServer(any(HttpServerOptions.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void leavesOrdinaryListenerPortsAlone() {
|
||||
ElbV2DataPlane dataPlane = dataPlaneWith(4566, true, 443);
|
||||
assertFalse(dataPlane.isReservedByFloci(80));
|
||||
assertFalse(dataPlane.isReservedByFloci(8080));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user