diff --git a/docs/services/lambda.md b/docs/services/lambda.md index 3a00843f53..5b1ceb1c01 100644 --- a/docs/services/lambda.md +++ b/docs/services/lambda.md @@ -56,6 +56,10 @@ Floci Lambda runs your function code locally inside real Docker containers - clo | `DeleteFunctionEventInvokeConfig` | Remove the asynchronous invocation settings | | `ListFunctionEventInvokeConfigs` | List the asynchronous invocation settings of every version and alias of a function | +`UpdateFunctionConfiguration` validates `MemorySize` and `Timeout` as whole numbers in their +supported ranges before changing any stored settings. A rejected value returns +`InvalidParameterValueException` and leaves the function configuration unchanged. + The event invoke configuration is stored and returned as AWS does, and `AWS::Lambda::EventInvokeConfig` provisions it from a stack. Asynchronous invocations apply its retry, event age, destination settings, and dead-letter queue configurations (`DeadLetterConfig`). 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.java index eb37f995be..8e3121e7f5 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 @@ -790,7 +790,10 @@ public class LambdaService implements ResourceProvider { if (request.containsKey("Runtime")) { validateEnum(request.get("Runtime"), "runtime", RUNTIME_VALUES); } - validatePattern(request.get("KMSKeyArn"), "kmsKeyArn", KMS_KEY_ARN_PATTERN); + String kmsKeyArn = validatedOptionalString(request.get("KMSKeyArn"), "KMSKeyArn"); + String deadLetterTargetArn = deadLetterConfig == null ? null + : validatedOptionalString(deadLetterConfig.get("TargetArn"), "DeadLetterConfig.TargetArn"); + validatePattern(kmsKeyArn, "kmsKeyArn", KMS_KEY_ARN_PATTERN); if (request.containsKey("SnapStart")) { validateSnapStart(snapStart); } @@ -803,6 +806,10 @@ public class LambdaService implements ResourceProvider { if (request.containsKey("Handler")) { validateHandler((String) request.get("Handler")); } + int memorySize = request.containsKey("MemorySize") + ? validatedConfigurationInteger(request.get("MemorySize"), "MemorySize", 128, 10240) : 0; + int timeout = request.containsKey("Timeout") + ? validatedConfigurationInteger(request.get("Timeout"), "Timeout", 1, 900) : 0; Map requestedVpcConfig = fn.getVpcConfig(); if (requestedVpcConfigUpdate != null) { @@ -823,7 +830,7 @@ public class LambdaService implements ResourceProvider { fn.setHandler((String) request.get("Handler")); } if (request.containsKey("MemorySize")) { - fn.setMemorySize(((Number) request.get("MemorySize")).intValue()); + fn.setMemorySize(memorySize); } if (request.containsKey("Role")) { fn.setRole((String) request.get("Role")); @@ -832,7 +839,7 @@ public class LambdaService implements ResourceProvider { fn.setRuntime((String) request.get("Runtime")); } if (request.containsKey("Timeout")) { - fn.setTimeout(((Number) request.get("Timeout")).intValue()); + fn.setTimeout(timeout); } if (request.containsKey("Environment")) { if (environment != null && environment.containsKey("Variables")) { @@ -870,7 +877,7 @@ public class LambdaService implements ResourceProvider { if (request.containsKey("DeadLetterConfig")) { if (deadLetterConfig != null) { - fn.setDeadLetterTargetArn((String) deadLetterConfig.get("TargetArn")); + fn.setDeadLetterTargetArn(deadLetterTargetArn); } } @@ -879,7 +886,7 @@ public class LambdaService implements ResourceProvider { } if (request.containsKey("KMSKeyArn")) { - fn.setKmsKeyArn((String) request.get("KMSKeyArn")); + fn.setKmsKeyArn(kmsKeyArn); } if (request.containsKey("VpcConfig")) { @@ -931,6 +938,22 @@ public class LambdaService implements ResourceProvider { return fn; } + private static int validatedConfigurationInteger(Object value, String field, int minimum, int maximum) { + if (!(value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long) + || ((Number) value).longValue() < minimum || ((Number) value).longValue() > maximum) { + throw new AwsException("InvalidParameterValueException", + field + " must be an integer between " + minimum + " and " + maximum, 400); + } + return ((Number) value).intValue(); + } + + private static String validatedOptionalString(Object value, String field) { + if (value != null && !(value instanceof String)) { + throw new AwsException("InvalidParameterValueException", field + " must be a string", 400); + } + return (String) value; + } + /** * DeleteFunction without a Qualifier: removes the function and every version of it. */ diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaIntegrationTest.java index 96e0c97e12..68207581c1 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaIntegrationTest.java @@ -8,6 +8,7 @@ import org.junit.jupiter.api.TestMethodOrder; import java.io.ByteArrayOutputStream; import java.util.Base64; +import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -212,6 +213,77 @@ class LambdaIntegrationTest { .statusCode(404); } + @Test + @Order(12) + void invalidMemoryUpdateDoesNotChangeConfiguration() { + given() + .contentType("application/json") + .body(""" + {"FunctionName":"invalid-memory-update","Runtime":"nodejs20.x", + "Role":"arn:aws:iam::000000000000:role/lambda-role","Handler":"index.handler"} + """) + .when() + .post(BASE_PATH + "/functions") + .then() + .statusCode(201); + + given() + .contentType("application/json") + .body(""" + {"Description":"half-applied","MemorySize":"abc"} + """) + .when() + .put(BASE_PATH + "/functions/invalid-memory-update/configuration") + .then() + .statusCode(400) + .body("__type", containsString("InvalidParameterValueException")); + + given() + .when() + .get(BASE_PATH + "/functions/invalid-memory-update/configuration") + .then() + .statusCode(200) + .body("Description", anyOf(nullValue(), equalTo(""))) + .body("MemorySize", equalTo(128)); + } + + @Test + @Order(12) + void invalidArnTypesDoNotChangeConfiguration() { + given() + .contentType("application/json") + .body(""" + {"FunctionName":"invalid-arn-update","Runtime":"nodejs20.x", + "Role":"arn:aws:iam::000000000000:role/lambda-role","Handler":"index.handler"} + """) + .when() + .post(BASE_PATH + "/functions") + .then() + .statusCode(201); + + for (String request : List.of( + "{\"Description\":\"half-applied\",\"KMSKeyArn\":123}", + "{\"Description\":\"half-applied\",\"DeadLetterConfig\":{\"TargetArn\":123}}")) { + given() + .contentType("application/json") + .body(request) + .when() + .put(BASE_PATH + "/functions/invalid-arn-update/configuration") + .then() + .statusCode(400) + .body("__type", containsString("InvalidParameterValueException")); + + given() + .when() + .get(BASE_PATH + "/functions/invalid-arn-update/configuration") + .then() + .statusCode(200) + .body("Description", anyOf(nullValue(), equalTo(""))) + .body("KMSKeyArn", anyOf(nullValue(), equalTo(""))) + .body("DeadLetterConfig.TargetArn", anyOf(nullValue(), equalTo(""))); + } + } + @Test @Order(12) void deleteFunction() { 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.java index be6f7e5bc2..b6ed7c6401 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 @@ -1599,6 +1599,72 @@ class LambdaServiceTest { assertNull(service.getFunction(REGION, "update-desc-fn").getDescription()); } + @Test + void updateFunctionConfigurationRejectsInvalidMemoryBeforeMutation() { + LambdaFunction original = service.createFunction(REGION, baseRequest("invalid-memory-update")); + String revisionId = original.getRevisionId(); + + for (Object invalid : List.of("abc", 127, 10241, 32769, 256.5)) { + AwsException error = assertThrows(AwsException.class, + () -> service.updateFunctionConfiguration(REGION, "invalid-memory-update", + Map.of("Description", "half-applied", "MemorySize", invalid))); + assertEquals("InvalidParameterValueException", error.getErrorCode()); + LambdaFunction stored = service.getFunction(REGION, "invalid-memory-update"); + assertNull(stored.getDescription()); + assertEquals(256, stored.getMemorySize()); + assertEquals(revisionId, stored.getRevisionId()); + } + } + + @Test + void updateFunctionConfigurationAcceptsMaximumMemorySize() { + service.createFunction(REGION, baseRequest("maximum-memory-update")); + + LambdaFunction updated = service.updateFunctionConfiguration(REGION, "maximum-memory-update", + Map.of("MemorySize", 10240)); + + assertEquals(10240, updated.getMemorySize()); + } + + @Test + void updateFunctionConfigurationRejectsNonStringArnsBeforeMutation() { + LambdaFunction original = service.createFunction(REGION, baseRequest("invalid-arn-update")); + String revisionId = original.getRevisionId(); + + for (Map invalid : List.of( + Map.of("KMSKeyArn", 123), + Map.of("DeadLetterConfig", Map.of("TargetArn", 123)))) { + Map request = new HashMap<>(invalid); + request.put("Description", "half-applied"); + AwsException error = assertThrows(AwsException.class, + () -> service.updateFunctionConfiguration(REGION, "invalid-arn-update", request)); + + assertEquals("InvalidParameterValueException", error.getErrorCode()); + LambdaFunction stored = service.getFunction(REGION, "invalid-arn-update"); + assertNull(stored.getDescription()); + assertNull(stored.getKmsKeyArn()); + assertNull(stored.getDeadLetterTargetArn()); + assertEquals(revisionId, stored.getRevisionId()); + } + } + + @Test + void updateFunctionConfigurationRejectsInvalidTimeoutBeforeMutation() { + LambdaFunction original = service.createFunction(REGION, baseRequest("invalid-timeout-update")); + String revisionId = original.getRevisionId(); + + for (Object invalid : List.of("abc", 0, 901, 1.5)) { + AwsException error = assertThrows(AwsException.class, + () -> service.updateFunctionConfiguration(REGION, "invalid-timeout-update", + Map.of("Description", "half-applied", "Timeout", invalid))); + assertEquals("InvalidParameterValueException", error.getErrorCode()); + LambdaFunction stored = service.getFunction(REGION, "invalid-timeout-update"); + assertNull(stored.getDescription()); + assertEquals(10, stored.getTimeout()); + assertEquals(revisionId, stored.getRevisionId()); + } + } + @Test void createFunction_bareStringLayers_isRejectedAsSerializationError() { // Catches: a non-list Layers value being treated as absent instead of rejected