feat(ecs): apply EFS access point POSIX ownership to shared local volumes (#1686)

* feat(ecs): apply EFS access point POSIX ownership to shared local volumes

#1569 materialises an ECS task's efsVolumeConfiguration volume as a shared
local Docker named volume (floci-efs-<fileSystemId>). Docker creates a named
volume root:root 0755, so a task whose image runs as a non-root USER — as
containers mounting an EFS access point typically do — cannot create files on
it and fails with EACCES.

Real EFS initialises an access point's root directory per
RootDirectory.CreationInfo {OwnerUid, OwnerGid, Permissions} and applies the
access point's PosixUser to all I/O regardless of the process uid. This change
emulates that mechanism for the shared local volume, driven by generic config:

- New floci.storage.efs.* block (owner-uid, owner-gid, root-permissions,
  setgid, init-image, mount-user, mount-group-add). Every value is empty/false
  by default, so a shared volume behaves exactly as before (a plain named
  volume, no ownership change) unless explicitly configured.
- ContainerLifecycleManager.ensureSharedVolume(...) ensures the volume then, if
  ownership is configured, chowns/chmods the mount root once (run-once guard;
  optional setgid so subdirectories inherit the owner gid) inside a short-lived
  helper container.
- ContainerSpec/ContainerBuilder gain withUser/withGroupAdd; wired into
  createContainerCmd.withUser and HostConfig.withGroupAdd.
- EcsContainerManager initialises the EFS volume root via ensureSharedVolume
  before mounting it.

Unit tests cover the no-op default (no helper container, no image pull), the
one-off chown/chmod/setgid helper with the run-once guard, the builder
round-trip, and the EcsContainerManager wiring.

* fix(ecs): apply configured EFS PosixUser to containers mounting a shared volume

Wire the previously-inert floci.storage.efs.mount-user / mount-group-add config into the
ECS container spec: a container mounting an EFS-backed shared volume now runs under the
configured uid[:gid] and adds the supplementary group, emulating the access point's
PosixUser squashing. Without this a non-root image USER could not write a volume owned by
owner-uid/owner-gid. Also add the floci.storage.efs.* section to application.yml (the
source of truth for effective defaults) and correct the root-permissions Javadoc, which
implied leaving it empty always skips volume init even when owner/setgid are set.

* docs: regenerate action tables
This commit is contained in:
Alex Banna authored and GitHub committed 2026-07-08 23:24:48 -05:00
1 parent 312fe377fc
commit 45ed00ebc7
11 files changed
+475 -4

No files matched your search

@@ -8,6 +8,7 @@ import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -98,6 +99,31 @@ class ContainerBuilderTest {
assertEquals("host", spec.cgroupnsMode());
}
@Test
void withUserAndGroupAddRoundTripIntoSpec() {
TestFixture fixture = new TestFixture();
ContainerSpec spec = fixture.builder.newContainer("alpine")
.withUser("1001:1001")
.withGroupAdd("1001")
.withGroupAdd("2000")
.withGroupAdd(" ") // blank ignored
.build();
assertEquals("1001:1001", spec.user());
assertEquals(List.of("1001", "2000"), spec.groupAdd());
}
@Test
void userAndGroupAddDefaultToUnset() {
TestFixture fixture = new TestFixture();
ContainerSpec spec = fixture.builder.newContainer("alpine").build();
assertNull(spec.user());
assertEquals(List.of(), spec.groupAdd());
}
@Test
void imageRegistryBasePrefixesEveryContainerImage() {
TestFixture fixture = new TestFixture();
@@ -1,10 +1,16 @@
package io.github.hectorvent.floci.core.common.docker;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.InspectVolumeCmd;
import com.github.dockerjava.api.command.InspectVolumeResponse;
import com.github.dockerjava.api.command.RemoveContainerCmd;
import com.github.dockerjava.api.command.StartContainerCmd;
import com.github.dockerjava.api.command.WaitContainerCmd;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.core.command.WaitContainerResultCallback;
import io.github.hectorvent.floci.services.lambda.launcher.ImageCacheService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -12,6 +18,9 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Optional;
import java.util.OptionalInt;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
@@ -99,5 +108,114 @@ class ContainerLifecycleManagerVolumeTest {
assertFalse(manager.volumeExists("some-volume"));
}
@Test
void ensureSharedVolume_noOwnershipConfig_createsVolumeButNoHelperContainer() {
InspectVolumeCmd cmd = mock(InspectVolumeCmd.class);
when(dockerClient.inspectVolumeCmd("shared")).thenReturn(cmd);
when(cmd.exec()).thenReturn(mock(InspectVolumeResponse.class)); // volume already exists
manager.ensureSharedVolume("shared", OptionalInt.empty(), OptionalInt.empty(),
Optional.empty(), "busybox:stable");
// No ownership requested -> degrades to a plain named volume: no helper container is
// spun up and no init image is pulled (proves the clean-source default is a no-op).
verify(dockerClient, never()).createContainerCmd(anyString());
verifyNoInteractions(imageCacheService);
}
@Test
void ensureSharedVolume_withOwnership_runsChownChmodHelperExactlyOnce() {
InspectVolumeCmd ivc = mock(InspectVolumeCmd.class);
when(dockerClient.inspectVolumeCmd("shared")).thenReturn(ivc);
when(ivc.exec()).thenReturn(mock(InspectVolumeResponse.class));
CreateContainerCmd ccc = mock(CreateContainerCmd.class, RETURNS_SELF);
when(dockerClient.createContainerCmd("busybox:stable")).thenReturn(ccc);
CreateContainerResponse resp = mock(CreateContainerResponse.class);
when(resp.getId()).thenReturn("helper-id");
when(ccc.exec()).thenReturn(resp);
StartContainerCmd scc = mock(StartContainerCmd.class);
when(dockerClient.startContainerCmd("helper-id")).thenReturn(scc);
WaitContainerCmd wcc = mock(WaitContainerCmd.class);
when(dockerClient.waitContainerCmd("helper-id")).thenReturn(wcc);
WaitContainerResultCallback wcb = mock(WaitContainerResultCallback.class);
when(wcc.exec(any(WaitContainerResultCallback.class))).thenReturn(wcb);
when(wcb.awaitStatusCode(anyLong(), any())).thenReturn(0);
RemoveContainerCmd rcc = mock(RemoveContainerCmd.class, RETURNS_SELF);
when(dockerClient.removeContainerCmd("helper-id")).thenReturn(rcc);
manager.ensureSharedVolume("shared", OptionalInt.of(1001), OptionalInt.of(1001),
Optional.of("2775"), "busybox:stable");
// Second call is a no-op thanks to the run-once guard.
manager.ensureSharedVolume("shared", OptionalInt.of(1001), OptionalInt.of(1001),
Optional.of("2775"), "busybox:stable");
verify(imageCacheService, times(1)).ensureImageExists("busybox:stable");
// The setgid bit is carried by the 4-digit octal (2775) rather than a separate chmod g+s.
verify(ccc, times(1)).withCmd("sh", "-c",
"chown 1001:1001 /floci-shared-volume && chmod 2775 /floci-shared-volume && true");
verify(dockerClient, times(1)).createContainerCmd("busybox:stable");
verify(dockerClient, times(1)).removeContainerCmd("helper-id");
}
@Test
void ensureSharedVolume_partialOwnershipConfig_isRejected() {
InspectVolumeCmd cmd = mock(InspectVolumeCmd.class);
when(dockerClient.inspectVolumeCmd("shared")).thenReturn(cmd);
when(cmd.exec()).thenReturn(mock(InspectVolumeResponse.class));
// owner-uid without owner-gid is not a valid CreationInfo — reject rather than emit
// a malformed `chown 1001:` that fails in busybox.
assertThrows(IllegalArgumentException.class, () ->
manager.ensureSharedVolume("shared", OptionalInt.of(1001), OptionalInt.empty(),
Optional.empty(), "busybox:stable"));
verify(dockerClient, never()).createContainerCmd(anyString());
}
@Test
void ensureSharedVolume_invalidRootPermissions_isRejected() {
InspectVolumeCmd cmd = mock(InspectVolumeCmd.class);
when(dockerClient.inspectVolumeCmd("shared")).thenReturn(cmd);
when(cmd.exec()).thenReturn(mock(InspectVolumeResponse.class));
// Non-octal permissions must be rejected before being spliced into the helper's sh -c.
assertThrows(IllegalArgumentException.class, () ->
manager.ensureSharedVolume("shared", OptionalInt.of(1001), OptionalInt.of(1001),
Optional.of("999"), "busybox:stable"));
verify(dockerClient, never()).createContainerCmd(anyString());
}
@Test
void ensureSharedVolume_helperNonZeroExit_retriesOnNextLaunch() {
InspectVolumeCmd ivc = mock(InspectVolumeCmd.class);
when(dockerClient.inspectVolumeCmd("shared")).thenReturn(ivc);
when(ivc.exec()).thenReturn(mock(InspectVolumeResponse.class));
CreateContainerCmd ccc = mock(CreateContainerCmd.class, RETURNS_SELF);
when(dockerClient.createContainerCmd("busybox:stable")).thenReturn(ccc);
CreateContainerResponse resp = mock(CreateContainerResponse.class);
when(resp.getId()).thenReturn("helper-id");
when(ccc.exec()).thenReturn(resp);
when(dockerClient.startContainerCmd("helper-id")).thenReturn(mock(StartContainerCmd.class));
WaitContainerCmd wcc = mock(WaitContainerCmd.class);
when(dockerClient.waitContainerCmd("helper-id")).thenReturn(wcc);
WaitContainerResultCallback wcb = mock(WaitContainerResultCallback.class);
when(wcc.exec(any(WaitContainerResultCallback.class))).thenReturn(wcb);
when(wcb.awaitStatusCode(anyLong(), any())).thenReturn(1); // helper fails
when(dockerClient.removeContainerCmd("helper-id")).thenReturn(mock(RemoveContainerCmd.class, RETURNS_SELF));
// A non-zero helper exit must not memoise the volume as initialised — the next launch
// retries rather than leaving the root root:root 0755 forever.
manager.ensureSharedVolume("shared", OptionalInt.of(1001), OptionalInt.of(1001),
Optional.of("0777"), "busybox:stable");
manager.ensureSharedVolume("shared", OptionalInt.of(1001), OptionalInt.of(1001),
Optional.of("0777"), "busybox:stable");
verify(dockerClient, times(2)).createContainerCmd("busybox:stable");
}
}
@@ -19,6 +19,8 @@ import org.mockito.ArgumentCaptor;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
@@ -140,4 +142,72 @@ class EcsContainerManagerVolumesTest {
verify(builder, never()).withBind(any(), any());
verify(builder, never()).withReadOnlyBind(any(), any());
}
@Test
void efsVolumesInitialiseSharedVolumeOwnershipFromConfig() {
// Rebuild the manager with an EFS config that emulates an access point's CreationInfo +
// PosixUser (owner 1001:1001, "2775" — 0775 plus the setgid special bit via the 4-digit
// octal). The EFS mount must initialise the shared volume root with exactly those values
// before mounting it.
EmulatorConfig cfg = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(cfg.storage().efs().ownerUid()).thenReturn(OptionalInt.of(1001));
when(cfg.storage().efs().ownerGid()).thenReturn(OptionalInt.of(1001));
when(cfg.storage().efs().rootPermissions()).thenReturn(Optional.of("2775"));
when(cfg.storage().efs().initImage()).thenReturn("busybox:stable");
EcsContainerManager configured = new EcsContainerManager(containerBuilder, lifecycleManager,
mock(ContainerLogStreamer.class), mock(ContainerDetector.class), cfg,
mock(RegionResolver.class));
ContainerDefinition app = new ContainerDefinition();
app.setName("app");
app.setImage("app:latest");
app.setMountPoints(List.of(new MountPoint("customer-data", "/mnt/efs", false)));
TaskDefinition taskDef = new TaskDefinition();
taskDef.setFamily("efs-family");
taskDef.setContainerDefinitions(List.of(app));
taskDef.setVolumes(List.of(new Volume("customer-data", null,
new EfsVolumeConfiguration("fs-abc", "/dps", "ENABLED", null, null, null))));
EcsTask task = new EcsTask();
task.setTaskArn("arn:aws:ecs:us-east-1:000000000000:task/test-cluster/efsown");
configured.startTask(task, taskDef, List.of(), "us-east-1");
verify(lifecycleManager, times(1)).ensureSharedVolume("floci-efs-fs-abc",
OptionalInt.of(1001), OptionalInt.of(1001), Optional.of("2775"), "busybox:stable");
}
@Test
void efsMountAppliesConfiguredPosixUserAndGroupToContainer() {
// Rebuild with an EFS config carrying a PosixUser (mount-user 1001:1001) and a supplementary
// group (mount-group-add 2000). A container mounting the EFS volume must run under that user
// and add that group, emulating the access point's PosixUser squashing — otherwise the
// container keeps its image USER and cannot write the volume owned by ownerUid/ownerGid.
EmulatorConfig cfg = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(cfg.storage().efs().mountUser()).thenReturn(Optional.of("1001:1001"));
when(cfg.storage().efs().mountGroupAdd()).thenReturn(OptionalInt.of(2000));
EcsContainerManager configured = new EcsContainerManager(containerBuilder, lifecycleManager,
mock(ContainerLogStreamer.class), mock(ContainerDetector.class), cfg,
mock(RegionResolver.class));
ContainerDefinition app = new ContainerDefinition();
app.setName("app");
app.setImage("app:latest");
app.setMountPoints(List.of(new MountPoint("customer-data", "/mnt/efs", false)));
TaskDefinition taskDef = new TaskDefinition();
taskDef.setFamily("efs-family");
taskDef.setContainerDefinitions(List.of(app));
taskDef.setVolumes(List.of(new Volume("customer-data", null,
new EfsVolumeConfiguration("fs-abc", "/dps", "ENABLED", null, null, null))));
EcsTask task = new EcsTask();
task.setTaskArn("arn:aws:ecs:us-east-1:000000000000:task/test-cluster/efsposix");
configured.startTask(task, taskDef, List.of(), "us-east-1");
verify(builder, times(1)).withUser("1001:1001");
verify(builder, times(1)).withGroupAdd("2000");
}
}