mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ecs): enforce host-volume bind-mount safety fail-closed at register and run time (#3543)
* fix: enforce host-volume bind-mount safety fail-closed at register and run time Reject volumes[].host.sourcePath by default unless an operator opts in via floci.services.ecs.host-volume-roots or allow-unsafe-host-volumes. Block the Docker socket and any ancestor directory that contains it (/var/run, /run, /var) unconditionally, even with the unsafe flag set. Extract the check into HostVolumePolicy and enforce it again immediately before each bind mount in EcsContainerManager.startTask, not only once at RegisterTaskDefinition, to narrow the window for a swapped-in symlink and to cover task definitions persisted before this policy existed. * fix(ecs): protect the Docker socket Floci actually connects to The host-volume policy only blocked the literal /var/run/docker.sock and /run/docker.sock paths, so a task definition could bind-mount the daemon socket when Floci reached it through DOCKER_HOST or an active Docker context (Colima, OrbStack, Rancher Desktop, Podman). Resolve the daemon endpoint through the same DockerClientProducer path the Docker client uses and add its unix:// socket path to the protected candidates. The literal paths stay protected as defence in depth.
This commit is contained in:
1 parent
3b31c5a663
commit
126af73141
23 files changed
+628
-26
No files matched your search
+5
-1
@@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.ecs;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.ecs.container.HostVolumePolicy;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -13,6 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -50,7 +53,8 @@ class EcsJsonHandlerCommandEntryPointTest {
|
||||
return td;
|
||||
});
|
||||
|
||||
handler = new EcsJsonHandler(service, objectMapper);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
handler = new EcsJsonHandler(service, objectMapper, new HostVolumePolicy(config));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+5
-1
@@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.ecs;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.ecs.container.HostVolumePolicy;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -13,6 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -45,7 +48,8 @@ class EcsJsonHandlerRuntimePlatformLogConfigurationTest {
|
||||
return td;
|
||||
});
|
||||
|
||||
handler = new EcsJsonHandler(service, objectMapper);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
handler = new EcsJsonHandler(service, objectMapper, new HostVolumePolicy(config));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+10
-2
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ecs.container.EcsContainerManager;
|
||||
import io.github.hectorvent.floci.services.ecs.container.HostVolumePolicy;
|
||||
import io.github.hectorvent.floci.services.ecs.model.LaunchType;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
@@ -47,7 +48,13 @@ class EcsJsonHandlerTaskDefinitionPersistenceTest {
|
||||
FileStorageFactory storage = new FileStorageFactory(dataDir);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
EcsJsonHandler handler = new EcsJsonHandler(serviceWithStorage(storage), objectMapper);
|
||||
// This test exercises persistence round-tripping, not host-volume safety, so the fixed
|
||||
// literal sourcePath "/host/data" below needs an explicit opt-in under the new
|
||||
// fail-closed default: allow any host path for this handler instance.
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ecs().allowUnsafeHostVolumes()).thenReturn(true);
|
||||
EcsJsonHandler handler = new EcsJsonHandler(serviceWithStorage(storage), objectMapper,
|
||||
new HostVolumePolicy(config));
|
||||
JsonNode request = objectMapper.readTree("""
|
||||
{
|
||||
"family": "restart-family",
|
||||
@@ -91,7 +98,8 @@ class EcsJsonHandlerTaskDefinitionPersistenceTest {
|
||||
FileStorageFactory storage = new FileStorageFactory(dataDir);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
EcsService service = serviceWithStorage(storage);
|
||||
EcsJsonHandler handler = new EcsJsonHandler(service, objectMapper);
|
||||
EcsJsonHandler handler = new EcsJsonHandler(service, objectMapper,
|
||||
new HostVolumePolicy(mock(EmulatorConfig.class, RETURNS_DEEP_STUBS)));
|
||||
|
||||
JsonNode registerReq = objectMapper.readTree("""
|
||||
{
|
||||
|
||||
+239
-1
@@ -2,14 +2,26 @@ package io.github.hectorvent.floci.services.ecs;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.ecs.container.HostVolumePolicy;
|
||||
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -18,6 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -34,6 +47,7 @@ class EcsJsonHandlerVolumesTest {
|
||||
|
||||
private EcsService service;
|
||||
private ObjectMapper objectMapper;
|
||||
private EmulatorConfig config;
|
||||
private EcsJsonHandler handler;
|
||||
|
||||
@BeforeEach
|
||||
@@ -51,11 +65,40 @@ class EcsJsonHandlerVolumesTest {
|
||||
return td;
|
||||
});
|
||||
|
||||
handler = new EcsJsonHandler(service, objectMapper);
|
||||
// Deep-stubbed: unstubbed leaf methods return Optional.empty()/false, the same
|
||||
// fail-closed defaults as production, so host-volume-roots is empty and
|
||||
// allow-unsafe-host-volumes is false unless a test stubs otherwise. That means any
|
||||
// test registering a host-volume sourcePath must explicitly opt in (a root or the
|
||||
// unsafe flag) or expect the default rejection.
|
||||
config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
handler = new EcsJsonHandler(service, objectMapper, new HostVolumePolicy(config));
|
||||
}
|
||||
|
||||
private static String registerRequestWithHostVolume(String sourcePath) {
|
||||
return """
|
||||
{
|
||||
"family": "host-volume-family",
|
||||
"containerDefinitions": [
|
||||
{
|
||||
"name": "app",
|
||||
"image": "alpine:latest",
|
||||
"mountPoints": [
|
||||
{"sourceVolume": "vol", "containerPath": "/app/data", "readOnly": false}
|
||||
]
|
||||
}
|
||||
],
|
||||
"volumes": [
|
||||
{"name": "vol", "host": {"sourcePath": "%s"}}
|
||||
]
|
||||
}
|
||||
""".formatted(sourcePath.replace("\\", "\\\\"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionRoundTripsVolumesAndMountPoints() throws Exception {
|
||||
// This test is about JSON round-trip fidelity, not host-volume safety, so opt out of
|
||||
// the fail-closed default explicitly.
|
||||
when(config.services().ecs().allowUnsafeHostVolumes()).thenReturn(true);
|
||||
String requestJson = """
|
||||
{
|
||||
"family": "volumes-family",
|
||||
@@ -246,4 +289,199 @@ class EcsJsonHandlerVolumesTest {
|
||||
assertTrue(efs.path("authorizationConfig").path("accessPointId").isMissingNode(),
|
||||
"accessPointId should not be set");
|
||||
}
|
||||
|
||||
// ── Host-volume safety ──────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionRejectsRelativeHostSourcePath() throws Exception {
|
||||
JsonNode request = objectMapper.readTree(registerRequestWithHostVolume("relative/path"));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterTaskDefinition", request, "us-east-1"));
|
||||
assertEquals("InvalidParameterException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionRejectsTraversalHostSourcePath() throws Exception {
|
||||
JsonNode request = objectMapper.readTree(registerRequestWithHostVolume("/host/abs/../../etc"));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterTaskDefinition", request, "us-east-1"));
|
||||
assertEquals("InvalidParameterException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionRejectsBareRootHostSourcePath() throws Exception {
|
||||
JsonNode request = objectMapper.readTree(registerRequestWithHostVolume("/"));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterTaskDefinition", request, "us-east-1"));
|
||||
assertEquals("InvalidParameterException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionRejectsDockerSocketHostVolume() throws Exception {
|
||||
// Allow any host path so a rejection here can only come from the Docker-socket check
|
||||
// itself, not the fail-closed default (proving the socket-specific rule fires).
|
||||
when(config.services().ecs().allowUnsafeHostVolumes()).thenReturn(true);
|
||||
|
||||
JsonNode varRun = objectMapper.readTree(registerRequestWithHostVolume("/var/run/docker.sock"));
|
||||
AwsException varRunEx = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterTaskDefinition", varRun, "us-east-1"));
|
||||
assertEquals("InvalidParameterException", varRunEx.getErrorCode());
|
||||
|
||||
JsonNode run = objectMapper.readTree(registerRequestWithHostVolume("/run/docker.sock"));
|
||||
AwsException runEx = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterTaskDefinition", run, "us-east-1"));
|
||||
assertEquals("InvalidParameterException", runEx.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionRejectsDockerSocketAncestorDirectories() throws Exception {
|
||||
// Mounting a directory that CONTAINS the socket (/var/run, /run) exposes docker.sock
|
||||
// just as directly as naming the socket file outright, and must be blocked even when
|
||||
// allow-unsafe-host-volumes is true.
|
||||
when(config.services().ecs().allowUnsafeHostVolumes()).thenReturn(true);
|
||||
|
||||
JsonNode varRun = objectMapper.readTree(registerRequestWithHostVolume("/var/run"));
|
||||
AwsException varRunEx = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterTaskDefinition", varRun, "us-east-1"));
|
||||
assertEquals("InvalidParameterException", varRunEx.getErrorCode());
|
||||
|
||||
JsonNode run = objectMapper.readTree(registerRequestWithHostVolume("/run"));
|
||||
AwsException runEx = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterTaskDefinition", run, "us-east-1"));
|
||||
assertEquals("InvalidParameterException", runEx.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionRejectsHostSourcePathByDefault() throws Exception {
|
||||
// With host-volume-roots empty and allow-unsafe-host-volumes false (the shipped
|
||||
// defaults, unstubbed here), any host sourcePath is rejected fail-closed and the
|
||||
// message names both config keys so an operator knows how to opt in.
|
||||
JsonNode request = objectMapper.readTree(registerRequestWithHostVolume("/some/data"));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterTaskDefinition", request, "us-east-1"));
|
||||
assertEquals("InvalidParameterException", ex.getErrorCode());
|
||||
assertTrue(ex.getMessage().contains("floci.services.ecs.host-volume-roots"),
|
||||
"message must name the roots config key: " + ex.getMessage());
|
||||
assertTrue(ex.getMessage().contains("FLOCI_SERVICES_ECS_HOST_VOLUME_ROOTS"),
|
||||
"message must name the roots env var: " + ex.getMessage());
|
||||
assertTrue(ex.getMessage().contains("floci.services.ecs.allow-unsafe-host-volumes"),
|
||||
"message must name the unsafe-flag config key: " + ex.getMessage());
|
||||
assertTrue(ex.getMessage().contains("FLOCI_SERVICES_ECS_ALLOW_UNSAFE_HOST_VOLUMES"),
|
||||
"message must name the unsafe-flag env var: " + ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionRejectsHostSourcePathOutsideConfiguredRoots() throws Exception {
|
||||
when(config.services().ecs().hostVolumeRoots()).thenReturn(Optional.of(List.of("/approved")));
|
||||
JsonNode request = objectMapper.readTree(registerRequestWithHostVolume("/not-approved/data"));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterTaskDefinition", request, "us-east-1"));
|
||||
assertEquals("InvalidParameterException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionAllowsHostSourcePathInsideConfiguredRoots() throws Exception {
|
||||
when(config.services().ecs().hostVolumeRoots()).thenReturn(Optional.of(List.of("/approved")));
|
||||
JsonNode request = objectMapper.readTree(registerRequestWithHostVolume("/approved/sub/data"));
|
||||
|
||||
Response response = handler.handle("RegisterTaskDefinition", request, "us-east-1");
|
||||
JsonNode td = objectMapper.valueToTree(response.getEntity()).path("taskDefinition");
|
||||
assertEquals("/approved/sub/data",
|
||||
td.path("volumes").get(0).path("host").path("sourcePath").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionAllowUnsafeHostVolumesBypassesRootCheckButNotDockerSocket() throws Exception {
|
||||
when(config.services().ecs().hostVolumeRoots()).thenReturn(Optional.of(List.of("/approved")));
|
||||
when(config.services().ecs().allowUnsafeHostVolumes()).thenReturn(true);
|
||||
|
||||
JsonNode outsideRoot = objectMapper.readTree(registerRequestWithHostVolume("/anywhere/else"));
|
||||
Response response = handler.handle("RegisterTaskDefinition", outsideRoot, "us-east-1");
|
||||
JsonNode td = objectMapper.valueToTree(response.getEntity()).path("taskDefinition");
|
||||
assertEquals("/anywhere/else",
|
||||
td.path("volumes").get(0).path("host").path("sourcePath").asText());
|
||||
|
||||
JsonNode socket = objectMapper.readTree(registerRequestWithHostVolume("/var/run/docker.sock"));
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterTaskDefinition", socket, "us-east-1"));
|
||||
assertEquals("InvalidParameterException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionRejectsTheSocketNamedByDockerHostEnv(@TempDir Path tempDir) throws Exception {
|
||||
// With floci.docker.docker-host at its default, Floci's own Docker client connects to
|
||||
// whatever DOCKER_HOST names, so that socket is the live daemon socket and must be
|
||||
// protected exactly like /var/run/docker.sock, even with allow-unsafe-host-volumes.
|
||||
Path daemonSocket = tempDir.resolve("floci-docker.sock");
|
||||
when(config.docker().dockerHost()).thenReturn("unix:///var/run/docker.sock");
|
||||
when(config.docker().dockerConfigPath())
|
||||
.thenReturn(Optional.of(tempDir.resolve("no-docker-config").toString()));
|
||||
when(config.services().ecs().allowUnsafeHostVolumes()).thenReturn(true);
|
||||
handler = new EcsJsonHandler(service, objectMapper, new HostVolumePolicy(config,
|
||||
environment(Map.of("DOCKER_HOST", "unix://" + daemonSocket))));
|
||||
|
||||
JsonNode request = objectMapper.readTree(registerRequestWithHostVolume(daemonSocket.toString()));
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterTaskDefinition", request, "us-east-1"));
|
||||
assertEquals("InvalidParameterException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionRejectsTheDirectoryHoldingTheActiveContextSocket(@TempDir Path tempDir)
|
||||
throws Exception {
|
||||
// Colima, OrbStack and Rancher Desktop publish their daemon socket through a Docker
|
||||
// context rather than DOCKER_HOST. Floci's client follows the active context, so the
|
||||
// directory holding that socket exposes the daemon just like /var/run does.
|
||||
Path dockerConfigDir = tempDir.resolve("docker-config");
|
||||
Path socketDir = tempDir.resolve("colima");
|
||||
writeContextFixture(dockerConfigDir, "colima", "unix://" + socketDir.resolve("docker.sock"));
|
||||
when(config.docker().dockerHost()).thenReturn("unix:///var/run/docker.sock");
|
||||
when(config.docker().dockerConfigPath()).thenReturn(Optional.of(dockerConfigDir.toString()));
|
||||
when(config.services().ecs().allowUnsafeHostVolumes()).thenReturn(true);
|
||||
handler = new EcsJsonHandler(service, objectMapper, new HostVolumePolicy(config, environment(Map.of())));
|
||||
|
||||
JsonNode request = objectMapper.readTree(registerRequestWithHostVolume(socketDir.toString()));
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterTaskDefinition", request, "us-east-1"));
|
||||
assertEquals("InvalidParameterException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
private static Function<String, String> environment(Map<String, String> variables) {
|
||||
return variables::get;
|
||||
}
|
||||
|
||||
/** Lays out {@code config.json} and a context's {@code meta.json} the way the Docker CLI does. */
|
||||
private static void writeContextFixture(Path dockerConfigDir, String contextName, String host) throws Exception {
|
||||
Files.createDirectories(dockerConfigDir);
|
||||
Files.writeString(dockerConfigDir.resolve("config.json"), "{\"currentContext\":\"" + contextName + "\"}");
|
||||
byte[] nameHash = MessageDigest.getInstance("SHA-256").digest(contextName.getBytes(StandardCharsets.UTF_8));
|
||||
Path metaDir = dockerConfigDir.resolve("contexts").resolve("meta").resolve(HexFormat.of().formatHex(nameHash));
|
||||
Files.createDirectories(metaDir);
|
||||
Files.writeString(metaDir.resolve("meta.json"),
|
||||
"{\"Name\":\"" + contextName + "\",\"Metadata\":{},"
|
||||
+ "\"Endpoints\":{\"docker\":{\"Host\":\"" + host + "\",\"SkipTLSVerify\":false}}}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionRejectsSymlinkEscapeOutsideConfiguredRoot(@TempDir Path tempDir) throws IOException {
|
||||
Path approvedRoot = tempDir.resolve("approved");
|
||||
Path outsideTarget = tempDir.resolve("outside");
|
||||
Files.createDirectories(approvedRoot);
|
||||
Files.createDirectories(outsideTarget);
|
||||
Path escapeLink = approvedRoot.resolve("escape");
|
||||
Files.createSymbolicLink(escapeLink, outsideTarget);
|
||||
|
||||
when(config.services().ecs().hostVolumeRoots())
|
||||
.thenReturn(Optional.of(List.of(approvedRoot.toString())));
|
||||
JsonNode request = objectMapper.readTree(registerRequestWithHostVolume(escapeLink.toString()));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterTaskDefinition", request, "us-east-1"));
|
||||
assertEquals("InvalidParameterException", ex.getErrorCode());
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -79,7 +79,7 @@ class EcsContainerManagerAwsBaselineTest {
|
||||
|
||||
manager = new EcsContainerManager(containerBuilder, lifecycleManager, logStreamer,
|
||||
containerDetector, config, regionResolver, awsEnv, mock(SsmService.class),
|
||||
mock(SecretsManagerService.class), ecrRegistryManager);
|
||||
mock(SecretsManagerService.class), ecrRegistryManager, mock(HostVolumePolicy.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+1
-1
@@ -80,7 +80,7 @@ class EcsContainerManagerEcrRewriteTest {
|
||||
|
||||
manager = new EcsContainerManager(containerBuilder, lifecycleManager, logStreamer,
|
||||
containerDetector, config, regionResolver, awsEnv, ssmService, secretsManagerService,
|
||||
ecrRegistryManager);
|
||||
ecrRegistryManager, mock(HostVolumePolicy.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+1
-1
@@ -118,6 +118,6 @@ class EcsContainerManagerLifecycleTest {
|
||||
mock(ContainerBuilder.class), lifecycleManager, mock(ContainerLogStreamer.class),
|
||||
mock(ContainerDetector.class), mock(EmulatorConfig.class), mock(RegionResolver.class),
|
||||
mock(LaunchedContainerAwsEnv.class), mock(SsmService.class), mock(SecretsManagerService.class),
|
||||
mock(EcrRegistryManager.class));
|
||||
mock(EcrRegistryManager.class), mock(HostVolumePolicy.class));
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -76,7 +76,7 @@ class EcsContainerManagerOverridesTest {
|
||||
|
||||
manager = new EcsContainerManager(containerBuilder, lifecycleManager, logStreamer,
|
||||
containerDetector, config, regionResolver, awsEnv, ssmService, secretsManagerService,
|
||||
ecrRegistryManager);
|
||||
ecrRegistryManager, mock(HostVolumePolicy.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+1
-1
@@ -91,7 +91,7 @@ class EcsContainerManagerPortMappingsTest {
|
||||
|
||||
manager = new EcsContainerManager(containerBuilder, lifecycleManager, logStreamer,
|
||||
containerDetector, config, regionResolver, awsEnv, ssmService, secretsManagerService,
|
||||
ecrRegistryManager);
|
||||
ecrRegistryManager, mock(HostVolumePolicy.class));
|
||||
}
|
||||
|
||||
private void startWith(List<PortMapping> portMappings) {
|
||||
|
||||
+1
-1
@@ -70,7 +70,7 @@ class EcsContainerManagerSecretsTest {
|
||||
|
||||
manager = new EcsContainerManager(containerBuilder, lifecycleManager, logStreamer,
|
||||
containerDetector, config, regionResolver, awsEnv, ssmService, secretsManagerService,
|
||||
ecrRegistryManager);
|
||||
ecrRegistryManager, mock(HostVolumePolicy.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+39
-3
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.ecs.container;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
|
||||
@@ -27,6 +28,7 @@ import java.util.Optional;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
@@ -78,9 +80,14 @@ class EcsContainerManagerVolumesTest {
|
||||
ecrRegistryManager = mock(EcrRegistryManager.class);
|
||||
when(ecrRegistryManager.rewriteImageUri(anyString())).thenAnswer(inv -> inv.getArgument(0));
|
||||
|
||||
// These tests bind-mount hand-built "/host/abs/..." paths that don't sit under any
|
||||
// real approved root, so opt out of the fail-closed default explicitly; the Docker
|
||||
// socket ancestor block still applies regardless (see the rejection test below).
|
||||
when(config.services().ecs().allowUnsafeHostVolumes()).thenReturn(true);
|
||||
|
||||
manager = new EcsContainerManager(containerBuilder, lifecycleManager, logStreamer,
|
||||
containerDetector, config, regionResolver, awsEnv, ssmService, secretsManagerService,
|
||||
ecrRegistryManager);
|
||||
ecrRegistryManager, new HostVolumePolicy(config));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -175,7 +182,7 @@ class EcsContainerManagerVolumesTest {
|
||||
EcsContainerManager configured = new EcsContainerManager(containerBuilder, lifecycleManager,
|
||||
mock(ContainerLogStreamer.class), mock(ContainerDetector.class), cfg,
|
||||
mock(RegionResolver.class), awsEnv, mock(SsmService.class),
|
||||
mock(SecretsManagerService.class), ecrRegistryManager);
|
||||
mock(SecretsManagerService.class), ecrRegistryManager, new HostVolumePolicy(cfg));
|
||||
|
||||
ContainerDefinition app = new ContainerDefinition();
|
||||
app.setName("app");
|
||||
@@ -210,7 +217,7 @@ class EcsContainerManagerVolumesTest {
|
||||
EcsContainerManager configured = new EcsContainerManager(containerBuilder, lifecycleManager,
|
||||
mock(ContainerLogStreamer.class), mock(ContainerDetector.class), cfg,
|
||||
mock(RegionResolver.class), awsEnv, mock(SsmService.class),
|
||||
mock(SecretsManagerService.class), ecrRegistryManager);
|
||||
mock(SecretsManagerService.class), ecrRegistryManager, new HostVolumePolicy(cfg));
|
||||
|
||||
ContainerDefinition app = new ContainerDefinition();
|
||||
app.setName("app");
|
||||
@@ -231,4 +238,33 @@ class EcsContainerManagerVolumesTest {
|
||||
verify(builder, times(1)).withUser("1001:1001");
|
||||
verify(builder, times(1)).withGroupAdd("2000");
|
||||
}
|
||||
|
||||
/**
|
||||
* The host-volume policy is enforced again at mount time, immediately before the bind
|
||||
* call, not only once at RegisterTaskDefinition time. A mount whose source is an ancestor
|
||||
* of the Docker socket (here {@code /var/run}, which contains {@code docker.sock}) must be
|
||||
* rejected even though this test class's setUp already allows unsafe host volumes: the
|
||||
* socket-ancestor block applies unconditionally. The rejection must propagate out of
|
||||
* startTask (not be swallowed) and no bind must have been issued for it.
|
||||
*/
|
||||
@Test
|
||||
void startTaskRejectsMountThatExposesDockerSocketEvenWhenUnsafeVolumesAllowed() {
|
||||
ContainerDefinition app = new ContainerDefinition();
|
||||
app.setName("app");
|
||||
app.setImage("app:latest");
|
||||
app.setMountPoints(List.of(new MountPoint("run-vol", "/host-run", false)));
|
||||
|
||||
TaskDefinition taskDef = new TaskDefinition();
|
||||
taskDef.setFamily("socket-family");
|
||||
taskDef.setContainerDefinitions(List.of(app));
|
||||
taskDef.setVolumes(List.of(new Volume("run-vol", "/var/run")));
|
||||
|
||||
EcsTask task = new EcsTask();
|
||||
task.setTaskArn("arn:aws:ecs:us-east-1:000000000000:task/test-cluster/socket1");
|
||||
|
||||
assertThrows(AwsException.class, () -> manager.startTask(task, taskDef, List.of(), "us-east-1"));
|
||||
|
||||
verify(builder, never()).withBind(any(), any());
|
||||
verify(builder, never()).withReadOnlyBind(any(), any());
|
||||
}
|
||||
}
|
||||
+6
-2
@@ -55,6 +55,9 @@ class EventBridgeInvokerTest {
|
||||
when(regionResolver.getAccountId()).thenReturn("000000000000");
|
||||
when(eventBridgeService.putEvents(anyList(), anyString(), any()))
|
||||
.thenReturn(new EventBridgeService.PutEventsResult(0, List.of()));
|
||||
io.github.hectorvent.floci.config.EmulatorConfig emulatorConfig =
|
||||
mock(io.github.hectorvent.floci.config.EmulatorConfig.class,
|
||||
org.mockito.Mockito.RETURNS_DEEP_STUBS);
|
||||
invoker = new EventBridgeInvoker(
|
||||
lambdaService,
|
||||
sqsService,
|
||||
@@ -63,10 +66,11 @@ class EventBridgeInvokerTest {
|
||||
firehoseService,
|
||||
eventBridgeService,
|
||||
ecsService,
|
||||
new io.github.hectorvent.floci.services.ecs.EcsJsonHandler(ecsService, new ObjectMapper()),
|
||||
new io.github.hectorvent.floci.services.ecs.EcsJsonHandler(ecsService, new ObjectMapper(),
|
||||
new io.github.hectorvent.floci.services.ecs.container.HostVolumePolicy(emulatorConfig)),
|
||||
regionResolver,
|
||||
new ObjectMapper(),
|
||||
mock(io.github.hectorvent.floci.config.EmulatorConfig.class)
|
||||
emulatorConfig
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+4
-1
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.services.dynamodb.DynamoDbJsonHandler;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
|
||||
import io.github.hectorvent.floci.services.ecs.EcsJsonHandler;
|
||||
import io.github.hectorvent.floci.services.ecs.EcsService;
|
||||
import io.github.hectorvent.floci.services.ecs.container.HostVolumePolicy;
|
||||
import io.github.hectorvent.floci.services.ecs.model.NetworkConfiguration;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaExecutorService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaFunctionStore;
|
||||
@@ -28,6 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -57,7 +59,8 @@ class AslExecutorEcsRunTaskModeTest {
|
||||
void setUp() {
|
||||
ecsService = mock(EcsService.class);
|
||||
// A real handler so parseNetworkConfiguration / parseContainerOverrides actually run.
|
||||
EcsJsonHandler ecsJsonHandler = new EcsJsonHandler(ecsService, objectMapper);
|
||||
EcsJsonHandler ecsJsonHandler = new EcsJsonHandler(ecsService, objectMapper,
|
||||
new HostVolumePolicy(mock(EmulatorConfig.class, RETURNS_DEEP_STUBS)));
|
||||
|
||||
executor = new AslExecutor(
|
||||
mock(LambdaExecutorService.class),
|
||||
|
||||
Reference in new issue
Block a user