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

@@ -5,6 +5,8 @@ import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.StreamType;
import com.github.dockerjava.core.command.ExecStartResultCallback;
import com.github.dockerjava.core.command.LogContainerResultCallback;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
import io.github.hectorvent.floci.services.ecs.model.EcsTask;
import io.github.hectorvent.floci.services.ecs.model.FirelensConfiguration;
@@ -45,6 +47,9 @@ class EcsContainerManagerFirelensDockerIntegrationTest {
@Inject
DockerClient dockerClient;
@Inject
EmulatorConfig config;
private EcsTaskHandle taskHandle;
@BeforeEach
@@ -92,7 +97,16 @@ class EcsContainerManagerFirelensDockerIntegrationTest {
// The Docker daemon, not the application container, makes this connection, so the
// address has to be a path on the host: the socket the router creates inside the
// task's own volume, which the daemon reaches at the volume's mountpoint.
assertEquals("unix://" + firelensVolumeMountpoint() + "/fluent.sock", appLogDriverAddress(appId));
String socketPath = firelensVolumeMountpoint() + "/fluent.sock";
assertTrue(socketPath.getBytes(StandardCharsets.UTF_8).length <= 107);
assertTrue(taskHandle.getFirelensVolumeName().length() <= 55);
assertEquals("unix://" + socketPath, appLogDriverAddress(appId));
Map<String, String> labels = dockerClient.inspectVolumeCmd(taskHandle.getFirelensVolumeName())
.exec().getLabels();
assertEquals(ContainerStorageHelper.ownerIdentity(config), labels.get(ContainerStorageHelper.OWNER_LABEL));
assertEquals("ecs", labels.get(ContainerStorageHelper.SERVICE_LABEL));
config.docker().resourceNamespace().ifPresent(namespace ->
assertEquals(namespace, labels.get("floci_namespace")));
String routerLogs = awaitRouterLogs(routerId, marker);
assertTrue(routerLogs.contains(marker),
@@ -50,9 +50,12 @@ import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
@@ -83,6 +86,7 @@ class EcsContainerManagerFirelensTest {
private InputStream uploadStream;
private byte[] uploadedArchive;
private EcsContainerManager manager;
private EmulatorConfig config;
@BeforeEach
void setUp() {
@@ -95,6 +99,7 @@ class EcsContainerManagerFirelensTest {
lifecycleManager = mock(ContainerLifecycleManager.class);
when(lifecycleManager.resolveImageForLaunch(any(), any()))
.thenAnswer(invocation -> new LaunchImage(invocation.getArgument(0), null));
when(lifecycleManager.removeVolume(anyString())).thenReturn(true);
when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerInfo("app-id", Map.of()));
when(lifecycleManager.create(any())).thenReturn("router-id");
when(lifecycleManager.startCreated(anyString(), any())).thenReturn(new ContainerInfo("router-id", Map.of()));
@@ -120,7 +125,7 @@ class EcsContainerManagerFirelensTest {
logStreamer = mock(ContainerLogStreamer.class);
when(logStreamer.generateLogStreamName(anyString())).thenReturn("stream");
ContainerDetector containerDetector = mock(ContainerDetector.class);
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
RegionResolver regionResolver = mock(RegionResolver.class);
LaunchedContainerAwsEnv awsEnv = mock(LaunchedContainerAwsEnv.class);
when(awsEnv.sdkBaselineEnv(any(), any())).thenReturn(List.of());
@@ -514,6 +519,88 @@ class EcsContainerManagerFirelensTest {
verifyFirelensVolumeRemoved();
}
@Test
void socketIdentityIncludesTheCompleteOwnerAndTask() {
String longNamespace = "firelens-" + "namespace".repeat(20);
when(config.docker().resourceNamespace()).thenReturn(Optional.of(longNamespace));
String name = EcsContainerManager.firelensVolumeName(config, task().getTaskArn());
assertEquals(name, EcsContainerManager.firelensVolumeName(config, task().getTaskArn()));
assertTrue(name.length() <= 55);
assertNotEquals(name, EcsContainerManager.firelensVolumeName(config, task().getTaskArn() + "other"));
when(config.docker().resourceNamespace()).thenReturn(Optional.of(longNamespace + "other"));
assertNotEquals(name, EcsContainerManager.firelensVolumeName(config, task().getTaskArn()));
when(config.docker().resourceNamespace()).thenReturn(Optional.of(longNamespace));
when(config.port()).thenReturn(4567);
assertNotEquals(name, EcsContainerManager.firelensVolumeName(config, task().getTaskArn()));
}
@ParameterizedTest
@ValueSource(strings = {"", " ", "long", "multibyte"})
void invalidSocketMountpointIsCompensatedBeforeCreatingContainers(String mountpoint) {
String path = switch (mountpoint) {
case "long" -> "/" + "x".repeat(108);
case "multibyte" -> "/" + "é".repeat(54);
default -> mountpoint;
};
when(dockerClient.inspectVolumeCmd(anyString()).exec().getMountpoint()).thenReturn(path);
assertThrows(AwsException.class, () -> manager.startTask(
task(), retryTaskDefinition("fluentbit", false, ""), null, "us-east-1"));
verify(lifecycleManager, never()).create(any());
verify(lifecycleManager, never()).createAndStart(any());
verifyFirelensVolumeRemoved();
}
@Test
void failedPreparationCleanupRetainsTheVolumeForRetry() {
when(dockerClient.inspectVolumeCmd(anyString()).exec().getMountpoint()).thenReturn(null);
when(lifecycleManager.removeVolume(anyString())).thenReturn(false, true);
EcsTaskLaunchException failure = assertThrows(EcsTaskLaunchException.class, () -> manager.startTask(
task(), retryTaskDefinition("fluentbit", false, ""), null, "us-east-1"));
assertTrue(failure.getHandle().hasTaskVolumes());
manager.cleanupStoppedTask(failure.getHandle());
assertFalse(failure.getHandle().hasTaskVolumes());
}
@Test
void socketVolumeKeepsFullOwnershipLabels() {
when(config.docker().resourceNamespace()).thenReturn(Optional.of("long-owner-namespace"));
when(config.port()).thenReturn(4566);
manager.startTask(task(), retryTaskDefinition("fluentbit", false, ""), null, "us-east-1");
ArgumentCaptor<Map<String, String>> labels = ArgumentCaptor.captor();
verify(lifecycleManager).ensureVolume(anyString(), labels.capture());
assertEquals("long-owner-namespace/4566", labels.getValue().get("io.floci.owner"));
assertEquals("ecs", labels.getValue().get("io.floci.service"));
assertTrue(labels.getValue().containsKey("io.floci.ecs.run"));
}
@ParameterizedTest
@ValueSource(strings = {"floci-aws-long-namespace-ecs-firelens-abc123",
"floci-aws-ecs-firelens-043ba6e4a0fd36cf98016b7b4e985125"})
void handleCleanupRetriesBothLegacyAndBoundedVolumeNames(String volumeName) {
EcsTaskHandle handle = new EcsTaskHandle(task().getTaskArn(), Map.of(), Map.of(),
volumeName, null, "us-east-1");
when(lifecycleManager.removeVolume(volumeName)).thenReturn(false, true);
manager.cleanupStoppedTask(handle);
assertTrue(handle.hasTaskVolumes());
manager.cleanupStoppedTask(handle);
assertFalse(handle.hasTaskVolumes());
}
@ParameterizedTest
@CsvSource({"107,true", "108,false"})
void socketLengthChecksTheCompleteHostPath(int bytes, boolean accepted) {
String mountpoint = "/" + "x".repeat(bytes - 13);
when(dockerClient.inspectVolumeCmd(anyString()).exec().getMountpoint()).thenReturn(mountpoint);
if (accepted) {
manager.startTask(task(), retryTaskDefinition("fluentbit", false, ""), null, "us-east-1");
} else {
assertThrows(AwsException.class, () -> manager.startTask(
task(), retryTaskDefinition("fluentbit", false, ""), null, "us-east-1"));
verify(lifecycleManager, never()).create(any());
verifyFirelensVolumeRemoved();
}
}
private DockerArchiveUploadFixture failingUploads() {
DockerArchiveUploadFixture uploads = new DockerArchiveUploadFixture(dockerClient, "router-id");
uploads.failures = Integer.MAX_VALUE;
@@ -532,7 +619,7 @@ class EcsContainerManagerFirelensTest {
private void verifyFirelensVolumeRemoved() {
ArgumentCaptor<String> volumeName = ArgumentCaptor.forClass(String.class);
verify(lifecycleManager).ensureVolume(volumeName.capture());
verify(lifecycleManager).ensureVolume(volumeName.capture(), any());
verify(lifecycleManager).removeVolume(volumeName.getValue());
}
@@ -37,6 +37,7 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
@@ -134,7 +135,7 @@ class EcsContainerManagerVolumesFromTest {
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
InspectVolumeCmd inspectVolumeCmd = mock(InspectVolumeCmd.class);
InspectVolumeResponse volume = mock(InspectVolumeResponse.class);
when(dockerClient.inspectVolumeCmd("floci-aws-ecs-firelens-volumesfrom1")).thenReturn(inspectVolumeCmd);
when(dockerClient.inspectVolumeCmd(anyString())).thenReturn(inspectVolumeCmd);
when(inspectVolumeCmd.exec()).thenReturn(volume);
when(volume.getMountpoint()).thenReturn("/var/lib/docker/volumes/floci-ecs-firelens-volumesfrom1/_data");
CopyArchiveToContainerCmd copyCmd = mock(CopyArchiveToContainerCmd.class, RETURNS_SELF);
@@ -166,8 +167,8 @@ class EcsContainerManagerVolumesFromTest {
lifecycleOrder.verify(lifecycleManager).createAndStart(any(ContainerSpec.class));
verify(routerBuilder).withVolumesFrom("source-id", false);
verify(appBuilder).withVolumesFrom("source-id", true);
verify(lifecycleManager).ensureVolume("floci-aws-ecs-firelens-volumesfrom1");
verify(routerBuilder).withNamedVolume("floci-aws-ecs-firelens-volumesfrom1", "/var/run");
verify(lifecycleManager).ensureVolume(eq(handle.getFirelensVolumeName()), any());
verify(routerBuilder).withNamedVolume(handle.getFirelensVolumeName(), "/var/run");
verify(copyCmd).withRemotePath("/fluent-bit/etc");
verify(routerBuilder, never()).withLoopbackPortBinding(anyInt(), anyInt());
verify(appBuilder, never()).withNetworkMode(anyString());
@@ -184,7 +185,7 @@ class EcsContainerManagerVolumesFromTest {
verify(logStreamer, never()).attachForAccount(any(), eq("app-id"), any(), any(), any(), any());
verify(logStreamer).attachForAccount(eq("000000000000"), eq("source-id"), any(), any(), eq("us-east-1"), any());
verify(logStreamer).attachForAccount(eq("000000000000"), eq("router-id"), any(), any(), eq("us-east-1"), any());
assertEquals("floci-aws-ecs-firelens-volumesfrom1", handle.getFirelensVolumeName());
assertTrue(handle.getFirelensVolumeName().length() <= 55);
assertEquals(List.of("source", "router", "app"), handle.getContainerIds().keySet().stream().toList());
assertEquals(List.of("app", "router", "source"),
ecsTask.getContainers().stream().map(Container::getName).toList());
@@ -206,7 +207,7 @@ class EcsContainerManagerVolumesFromTest {
verify(containerBuilder, never()).newContainer(anyString());
verify(lifecycleManager, never()).create(any());
verify(lifecycleManager, never()).createAndStart(any());
verify(lifecycleManager, never()).ensureVolume(anyString());
verify(lifecycleManager, never()).ensureVolume(anyString(), any());
}
@Test
@@ -235,7 +236,7 @@ class EcsContainerManagerVolumesFromTest {
verify(containerBuilder, never()).newContainer(anyString());
verify(lifecycleManager, never()).create(any());
verify(lifecycleManager, never()).createAndStart(any());
verify(lifecycleManager, never()).ensureVolume(anyString());
verify(lifecycleManager, never()).ensureVolume(anyString(), any());
}
@Test