Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(ecs): use caller endpoint for exec stream URLs (#5666)

Aniket Shukla6e8a083d2ba3
diff --git a/docs/services/ecs.md b/docs/services/ecs.md
index 4b820c0645..03a4adb1a0 100644
--- a/docs/services/ecs.md
+++ b/docs/services/ecs.md
@@ -369,7 +369,12 @@ a mock-mode task reports the `ExecuteCommandAgent` as running but has no runtime
`ExecuteCommand` answers `TargetNotConnectedException`.
The response carries a `session` with a `streamUrl` pointing at Floci's own data channel and a
-single-use `tokenValue`, so the AWS CLI works as documented:
+single-use `tokenValue`, so the AWS CLI works as documented. The channel URL uses the incoming
+HTTP request's hostname, port and application base path instead of Floci's configured internal
+hostname. HTTP endpoints produce `ws://` URLs and HTTPS endpoints produce `wss://` URLs. Calls
+made inside Floci without an HTTP request retain the configured base URL. Reverse proxies must
+expose the data channel under the same endpoint and configure trusted forwarding on the server
+if they terminate TLS or rewrite the application base path:
```bash
aws ecs execute-command --cluster my-cluster --task <task-arn> \
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java
index 8bef704a59..d27dc680ec 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java
@@ -324,7 +324,7 @@ public class AwsJson11Controller {
case "kms" -> kmsJsonHandler.handle(action, request, region);
case "cognito-idp" -> cognitoJsonHandler.handle(action, request, region);
case "acm" -> acmJsonHandler.handle(action, request, region);
- case "ecs" -> ecsJsonHandler.handle(action, request, region);
+ case "ecs" -> ecsJsonHandler.handle(action, request, region, uriInfo.getBaseUri());
case "ecr" -> ecrJsonHandler.handle(action, request, region);
case "glue" -> glueJsonHandler.handle(action, request, region);
case "athena" -> athenaJsonHandler.handle(action, request, region);
diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java
index 24e87852ab..24d4d0615f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java
@@ -55,6 +55,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
+import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -87,6 +88,10 @@ public class EcsJsonHandler {
}
public Response handle(String action, JsonNode request, String region) {
+ return handle(action, request, region, null);
+ }
+
+ public Response handle(String action, JsonNode request, String region, URI requestBaseUri) {
return switch (action) {
// Clusters
case "CreateCluster" -> handleCreateCluster(request, region);
@@ -111,7 +116,7 @@ public class EcsJsonHandler {
case "ListTasks" -> handleListTasks(request, region);
case "UpdateTaskProtection" -> handleUpdateTaskProtection(request, region);
case "GetTaskProtection" -> handleGetTaskProtection(request, region);
- case "ExecuteCommand" -> handleExecuteCommand(request, region);
+ case "ExecuteCommand" -> handleExecuteCommand(request, region, requestBaseUri);
case "CreateService" -> handleCreateService(request, region);
case "UpdateService" -> handleUpdateService(request, region);
@@ -515,7 +520,7 @@ public class EcsJsonHandler {
return Response.ok(resp).build();
}
- private Response handleExecuteCommand(JsonNode req, String region) {
+ private Response handleExecuteCommand(JsonNode req, String region, URI requestBaseUri) {
String cluster = req.has("cluster") ? req.path("cluster").asText() : null;
String task = req.path("task").asText();
String container = req.hasNonNull("container") ? req.path("container").asText() : null;
@@ -535,7 +540,7 @@ public class EcsJsonHandler {
resp.put("interactive", interactive);
ObjectNode session = resp.putObject("session");
session.put("sessionId", result.session().sessionId());
- session.put("streamUrl", service.execStreamUrl(result.session()));
+ session.put("streamUrl", service.execStreamUrl(result.session(), requestBaseUri));
session.put("tokenValue", result.session().tokenValue());
return Response.ok(resp).build();
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java
index bfb24d09b2..6e386c8434 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java
@@ -72,8 +72,10 @@ import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
+import jakarta.ws.rs.core.UriBuilder;
import org.jboss.logging.Logger;
+import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
@@ -2275,12 +2277,19 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
"The container " + containerName + " is not part of the task.", 400));
}
- /** The {@code ws://} URL a client opens for an exec session's data channel. */
+ /** The data channel URL for a caller without an HTTP request endpoint. */
public String execStreamUrl(ExecSession session) {
- String websocketBase = baseUrl.startsWith("https://")
- ? "wss://" + baseUrl.substring("https://".length())
- : "ws://" + baseUrl.substring(baseUrl.indexOf("://") + 3);
- return websocketBase + EcsExecChannelHandler.CHANNEL_PATH_PREFIX + session.sessionId();
+ return execStreamUrl(session, null);
+ }
+
+ /** Uses the request's reachable endpoint, or configured base URL for non-HTTP callers. */
+ public String execStreamUrl(ExecSession session, URI requestBaseUri) {
+ URI endpoint = requestBaseUri != null ? requestBaseUri : URI.create(baseUrl);
+ return UriBuilder.fromUri(endpoint)
+ .scheme("https".equalsIgnoreCase(endpoint.getScheme()) ? "wss" : "ws")
+ .path(EcsExecChannelHandler.CHANNEL_PATH_PREFIX)
+ .path(session.sessionId())
+ .build().toString();
}
/** A container reached through the task metadata endpoint, with the task it belongs to. */
diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsExecStreamUrlTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsExecStreamUrlTest.java
new file mode 100644
index 0000000000..2fee83c379
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsExecStreamUrlTest.java
@@ -0,0 +1,49 @@
+package io.github.hectorvent.floci.services.ecs;
+
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.services.ecs.exec.ExecSession;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+import java.net.URI;
+import java.time.Instant;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class EcsExecStreamUrlTest {
+
+ private static final ExecSession SESSION = new ExecSession("exec-session", "token", "task", "cluster",
+ "app", "container", "runtime", List.of("/bin/sh"), true, Instant.EPOCH);
+
+ @ParameterizedTest
+ @CsvSource({
+ "http://localhost:6543/, ws://localhost:6543/v1/data-channel/exec-session",
+ "https://public.example/, wss://public.example/v1/data-channel/exec-session",
+ "https://public.example:8443/floci/, wss://public.example:8443/floci/v1/data-channel/exec-session",
+ "http://[::1]:6543/, ws://[::1]:6543/v1/data-channel/exec-session"
+ })
+ void preservesTheRequestEndpointAndChoosesTheWebSocketScheme(String endpoint, String expected) {
+ EcsService service = service("http://floci:4566");
+
+ assertEquals(expected, service.execStreamUrl(SESSION, URI.create(endpoint)));
+ }
+
+ @Test
+ void nonHttpCallersKeepTheConfiguredEndpointWithoutDuplicateSlashes() {
+ EcsService service = service("https://floci:4566/floci/");
+
+ assertEquals("wss://floci:4566/floci/v1/data-channel/exec-session", service.execStreamUrl(SESSION));
+ assertEquals(service.execStreamUrl(SESSION), service.execStreamUrl(SESSION, null));
+ }
+
+ private static EcsService service(String baseUrl) {
+ EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
+ when(config.effectiveBaseUrl()).thenReturn(baseUrl);
+ return new EcsService(null, null, config, null, null, null);
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsExecuteCommandIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsExecuteCommandIntegrationTest.java
index 7c4bcc4df5..bb6157670e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsExecuteCommandIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsExecuteCommandIntegrationTest.java
@@ -2,14 +2,22 @@ package io.github.hectorvent.floci.services.ecs;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.RestAssured;
import io.restassured.response.Response;
+import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.net.URI;
+import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
+import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* {@code ExecuteCommand}'s control-plane half: what a task has to be for a session to be opened at
@@ -22,6 +30,9 @@ import static org.hamcrest.Matchers.hasSize;
@QuarkusTest
class EcsExecuteCommandIntegrationTest {
+ @Inject
+ EcsService service;
+
private static final String TARGET = "AmazonEC2ContainerServiceV20141113.";
private static final String CT = "application/x-amz-json-1.1";
private static final String CLUSTER = "exec-cluster";
@@ -54,6 +65,29 @@ class EcsExecuteCommandIntegrationTest {
.jsonPath().getString("tasks[0].taskArn");
}
+ @ParameterizedTest
+ @ValueSource(strings = {"localhost", "127.0.0.1"})
+ void streamUrlUsesTheCallingHttpEndpoint(String hostname) {
+ String taskArn = runTask("exec-caller-" + hostname.replace('.', '-'), true);
+ // A synthetic runtime reaches URL generation without opening a Docker exec session.
+ service.describeTasks(CLUSTER, List.of(taskArn), "us-east-1").getFirst()
+ .getContainers().getFirst().setRuntimeId("runtime-for-url-test");
+
+ Response response = given().baseUri("http://" + hostname).port(RestAssured.port)
+ .contentType(CT).header("X-Amz-Target", TARGET + "ExecuteCommand")
+ .header("X-Forwarded-Host", "untrusted.invalid:1234")
+ .header("X-Forwarded-Proto", "https")
+ .body("{\"cluster\":\"" + CLUSTER + "\",\"task\":\"" + taskArn
+ + "\",\"container\":\"app\",\"command\":\"/bin/sh\",\"interactive\":true}")
+ .when().post("/").then().statusCode(200).extract().response();
+
+ URI streamUrl = URI.create(response.jsonPath().getString("session.streamUrl"));
+ assertEquals("ws", streamUrl.getScheme());
+ assertEquals(hostname, streamUrl.getHost());
+ assertEquals(RestAssured.port, streamUrl.getPort());
+ assertEquals("/v1/data-channel/" + response.jsonPath().getString("session.sessionId"), streamUrl.getPath());
+ }
+
@Test
void aTaskRunWithExecuteCommandEnabledReportsTheAgent() {
String taskArn = runTask("exec-enabled", true);
diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/exec/EcsExecChannelDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/exec/EcsExecChannelDockerIntegrationTest.java
index 953ab81495..3c93fb2a43 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ecs/exec/EcsExecChannelDockerIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/exec/EcsExecChannelDockerIntegrationTest.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.ecs.exec;
import com.github.dockerjava.api.DockerClient;
+import io.github.hectorvent.floci.services.ecs.EcsService;
import io.github.hectorvent.floci.services.ecs.container.EcsContainerManager;
import io.github.hectorvent.floci.services.ecs.container.EcsTaskHandle;
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
@@ -21,6 +22,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.ByteArrayOutputStream;
+import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;
@@ -51,6 +53,9 @@ class EcsExecChannelDockerIntegrationTest {
@Inject
EcsContainerManager containerManager;
+ @Inject
+ EcsService service;
+
@Inject
EcsExecSessionRegistry sessions;
@@ -97,7 +102,8 @@ class EcsExecChannelDockerIntegrationTest {
List.of("/bin/sh", "-c", "printf '\\000\\001\\002\\003\\377\\376'; "
+ "head -c 5000 /dev/zero; printf 'exec-channel-ok'"), interactive);
- PluginClient client = new PluginClient(vertx, session);
+ PluginClient client = new PluginClient(vertx, session,
+ URI.create(service.execStreamUrl(session, URI.create("http://localhost:" + RestAssured.port + "/"))));
try {
client.open();
@@ -160,7 +166,8 @@ class EcsExecChannelDockerIntegrationTest {
"arn:aws:ecs:us-east-1:000000000000:container/exec/app", runtimeId,
List.of("/bin/sh", "-c", "echo exec-uid-$(id -u)-end"), false);
- PluginClient client = new PluginClient(vertx, session);
+ PluginClient client = new PluginClient(vertx, session,
+ URI.create(service.execStreamUrl(session, URI.create("http://localhost:" + RestAssured.port + "/"))));
try {
client.open();
client.next();
@@ -197,21 +204,22 @@ class EcsExecChannelDockerIntegrationTest {
private final Vertx vertx;
private final ExecSession session;
+ private final URI streamUrl;
private final BlockingQueue<AgentMessage> received = new LinkedBlockingQueue<>();
private HttpClient httpClient;
private WebSocket webSocket;
private long sequence;
- PluginClient(Vertx vertx, ExecSession session) {
+ PluginClient(Vertx vertx, ExecSession session, URI streamUrl) {
this.vertx = vertx;
this.session = session;
+ this.streamUrl = streamUrl;
}
void open() throws Exception {
httpClient = vertx.createHttpClient();
- webSocket = httpClient.webSocket(RestAssured.port, "localhost",
- EcsExecChannelHandler.CHANNEL_PATH_PREFIX + session.sessionId())
+ webSocket = httpClient.webSocket(streamUrl.getPort(), streamUrl.getHost(), streamUrl.getRawPath())
.toCompletionStage().toCompletableFuture().get(20, TimeUnit.SECONDS);
webSocket.binaryMessageHandler(buffer -> received.add(AgentMessage.decode(buffer.getBytes())));
webSocket.writeTextMessage("{\"MessageSchemaVersion\":\"1.0\",\"RequestId\":\""