Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(ecs): apply EFS access point PosixUser and CreationInfo to task volumes (#5606)

Vũ Thế Huyb4c012fba4a8
diff --git a/docs/services/ecs.md b/docs/services/ecs.md
index 03a4adb1a0..ececc10029 100644
--- a/docs/services/ecs.md
+++ b/docs/services/ecs.md
@@ -914,7 +914,11 @@ A rejected `sourcePath` fails `RegisterTaskDefinition` with `InvalidParameterExc
### EFS volume ownership
-A task's `efsVolumeConfiguration` volumes are backed by shared local Docker volumes (Floci cannot mount a real EFS file system). A Docker named volume is created `root:root 0755`, so a task whose image runs as a non-root `USER` cannot write it. To emulate an [EFS access point](https://docs.aws.amazon.com/efs/latest/ug/efs-access-points.html)'s `RootDirectory.CreationInfo` and `PosixUser`, configure `floci.storage.efs.*` (all opt-in; the default is a plain named volume, so existing behaviour is unchanged):
+A task's `efsVolumeConfiguration` volumes are backed by shared local Docker volumes (Floci cannot mount a real EFS file system). When `authorizationConfig.accessPointId` identifies an access point stored by the EFS service in the task's region, Floci initialises the volume root using that access point's `RootDirectory.CreationInfo` (`OwnerUid`, `OwnerGid`, and `Permissions`). Its `PosixUser` adds the access point's GID and secondary GIDs to the mounting container as supplementary groups, preserving the container's own user. Access points with different settings initialise their own isolated volumes without requiring environment variables.
+
+The `floci.storage.efs.*` settings below provide the fallback: ownership and permissions when the access point has no `CreationInfo`, and user and supplementary group when it has no `PosixUser`. They also apply when no access point is specified or its metadata is unavailable; unavailable metadata produces a warning. Without access point settings or fallback configuration, a plain named volume retains Docker's `root:root 0755` defaults.
+
+Unlike [real EFS user enforcement](https://docs.aws.amazon.com/efs/latest/ug/enforce-identity-access-points.html), which applies identity per file system request, this local emulation preserves the container process identity and grants volume access by attaching the POSIX user's group and secondary groups as container supplementary groups. Explicit user overrides remain available via `floci.storage.efs.mount-user`. Root ownership retains unsigned 32-bit IDs, but the Docker runtime can reject supplementary group IDs above `2147483647`.
| Key (`floci.storage.efs.`) | Env | AWS equivalent | Description |
|---|---|---|---|
@@ -923,9 +927,9 @@ A task's `efsVolumeConfiguration` volumes are backed by shared local Docker volu
| `root-permissions` | `FLOCI_STORAGE_EFS_ROOT_PERMISSIONS` | `CreationInfo.Permissions` | 3-4 octal digits, e.g. `0777`, or `2775` for the setgid bit |
| `mount-user` | `FLOCI_STORAGE_EFS_MOUNT_USER` | `PosixUser {Uid,Gid}` | Run mounting containers as `uid[:gid]` |
| `mount-group-add` | `FLOCI_STORAGE_EFS_MOUNT_GROUP_ADD` | `PosixUser` supplementary | Supplementary gid added to mounting containers |
-| `init-image` | `FLOCI_STORAGE_EFS_INIT_IMAGE` | — | Image for the one-off `chown`/`chmod` of the volume root (default `busybox:stable`) |
+| `init-image` | `FLOCI_STORAGE_EFS_INIT_IMAGE` | N/A | Image for the one-off `chown`/`chmod` of the volume root (default `busybox:stable`) |
-`owner-uid` and `owner-gid` must be set together (a partial `CreationInfo` is not valid on AWS). The volume root is initialised once per volume; express the setgid bit through a 4-digit `root-permissions` (e.g. `2775`) so subdirectories inherit the owner gid.
+`owner-uid` and `owner-gid` must be set together (a partial `CreationInfo` is not valid on AWS). The volume root is initialised once per volume per Floci process; express the setgid bit through a 4-digit `root-permissions` (e.g. `2775`) so subdirectories inherit the owner gid. Initialisation changes only the root's ownership and permissions and preserves existing files. A Floci restart reapplies these settings on the next mount, unlike real EFS, which preserves permissions on an existing root directory.
### Mock mode
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java
index 716aeb5cdc..beb2cb4a3d 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java
@@ -44,6 +44,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
+import java.util.OptionalLong;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@@ -74,6 +75,9 @@ public class ContainerLifecycleManager {
private static final Pattern KEYRING_QUOTA_PATTERN =
Pattern.compile("join keyctl.*disk quota exceeded", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
+ /** Matches 3-4 octal digits (e.g. "0755", "755", "2775"), matching CreationInfo.Permissions. */
+ public static final Pattern PERMISSIONS_PATTERN = Pattern.compile("^[0-7]{3,4}$");
+
private static final long NANO_CPUS_PER_CPU = 1_000_000_000L;
private static final String CONTAINER_NETWORK_MODE_PREFIX = "container:";
@@ -656,6 +660,26 @@ public class ContainerLifecycleManager {
*/
public void ensureSharedVolume(String volumeName, OptionalInt ownerUid, OptionalInt ownerGid,
Optional<String> rootPermissions, String initImage) {
+ ensureSharedVolumeRoot(volumeName,
+ ownerUid.isPresent() ? OptionalLong.of(ownerUid.getAsInt()) : OptionalLong.empty(),
+ ownerGid.isPresent() ? OptionalLong.of(ownerGid.getAsInt()) : OptionalLong.empty(),
+ rootPermissions, initImage);
+ }
+
+ /** True when {@code permissions} is 3 to 4 octal digits (e.g. "0755", "755", "2775"). */
+ public static boolean isValidPermissions(String permissions) {
+ return permissions != null && PERMISSIONS_PATTERN.matcher(permissions).matches();
+ }
+
+ /** Initialises an access point's root without narrowing AWS's unsigned 32-bit POSIX ids. */
+ public void ensureSharedVolume(String volumeName, long ownerUid, long ownerGid,
+ String rootPermissions, String initImage) {
+ ensureSharedVolumeRoot(volumeName, OptionalLong.of(ownerUid), OptionalLong.of(ownerGid),
+ Optional.of(rootPermissions), initImage);
+ }
+
+ private void ensureSharedVolumeRoot(String volumeName, OptionalLong ownerUid, OptionalLong ownerGid,
+ Optional<String> rootPermissions, String initImage) {
// A volume created now is root:root 0755 again, so a memo left by a volume of the same name
// that was removed since (e.g. DeleteApplication, docker volume rm) is stale.
if (createVolumeIfAbsent(volumeName, ContainerStorageHelper.defaultLabels(config))) {
@@ -674,7 +698,7 @@ public class ContainerLifecycleManager {
// Validate before splicing into the helper's `sh -c`, matching CreationInfo.Permissions
// (^[0-7]{3,4}$), so a typo can't produce a mangled script that soft-fails.
rootPermissions.ifPresent(p -> {
- if (!p.matches("^[0-7]{3,4}$")) {
+ if (!isValidPermissions(p)) {
throw new IllegalArgumentException(
"floci.storage.efs root-permissions must be 3-4 octal digits (e.g. \"0777\","
+ " or \"2775\" for setgid): " + p);
@@ -694,7 +718,7 @@ public class ContainerLifecycleManager {
});
}
- private void initSharedVolumeRoot(String volumeName, OptionalInt ownerUid, OptionalInt ownerGid,
+ private void initSharedVolumeRoot(String volumeName, OptionalLong ownerUid, OptionalLong ownerGid,
Optional<String> rootPermissions, String initImage) {
String mount = "/floci-shared-volume";
StringBuilder script = new StringBuilder();
@@ -702,7 +726,7 @@ public class ContainerLifecycleManager {
// always has both operands (no trailing colon). setgid is expressed via a 4-digit octal
// rootPermissions (e.g. "2775"), matching CreationInfo.Permissions.
if (ownerUid.isPresent() && ownerGid.isPresent()) {
- script.append("chown ").append(ownerUid.getAsInt()).append(':').append(ownerGid.getAsInt())
+ script.append("chown ").append(ownerUid.getAsLong()).append(':').append(ownerGid.getAsLong())
.append(' ').append(mount).append(" && ");
}
rootPermissions.ifPresent(p -> script.append("chmod ").append(p).append(' ').append(mount).append(" && "));
diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java
index de674b2c49..7be5e6f09d 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java
@@ -53,6 +53,10 @@ import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
import io.github.hectorvent.floci.services.ecs.model.TaskNetworkInterface;
import io.github.hectorvent.floci.services.ecs.model.Volume;
import io.github.hectorvent.floci.services.ecs.model.VolumeFrom;
+import io.github.hectorvent.floci.services.efs.EfsService;
+import io.github.hectorvent.floci.services.efs.model.AccessPointDescription;
+import io.github.hectorvent.floci.services.efs.model.CreationInfo;
+import io.github.hectorvent.floci.services.efs.model.PosixUser;
import io.github.hectorvent.floci.services.lambda.launcher.ImageCacheService.LaunchImage;
import io.github.hectorvent.floci.services.s3.S3Service;
import io.github.hectorvent.floci.services.s3.model.S3Object;
@@ -142,6 +146,7 @@ public class EcsContainerManager {
private final EcsTaskRoleCredentials taskRoleCredentials;
private final EcsCredentialsProxy credentialsProxy;
private final EcsTaskLinkLocalAddresses linkLocalAddresses;
+ private final EfsService efsService;
// Stamped as RUN_LABEL on every container this process creates, so a sweep can tell its own apart.
private final String runId = UUID.randomUUID().toString();
// A sweep failure is retried before each task launch; each distinct failure is a WARN once.
@@ -167,7 +172,8 @@ public class EcsContainerManager {
SecurityGroupFirewallManager firewallManager,
EcsTaskRoleCredentials taskRoleCredentials,
EcsCredentialsProxy credentialsProxy,
- EcsTaskLinkLocalAddresses linkLocalAddresses) {
+ EcsTaskLinkLocalAddresses linkLocalAddresses,
+ EfsService efsService) {
this.containerBuilder = containerBuilder;
this.lifecycleManager = lifecycleManager;
this.logStreamer = logStreamer;
@@ -185,6 +191,29 @@ public class EcsContainerManager {
this.taskRoleCredentials = taskRoleCredentials;
this.credentialsProxy = credentialsProxy;
this.linkLocalAddresses = linkLocalAddresses;
+ this.efsService = efsService;
+ }
+
+ public EcsContainerManager(ContainerBuilder containerBuilder,
+ ContainerLifecycleManager lifecycleManager,
+ ContainerLogStreamer logStreamer,
+ ContainerDetector containerDetector,
+ EmulatorConfig config,
+ RegionResolver regionResolver,
+ LaunchedContainerAwsEnv awsEnv,
+ SsmService ssmService,
+ SecretsManagerService secretsManagerService,
+ S3Service s3Service,
+ EcrRegistryManager ecrRegistryManager,
+ HostVolumePolicy hostVolumePolicy,
+ Ec2Service ec2Service,
+ SecurityGroupFirewallManager firewallManager,
+ EcsTaskRoleCredentials taskRoleCredentials,
+ EcsCredentialsProxy credentialsProxy,
+ EcsTaskLinkLocalAddresses linkLocalAddresses) {
+ this(containerBuilder, lifecycleManager, logStreamer, containerDetector, config, regionResolver,
+ awsEnv, ssmService, secretsManagerService, s3Service, ecrRegistryManager, hostVolumePolicy,
+ ec2Service, firewallManager, taskRoleCredentials, credentialsProxy, linkLocalAddresses, null);
}
public EcsContainerManager(ContainerBuilder containerBuilder,
@@ -476,7 +505,7 @@ public class EcsContainerManager {
specBuilder.withBind(sourcePath, mp.containerPath());
}
} else if (efs != null) {
- mountEfsVolume(specBuilder, efs, mp);
+ mountEfsVolume(specBuilder, efs, mp, region);
} else if (ephemeralVolume != null) {
if (taskVolumeNames.add(ephemeralVolume)) {
Map<String, String> labels = new LinkedHashMap<>(ownerLabels());
@@ -2281,17 +2310,50 @@ public class EcsContainerManager {
* Materialises an EFS-configured task volume as a shared local Docker named volume scoped to
* both the file system and the mount's effective root (see {@link #efsVolumeToken}), then
* initialises the volume root's POSIX ownership to emulate the EFS access point's
- * RootDirectory.CreationInfo (no-op unless {@code floci.storage.efs} configures owner/permissions)
- * and applies the configured PosixUser emulation (uid[:gid] and/or supplementary group) so a
- * non-root task image can read/write the shared volume.
+ * RootDirectory.CreationInfo and PosixUser. Missing access point metadata falls back to
+ * {@code floci.storage.efs}. Identity is emulated by changing the container's user and groups.
*/
- private void mountEfsVolume(ContainerBuilder.Builder specBuilder, EfsVolumeConfiguration efs, MountPoint mp) {
+ private void mountEfsVolume(ContainerBuilder.Builder specBuilder, EfsVolumeConfiguration efs, MountPoint mp,
+ String region) {
String efsVolumeName = efsVolumeName(efs.fileSystemId(), efs.accessPointId(), efs.rootDirectory());
EmulatorConfig.EfsSharingConfig efsCfg = config.storage().efs();
- lifecycleManager.ensureSharedVolume(efsVolumeName,
- efsCfg.ownerUid(), efsCfg.ownerGid(), efsCfg.rootPermissions(),
- efsCfg.initImage());
+ AccessPointDescription accessPoint = resolveEfsAccessPoint(efs, region);
+ CreationInfo creation = accessPoint != null && accessPoint.getRootDirectory() != null
+ ? accessPoint.getRootDirectory().getCreationInfo() : null;
+ if (creation != null) {
+ if (creation.getOwnerUid() == null || creation.getOwnerGid() == null
+ || creation.getPermissions() == null) {
+ throw new AwsException("ClientException",
+ "EFS access point " + efs.accessPointId() + " has incomplete CreationInfo", 400);
+ }
+ if (!ContainerLifecycleManager.isValidPermissions(creation.getPermissions())) {
+ throw new AwsException("ClientException",
+ "EFS access point " + efs.accessPointId() + " has invalid CreationInfo permissions: "
+ + creation.getPermissions(), 400);
+ }
+ lifecycleManager.ensureSharedVolume(efsVolumeName, creation.getOwnerUid(), creation.getOwnerGid(),
+ creation.getPermissions(), efsCfg.initImage());
+ } else {
+ lifecycleManager.ensureSharedVolume(efsVolumeName,
+ efsCfg.ownerUid(), efsCfg.ownerGid(), efsCfg.rootPermissions(), efsCfg.initImage());
+ }
specBuilder.withNamedVolume(efsVolumeName, mp.containerPath(), mp.readOnly());
+ PosixUser posixUser = accessPoint != null ? accessPoint.getPosixUser() : null;
+ if (posixUser != null) {
+ if (posixUser.getUid() == null || posixUser.getGid() == null) {
+ throw new AwsException("ClientException",
+ "EFS access point " + efs.accessPointId() + " has incomplete PosixUser", 400);
+ }
+ specBuilder.withGroupAdd(String.valueOf(posixUser.getGid()));
+ if (posixUser.getSecondaryGids() != null) {
+ for (Long gid : posixUser.getSecondaryGids()) {
+ if (gid != null) {
+ specBuilder.withGroupAdd(String.valueOf(gid));
+ }
+ }
+ }
+ return;
+ }
efsCfg.mountUser().ifPresent(u -> {
if (!u.matches("^\\d+(:\\d+)?$")) {
throw new IllegalArgumentException(
@@ -2302,6 +2364,26 @@ public class EcsContainerManager {
efsCfg.mountGroupAdd().ifPresent(gid -> specBuilder.withGroupAdd(String.valueOf(gid)));
}
+ private AccessPointDescription resolveEfsAccessPoint(EfsVolumeConfiguration efs, String region) {
+ if (efs.accessPointId() == null || efs.accessPointId().isBlank()) {
+ return null;
+ }
+ List<AccessPointDescription> accessPoints = efsService != null
+ ? efsService.describeAccessPoints(region, null, efs.accessPointId()) : List.of();
+ if (accessPoints.isEmpty()) {
+ LOG.warnv("EFS access point {0} metadata unavailable in region {1}; using floci.storage.efs fallback",
+ efs.accessPointId(), region);
+ return null;
+ }
+ AccessPointDescription accessPoint = accessPoints.getFirst();
+ if (!efs.fileSystemId().equals(accessPoint.getFileSystemId())) {
+ throw new AwsException("ClientException",
+ "EFS access point " + efs.accessPointId() + " does not belong to file system " + efs.fileSystemId(),
+ 400);
+ }
+ return accessPoint;
+ }
+
/**
* Name of the local Docker named volume backing an EFS-configured task volume, scoped to
* both the file system and the mount's effective root so two mounts of the same file system
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManagerVolumeTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManagerVolumeTest.java
index bc64df6787..0fd8eeffdb 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManagerVolumeTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManagerVolumeTest.java
@@ -506,6 +506,18 @@ class ContainerLifecycleManagerVolumeTest {
verify(dockerClient, never()).createContainerCmd(anyString());
}
+ @Test
+ void isValidPermissions_matchesThreeOrFourOctalDigits() {
+ assertTrue(ContainerLifecycleManager.isValidPermissions("755"));
+ assertTrue(ContainerLifecycleManager.isValidPermissions("0755"));
+ assertTrue(ContainerLifecycleManager.isValidPermissions("2775"));
+ assertFalse(ContainerLifecycleManager.isValidPermissions(null));
+ assertFalse(ContainerLifecycleManager.isValidPermissions(""));
+ assertFalse(ContainerLifecycleManager.isValidPermissions("888"));
+ assertFalse(ContainerLifecycleManager.isValidPermissions("07777"));
+ assertFalse(ContainerLifecycleManager.isValidPermissions("rwxr-xr-x"));
+ }
+
@Test
void ensureSharedVolume_helperNonZeroExit_retriesOnNextLaunch() {
InspectVolumeCmd ivc = mock(InspectVolumeCmd.class);
diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerEfsIsolationDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerEfsIsolationDockerIntegrationTest.java
index bdd6b09854..c695d2c511 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerEfsIsolationDockerIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerEfsIsolationDockerIntegrationTest.java
@@ -9,6 +9,13 @@ 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.github.hectorvent.floci.services.efs.EfsService;
+import io.github.hectorvent.floci.services.efs.model.AccessPointDescription;
+import io.github.hectorvent.floci.services.efs.model.CreateAccessPointRequest;
+import io.github.hectorvent.floci.services.efs.model.CreateFileSystemRequest;
+import io.github.hectorvent.floci.services.efs.model.CreationInfo;
+import io.github.hectorvent.floci.services.efs.model.PosixUser;
+import io.github.hectorvent.floci.services.efs.model.RootDirectory;
import io.github.hectorvent.floci.testing.TestImages;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
@@ -45,6 +52,9 @@ class EcsContainerManagerEfsIsolationDockerIntegrationTest {
@Inject
DockerClient dockerClient;
+ @Inject
+ EfsService efsService;
+
private final List<String> volumesToCleanUp = new ArrayList<>();
@BeforeEach
@@ -123,6 +133,47 @@ class EcsContainerManagerEfsIsolationDockerIntegrationTest {
"a second task through the same access point must share its data");
}
+ @Test
+ void accessPointsApplyTheirOwnOwnershipPermissionsAndIdentity() {
+ CreateFileSystemRequest fileSystem = new CreateFileSystemRequest();
+ fileSystem.setCreationToken(UUID.randomUUID().toString());
+ String fileSystemId = efsService.createFileSystem(fileSystem, "us-east-1").getFileSystemId();
+ List<String> accessPoints = new ArrayList<>();
+ try {
+ for (long uid : List.of(10001L, 10002L, 3000000000L)) {
+ long processUid = uid == 3000000000L ? 10003L : uid;
+ CreationInfo creation = new CreationInfo();
+ creation.setOwnerUid(uid);
+ creation.setOwnerGid(processUid);
+ String mode = uid == 10001 ? "750" : uid == 10002 ? "700" : "770";
+ creation.setPermissions("0" + mode);
+ RootDirectory root = new RootDirectory();
+ root.setPath("/user-" + uid);
+ root.setCreationInfo(creation);
+ PosixUser user = new PosixUser();
+ user.setUid(processUid);
+ user.setGid(processUid);
+ user.setSecondaryGids(List.of(20001L, 20002L));
+ CreateAccessPointRequest request = new CreateAccessPointRequest();
+ request.setFileSystemId(fileSystemId);
+ request.setRootDirectory(root);
+ request.setPosixUser(user);
+ AccessPointDescription ap = efsService.createAccessPoint("us-east-1", request);
+ accessPoints.add(ap.getAccessPointId());
+ String check = "test \"$(stat -c '%u:%g %a' /mnt/efs)\" = '" + uid + ":" + processUid + " " + mode
+ + "' && id -G | grep -qw " + processUid
+ + " && id -G | grep -qw 20001 && id -G | grep -qw 20002";
+ assertEquals(0, runEfsCommand(fileSystemId, null, ap.getAccessPointId(),
+ check + " && mkdir /mnt/efs/sub && echo persisted > /mnt/efs/sub/marker"));
+ assertEquals(0, runEfsCommand(fileSystemId, null, ap.getAccessPointId(),
+ check + " && grep -qx persisted /mnt/efs/sub/marker"));
+ }
+ } finally {
+ accessPoints.forEach(id -> efsService.deleteAccessPoint("us-east-1", id));
+ efsService.deleteFileSystem("us-east-1", fileSystemId);
+ }
+ }
+
/**
* Runs a single-container ECS task that mounts {@code fileSystemId} (scoped by
* {@code rootDirectory}/{@code accessPointId}) at {@code /mnt/efs} and executes
@@ -156,6 +207,7 @@ class EcsContainerManagerEfsIsolationDockerIntegrationTest {
.awaitStatusCode(60, TimeUnit.SECONDS);
} finally {
containerManager.stopTask(handle);
+ containerManager.cleanupStoppedTask(handle);
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesTest.java
index a7eba5b702..ab02341d75 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesTest.java
@@ -17,6 +17,11 @@ 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.github.hectorvent.floci.services.efs.EfsService;
+import io.github.hectorvent.floci.services.efs.model.AccessPointDescription;
+import io.github.hectorvent.floci.services.efs.model.CreationInfo;
+import io.github.hectorvent.floci.services.efs.model.PosixUser;
+import io.github.hectorvent.floci.services.efs.model.RootDirectory;
import io.github.hectorvent.floci.services.lambda.launcher.ImageCacheService.LaunchImage;
import io.github.hectorvent.floci.services.s3.S3Service;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
@@ -28,6 +33,8 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
+import java.nio.file.Path;
+import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -73,6 +80,7 @@ class EcsContainerManagerVolumesTest {
private LaunchedContainerAwsEnv awsEnv;
private EcrRegistryManager ecrRegistryManager;
private EcsContainerManager manager;
+ private EfsService efsService;
private static Stream<Volume> ephemeralVolumes() {
return Stream.of(new Volume("shared", null),
@@ -244,6 +252,7 @@ class EcsContainerManagerVolumesTest {
@BeforeEach
void setUp() {
+ efsService = mock(EfsService.class);
builder = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
containerBuilder = mock(ContainerBuilder.class);
when(containerBuilder.newContainer(anyString())).thenReturn(builder);
@@ -290,9 +299,12 @@ class EcsContainerManagerVolumesTest {
TaskDefinition taskDef = new TaskDefinition();
taskDef.setFamily("test-family");
taskDef.setContainerDefinitions(List.of(app));
+ // "/host/abs" has no drive letter on Windows, so build the absolute paths per platform.
+ String configHostPath = Path.of("/host/abs/app/config.yml").toAbsolutePath().toString();
+ String awsHostPath = Path.of("/host/abs/home/.aws").toAbsolutePath().toString();
taskDef.setVolumes(List.of(
- new Volume("config-vol", "/host/abs/app/config.yml"),
- new Volume("aws-vol", "/host/abs/home/.aws")));
+ new Volume("config-vol", configHostPath),
+ new Volume("aws-vol", awsHostPath)));
EcsTask task = new EcsTask();
task.setTaskArn("arn:aws:ecs:us-east-1:000000000000:task/test-cluster/abc123");
@@ -303,14 +315,14 @@ class EcsContainerManagerVolumesTest {
ArgumentCaptor<String> roHost = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> roPath = ArgumentCaptor.forClass(String.class);
verify(builder, times(1)).withReadOnlyBind(roHost.capture(), roPath.capture());
- assertEquals("/host/abs/app/config.yml", roHost.getValue());
+ assertEquals(configHostPath, roHost.getValue());
assertEquals("/app/config.yml", roPath.getValue());
// Read-write mountPoint -> withBind(hostSourcePath, containerPath).
ArgumentCaptor<String> rwHost = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> rwPath = ArgumentCaptor.forClass(String.class);
verify(builder, times(1)).withBind(rwHost.capture(), rwPath.capture());
- assertEquals("/host/abs/home/.aws", rwHost.getValue());
+ assertEquals(awsHostPath, rwHost.getValue());
assertEquals("/root/.aws", rwPath.getValue());
// The unresolved "missing-vol" mountPoint contributes no bind beyond the two above.
@@ -394,6 +406,150 @@ class EcsContainerManagerVolumesTest {
OptionalInt.of(1001), OptionalInt.of(1001), Optional.of("2775"), "busybox:stable");
}
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void accessPointSettingsOverrideEachFallbackIndependently(boolean creationInfo) {
+ EmulatorConfig cfg = efsFallbackConfig();
+ AccessPointDescription ap = new AccessPointDescription();
+ ap.setFileSystemId("fs-test");
+ if (creationInfo) {
+ CreationInfo creation = new CreationInfo();
+ creation.setOwnerUid(3000000000L);
+ creation.setOwnerGid(3000000001L);
+ creation.setPermissions("0750");
+ RootDirectory root = new RootDirectory();
+ root.setCreationInfo(creation);
+ ap.setRootDirectory(root);
+ } else {
+ PosixUser user = new PosixUser();
+ user.setUid(10001L);
+ user.setGid(10002L);
+ user.setSecondaryGids(List.of(20001L, 20002L));
+ ap.setPosixUser(user);
+ }
+ when(efsService.describeAccessPoints("eu-west-1", null, "fsap-test")).thenReturn(List.of(ap));
+ managerWithEfs(cfg).startTask(task("000000000000", "eu-west-1"),
+ accessPointDefinition("fsap-test"), List.of(), "eu-west-1");
+ if (creationInfo) {
+ verify(lifecycleManager).ensureSharedVolume(anyString(), eq(3000000000L), eq(3000000001L),
+ eq("0750"), eq("busybox:stable"));
+ verify(builder).withUser("3001:3002");
+ verify(builder).withGroupAdd("4001");
+ } else {
+ verifyFallbackOwnership();
+ verify(builder).withUser("42:42");
+ verify(builder).withGroupAdd("10002");
+ verify(builder).withGroupAdd("20001");
+ verify(builder).withGroupAdd("20002");
+ verify(builder, never()).withUser("10001:10002");
+ verify(builder, never()).withUser("3001:3002");
+ verify(builder, never()).withGroupAdd("4001");
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void accessPointWithoutSettingsOrUnavailableMetadataUsesFallback(boolean exists) {
+ AccessPointDescription ap = new AccessPointDescription();
+ ap.setFileSystemId("fs-test");
+ when(efsService.describeAccessPoints("us-east-1", null, "fsap-test"))
+ .thenReturn(exists ? List.of(ap) : List.of());
+ managerWithEfs(efsFallbackConfig()).startTask(task("000000000000", "us-east-1"),
+ accessPointDefinition("fsap-test"), List.of(), "us-east-1");
+ verifyFallbackOwnership();
+ verify(builder).withUser("3001:3002");
+ verify(builder).withGroupAdd("4001");
+ }
+
+ @Test
+ void accessPointOnAnotherFileSystemRejectsLaunch() {
+ AccessPointDescription ap = new AccessPointDescription();
+ ap.setFileSystemId("fs-other");
+ when(efsService.describeAccessPoints("us-east-1", null, "fsap-test")).thenReturn(List.of(ap));
+ AwsException failure = assertThrows(AwsException.class,
+ () -> managerWithEfs(efsFallbackConfig()).startTask(task("000000000000", "us-east-1"),
+ accessPointDefinition("fsap-test"), List.of(), "us-east-1"));
+ assertEquals("ClientException", failure.getErrorCode());
+ verify(lifecycleManager, never()).createAndStart(any());
+ }
+
+ @Test
+ void accessPointPosixUserWithNullSecondaryGidSkipsNull() {
+ EmulatorConfig cfg = efsFallbackConfig();
+ AccessPointDescription ap = new AccessPointDescription();
+ ap.setFileSystemId("fs-test");
+ PosixUser user = new PosixUser();
+ user.setUid(10001L);
+ user.setGid(10002L);
+ user.setSecondaryGids(Arrays.asList(20001L, null, 20002L));
+ ap.setPosixUser(user);
+ when(efsService.describeAccessPoints("eu-west-1", null, "fsap-test")).thenReturn(List.of(ap));
+ managerWithEfs(cfg).startTask(task("000000000000", "eu-west-1"),
+ accessPointDefinition("fsap-test"), List.of(), "eu-west-1");
+ verify(builder).withGroupAdd("10002");
+ verify(builder).withGroupAdd("20001");
+ verify(builder).withGroupAdd("20002");
+ verify(builder, never()).withGroupAdd("null");
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"invalid", "888", "07777", ""})
+ void accessPointWithInvalidCreationInfoPermissionsRejectsLaunch(String permissions) {
+ AccessPointDescription ap = new AccessPointDescription();
+ ap.setFileSystemId("fs-test");
+ CreationInfo creation = new CreationInfo();
+ creation.setOwnerUid(1000L);
+ creation.setOwnerGid(1000L);
+ creation.setPermissions(permissions);
+ RootDirectory root = new RootDirectory();
+ root.setCreationInfo(creation);
+ ap.setRootDirectory(root);
+ when(efsService.describeAccessPoints("us-east-1", null, "fsap-test")).thenReturn(List.of(ap));
+ AwsException failure = assertThrows(AwsException.class,
+ () -> managerWithEfs(efsFallbackConfig()).startTask(task("000000000000", "us-east-1"),
+ accessPointDefinition("fsap-test"), List.of(), "us-east-1"));
+ assertEquals("ClientException", failure.getErrorCode());
+ assertTrue(failure.getMessage().contains("permissions"));
+ }
+
+ private EmulatorConfig efsFallbackConfig() {
+ 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(1002));
+ when(cfg.storage().efs().rootPermissions()).thenReturn(Optional.of("0775"));
+ when(cfg.storage().efs().initImage()).thenReturn("busybox:stable");
+ when(cfg.storage().efs().mountUser()).thenReturn(Optional.of("3001:3002"));
+ when(cfg.storage().efs().mountGroupAdd()).thenReturn(OptionalInt.of(4001));
+ return cfg;
+ }
+
+ private EcsContainerManager managerWithEfs(EmulatorConfig cfg) {
+ return new EcsContainerManager(containerBuilder, lifecycleManager,
+ mock(ContainerLogStreamer.class), mock(ContainerDetector.class), cfg,
+ mock(RegionResolver.class), awsEnv, mock(SsmService.class),
+ mock(SecretsManagerService.class), mock(S3Service.class), ecrRegistryManager, new HostVolumePolicy(cfg),
+ null, null, null, null, null, efsService);
+ }
+
+ private static TaskDefinition accessPointDefinition(String accessPointId) {
+ ContainerDefinition app = new ContainerDefinition();
+ app.setName("app");
+ app.setImage("app:latest");
+ app.setUser("42:42");
+ app.setMountPoints(List.of(new MountPoint("data", "/data", false)));
+ TaskDefinition definition = new TaskDefinition();
+ definition.setFamily("efs-access-point");
+ definition.setContainerDefinitions(List.of(app));
+ definition.setVolumes(List.of(new Volume("data", null,
+ new EfsVolumeConfiguration("fs-test", null, "ENABLED", null, accessPointId, null))));
+ return definition;
+ }
+
+ private void verifyFallbackOwnership() {
+ verify(lifecycleManager).ensureSharedVolume(anyString(), eq(OptionalInt.of(1001)), eq(OptionalInt.of(1002)),
+ eq(Optional.of("0775")), eq("busybox:stable"));
+ }
+
@Test
void efsMountAppliesConfiguredPosixUserAndGroupToContainer() {
// Rebuild with an EFS config carrying a PosixUser (mount-user 1001:1001) and a supplementary