fix(ecs): run ECS Exec sessions as root like the real agent (#5074)

The exec ignored the container's user; it now always runs as uid 0.

* Fix #4850

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-04 13:29:09 -04:00
1 parent efa46548d3
commit 30be71c56e
3 files changed
+51 -1

No files matched your search

+2
View File
@@ -377,6 +377,8 @@ Floci plays the SSM agent's half of the Session Manager protocol on that channel
`AgentMessage` framing, the handshake, sequenced acknowledgements and terminal resizes) and bridges
it to a `docker exec` in the container. Deliberate limits:
- The session runs as root (uid 0) whatever the container definition's `user`, as the real ECS Exec
agent does.
- The command runs through `/bin/sh -c`, so an image without a shell cannot be exec'd into.
- Sessions are in memory, single use, and expire after five minutes if nobody connects.
- `ExecuteCommand` logging (the `executeCommandConfiguration` on a cluster, which sends session
@@ -52,6 +52,7 @@ public class EcsExecChannelHandler {
/** Where the stream URL points. The path mirrors Session Manager's own data-channel path. */
public static final String CHANNEL_PATH_PREFIX = "/v1/data-channel/";
private static final String AGENT_VERSION = "3.3.0.0";
private static final String EXEC_USER = "0";
private final EcsExecSessionRegistry sessions;
private final ContainerLifecycleManager lifecycleManager;
@@ -151,12 +152,14 @@ public class EcsExecChannelHandler {
/**
* Creates the exec and offers the handshake. The command is not started yet: the agent
* offers the session type first and only runs anything once the client has accepted it,
* so the client cannot be handed output for a channel it has not finished opening.
* so the client cannot be handed output for a channel it has not finished opening. The exec
* runs as root whatever the container's own user, as the real ECS Exec agent does.
*/
private void createExec() {
ExecSession current = session;
vertx.<String>executeBlocking(() -> lifecycleManager.getDockerClient()
.execCreateCmd(current.runtimeId())
.withUser(EXEC_USER)
.withCmd(current.command().toArray(new String[0]))
.withAttachStdin(true)
.withAttachStdout(true)
@@ -119,6 +119,51 @@ class EcsExecChannelDockerIntegrationTest {
}
}
@Test
void theSessionRunsAsRootEvenWhenTheContainerDoesNot() throws Exception {
String suffix = UUID.randomUUID().toString().substring(0, 8);
ContainerDefinition app = new ContainerDefinition();
app.setName("app");
app.setImage(BUSYBOX_IMAGE);
app.setUser("1000:1000");
app.setCommand(List.of("sh", "-c", "trap 'exit 0' TERM; sleep 120 & wait"));
TaskDefinition taskDefinition = new TaskDefinition();
taskDefinition.setFamily("exec-root-" + suffix);
taskDefinition.setContainerDefinitions(List.of(app));
EcsTask task = new EcsTask();
task.setTaskArn(TASK_ARN + suffix);
task.setEnableExecuteCommand(true);
EcsTaskHandle handle = containerManager.startTask(task, taskDefinition, List.of(), "us-east-1");
try {
String runtimeId = handle.getContainerIds().get("app");
assertEquals("1000:1000",
dockerClient.inspectContainerCmd(runtimeId).exec().getConfig().getUser(),
"the container itself must run as the task definition's user");
ExecSession session = sessions.create(task.getTaskArn(),
"arn:aws:ecs:us-east-1:000000000000:cluster/exec-cluster", "app",
"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);
try {
client.open();
client.next();
client.sendHandshakeResponse();
String output = client.readUntil("-end");
assertTrue(output.contains("exec-uid-0-end"),
"the session must run as root, got: " + output);
} finally {
client.close();
}
} finally {
containerManager.stopTask(handle);
}
}
/** Reads the ExecuteCommandAgent status a task's containers report. */
private enum ManagedAgentStatus {
RUNNING, ABSENT;