mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(ecs): apply EFS access point PosixUser and CreationInfo to task volumes (#5606)
* fix(ecs): apply EFS access point PosixUser and CreationInfo to task volumes An ECS task mounting EFS through an access point ignored the access point's own settings, so the volume stayed root:root 0755 and a non-root task could not write to it. Resolve the access point from EfsService and initialise the volume root from RootDirectory.CreationInfo, and run the container as PosixUser (uid:gid plus secondary gids). Each part falls back independently to floci.storage.efs.*. An access point on another file system, or with incomplete CreationInfo/PosixUser, rejects the launch. Owner ids use long so unsigned 32-bit POSIX ids are not narrowed. Closes #5480 * fix(ecs): attach PosixUser groups as supplementary groups instead of replacing container user Preserve the container definition or image user when mounting an EFS access point, and attach the PosixUser gid and secondary gids as supplementary groups instead of overriding the container user. Also stabilize ElbV2PreserveHostHeaderIntegrationTest socket connection to avoid public DNS resolution timeouts in CI. * fix(ecs): validate CreationInfo permissions and filter null secondary GIDs * revert(elbv2): revert listener test wait and loopback address change * fix(ecs): share octal permissions check with ContainerLifecycleManager
This commit is contained in:
1 parent
fa682b2d62
commit
b4c012fba4
6 files changed
+349
-19
No files matched your search
+27
-3
@@ -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(" && "));
|
||||
|
||||
+91
-9
@@ -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
|
||||
|
||||
Reference in new issue
Block a user