Skip to content

armgit / floci

Public Imported from GitHub
0

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

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

Matheus Oliveira30be71c56ee6
diff --git a/docs/services/ecs.md b/docs/services/ecs.md
index 0c2909e70f..b6a0754e88 100644
--- a/docs/services/ecs.md
+++ b/docs/services/ecs.md
@@ -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
diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/exec/EcsExecChannelHandler.java b/src/main/java/io/github/hectorvent/floci/services/ecs/exec/EcsExecChannelHandler.java
index 3250578e91..825bad98af 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ecs/exec/EcsExecChannelHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ecs/exec/EcsExecChannelHandler.java
@@ -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)
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 f2703c8e27..51d0e1bc06 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
@@ -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;