mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): mount hot-reload read-only and refuse Docker socket directories (#4154)
Bind-mount the hot-reload host directory at /var/task read-only, as /var/task is in AWS Lambda, and, when no allow-list is set, refuse the host root, /var, /proc, and anything under /run or /var/run, where the Docker socket lives or can be reached. Move the no-allow-list warning onto StartupEvent, beside NetworkExposureGuard, so it reaches an operator who has not called Lambda yet.
This commit is contained in:
1 parent
7cd441231f
commit
83ae2b4697
8 files changed
+258
-12
No files matched your search
@@ -0,0 +1,43 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class HotReloadExposureWarningTest {
|
||||
|
||||
private static EmulatorConfig configWith(boolean enabled, List<String> allowedPaths) {
|
||||
EmulatorConfig.LambdaServiceConfig.HotReload hotReload =
|
||||
mock(EmulatorConfig.LambdaServiceConfig.HotReload.class);
|
||||
when(hotReload.enabled()).thenReturn(enabled);
|
||||
when(hotReload.allowedPaths()).thenReturn(Optional.ofNullable(allowedPaths));
|
||||
EmulatorConfig.LambdaServiceConfig lambda = mock(EmulatorConfig.LambdaServiceConfig.class);
|
||||
when(lambda.hotReload()).thenReturn(hotReload);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
when(services.lambda()).thenReturn(lambda);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.services()).thenReturn(services);
|
||||
return config;
|
||||
}
|
||||
|
||||
@Test
|
||||
void warnsWhenHotReloadIsEnabledWithNoAllowList() {
|
||||
assertTrue(HotReloadExposureWarning.hotReloadAcceptsAnyPath(configWith(true, null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void staysQuietWhenAnAllowListIsSet() {
|
||||
assertFalse(HotReloadExposureWarning.hotReloadAcceptsAnyPath(configWith(true, List.of("/home/ci/code"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void staysQuietWhenHotReloadIsDisabled() {
|
||||
assertFalse(HotReloadExposureWarning.hotReloadAcceptsAnyPath(configWith(false, null)));
|
||||
}
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* Hot-reload enabled with no allow-list, as in the test configuration: directories that can hold
|
||||
* the Docker socket are refused over the wire, and ordinary code directories are not.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class LambdaHotReloadDockerSocketIntegrationTest {
|
||||
|
||||
private static int counter;
|
||||
|
||||
/** This test shares the default profile's emulator state with every other test on it. */
|
||||
private static final List<String> CREATED = new ArrayList<>();
|
||||
|
||||
private static ValidatableResponse createFunction(String s3Key) {
|
||||
return createFunction("hot-reload-socket-guard-" + (++counter), s3Key);
|
||||
}
|
||||
|
||||
private static ValidatableResponse createFunction(String name, String s3Key) {
|
||||
return given()
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{
|
||||
"FunctionName": "%s",
|
||||
"Runtime": "python3.12",
|
||||
"Role": "arn:aws:iam::000000000000:role/r",
|
||||
"Handler": "handler.handler",
|
||||
"Code": { "S3Bucket": "hot-reload", "S3Key": "%s" }
|
||||
}
|
||||
""".formatted(name, s3Key))
|
||||
.when()
|
||||
.post("/2015-03-31/functions")
|
||||
.then();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"/", "/var/run", "/run", "/var/run/docker.sock", "/proc/1/root/var/run"})
|
||||
void createFunctionRejectsDirectoriesThatCanHoldTheDockerSocket(String s3Key) {
|
||||
createFunction(s3Key)
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValueException"))
|
||||
.body("message", containsString("Docker socket"));
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"/tmp/floci-hot-reload", "/home/ci/code"})
|
||||
void createFunctionAcceptsOrdinaryCodeDirectories(String s3Key) {
|
||||
String name = "hot-reload-socket-guard-accepted-" + (++counter);
|
||||
CREATED.add(name);
|
||||
|
||||
createFunction(name, s3Key).statusCode(201);
|
||||
}
|
||||
|
||||
/**
|
||||
* The functions the accept cases created, deleted as an ordered test rather than in
|
||||
* {@code @AfterAll}: Quarkus has already stopped the app by then, so the call cannot be made.
|
||||
*/
|
||||
@Test
|
||||
@Order(2)
|
||||
void cleanup_deleteCreatedFunctions() {
|
||||
for (String name : CREATED) {
|
||||
given()
|
||||
.when()
|
||||
.delete("/2015-03-31/functions/" + name)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
}
|
||||
CREATED.clear();
|
||||
}
|
||||
}
|
||||
@@ -991,6 +991,50 @@ class LambdaServiceTest {
|
||||
assertEquals("InvalidParameterValueException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"/",
|
||||
"/var",
|
||||
"/var/run",
|
||||
"/var/run/docker.sock",
|
||||
"/run",
|
||||
"/run/user/1000",
|
||||
"/run/../var/run",
|
||||
"/home/ci/code/../../../var/run",
|
||||
"/proc",
|
||||
"/proc/1/root/var/run",
|
||||
"/proc/self/root/var/run/docker.sock"
|
||||
})
|
||||
void hotReload_withoutAllowListRejectsDirectoriesThatCanHoldTheDockerSocket(String s3Key) {
|
||||
LambdaService svc = serviceWithHotReload(true, null);
|
||||
Map<String, Object> req = baseRequest("hr-socket");
|
||||
req.put("Code", Map.of("S3Bucket", "hot-reload", "S3Key", s3Key));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () -> svc.createFunction(REGION, req), s3Key);
|
||||
|
||||
assertEquals("InvalidParameterValueException", ex.getErrorCode());
|
||||
assertTrue(ex.getMessage().contains("Docker socket"), ex.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"/var/lib/app", "/varnish", "/running", "/process/code", "/tmp/my-fn", "/home/ci/code"})
|
||||
void hotReload_withoutAllowListStillAcceptsOrdinaryCodeDirectories(String s3Key) {
|
||||
LambdaService svc = serviceWithHotReload(true, null);
|
||||
Map<String, Object> req = baseRequest("hr-ordinary");
|
||||
req.put("Code", Map.of("S3Bucket", "hot-reload", "S3Key", s3Key));
|
||||
|
||||
assertEquals(s3Key, svc.createFunction(REGION, req).getHotReloadHostPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void hotReload_anExplicitAllowListIsTheOperatorsChoiceAndOverridesTheSocketGuard() {
|
||||
LambdaService svc = serviceWithHotReload(true, List.of("/run/my-code"));
|
||||
Map<String, Object> req = baseRequest("hr-explicit");
|
||||
req.put("Code", Map.of("S3Bucket", "hot-reload", "S3Key", "/run/my-code/app"));
|
||||
|
||||
assertEquals("/run/my-code/app", svc.createFunction(REGION, req).getHotReloadHostPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void hotReload_dockerHostPathIsAlwaysPosixSeparated() {
|
||||
assertEquals("/home/ci/code/lib", LambdaService.toDockerHostPath(Path.of("/home/ci/code/lib")));
|
||||
|
||||
+20
@@ -23,6 +23,7 @@ import com.github.dockerjava.api.command.ExecCreateCmdResponse;
|
||||
import com.github.dockerjava.api.command.ExecStartCmd;
|
||||
import com.github.dockerjava.api.command.CopyArchiveFromContainerCmd;
|
||||
import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import com.github.dockerjava.api.model.AccessMode;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.StreamType;
|
||||
import com.github.dockerjava.api.model.Mount;
|
||||
@@ -223,6 +224,25 @@ class ContainerLauncherTest {
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchFunction_hotReloadMountsTheHostDirectoryReadOnlyAtVarTask() {
|
||||
LambdaFunction fn = new LambdaFunction();
|
||||
fn.setFunctionName("hot-reload-fn");
|
||||
fn.setRuntime("nodejs20.x");
|
||||
fn.setHandler("index.handler");
|
||||
fn.setHotReloadHostPath("/home/ci/code");
|
||||
|
||||
launcher.launch(fn);
|
||||
|
||||
ContainerSpec spec = captureRealContainerSpec();
|
||||
assertEquals(1, spec.binds().stream()
|
||||
.filter(b -> "/home/ci/code".equals(b.getPath()) && "/var/task".equals(b.getVolume().getPath()))
|
||||
.count());
|
||||
assertEquals(AccessMode.ro, spec.binds().stream()
|
||||
.filter(b -> "/var/task".equals(b.getVolume().getPath()))
|
||||
.findFirst().orElseThrow().getAccessMode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchFunction_usesArm64DockerPlatformWhenArchitectureHonouringIsEnabled() throws Exception {
|
||||
EmulatorConfig.LambdaServiceConfig lambda = config.services().lambda();
|
||||
|
||||
Reference in new issue
Block a user