Light, fluffy, and always free - The AWS Local Emulator alternative
fix(lambda): resolve handler paths with subdirectories and skip validation for dotnet runtimes (#404)
Christine Nguyenf183cf2fb552
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.javaindex 345ce077a2..495950824c 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java@@ -647,11 +647,18 @@ public class LambdaService {fn.setCodeLocalPath(codePath.toAbsolutePath().normalize().toString());fn.setCodeSizeBytes(zipBytes.length);- // For non-Java runtimes, verify handler file exists- if (fn.getRuntime() != null && !fn.getRuntime().startsWith("java")) {+ // For file-based runtimes, verify handler file exists (skip Java and .NET which use different handler formats)+ if (fn.getRuntime() != null && !fn.getRuntime().startsWith("java") && !fn.getRuntime().startsWith("dotnet")) {String handlerFile = fn.getHandler().split("\\.")[0];boolean found = Files.walk(codePath)- .anyMatch(p -> p.getFileName().toString().startsWith(handlerFile));+ .filter(Files::isRegularFile)+ .anyMatch(p -> {+ String relative = codePath.relativize(p).toString();+ String withoutExt = relative.contains(".")+ ? relative.substring(0, relative.lastIndexOf('.'))+ : relative;+ return withoutExt.equals(handlerFile);+ });if (!found) {throw new AwsException("InvalidParameterValueException","Handler file '" + handlerFile + "' not found in deployment package", 400);diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.javaindex 37c9c0eda9..821a2eecde 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java@@ -9,9 +9,13 @@ import io.github.hectorvent.floci.services.lambda.zip.ZipExtractor;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import java.io.ByteArrayOutputStream;import java.nio.file.Path;+import java.util.Base64;import java.util.List;import java.util.Map;+import java.util.zip.ZipEntry;+import java.util.zip.ZipOutputStream;import static org.junit.jupiter.api.Assertions.*;@@ -159,6 +163,70 @@ class LambdaServiceTest {assertTrue(ex.getMessage().contains("Handler"));}+ private static String createZipBase64(String... entryPaths) throws Exception {+ ByteArrayOutputStream baos = new ByteArrayOutputStream();+ try (ZipOutputStream zos = new ZipOutputStream(baos)) {+ for (String path : entryPaths) {+ zos.putNextEntry(new ZipEntry(path));+ zos.write("exports.handler = async () => ({});\n".getBytes());+ zos.closeEntry();+ }+ }+ return Base64.getEncoder().encodeToString(baos.toByteArray());+ }++ @Test+ void createFunctionWithSubdirectoryHandler() throws Exception {+ Map<String, Object> req = new java.util.HashMap<>(Map.of(+ "FunctionName", "subdir-handler-fn",+ "Runtime", "nodejs20.x",+ "Role", "arn:aws:iam::000000000000:role/test-role",+ "Handler", "src/index.handler",+ "Code", Map.of("ZipFile", createZipBase64("src/index.js"))+ ));+ LambdaFunction fn = service.createFunction(REGION, req);+ assertEquals("src/index.handler", fn.getHandler());+ }++ @Test+ void createFunctionWithRootHandler() throws Exception {+ Map<String, Object> req = new java.util.HashMap<>(Map.of(+ "FunctionName", "root-handler-fn",+ "Runtime", "nodejs20.x",+ "Role", "arn:aws:iam::000000000000:role/test-role",+ "Handler", "index.handler",+ "Code", Map.of("ZipFile", createZipBase64("index.js"))+ ));+ LambdaFunction fn = service.createFunction(REGION, req);+ assertEquals("index.handler", fn.getHandler());+ }++ @Test+ void createFunctionWithMissingHandler() throws Exception {+ Map<String, Object> req = new java.util.HashMap<>(Map.of(+ "FunctionName", "missing-handler-fn",+ "Runtime", "nodejs20.x",+ "Role", "arn:aws:iam::000000000000:role/test-role",+ "Handler", "src/index.handler",+ "Code", Map.of("ZipFile", createZipBase64("other.js"))+ ));+ AwsException ex = assertThrows(AwsException.class, () -> service.createFunction(REGION, req));+ assertEquals("InvalidParameterValueException", ex.getErrorCode());+ }++ @Test+ void createDotnetFunctionWithAssemblyHandler() throws Exception {+ Map<String, Object> req = new java.util.HashMap<>(Map.of(+ "FunctionName", "dotnet-fn",+ "Runtime", "dotnet6",+ "Role", "arn:aws:iam::000000000000:role/test-role",+ "Handler", "blank-net-lambda::blank_net_lambda.Function::FunctionHandler",+ "Code", Map.of("ZipFile", createZipBase64("blank-net-lambda.dll"))+ ));+ LambdaFunction fn = service.createFunction(REGION, req);+ assertEquals("blank-net-lambda::blank_net_lambda.Function::FunctionHandler", fn.getHandler());+ }+@Testvoid updateFunctionCodeUpdatesRevision() {service.createFunction(REGION, baseRequest("update-fn"));