feat(ecs): scope EFS volume mounts to rootDirectory and accessPointId (#3449)

Fold rootDirectory (or the access point's own root when
accessPointId is set) into the shared EFS named volume's identity,
so different roots on the same fileSystemId no longer collide.

* Fix #2563

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-09-12 10:58:07 -04:00
1 parent d11fff3d6d
commit 543bde7d5d
7 files changed
+467 -42

No files matched your search

@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.ecs;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
import jakarta.ws.rs.core.Response;
@@ -12,6 +13,7 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
@@ -116,7 +118,7 @@ class EcsJsonHandlerVolumesTest {
"name": "customer-data",
"efsVolumeConfiguration": {
"fileSystemId": "fs-0123456789abcdef0",
"rootDirectory": "/dps",
"rootDirectory": "/",
"transitEncryption": "ENABLED",
"transitEncryptionPort": 2999,
"authorizationConfig": {"accessPointId": "fsap-0abc", "iam": "ENABLED"}
@@ -136,10 +138,112 @@ class EcsJsonHandlerVolumesTest {
assertTrue(vol.path("host").isMissingNode(), "EFS volume must not carry a host shape");
JsonNode efs = vol.path("efsVolumeConfiguration");
assertEquals("fs-0123456789abcdef0", efs.path("fileSystemId").asText());
assertEquals("/dps", efs.path("rootDirectory").asText());
assertEquals("/", efs.path("rootDirectory").asText());
assertEquals("ENABLED", efs.path("transitEncryption").asText());
assertEquals(2999, efs.path("transitEncryptionPort").asInt());
assertEquals("fsap-0abc", efs.path("authorizationConfig").path("accessPointId").asText());
assertEquals("ENABLED", efs.path("authorizationConfig").path("iam").asText());
}
@Test
void registerTaskDefinitionRejectsAccessPointWithNonDefaultRootDirectory() throws Exception {
String requestJson = """
{
"family": "efs-invalid-family",
"containerDefinitions": [
{
"name": "app",
"image": "alpine:latest",
"mountPoints": [
{"sourceVolume": "customer-data", "containerPath": "/mnt/efs", "readOnly": false}
]
}
],
"volumes": [
{
"name": "customer-data",
"efsVolumeConfiguration": {
"fileSystemId": "fs-0123456789abcdef0",
"rootDirectory": "/dps",
"authorizationConfig": {"accessPointId": "fsap-0abc"}
}
}
]
}
""";
JsonNode request = objectMapper.readTree(requestJson);
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("RegisterTaskDefinition", request, "us-east-1"));
assertEquals("InvalidParameterException", ex.getErrorCode());
}
@Test
void registerTaskDefinitionAllowsAccessPointWithOmittedRootDirectory() throws Exception {
String requestJson = """
{
"family": "efs-valid-access-point-family",
"containerDefinitions": [
{
"name": "app",
"image": "alpine:latest",
"mountPoints": [
{"sourceVolume": "customer-data", "containerPath": "/mnt/efs", "readOnly": false}
]
}
],
"volumes": [
{
"name": "customer-data",
"efsVolumeConfiguration": {
"fileSystemId": "fs-0123456789abcdef0",
"authorizationConfig": {"accessPointId": "fsap-0abc"}
}
}
]
}
""";
JsonNode request = objectMapper.readTree(requestJson);
Response response = handler.handle("RegisterTaskDefinition", request, "us-east-1");
JsonNode efs = objectMapper.valueToTree(response.getEntity())
.path("taskDefinition").path("volumes").get(0).path("efsVolumeConfiguration");
assertEquals("fsap-0abc", efs.path("authorizationConfig").path("accessPointId").asText());
assertTrue(efs.path("rootDirectory").isMissingNode(), "rootDirectory should not be set");
}
@Test
void registerTaskDefinitionAllowsNonDefaultRootDirectoryWithoutAccessPoint() throws Exception {
String requestJson = """
{
"family": "efs-valid-root-directory-family",
"containerDefinitions": [
{
"name": "app",
"image": "alpine:latest",
"mountPoints": [
{"sourceVolume": "customer-data", "containerPath": "/mnt/efs", "readOnly": false}
]
}
],
"volumes": [
{
"name": "customer-data",
"efsVolumeConfiguration": {
"fileSystemId": "fs-0123456789abcdef0",
"rootDirectory": "/dps"
}
}
]
}
""";
JsonNode request = objectMapper.readTree(requestJson);
Response response = handler.handle("RegisterTaskDefinition", request, "us-east-1");
JsonNode efs = objectMapper.valueToTree(response.getEntity())
.path("taskDefinition").path("volumes").get(0).path("efsVolumeConfiguration");
assertEquals("/dps", efs.path("rootDirectory").asText());
assertTrue(efs.path("authorizationConfig").path("accessPointId").isMissingNode(),
"accessPointId should not be set");
}
}
@@ -0,0 +1,169 @@
package io.github.hectorvent.floci.services.ecs.container;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.core.command.WaitContainerResultCallback;
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
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.EfsVolumeConfiguration;
import io.github.hectorvent.floci.services.ecs.model.MountPoint;
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
import io.github.hectorvent.floci.services.ecs.model.Volume;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Docker-backed proof that an EFS-configured task volume's local mount is scoped to
* {@code (fileSystemId, rootDirectory, accessPointId)}, not just {@code fileSystemId} (#2563).
* Each scenario runs a real busybox container against the same {@code fileSystemId}, writing
* and checking marker files under {@code /mnt/efs} with a shell one-liner whose exit code
* proves whether the previous task's data was visible.
*/
@QuarkusTest
class EcsContainerManagerEfsIsolationDockerIntegrationTest {
private static final String IMAGE = "public.ecr.aws/docker/library/busybox:latest";
@Inject
EcsContainerManager containerManager;
@Inject
ContainerLifecycleManager lifecycleManager;
@Inject
DockerClient dockerClient;
private final List<String> volumesToCleanUp = new ArrayList<>();
@BeforeEach
void requireDocker() {
Assumptions.assumeTrue(isDockerAvailable(),
"Docker daemon must be available for ECS EFS isolation integration tests");
}
@AfterEach
void cleanUpVolumes() {
volumesToCleanUp.forEach(lifecycleManager::removeVolume);
volumesToCleanUp.clear();
}
/**
* Task A writes a marker under {@code rootDirectory} {@code /team-a}; task B, on the same
* file system but a different root, must not see it (if the bug were still present, one
* shared volume keyed only by {@code fileSystemId} would leak team-a's marker into team-b's
* task before it ever reaches its own write); a third task back on {@code /team-a} must
* likewise not see what team-b wrote.
*/
@Test
void differentRootDirectoriesOnSameFileSystemAreIsolated() {
String fileSystemId = "fs-" + UUID.randomUUID().toString().substring(0, 8);
assertEquals(0, runEfsCommand(fileSystemId, "/team-a", null,
"echo team-a > /mnt/efs/only-a.txt"),
"task A should write its marker cleanly");
assertEquals(0, runEfsCommand(fileSystemId, "/team-b", null,
"test ! -e /mnt/efs/only-a.txt && echo team-b > /mnt/efs/only-b.txt"),
"task B must not see task A's rootDirectory-scoped data");
assertEquals(0, runEfsCommand(fileSystemId, "/team-a", null,
"test ! -e /mnt/efs/only-b.txt"),
"task on /team-a must not see /team-b's data");
}
/**
* A second task using the identical {@code rootDirectory} must see the first task's data:
* sharing an identical configuration is existing, desired behaviour.
*/
@Test
void sameRootDirectoryOnSameFileSystemStillShares() {
String fileSystemId = "fs-" + UUID.randomUUID().toString().substring(0, 8);
assertEquals(0, runEfsCommand(fileSystemId, "/shared", null,
"echo hello > /mnt/efs/shared.txt"),
"first task should write its marker cleanly");
assertEquals(0, runEfsCommand(fileSystemId, "/shared", null,
"grep -qx hello /mnt/efs/shared.txt"),
"a second task with the same rootDirectory must share the first task's data");
}
/**
* A plain {@code rootDirectory} mount (no {@code accessPointId}) on the same file system
* must not see access-point-scoped data and vice versa, while a second task through the
* same access point must still share its data.
*/
@Test
void accessPointIdIsolatesFromRootDirectoryOnSameFileSystem() {
String fileSystemId = "fs-" + UUID.randomUUID().toString().substring(0, 8);
String accessPointId = "fsap-" + UUID.randomUUID().toString().substring(0, 8);
assertEquals(0, runEfsCommand(fileSystemId, null, accessPointId,
"echo via-ap > /mnt/efs/via-ap.txt"),
"task mounted through the access point should write its marker cleanly");
assertEquals(0, runEfsCommand(fileSystemId, "/", null,
"test ! -e /mnt/efs/via-ap.txt"),
"a plain rootDirectory mount must not see access-point-scoped data");
assertEquals(0, runEfsCommand(fileSystemId, null, accessPointId,
"grep -qx via-ap /mnt/efs/via-ap.txt"),
"a second task through the same access point must share its data");
}
/**
* Runs a single-container ECS task that mounts {@code fileSystemId} (scoped by
* {@code rootDirectory}/{@code accessPointId}) at {@code /mnt/efs} and executes
* {@code shellCommand}, then blocks until the container exits and returns its exit code.
*/
private int runEfsCommand(String fileSystemId, String rootDirectory, String accessPointId, String shellCommand) {
String suffix = UUID.randomUUID().toString().substring(0, 8);
ContainerDefinition app = new ContainerDefinition();
app.setName("app");
app.setImage(IMAGE);
app.setCommand(List.of("sh", "-c", shellCommand));
app.setMountPoints(List.of(new MountPoint("efs-data", "/mnt/efs", false)));
TaskDefinition taskDef = new TaskDefinition();
taskDef.setFamily("efs-isolation-" + suffix);
taskDef.setContainerDefinitions(List.of(app));
taskDef.setVolumes(List.of(new Volume("efs-data", null,
new EfsVolumeConfiguration(fileSystemId, rootDirectory, null, null, accessPointId, null))));
EcsTask task = new EcsTask();
task.setTaskArn("arn:aws:ecs:us-east-1:000000000000:task/efs-isolation/" + suffix);
volumesToCleanUp.add(EcsContainerManager.efsVolumeName(fileSystemId, accessPointId, rootDirectory));
EcsTaskHandle handle = containerManager.startTask(task, taskDef, List.of(), "us-east-1");
String dockerId = handle.getContainerIds().get("app");
try {
return dockerClient.waitContainerCmd(dockerId)
.exec(new WaitContainerResultCallback())
.awaitStatusCode(60, TimeUnit.SECONDS);
} finally {
containerManager.stopTask(handle);
}
}
private boolean isDockerAvailable() {
try {
dockerClient.pingCmd().exec();
return true;
} catch (Exception e) {
return false;
}
}
}
@@ -0,0 +1,87 @@
package io.github.hectorvent.floci.services.ecs.container;
import org.junit.jupiter.api.Test;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit tests for {@link EcsContainerManager#efsVolumeName}, the naming logic that scopes an
* EFS-backed task volume's shared Docker named volume to both {@code fileSystemId} and its
* effective root (#2563). The name must be stable for a given (fileSystemId, rootDirectory,
* accessPointId) combination (so identical mounts keep sharing one volume), distinct across
* different roots on the same file system (so mounts do not collide on the same tree), and
* always a legal Docker volume name.
*/
class EcsContainerManagerEfsVolumeNamingTest {
private static final Pattern DOCKER_VOLUME_NAME = Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9_.-]*$");
@Test
void defaultsToPlainFileSystemNameWhenNoRootOrAccessPointIsSet() {
assertEquals("floci-efs-fs-abc", EcsContainerManager.efsVolumeName("fs-abc", null, null));
assertEquals("floci-efs-fs-abc", EcsContainerManager.efsVolumeName("fs-abc", null, ""));
assertEquals("floci-efs-fs-abc", EcsContainerManager.efsVolumeName("fs-abc", null, "/"));
}
@Test
void differentRootDirectoriesOnSameFileSystemYieldDifferentVolumes() {
String rootA = EcsContainerManager.efsVolumeName("fs-shared", null, "/team-a");
String rootB = EcsContainerManager.efsVolumeName("fs-shared", null, "/team-b");
String rootNone = EcsContainerManager.efsVolumeName("fs-shared", null, null);
assertNotEquals(rootA, rootB, "different rootDirectory values must isolate the volume");
assertNotEquals(rootA, rootNone, "a non-root rootDirectory must not collapse to the plain name");
assertTrue(DOCKER_VOLUME_NAME.matcher(rootA).matches(), "must be docker-safe, was: " + rootA);
assertTrue(DOCKER_VOLUME_NAME.matcher(rootB).matches(), "must be docker-safe, was: " + rootB);
}
@Test
void sameRootDirectoryOnSameFileSystemStillShares() {
String first = EcsContainerManager.efsVolumeName("fs-shared", null, "/team-a");
String second = EcsContainerManager.efsVolumeName("fs-shared", null, "/team-a");
assertEquals(first, second, "identical (fileSystemId, rootDirectory) must keep sharing one volume");
}
@Test
void accessPointIdTakesPrecedenceOverRootDirectory() {
String viaAccessPoint = EcsContainerManager.efsVolumeName("fs-shared", "fsap-111", "/ignored");
String viaRootOnly = EcsContainerManager.efsVolumeName("fs-shared", null, "/ignored");
assertNotEquals(viaAccessPoint, viaRootOnly,
"an access point's own root must take precedence over rootDirectory, not merge with it");
}
@Test
void differentAccessPointsOnSameFileSystemYieldDifferentVolumes() {
String apOne = EcsContainerManager.efsVolumeName("fs-shared", "fsap-111", null);
String apTwo = EcsContainerManager.efsVolumeName("fs-shared", "fsap-222", null);
assertNotEquals(apOne, apTwo, "different accessPointId values must isolate the volume");
}
@Test
void sameAccessPointStillShares() {
String first = EcsContainerManager.efsVolumeName("fs-shared", "fsap-111", null);
String second = EcsContainerManager.efsVolumeName("fs-shared", "fsap-111", "/whatever-is-ignored");
assertEquals(first, second,
"the same accessPointId must keep sharing one volume regardless of rootDirectory");
}
@Test
void equivalentRootDirectorySpellingsShareTheSameVolume() {
String canonical = EcsContainerManager.efsVolumeName("fs-shared", null, "/team-a");
assertEquals(canonical, EcsContainerManager.efsVolumeName("fs-shared", null, "/team-a/"),
"a trailing slash must not create a distinct volume for the same directory");
assertEquals(canonical, EcsContainerManager.efsVolumeName("fs-shared", null, "//team-a"),
"a repeated leading slash must not create a distinct volume for the same directory");
assertEquals(canonical, EcsContainerManager.efsVolumeName("fs-shared", null, "team-a"),
"a missing leading slash must not create a distinct volume for the same directory");
}
}
@@ -123,6 +123,11 @@ class EcsContainerManagerVolumesTest {
verify(builder, never()).withBind("/app/nope", "/app/nope");
}
/**
* Each EFS volume resolves to a shared local Docker named volume keyed by file system id
* AND effective root (here: rootDirectory, since neither volume sets an accessPointId),
* read-write or read-only per the mountPoint.
*/
@Test
void efsVolumeMountPointsResolveToSharedNamedVolumesByAccessMode() {
ContainerDefinition app = new ContainerDefinition();
@@ -146,10 +151,10 @@ class EcsContainerManagerVolumesTest {
manager.startTask(task, taskDef, List.of(), "us-east-1");
// Each EFS volume -> a shared local Docker named volume keyed by file system id,
// read-write or read-only per the mountPoint.
verify(builder, times(1)).withNamedVolume("floci-efs-fs-0123456789abcdef0", "/mnt/efs", false);
verify(builder, times(1)).withNamedVolume("floci-efs-fs-00000000000000001", "/mnt/shared", true);
verify(builder, times(1)).withNamedVolume(
EcsContainerManager.efsVolumeName("fs-0123456789abcdef0", null, "/dps"), "/mnt/efs", false);
verify(builder, times(1)).withNamedVolume(
EcsContainerManager.efsVolumeName("fs-00000000000000001", null, null), "/mnt/shared", true);
// EFS volumes are never bind-mounted as host paths.
verify(builder, never()).withBind(any(), any());
@@ -188,7 +193,8 @@ class EcsContainerManagerVolumesTest {
configured.startTask(task, taskDef, List.of(), "us-east-1");
verify(lifecycleManager, times(1)).ensureSharedVolume("floci-efs-fs-abc",
verify(lifecycleManager, times(1)).ensureSharedVolume(
EcsContainerManager.efsVolumeName("fs-abc", null, "/dps"),
OptionalInt.of(1001), OptionalInt.of(1001), Optional.of("2775"), "busybox:stable");
}