mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(ecs): honour privileged EC2 task containers (#6659)
This commit is contained in:
1 parent
95960f80e8
commit
1bf019f89a
7 files changed
+112
-1
No files matched your search
@@ -855,6 +855,7 @@ Every `awsvpc` task receives an ENI in its subnet. With `FLOCI_NETWORK_SECURITY_
|
||||
| `FLOCI_SERVICES_ECS_DEFAULT_CPU_UNITS` | `256` | Default CPU units when the task definition omits it |
|
||||
| `FLOCI_SERVICES_ECS_HOST_VOLUME_ROOTS` | *(unset)* | Approved parent directories for host volume bind mounts (`volumes[].host.sourcePath`) and container devices (`linuxParameters.devices[].hostPath`) |
|
||||
| `FLOCI_SERVICES_ECS_ALLOW_UNSAFE_HOST_VOLUMES` | `false` | Allow any host path, bypassing the `HOST_VOLUME_ROOTS` allowlist; traversal, the bare root, and the Docker socket are still always rejected |
|
||||
| `FLOCI_SERVICES_ECS_ALLOW_PRIVILEGED_CONTAINERS` | `false` | Allow EC2 task containers requesting `privileged: true` to run with Docker privilege on the host |
|
||||
| `FLOCI_SERVICES_ECS_RECONCILE_CONTAINERS_ON_STARTUP` | `true` | On startup, in Docker mode, remove the ECS containers a previous run of this Floci left on the daemon before the scheduler starts replacement tasks (see [Restarts](#restarts)) |
|
||||
| `FLOCI_SERVICES_ECS_IMAGE_PULL_BEHAVIOR` | `default` | How task images are pulled at launch, as the ECS agent's `ECS_IMAGE_PULL_BEHAVIOR`: `default`, `always`, `once` or `prefer-cached` (see [Image pulls](#image-pulls)) |
|
||||
| `FLOCI_SERVICES_ECS_TASK_ROLE_CREDENTIALS_ENABLED` | `false` | Vend real task IAM role credentials to task containers |
|
||||
@@ -984,6 +985,8 @@ Always rejected, regardless of configuration:
|
||||
|
||||
A container device's `hostPath` (`linuxParameters.devices`) goes through the same checks and the same allowlist, since a device hands the container the host's hardware.
|
||||
|
||||
An EC2 task definition may contain `privileged: true`, but Docker-backed tasks using it stop before launch unless the Floci operator sets `FLOCI_SERVICES_ECS_ALLOW_PRIVILEGED_CONTAINERS=true`. A privileged container has access to all host devices and can bypass the host path restrictions above, so enable this only when task definition callers are trusted.
|
||||
|
||||
A rejected `sourcePath` or device `hostPath` fails `RegisterTaskDefinition` with `InvalidParameterException`; a rejection caught again at `RunTask` time (e.g. a task definition registered before this policy existed) stops the task with that message as its `stoppedReason`. Named Docker volumes, EFS volumes, and host volumes with no `sourcePath` (ephemeral storage shared within a task) are unaffected by these checks.
|
||||
|
||||
### EFS volume ownership
|
||||
|
||||
@@ -2414,6 +2414,10 @@ public interface EmulatorConfig {
|
||||
@WithDefault("false")
|
||||
boolean allowUnsafeHostVolumes();
|
||||
|
||||
/** Allow ECS task definitions to launch Docker containers with host-level privilege. */
|
||||
@WithDefault("false")
|
||||
boolean allowPrivilegedContainers();
|
||||
|
||||
/**
|
||||
* When true, Floci removes on startup, in Docker mode, every ECS container a previous run
|
||||
* of <em>this same</em> Floci left on the daemon (matched by the {@code io.floci.owner}
|
||||
|
||||
+10
@@ -248,6 +248,13 @@ public class EcsContainerManager {
|
||||
|
||||
EcsTaskHandle startTask(EcsTask task, TaskDefinition taskDef,
|
||||
List<ContainerOverride> containerOverrides, String region, long firelensBackoffMillis) {
|
||||
if (!config.services().ecs().allowPrivilegedContainers()
|
||||
&& taskDef.getContainerDefinitions().stream()
|
||||
.anyMatch(def -> Boolean.TRUE.equals(def.getPrivileged()))) {
|
||||
throw new AwsException("ClientException",
|
||||
"Privileged containers require floci.services.ecs.allow-privileged-containers"
|
||||
+ " (FLOCI_SERVICES_ECS_ALLOW_PRIVILEGED_CONTAINERS) to be enabled.", 400);
|
||||
}
|
||||
String taskId = extractTaskId(task.getTaskArn());
|
||||
|
||||
Map<String, String> containerIds = new LinkedHashMap<>();
|
||||
@@ -440,6 +447,9 @@ public class EcsContainerManager {
|
||||
if (Boolean.TRUE.equals(def.getReadonlyRootFilesystem())) {
|
||||
specBuilder.withReadonlyRootfs();
|
||||
}
|
||||
if (Boolean.TRUE.equals(def.getPrivileged())) {
|
||||
specBuilder.withPrivileged(true);
|
||||
}
|
||||
// Checked at registration too; again here, as for a host volume, for a definition
|
||||
// persisted before the policy covered devices.
|
||||
checkDeviceHostPaths(def);
|
||||
|
||||
@@ -754,6 +754,7 @@ floci:
|
||||
host-port-bind-address: "127.0.0.1"
|
||||
host-volume-roots: []
|
||||
allow-unsafe-host-volumes: false
|
||||
allow-privileged-containers: false
|
||||
# Remove the ECS containers a previous run of this deployment left on the daemon before
|
||||
# the service scheduler starts replacements. Scoped to this deployment's own containers.
|
||||
reconcile-containers-on-startup: true
|
||||
|
||||
+37
-1
@@ -53,7 +53,8 @@ class EcsLinuxParametersDockerIntegrationTest {
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.ecs.mock", "false",
|
||||
"floci.network.security-group-enforcement.enabled", "false",
|
||||
"floci.services.ecs.host-volume-roots", "/dev/null");
|
||||
"floci.services.ecs.host-volume-roots", "/dev/null",
|
||||
"floci.services.ecs.allow-privileged-containers", "true");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,4 +138,39 @@ class EcsLinuxParametersDockerIntegrationTest {
|
||||
assertTrue(top.getProcesses()[0][command].contains("docker-init"),
|
||||
"PID 1 is the init process, with the command under it: " + Arrays.deepToString(top.getProcesses()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void privilegedEc2ContainerRunsWithDockerPrivilege() {
|
||||
String family = "privileged-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String taskDefinitionArn = given().contentType(ECS_CT)
|
||||
.header("X-Amz-Target", ECS_TARGET + "RegisterTaskDefinition")
|
||||
.body("""
|
||||
{
|
||||
"family": "%s",
|
||||
"requiresCompatibilities": ["EC2"],
|
||||
"networkMode": "bridge",
|
||||
"containerDefinitions": [{
|
||||
"name": "worker",
|
||||
"image": "%s",
|
||||
"memory": 256,
|
||||
"command": ["sleep", "120"],
|
||||
"privileged": true
|
||||
}]
|
||||
}
|
||||
""".formatted(family, TestImages.BUSYBOX))
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().path("taskDefinition.taskDefinitionArn");
|
||||
|
||||
clusterName = family;
|
||||
ecsService.createCluster(clusterName, REGION);
|
||||
EcsTask task = ecsService.runTask(clusterName, taskDefinitionArn, 1,
|
||||
null, null, null, null, null, REGION).getFirst();
|
||||
taskArn = task.getTaskArn();
|
||||
EcsTask described = ecsService.describeTasks(clusterName, List.of(taskArn), REGION).getFirst();
|
||||
assertEquals("RUNNING", described.getLastStatus(), "the container must be up: " + described.getStoppedReason());
|
||||
|
||||
String containerId = described.getContainers().getFirst().getRuntimeId();
|
||||
InspectContainerResponse inspected = dockerClient.inspectContainerCmd(containerId).exec();
|
||||
assertEquals(Boolean.TRUE, inspected.getHostConfig().getPrivileged());
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package io.github.hectorvent.floci.services.ecs.container;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
|
||||
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsTask;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
import io.github.hectorvent.floci.services.ssm.SsmService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
|
||||
class EcsContainerManagerPrivilegeTest {
|
||||
|
||||
@Test
|
||||
void privilegedTaskIsRefusedBeforeDockerAccessByDefault() {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
EcsContainerManager manager = new EcsContainerManager(containerBuilder, lifecycleManager,
|
||||
mock(ContainerLogStreamer.class), mock(ContainerDetector.class), config,
|
||||
mock(RegionResolver.class), mock(LaunchedContainerAwsEnv.class),
|
||||
mock(SsmService.class), mock(SecretsManagerService.class), mock(S3Service.class),
|
||||
mock(EcrRegistryManager.class), new HostVolumePolicy(config));
|
||||
|
||||
ContainerDefinition container = new ContainerDefinition();
|
||||
container.setName("worker");
|
||||
container.setPrivileged(true);
|
||||
TaskDefinition definition = new TaskDefinition();
|
||||
definition.setContainerDefinitions(List.of(container));
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> manager.startTask(new EcsTask(), definition, List.of(), "us-east-1"));
|
||||
|
||||
assertEquals("ClientException", failure.getErrorCode());
|
||||
assertEquals(400, failure.getHttpStatus());
|
||||
assertEquals("Privileged containers require floci.services.ecs.allow-privileged-containers"
|
||||
+ " (FLOCI_SERVICES_ECS_ALLOW_PRIVILEGED_CONTAINERS) to be enabled.", failure.getMessage());
|
||||
verifyNoInteractions(containerBuilder, lifecycleManager);
|
||||
}
|
||||
}
|
||||
@@ -354,6 +354,7 @@ floci:
|
||||
dynamic-awsvpc-host-ports: false
|
||||
host-volume-roots: []
|
||||
allow-unsafe-host-volumes: false
|
||||
allow-privileged-containers: false
|
||||
image-pull-behavior: prefer-cached
|
||||
appconfig:
|
||||
enabled: true
|
||||
|
||||
Reference in new issue
Block a user