Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(tls): fail startup when public listener cannot bind (#3362)

Niels Kootstra2fd55b53df18
diff --git a/src/main/java/io/github/hectorvent/floci/config/TlsProxyServer.java b/src/main/java/io/github/hectorvent/floci/config/TlsProxyServer.java
index dfff5303a8..37e7763805 100644
--- a/src/main/java/io/github/hectorvent/floci/config/TlsProxyServer.java
+++ b/src/main/java/io/github/hectorvent/floci/config/TlsProxyServer.java
@@ -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);
+ }
+ }
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/config/TlsProxyServerReservedPortTest.java b/src/test/java/io/github/hectorvent/floci/config/TlsProxyServerReservedPortTest.java
index a536f1ae53..1a0a1809bf 100644
--- a/src/test/java/io/github/hectorvent/floci/config/TlsProxyServerReservedPortTest.java
+++ b/src/test/java/io/github/hectorvent/floci/config/TlsProxyServerReservedPortTest.java
@@ -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);