fix(ecs): bound FireLens socket volume paths (#6787)

Bound the FireLens socket volume name while retaining full ownership labels and cleanup retries, and validate the inspected host path before creating containers.

Signed-off-by: Andre Kurait <akurait@amazon.com>
Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-28 00:30:44 -04:00
1 parent a46a2b32fd
commit 2d3fb0ce87
6 files changed
+143 -20

No files matched your search

@@ -335,8 +335,10 @@ public class EcsContainerManager {
if (firelensRouter != null) {
firelensConfig = firelensConfig(task, taskDef, firelensRouter, firelensLogOptions);
firelensExternalConfig = s3ExternalConfig(firelensRouter);
firelensVolumeName = ContainerStorageHelper.dockerName(config, "ecs-firelens-" + taskId);
lifecycleManager.ensureVolume(firelensVolumeName);
firelensVolumeName = firelensVolumeName(config, task.getTaskArn());
Map<String, String> labels = new LinkedHashMap<>(ownerLabels());
labels.put(ContainerStorageHelper.SERVICE_LABEL, "ecs");
lifecycleManager.ensureVolume(firelensVolumeName, labels);
firelensSocketAddress = unixSocketAddress(firelensVolumeName);
}
} catch (RuntimeException e) {
@@ -346,6 +348,14 @@ public class EcsContainerManager {
// so without this they would stay valid with nothing behind them until they expire.
revokeTaskRoleCredentials(task.getTaskArn());
releaseTaskLinkLocalAddresses(task.getTaskArn());
if (firelensVolumeName != null) {
EcsTaskHandle partial = new EcsTaskHandle(task.getTaskArn(), Map.of(), Map.of(),
firelensVolumeName, null, region);
removeTaskVolumes(partial);
if (partial.hasTaskVolumes()) {
throw new EcsTaskLaunchException(e, partial);
}
}
throw e;
}
@@ -1438,7 +1448,6 @@ public class EcsContainerManager {
cleanupProtectedNetwork(handle);
new ArrayList<>(handle.getLogStreamsByContainerId().keySet())
.forEach(dockerId -> finalizeLogStream(handle, dockerId));
removeFirelensVolume(handle);
revokeTaskRoleCredentials(handle.getTaskArn());
if (allRemoved) {
removeTaskVolumes(handle);
@@ -1529,7 +1538,6 @@ public class EcsContainerManager {
terminatedContainerIds.forEach(dockerId -> finalizeLogStream(handle, dockerId));
if (handle.allContainersRemoved()) {
cleanupProtectedNetwork(handle);
removeFirelensVolume(handle);
removeTaskVolumes(handle);
// Only now: a container that survived a failed removal still holds its address.
releaseTaskLinkLocalAddresses(handle.getTaskArn());
@@ -1574,13 +1582,6 @@ public class EcsContainerManager {
}
}
private void removeFirelensVolume(EcsTaskHandle handle) {
if (handle == null || handle.getFirelensVolumeName() == null) {
return;
}
lifecycleManager.removeVolume(handle.getFirelensVolumeName());
}
private void removeTaskVolumes(EcsTaskHandle handle) {
for (String volumeName : handle.getTaskVolumeNames()) {
if (lifecycleManager.removeVolume(volumeName)) {
@@ -1729,6 +1730,13 @@ public class EcsContainerManager {
: FirelensConfigGenerator.fluentBitConfig(ctx);
}
static String firelensVolumeName(EmulatorConfig config, String taskArn) {
// Keep the host Unix socket path bounded without shortening the ownership labels.
String identity = ContainerStorageHelper.ownerIdentity(config) + "\n" + taskArn;
return ContainerStorageHelper.dockerName(null,
"ecs-firelens-" + Digests.sha256Hex(identity).substring(0, 32));
}
private String unixSocketAddress(String volumeName) {
try {
String mountpoint = lifecycleManager.getDockerClient()
@@ -1740,6 +1748,10 @@ public class EcsContainerManager {
String path = mountpoint.endsWith("/")
? mountpoint + "fluent.sock"
: mountpoint + "/fluent.sock";
if (path.getBytes(StandardCharsets.UTF_8).length > 107) {
throw new AwsException("ClientException",
"FireLens socket path exceeds the Linux Unix socket limit of 107 bytes: " + path, 500);
}
return "unix://" + path;
} catch (AwsException e) {
throw e;
@@ -74,6 +74,9 @@ public class EcsTaskHandle {
this.networkInterfaceId = networkInterfaceId;
this.region = region;
this.taskVolumeNames = new LinkedHashSet<>(taskVolumeNames);
if (firelensVolumeName != null) {
this.taskVolumeNames.add(firelensVolumeName);
}
this.stopTimeouts = new LinkedHashMap<>(stopTimeouts);
}