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

This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-14 15:04:18 -04:00
1 parent bb06acdeb1
commit 6e8a083d2b
7 files changed
+125 -15

No files matched your search

+6 -1
View File
@@ -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> \
@@ -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);
@@ -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();
}
@@ -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. */
@@ -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);
}
}
@@ -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);
@@ -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\":\""