mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -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
@@ -92,7 +92,7 @@ aws lambda invoke --function-name my-function out.json
|
||||
|
||||
## Hot-Reload via Bind Mount
|
||||
|
||||
For the tightest inner-loop development cycle, Floci supports a **bind-mount hot-reload** mode. Instead of packaging code into a ZIP and uploading it to S3, you point Floci directly at a directory on your host machine. The directory is bind-mounted into `/var/task` inside the container, so every invocation runs the files as they currently exist on disk, with no upload or redeploy.
|
||||
For the tightest inner-loop development cycle, Floci supports a **bind-mount hot-reload** mode. Instead of packaging code into a ZIP and uploading it to S3, you point Floci directly at a directory on your host machine. The directory is bind-mounted read-only into `/var/task` inside the container, as `/var/task` is read-only in AWS Lambda, so every invocation runs the files as they currently exist on disk, with no upload or redeploy.
|
||||
|
||||
This is enabled by using the magic bucket name `hot-reload` when creating a function:
|
||||
|
||||
@@ -126,7 +126,7 @@ FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ENABLED=true
|
||||
FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS=/home/user/projects,/tmp
|
||||
```
|
||||
|
||||
An `S3Key` is accepted when it is one of the listed directories or inside one. `.` and `..` segments are resolved first, so `/home/user/projects/../secrets` and a sibling such as `/home/user/projects-old` are rejected. Symbolic links on the Docker host are not resolved by Floci. A path containing `:` is rejected. Without an allow-list any absolute host path is accepted, and Floci logs a warning at startup when hot-reload is enabled that way.
|
||||
An `S3Key` is accepted when it is one of the listed directories or inside one. `.` and `..` segments are resolved first, so `/home/user/projects/../secrets` and a sibling such as `/home/user/projects-old` are rejected. Symbolic links on the Docker host are not resolved by Floci. A path containing `:` is rejected. Without an allow-list any absolute host path is accepted except the host root, `/var`, `/proc`, and anything under `/run` or `/var/run`, where the Docker socket lives or can be reached. Floci logs a warning at startup when hot-reload is enabled without an allow-list.
|
||||
|
||||
**Docker Compose setup**: enable the feature and share the Docker socket:
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/**
|
||||
* Warns at startup when Lambda hot-reload is enabled with no allow-list, because any absolute host
|
||||
* path is then a legal bind-mount target. Unlike {@link NetworkExposureGuard} this only warns: the
|
||||
* combination is the documented default of the published compose file, so refusing to start would
|
||||
* break it.
|
||||
*
|
||||
* <p>Startup rather than first use: the operator who most needs the message is the one who has not
|
||||
* touched Lambda yet.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class HotReloadExposureWarning {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(HotReloadExposureWarning.class);
|
||||
|
||||
private final EmulatorConfig config;
|
||||
|
||||
@Inject
|
||||
public HotReloadExposureWarning(EmulatorConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
void onStart(@Observes StartupEvent ignored) {
|
||||
if (hotReloadAcceptsAnyPath(config)) {
|
||||
LOG.warn("Lambda hot-reload is enabled without FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS: "
|
||||
+ "any absolute path on the Docker host can be bind-mounted into a function container");
|
||||
}
|
||||
}
|
||||
|
||||
static boolean hotReloadAcceptsAnyPath(EmulatorConfig config) {
|
||||
EmulatorConfig.LambdaServiceConfig.HotReload hotReload = config.services().lambda().hotReload();
|
||||
return hotReload.enabled() && hotReload.allowedPaths().isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -283,15 +283,6 @@ public class LambdaService implements ResourceProvider {
|
||||
void init() {
|
||||
initializeStorage();
|
||||
rehydrateConcurrency();
|
||||
warnWhenHotReloadHasNoAllowList();
|
||||
}
|
||||
|
||||
private void warnWhenHotReloadHasNoAllowList() {
|
||||
if (config != null && config.services().lambda().hotReload().enabled()
|
||||
&& config.services().lambda().hotReload().allowedPaths().isEmpty()) {
|
||||
LOG.warn("Lambda hot-reload is enabled without FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS: "
|
||||
+ "any absolute path on the Docker host can be bind-mounted into a function container");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2992,6 +2983,11 @@ public class LambdaService implements ResourceProvider {
|
||||
"Path '" + hostPath + "' is not under an allowed hot-reload mount prefix.", 400);
|
||||
}
|
||||
});
|
||||
if (config.services().lambda().hotReload().allowedPaths().isEmpty() && reachesDockerSocketDirectory(normalized)) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Path '" + hostPath + "' can expose the Docker socket. Set "
|
||||
+ "FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS to mount it.", 400);
|
||||
}
|
||||
String resolvedHostPath = toDockerHostPath(normalized);
|
||||
fn.setHotReloadHostPath(resolvedHostPath);
|
||||
fn.setCodeLocalPath(null);
|
||||
@@ -3011,6 +3007,19 @@ public class LambdaService implements ResourceProvider {
|
||||
return path.length() == 0 ? "/" : path.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* True for the host root, {@code /var}, and anything in {@code /run} or {@code /var/run}, where the
|
||||
* Docker socket lives. {@code /proc} counts too: {@code /proc/1/root/var/run} is a different string
|
||||
* for the same directory, and no code directory lives there either.
|
||||
*/
|
||||
static boolean reachesDockerSocketDirectory(Path normalized) {
|
||||
return normalized.getNameCount() == 0
|
||||
|| normalized.equals(Path.of("/var"))
|
||||
|| normalized.startsWith(Path.of("/run"))
|
||||
|| normalized.startsWith(Path.of("/var/run"))
|
||||
|| normalized.startsWith(Path.of("/proc"));
|
||||
}
|
||||
|
||||
private static boolean isUnderHotReloadPrefix(Path normalizedPath, String prefix) {
|
||||
if (prefix == null || prefix.isBlank()) {
|
||||
return false;
|
||||
|
||||
+1
-1
@@ -291,7 +291,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
// and the create->start copy block agree.
|
||||
boolean useCodeVolume = false;
|
||||
if (fn.isHotReload()) {
|
||||
specBuilder.withBind(fn.getHotReloadHostPath(), TASK_DIR);
|
||||
specBuilder.withReadOnlyBind(fn.getHotReloadHostPath(), TASK_DIR);
|
||||
} else if (fn.getCodeLocalPath() != null) {
|
||||
useCodeVolume = shouldUseCodeVolume(Path.of(fn.getCodeLocalPath()));
|
||||
if (useCodeVolume) {
|
||||
|
||||
@@ -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