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

@@ -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\":\""