mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(tls): fail startup when public listener cannot bind (#3362)
This commit is contained in:
1 parent
c99ecea0da
commit
2fd55b53df
2 files changed
+33
-5
No files matched your search
@@ -17,6 +17,7 @@ import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CompletionException;
|
||||
|
||||
/**
|
||||
* A TCP proxy server that enables HTTP and HTTPS on the same port (LocalStack parity).
|
||||
@@ -109,7 +110,8 @@ public class TlsProxyServer {
|
||||
NetServer server = vertx.createNetServer(options);
|
||||
server.connectHandler(connectHandler);
|
||||
proxyServers.add(server);
|
||||
server.listen().onComplete(ar -> {
|
||||
var bind = server.listen();
|
||||
bind.onComplete(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
failedPorts.remove(port);
|
||||
LOG.infov("TLS proxy: listening on port {0} (HTTP→{1}, HTTPS→{2})",
|
||||
@@ -128,6 +130,16 @@ public class TlsProxyServer {
|
||||
String.valueOf(port), ar.cause().getMessage());
|
||||
}
|
||||
});
|
||||
if (port == config.port()) {
|
||||
try {
|
||||
bind.toCompletionStage().toCompletableFuture().join();
|
||||
} catch (CompletionException e) {
|
||||
stop();
|
||||
Throwable cause = e.getCause() != null ? e.getCause() : e;
|
||||
throw new IllegalStateException(
|
||||
"TLS proxy failed to bind public port " + port, cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+20
-4
@@ -9,9 +9,8 @@ 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.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -38,7 +37,7 @@ class TlsProxyServerReservedPortTest {
|
||||
// stage it directly on failedPorts.
|
||||
NetServer server = mock(NetServer.class);
|
||||
when(server.connectHandler(any())).thenReturn(server);
|
||||
when(server.listen()).thenReturn(Promise.<NetServer>promise().future());
|
||||
when(server.listen()).thenReturn(Future.succeededFuture(server), Promise.<NetServer>promise().future());
|
||||
|
||||
Vertx vertx = mock(Vertx.class);
|
||||
when(vertx.createNetClient()).thenReturn(mock(NetClient.class));
|
||||
@@ -54,6 +53,14 @@ class TlsProxyServerReservedPortTest {
|
||||
private static TlsProxyServer proxyWith(int flociPort, boolean tlsEnabled, int awsHttpsPort,
|
||||
Future<NetServer> outcome,
|
||||
java.util.function.Consumer<TlsProxyServer> beforeStart) {
|
||||
return proxyWith(flociPort, tlsEnabled, awsHttpsPort,
|
||||
Future.succeededFuture(mock(NetServer.class)), outcome, beforeStart);
|
||||
}
|
||||
|
||||
private static TlsProxyServer proxyWith(int flociPort, boolean tlsEnabled, int awsHttpsPort,
|
||||
Future<NetServer> publicOutcome,
|
||||
Future<NetServer> awsHttpsOutcome,
|
||||
java.util.function.Consumer<TlsProxyServer> beforeStart) {
|
||||
EmulatorConfig.TlsConfig tls = mock(EmulatorConfig.TlsConfig.class);
|
||||
when(tls.enabled()).thenReturn(tlsEnabled);
|
||||
when(tls.awsHttpsPort()).thenReturn(awsHttpsPort);
|
||||
@@ -63,7 +70,7 @@ class TlsProxyServerReservedPortTest {
|
||||
|
||||
NetServer server = mock(NetServer.class);
|
||||
when(server.connectHandler(any())).thenReturn(server);
|
||||
when(server.listen()).thenReturn(outcome);
|
||||
when(server.listen()).thenReturn(publicOutcome, awsHttpsOutcome);
|
||||
|
||||
Vertx vertx = mock(Vertx.class);
|
||||
when(vertx.createNetClient()).thenReturn(mock(NetClient.class));
|
||||
@@ -114,6 +121,15 @@ class TlsProxyServerReservedPortTest {
|
||||
assertFalse(proxy.reservesPort(443));
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsStartupWhenThePublicPortCannotBind() {
|
||||
assertThrows(IllegalStateException.class, () -> proxyWith(4566, true, 443,
|
||||
Future.failedFuture(new RuntimeException("address already in use")),
|
||||
Future.succeededFuture(mock(NetServer.class)),
|
||||
ignored -> {
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotReserveAnUnrelatedPort() {
|
||||
TlsProxyServer proxy = proxyWith(4566, true, 443);
|
||||
|
||||
Reference in new issue
Block a user