feat(cloudformation): forward ECS task volumes and mounts (#8601)

* feat(cloudformation): forward ECS task volumes and mounts
* fix(cloudformation): share ECS volume parsing and validate mounts
* fix(ecs): share volume validation across API and CloudFormation
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-16 15:38:28 -04:00
1 parent 7331a6b1ce
commit a35fcfd9da
9 files changed
+596 -7

No files matched your search

@@ -4,10 +4,12 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.cloudformation.model.StackResource;
import io.floci.aws.services.ecs.EcsJsonHandler;
import io.floci.aws.services.ecs.EcsService;
import io.floci.aws.services.ecs.model.AwsVpcConfiguration;
import io.floci.aws.services.ecs.model.CapacityProviderStrategyItem;
@@ -59,10 +61,12 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
private static final TypeReference<List<ClusterSetting>> SETTINGS = new TypeReference<>() { };
private final EcsService ecsService;
private final EcsJsonHandler ecsJsonHandler;
@Inject
public EcsCfnProvisioner(EcsService ecsService) {
public EcsCfnProvisioner(EcsService ecsService, EcsJsonHandler ecsJsonHandler) {
this.ecsService = ecsService;
this.ecsJsonHandler = ecsJsonHandler;
}
@Override
@@ -262,6 +266,9 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
RegisterTaskDefinitionRequest request = new RegisterTaskDefinitionRequest();
request.setFamily(family);
request.setContainerDefinitions(parseContainerDefinitions(props.get("ContainerDefinitions"), ctx));
JsonNode volumes = props.path("Volumes");
request.setVolumes(ecsJsonHandler.parseVolumes(normalizeCfnEcsArray(volumes)));
EcsJsonHandler.validateMountSources(request.getContainerDefinitions(), request.getVolumes());
request.setNetworkMode(parseEnum(NetworkMode.class, ctx.resolveOptional(props, "NetworkMode")));
request.setCpu(ctx.resolveOptional(props, "Cpu"));
request.setMemory(ctx.resolveOptional(props, "Memory"));
@@ -516,7 +523,7 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
() -> ecsService.deleteService(cluster, name, true, region), "ServiceNotFoundException");
}
private static List<ContainerDefinition> parseContainerDefinitions(JsonNode node, ProvisionContext ctx) {
private List<ContainerDefinition> parseContainerDefinitions(JsonNode node, ProvisionContext ctx) {
List<ContainerDefinition> result = new ArrayList<>();
if (node == null || node.isNull()) {
return result;
@@ -540,6 +547,7 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
def.setMemoryReservation(item.path("MemoryReservation").asInt());
}
def.setPortMappings(parsePortMappings(item.path("PortMappings")));
def.setMountPoints(ecsJsonHandler.parseMountPoints(normalizeCfnEcsArray(item.path("MountPoints"))));
def.setEnvironment(parseEnvironment(item.path("Environment")));
def.setSecrets(parseSecrets(item.path("Secrets")));
def.setLogConfiguration(parseLogConfiguration(item.path("LogConfiguration")));
@@ -573,6 +581,49 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
optionalInt(node, "Timeout"), optionalInt(node, "Retries"), optionalInt(node, "StartPeriod"));
}
private static ArrayNode normalizeCfnEcsArray(JsonNode node) {
ArrayNode result = MAPPER.createArrayNode();
if (node != null && node.isArray()) {
node.forEach(item -> {
if (item.isObject()) {
result.add(normalizeCfnEcsNode(item));
}
});
}
return result;
}
private static JsonNode normalizeCfnEcsNode(JsonNode node) {
if (node.isObject()) {
ObjectNode result = MAPPER.createObjectNode();
node.fields().forEachRemaining(field -> {
String key = field.getKey();
JsonNode value = field.getValue();
String apiKey = switch (key) {
case "EFSVolumeConfiguration" -> "efsVolumeConfiguration";
case "FSxWindowsFileServerVolumeConfiguration" -> "fsxWindowsFileServerVolumeConfiguration";
case "S3FilesVolumeConfiguration" -> "s3filesVolumeConfiguration";
case "FilesystemId" -> "fileSystemId";
case "IAM" -> "iam";
default -> key.isEmpty() ? key : Character.toLowerCase(key.charAt(0)) + key.substring(1);
};
if ("TransitEncryptionPort".equals(key) && value.isTextual()) {
result.put(apiKey, value.asInt());
} else {
result.set(apiKey, "DriverOpts".equals(key) || "Labels".equals(key)
? value.deepCopy() : normalizeCfnEcsNode(value));
}
});
return result;
}
if (node.isArray()) {
ArrayNode result = MAPPER.createArrayNode();
node.forEach(item -> result.add(normalizeCfnEcsNode(item)));
return result;
}
return node.deepCopy();
}
private static Integer optionalInt(JsonNode node, String member) {
return node.hasNonNull(member) ? node.path(member).asInt() : null;
}
@@ -302,6 +302,7 @@ public class EcsJsonHandler {
? req.path("executionRoleArn").asText() : null);
request.setRequiresCompatibilities(parseCompatibilities(req.path("requiresCompatibilities")));
request.setVolumes(parseVolumes(req.path("volumes")));
validateMountSources(request.getContainerDefinitions(), request.getVolumes());
request.setRuntimePlatform(parseRuntimePlatform(req.path("runtimePlatform")));
request.setEphemeralStorage(parseEphemeralStorage(req.path("ephemeralStorage")));
request.setPidMode(parseChoice(req, "pidMode", PID_MODES));
@@ -1493,12 +1494,13 @@ public class EcsJsonHandler {
return new HealthCheck(command, interval, timeout, retries, startPeriod);
}
private List<Volume> parseVolumes(JsonNode node) {
public List<Volume> parseVolumes(JsonNode node) {
List<Volume> result = new ArrayList<>();
if (!node.isArray()) {
return result;
}
for (JsonNode item : node) {
validateVolume(item);
String hostSourcePath = item.path("host").path("sourcePath").asText(null);
if (hostSourcePath != null && !hostSourcePath.isBlank()) {
hostVolumePolicy.validate(hostSourcePath);
@@ -1513,6 +1515,61 @@ public class EcsJsonHandler {
return result;
}
private static void validateVolume(JsonNode item) {
if (!item.isObject()) {
return;
}
JsonNode efs = item.path("efsVolumeConfiguration");
String name = item.path("name").asText(null);
if ((efs.isObject() || item.path("s3filesVolumeConfiguration").isObject()
|| item.path("configuredAtLaunch").asBoolean(false))
&& (name == null || name.isBlank())) {
throw new AwsException("InvalidParameterException",
"EFS, S3 Files, and configured-at-launch volumes require a name.", 400);
}
if (efs.isObject()) {
JsonNode authorization = efs.path("authorizationConfig");
String accessPointId = authorization.path("accessPointId").asText(null);
String transitEncryption = efs.path("transitEncryption").asText(null);
String iam = authorization.path("iam").asText(null);
if (((accessPointId != null && !accessPointId.isBlank()) || "ENABLED".equals(iam))
&& !"ENABLED".equals(transitEncryption)) {
throw new AwsException("InvalidParameterException",
"Transit encryption must be ENABLED when EFS access point or IAM authorization is used.", 400);
}
}
int configurationCount = efs.isObject() ? 1 : 0;
for (String member : List.of("host", "dockerVolumeConfiguration",
"fsxWindowsFileServerVolumeConfiguration", "s3filesVolumeConfiguration", "configuredAtLaunch")) {
if (item.hasNonNull(member) && (!"configuredAtLaunch".equals(member) || item.path(member).asBoolean())) {
configurationCount++;
}
}
if (configurationCount > 1) {
throw new AwsException("InvalidParameterException",
"A task definition volume can specify only one volume configuration.", 400);
}
}
public static void validateMountSources(List<ContainerDefinition> containers, List<Volume> volumes) {
if (containers == null) {
return;
}
for (ContainerDefinition container : containers) {
if (container.getMountPoints() == null) {
continue;
}
for (MountPoint mount : container.getMountPoints()) {
String source = mount.sourceVolume();
if (source != null && !source.isBlank()
&& (volumes == null || volumes.stream().noneMatch(volume -> source.equals(volume.name())))) {
throw new AwsException("InvalidParameterException",
"Mount point sourceVolume must reference a task definition volume: " + source, 400);
}
}
}
}
/** Volume members the parser consumes; the rest round-trips verbatim. */
private static final Set<String> VOLUME_CONSUMED = Set.of("name", "efsVolumeConfiguration");
@@ -1548,7 +1605,7 @@ public class EcsJsonHandler {
auth.path("iam").asText(null));
}
private List<MountPoint> parseMountPoints(JsonNode node) {
public List<MountPoint> parseMountPoints(JsonNode node) {
List<MountPoint> result = new ArrayList<>();
if (!node.isArray()) {
return result;