Light, fluffy, and always free - The AWS Local Emulator alternative
fix(lambda): preserve Docker provided runtime bootstrap paths (#5974)
Andre Kuraitca47df9ec5b4
diff --git a/docs/services/lambda.md b/docs/services/lambda.mdindex bf1030a576..4f2545cdb7 100644--- a/docs/services/lambda.md+++ b/docs/services/lambda.md@@ -1176,6 +1176,11 @@ of `{}` or with an empty `Filters` array clears any existing filters.Any runtime that has an official AWS Lambda container image works with Floci (e.g. `nodejs22.x`, `python3.13`, `java21`, `go1.x`, `provided.al2023`).+With the Docker executor, ZIP-based `provided.*` runtimes execute the package's+`/var/task/bootstrap`, falling back to `/opt/bootstrap` only when the package has+no bootstrap. The supplied executable stays in place, so scripts that locate+companion files relative to their own path continue to work.+By default, Floci builds each runtime image reference from `floci.services.lambda.ecr-base-uri`and the runtime tag. To pin one runtime to a full image reference, set`floci.services.lambda.runtime-images` in the Floci configuration:diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.javaindex d68071c8e5..97a8562f85 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java@@ -37,6 +37,7 @@ import java.io.Closeable;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;+import java.nio.charset.StandardCharsets;import java.nio.file.AtomicMoveNotSupportedException;import java.nio.file.Files;import java.nio.file.Path;@@ -383,16 +384,6 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {copyDirToContainer(dockerClient, containerId, codePath, TASK_DIR, fn.getFunctionName());}- // For provided runtimes, also copy the 'bootstrap' file to /var/runtime (RUNTIME_DIR)- if (isProvidedRuntime(fn.getRuntime())) {- Path bootstrapPath = codePath.resolve("bootstrap");- if (Files.exists(bootstrapPath)) {- copyFileToContainer(dockerClient, containerId, bootstrapPath, RUNTIME_DIR, "bootstrap", fn.getFunctionName());- } else {- LOG.warnv("Provided runtime function {0} is missing 'bootstrap' file in {1}",- fn.getFunctionName(), fn.getCodeLocalPath());- }- }}// 3. Copy layer contents into /opt (layers are merged in order)@@ -413,6 +404,15 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {}}+ // The base image invokes /var/runtime/bootstrap. Delegate to the original+ // package bootstrap, or the merged layers when the package has none.+ // This also works with hot-reload and read-only code-volume mounts.+ if (isProvidedRuntime(fn.getRuntime()) && !"Image".equals(fn.getPackageType())) {+ RetryingTarCopier.copyBytes(dockerClient, containerId, RUNTIME_DIR, "bootstrap",+ ProvidedRuntimeBootstrap.SCRIPT.getBytes(StandardCharsets.UTF_8), 0755,+ COPY_MAX_ATTEMPTS, COPY_RETRY_BACKOFF_MS);+ }+// Now start the container with code in placelifecycleManager.startCreated(containerId, spec);watchForUnexpectedExit(dockerClient, containerId, runtimeApiServer);@@ -1325,20 +1325,6 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {return false;}- private void copyFileToContainer(DockerClient dockerClient, String containerId,- Path sourceFile, String remotePath, String entryName, String functionName) {- copyFileToContainer(dockerClient, containerId, sourceFile, remotePath, entryName, functionName,- COPY_MAX_ATTEMPTS, COPY_RETRY_BACKOFF_MS);- }-- void copyFileToContainer(DockerClient dockerClient, String containerId,- Path sourceFile, String remotePath, String entryName, String functionName,- int maxAttempts, long backoffMillis) {- RetryingTarCopier.copyFile(dockerClient, containerId, remotePath, entryName, sourceFile,- 0755, maxAttempts, backoffMillis);- LOG.debugv("Copied file {0} as {1} into container {2} at {3}", sourceFile, entryName, containerId, remotePath);- }-private static boolean isProvidedRuntime(String runtime) {return runtime != null && runtime.startsWith("provided");}diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrap.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrap.javanew file mode 100644index 0000000000..dfa562d681--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrap.java@@ -0,0 +1,25 @@+package io.github.hectorvent.floci.services.lambda.launcher;++/**+ * Keeps the image's runtime entrypoint while executing the supplied bootstrap in+ * its package or layer directory. Moving the supplied file changes its {@code $0}+ * and breaks runtimes that locate companion files relative to that path.+ */+final class ProvidedRuntimeBootstrap {++ static final String SCRIPT = """+ #!/bin/sh+ set -eu+ if [ -e /var/task/bootstrap ]; then+ exec /var/task/bootstrap "$@"+ fi+ if [ -e /opt/bootstrap ]; then+ exec /opt/bootstrap "$@"+ fi+ echo 'no bootstrap found at /var/task/bootstrap or /opt/bootstrap' >&2+ exit 127+ """;++ private ProvidedRuntimeBootstrap() {+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherCopyRetryTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherCopyRetryTest.javaindex 24276bca39..41d92df551 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherCopyRetryTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherCopyRetryTest.java@@ -13,23 +13,13 @@ import java.util.Optional;import java.util.concurrent.atomic.AtomicInteger;import static org.junit.jupiter.api.Assertions.assertEquals;-import static org.junit.jupiter.api.Assertions.assertSame;-import static org.junit.jupiter.api.Assertions.assertThrows;import static org.mockito.ArgumentMatchers.any;import static org.mockito.Mockito.RETURNS_DEEP_STUBS;import static org.mockito.Mockito.RETURNS_SELF;import static org.mockito.Mockito.mock;import static org.mockito.Mockito.when;-/**- * The transport-level retry ({@code RetryingDockerHttpClient}) must refuse to replay a one-shot- * {@code InputStream} body, after the first attempt the streamed tar bytes are gone. That leaves- * the Lambda launcher's tar-copies (function code, layers, and the TLS CA cert) exposed to- * transient broken pipes: one blip while copying {@code floci-selfsigned.crt} failed an entire- * LZA Accounts stage with {@code Lambda.InitError}. The copy is idempotent (tar extract over the- * same path), and the whole operation, fresh pipe, fresh streamer thread, fresh request, can be- * rebuilt per attempt, so the retry lives at the call site.- */+/** Directory copies retry with a fresh tar stream when the transport cannot replay its body. */class ContainerLauncherCopyRetryTest {private static final int MAX_ATTEMPTS = 3;@@ -46,51 +36,6 @@ class ContainerLauncherCopyRetryTest {return new ContainerLauncher(null, null, null, null, null, null, config, null, null, null, null);}- @Test- void copyFileToContainerRetriesTransientBrokenPipe(@TempDir Path dir) throws IOException {- Path cert = dir.resolve("floci-selfsigned.crt");- Files.writeString(cert, "-----BEGIN CERTIFICATE-----");- DockerClient docker = mock(DockerClient.class);- CopyArchiveToContainerCmd cmd = mock(CopyArchiveToContainerCmd.class, RETURNS_SELF);- AtomicInteger calls = new AtomicInteger();- when(cmd.exec()).thenAnswer(inv -> {- if (calls.incrementAndGet() == 1) {- throw brokenPipe();- }- return null;- });- when(docker.copyArchiveToContainerCmd(any())).thenReturn(cmd);-- launcher().copyFileToContainer(docker, "container-1", cert, "/etc", "floci-ca.crt", "fn",- MAX_ATTEMPTS, 0L);-- assertEquals(2, calls.get(),- "a broken pipe on the cert copy is a socket blip, not a launch failure");- }-- @Test- void copyFileToContainerDoesNotRetryDaemonRejection(@TempDir Path dir) throws IOException {- Path cert = dir.resolve("floci-selfsigned.crt");- Files.writeString(cert, "-----BEGIN CERTIFICATE-----");- DockerClient docker = mock(DockerClient.class);- CopyArchiveToContainerCmd cmd = mock(CopyArchiveToContainerCmd.class, RETURNS_SELF);- RuntimeException noSuchContainer = new RuntimeException("No such container: container-1");- AtomicInteger calls = new AtomicInteger();- when(cmd.exec()).thenAnswer(inv -> {- calls.incrementAndGet();- throw noSuchContainer;- });- when(docker.copyArchiveToContainerCmd(any())).thenReturn(cmd);-- RuntimeException thrown = assertThrows(RuntimeException.class,- () -> launcher().copyFileToContainer(docker, "container-1", cert, "/etc",- "floci-ca.crt", "fn", MAX_ATTEMPTS, 0L));-- assertSame(noSuchContainer, thrown.getCause(),- "a genuine daemon rejection must surface after a single attempt");- assertEquals(1, calls.get());- }-@Testvoid copyDirToContainerRetriesTransientBrokenPipe(@TempDir Path dir) throws IOException {Files.writeString(dir.resolve("index.js"), "exports.handler = () => {};");diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.javaindex 9a3490c5ae..395881c155 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java@@ -30,6 +30,7 @@ import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServer;import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServerFactory;import org.apache.commons.compress.archivers.tar.TarArchiveEntry;+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;@@ -91,6 +92,7 @@ class ContainerLauncherTest {ContainerLauncher launcher;/** Collects remote paths passed to withRemotePath across all copy mocks. */final java.util.List<String> capturedRemotePaths = new java.util.ArrayList<>();+ String capturedRuntimeBootstrap;@BeforeEachvoid setUp() {@@ -157,17 +159,20 @@ class ContainerLauncherTest {lenient().when(lifecycleManager.startCreated(eq("container-123"), any())).thenReturn(info);lenient().when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);- // Stub the Docker copy chain so copyDirToContainer / copyFileToContainer+ // Stub the Docker copy chain so code and bootstrap archive copies// don't throw when the mock DockerClient is used. Each invocation// returns a fresh mock that drains the tar InputStream on exec() to// prevent the background PipedOutputStream writer thread from blocking// when the pipe buffer fills.capturedRemotePaths.clear();+ capturedRuntimeBootstrap = null;lenient().when(dockerClient.copyArchiveToContainerCmd(any())).thenAnswer(inv -> {CopyArchiveToContainerCmd cmd = mock(CopyArchiveToContainerCmd.class);final java.io.InputStream[] captured = {null};+ String[] remote = {null};when(cmd.withRemotePath(any())).thenAnswer(pathInv -> {capturedRemotePaths.add(pathInv.getArgument(0));+ remote[0] = pathInv.getArgument(0);return cmd;});when(cmd.withTarInputStream(any())).thenAnswer(streamInv -> {@@ -176,6 +181,13 @@ class ContainerLauncherTest {});doAnswer(execInv -> {if (captured[0] != null) {+ if ("/var/runtime".equals(remote[0])) {+ try (TarArchiveInputStream tar = new TarArchiveInputStream(captured[0])) {+ assertEquals("bootstrap", tar.getNextEntry().getName());+ capturedRuntimeBootstrap = new String(tar.readAllBytes(), StandardCharsets.UTF_8);+ }+ return null;+ }try { captured[0].transferTo(java.io.OutputStream.nullOutputStream()); }catch (Exception ignored) {}}@@ -830,7 +842,7 @@ class ContainerLauncherTest {}@Test- void launchProvidedRuntime_copiesBootstrapBeforeStart() throws Exception {+ void launchProvidedRuntime_delegatesToOriginalBootstrapBeforeStart() throws Exception {Path codePath = Files.createDirectory(tempDir.resolve("provided-code"));Files.writeString(codePath.resolve("bootstrap"), "#!/bin/sh\necho hello");@@ -847,7 +859,7 @@ class ContainerLauncherTest {// path, so everything happens on the one real container (no populate helper).//// Ordering:- // real: create -> copy /var/task + copy bootstrap to /var/runtime -> start+ // real: create -> copy /var/task + install runtime delegate -> start// (two copyArchiveToContainerCmd calls on the real container: code, then bootstrap).InOrder inOrder = inOrder(lifecycleManager, dockerClient);inOrder.verify(lifecycleManager).create(any());@@ -859,7 +871,9 @@ class ContainerLauncherTest {assertTrue(capturedRemotePaths.contains("/var/task"),"small code should be tar-copied directly to /var/task");assertTrue(capturedRemotePaths.contains("/var/runtime"),- "bootstrap should be copied to /var/runtime on the real container");+ "runtime delegate should be copied to /var/runtime on the real container");+ assertEquals(ProvidedRuntimeBootstrap.SCRIPT, capturedRuntimeBootstrap);+ assertEquals("#!/bin/sh\necho hello", Files.readString(codePath.resolve("bootstrap")));// No populate helper for small code.verify(lifecycleManager, times(1)).create(any());@@ -867,6 +881,34 @@ class ContainerLauncherTest {verify(lifecycleManager, never()).createAndStart(any());}+ @Test+ void launchProvidedRuntimeWithoutPackageBootstrapStillInstallsLayerFallback() throws Exception {+ LambdaFunction fn = new LambdaFunction();+ fn.setFunctionName("layer-runtime");+ fn.setRuntime("provided.al2023");+ fn.setHandler("bootstrap");+ fn.setCodeLocalPath(Files.createDirectory(tempDir.resolve("without-bootstrap")).toString());++ launcher.launch(fn);++ assertEquals(ProvidedRuntimeBootstrap.SCRIPT, capturedRuntimeBootstrap);+ assertTrue(capturedRemotePaths.contains("/var/task"));+ }++ @Test+ void launchHotReloadProvidedRuntimeInstallsDelegateWithoutCopyingPackage() throws Exception {+ LambdaFunction fn = new LambdaFunction();+ fn.setFunctionName("hot-runtime");+ fn.setRuntime("provided.al2023");+ fn.setHandler("bootstrap");+ fn.setHotReloadHostPath(Files.createDirectory(tempDir.resolve("hot-runtime")).toString());++ launcher.launch(fn);++ assertEquals(ProvidedRuntimeBootstrap.SCRIPT, capturedRuntimeBootstrap);+ assertFalse(capturedRemotePaths.contains("/var/task"));+ }+@Testvoid launchFunction_awsConfigPath_bindsAndSkipsCredentials() throws Exception {EmulatorConfig.LambdaServiceConfig lambda = config.services().lambda();diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrapTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrapTest.javanew file mode 100644index 0000000000..cc1016ce3f--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrapTest.java@@ -0,0 +1,112 @@+package io.github.hectorvent.floci.services.lambda.launcher;++import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.io.TempDir;++import java.nio.charset.StandardCharsets;+import java.nio.file.Files;+import java.nio.file.Path;+import java.util.ArrayList;+import java.util.List;+import java.util.concurrent.TimeUnit;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertTrue;++class ProvidedRuntimeBootstrapTest {++ @TempDir+ Path root;+ Path task;+ Path layer;+ Path entrypoint;++ @BeforeEach+ void setUp() throws Exception {+ task = Files.createDirectories(root.resolve("task"));+ layer = Files.createDirectories(root.resolve("layer"));+ entrypoint = root.resolve("runtime-bootstrap");+ // Rebase only the two container directories to execute the exact launcher+ // logic on the test filesystem without Docker, root, or a chroot.+ Files.writeString(entrypoint, ProvidedRuntimeBootstrap.SCRIPT+ .replace("/var/task/", task + "/").replace("/opt/", layer + "/"));+ }++ @Test+ void packageScriptKeepsItsCompanionPathAndTakesPrecedence() throws Exception {+ script(task, """+ #!/bin/sh+ exec "${0%/*}/.payload/worker" "$@"+ """);+ Path payload = Files.createDirectories(task.resolve(".payload"));+ executable(payload.resolve("worker"), "#!/bin/sh\nprintf 'package:%s' \"$1\"\n");+ script(layer, "#!/bin/sh\nexit 93\n");+ Result result = run("two words");+ assertEquals(0, result.exit());+ assertEquals("package:two words", result.output());+ }++ @Test+ void layerScriptKeepsItsCompanionPathWhenPackageHasNoBootstrap() throws Exception {+ script(layer, "#!/bin/sh\ncat \"${0%/*}/value\"\n");+ Files.writeString(layer.resolve("value"), "layer");+ Result result = run();+ assertEquals(0, result.exit());+ assertEquals("layer", result.output());+ }++ @Test+ void executableBinaryRetainsArguments() throws Exception {+ Files.createSymbolicLink(task.resolve("bootstrap"), Path.of("/bin/echo"));+ Result result = run("binary", "two words");+ assertEquals(0, result.exit());+ assertEquals("binary two words\n", result.output());+ }++ @Test+ void missingBootstrapRefuses() throws Exception {+ Result result = run();+ assertEquals(127, result.exit());+ assertEquals("no bootstrap found at " + task + "/bootstrap or "+ + layer + "/bootstrap\n", result.output());+ }++ @Test+ void nonExecutablePackageDoesNotFallBackToLayer() throws Exception {+ Files.writeString(task.resolve("bootstrap"), "#!/bin/sh\nexit 0\n");+ script(layer, "#!/bin/sh\nprintf unexpected-fallback\n");+ Result result = run();+ assertTrue(result.exit() != 0);+ assertFalse(result.output().contains("unexpected-fallback"));+ }++ private void script(Path directory, String content) throws Exception {+ executable(directory.resolve("bootstrap"), content);+ }++ private void executable(Path path, String content) throws Exception {+ Files.writeString(path, content);+ assertTrue(path.toFile().setExecutable(true));+ }++ private Result run(String... arguments) throws Exception {+ List<String> command = new ArrayList<>(List.of("/bin/sh", entrypoint.toString()));+ command.addAll(List.of(arguments));+ Process process = new ProcessBuilder(command).redirectErrorStream(true).start();+ try {+ assertTrue(process.waitFor(5, TimeUnit.SECONDS), "bootstrap must terminate");+ return new Result(process.exitValue(),+ new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8));+ } finally {+ if (process.isAlive()) {+ process.destroyForcibly();+ assertTrue(process.waitFor(5, TimeUnit.SECONDS), "test process must be reaped");+ }+ }+ }++ private record Result(int exit, String output) {+ }+}