Light, fluffy, and always free - The AWS Local Emulator alternative
fix(lambda): support nested python handler module paths (#570)
Mateus Scheuer Macedoc68797758584
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 5f5be7ba51..3c8e787465 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@@ -1034,16 +1034,22 @@ public class LambdaService {// 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)- .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);- });+ String handlerFile = resolveHandlerFilePath(fn);+ boolean pythonRuntime = fn.getRuntime().startsWith("python");+ boolean found;+ try (var walk = Files.walk(codePath)) {+ found = walk+ .filter(Files::isRegularFile)+ .anyMatch(p -> {+ String relative = codePath.relativize(p).toString();+ String withoutExt = relative.contains(".")+ ? relative.substring(0, relative.lastIndexOf('.'))+ : relative;+ String normalized = withoutExt.replace('\\', '/');+ return normalized.equals(handlerFile)+ || (pythonRuntime && normalized.equals(handlerFile + "/__init__"));+ });+ }if (!found) {throw new AwsException("InvalidParameterValueException","Handler file '" + handlerFile + "' not found in deployment package", 400);@@ -1074,6 +1080,16 @@ public class LambdaService {extractZipCode(fn, Base64.getEncoder().encodeToString(obj.getData()));}+ private String resolveHandlerFilePath(LambdaFunction fn) {+ String handler = fn.getHandler();+ int lastDot = handler.lastIndexOf('.');+ String modulePath = lastDot >= 0 ? handler.substring(0, lastDot) : handler;+ if (fn.getRuntime().startsWith("python")) {+ return modulePath.replace('.', '/');+ }+ return modulePath;+ }+// ──────────────────────────── Permissions (Policy) ────────────────────────────public Map<String, Object> addPermission(String region, String functionName, Map<String, Object> request) {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 9ea9b95e84..b7dbaded4a 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@@ -264,6 +264,32 @@ class LambdaServiceTest {assertEquals("index.handler", fn.getHandler());}+ @Test+ void createFunctionWithNestedPythonModuleHandler() throws Exception {+ Map<String, Object> req = new java.util.HashMap<>(Map.of(+ "FunctionName", "nested-python-handler-fn",+ "Runtime", "python3.11",+ "Role", "arn:aws:iam::000000000000:role/test-role",+ "Handler", "apps.foo.src.lambda_handler.lambda_handler",+ "Code", Map.of("ZipFile", createZipBase64("apps/foo/src/lambda_handler.py"))+ ));+ LambdaFunction fn = service.createFunction(REGION, req);+ assertEquals("apps.foo.src.lambda_handler.lambda_handler", fn.getHandler());+ }++ @Test+ void createFunctionWithNestedPythonPackageHandler() throws Exception {+ Map<String, Object> req = new java.util.HashMap<>(Map.of(+ "FunctionName", "nested-python-package-handler-fn",+ "Runtime", "python3.11",+ "Role", "arn:aws:iam::000000000000:role/test-role",+ "Handler", "apps.foo.src.lambda_handler.lambda_handler",+ "Code", Map.of("ZipFile", createZipBase64("apps/foo/src/lambda_handler/__init__.py"))+ ));+ LambdaFunction fn = service.createFunction(REGION, req);+ assertEquals("apps.foo.src.lambda_handler.lambda_handler", fn.getHandler());+ }+@Testvoid createFunctionWithMissingHandler() throws Exception {Map<String, Object> req = new java.util.HashMap<>(Map.of(@@ -277,6 +303,20 @@ class LambdaServiceTest {assertEquals("InvalidParameterValueException", ex.getErrorCode());}+ @Test+ void createFunctionWithMissingNestedPythonModuleHandler() throws Exception {+ Map<String, Object> req = new java.util.HashMap<>(Map.of(+ "FunctionName", "missing-nested-python-handler-fn",+ "Runtime", "python3.11",+ "Role", "arn:aws:iam::000000000000:role/test-role",+ "Handler", "apps.foo.src.lambda_handler.lambda_handler",+ "Code", Map.of("ZipFile", createZipBase64("apps/foo/src/other.py"))+ ));+ AwsException ex = assertThrows(AwsException.class, () -> service.createFunction(REGION, req));+ assertEquals("InvalidParameterValueException", ex.getErrorCode());+ assertTrue(ex.getMessage().contains("apps/foo/src/lambda_handler"));+ }+@Testvoid createDotnetFunctionWithAssemblyHandler() throws Exception {Map<String, Object> req = new java.util.HashMap<>(Map.of(