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 3ec0659c0b..ff052e8925 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 @@ -310,6 +310,17 @@ public class LambdaService implements ResourceProvider { } public LambdaFunction createFunction(String region, Map request) { + Map environment = structureMember(request, "Environment"); + Map environmentVariables = environmentVariables(environment); + Map ephemeralStorage = structureMember(request, "EphemeralStorage"); + Map tracingConfig = structureMember(request, "TracingConfig"); + Map deadLetterConfig = structureMember(request, "DeadLetterConfig"); + Map vpcConfig = structureMember(request, "VpcConfig"); + Map snapStart = structureMember(request, "SnapStart"); + Map loggingConfig = structureMember(request, "LoggingConfig"); + Map imageConfig = structureMember(request, "ImageConfig"); + Map code = structureMember(request, "Code"); + String functionName = (String) request.get("FunctionName"); String role = (String) request.get("Role"); String handler = (String) request.get("Handler"); @@ -358,12 +369,8 @@ public class LambdaService implements ResourceProvider { fn.setRevisionId(UUID.randomUUID().toString()); // Handle environment variables - @SuppressWarnings("unchecked") - Map envBlock = (Map) request.get("Environment"); - if (envBlock != null) { - @SuppressWarnings("unchecked") - Map vars = (Map) envBlock.get("Variables"); - if (vars != null) fn.setEnvironment(vars); + if (environment != null) { + if (environmentVariables != null) fn.setEnvironment(environmentVariables); } // Handle tags @@ -376,19 +383,19 @@ public class LambdaService implements ResourceProvider { } // EphemeralStorage - if (request.get("EphemeralStorage") instanceof Map es) { - fn.setEphemeralStorageSize(toInt(es.get("Size"), 512)); + if (ephemeralStorage != null) { + fn.setEphemeralStorageSize(toInt(ephemeralStorage.get("Size"), 512)); } // TracingConfig - if (request.get("TracingConfig") instanceof Map tc) { - Object mode = tc.get("Mode"); + if (tracingConfig != null) { + Object mode = tracingConfig.get("Mode"); fn.setTracingMode(mode != null ? mode.toString() : "PassThrough"); } // DeadLetterConfig - if (request.get("DeadLetterConfig") instanceof Map dlq) { - fn.setDeadLetterTargetArn((String) dlq.get("TargetArn")); + if (deadLetterConfig != null) { + fn.setDeadLetterTargetArn((String) deadLetterConfig.get("TargetArn")); } // Layers @@ -404,15 +411,13 @@ public class LambdaService implements ResourceProvider { fn.setKmsKeyArn((String) request.get("KMSKeyArn")); } - if (request.get("VpcConfig") instanceof Map) { - @SuppressWarnings("unchecked") - Map vpc = (Map) request.get("VpcConfig"); - fn.setVpcConfig(vpc); - fn.setVpcId(resolveVpcId(region, vpc)); + if (vpcConfig != null) { + fn.setVpcConfig(vpcConfig); + fn.setVpcId(resolveVpcId(region, vpcConfig)); } - applySnapStart(fn, request.get("SnapStart")); - applyLoggingConfig(fn, request.get("LoggingConfig")); + applySnapStart(fn, snapStart); + applyLoggingConfig(fn, loggingConfig); List fileSystemConfigs = parseFileSystemConfigs(request.get("FileSystemConfigs")); @@ -420,9 +425,7 @@ public class LambdaService implements ResourceProvider { fn.setFileSystemConfigs(fileSystemConfigs); // ImageConfig (PackageType=Image overrides) - if (request.get("ImageConfig") instanceof Map ic) { - @SuppressWarnings("unchecked") - Map imageConfig = (Map) ic; + if (imageConfig != null) { if (imageConfig.get("Command") instanceof List cmd) { fn.setImageConfigCommand(cmd.stream().map(Object::toString).toList()); } @@ -435,8 +438,6 @@ public class LambdaService implements ResourceProvider { } // Handle code deployment - @SuppressWarnings("unchecked") - Map code = (Map) request.get("Code"); if (code != null) { String imageUri = (String) code.get("ImageUri"); if (imageUri != null) { @@ -596,6 +597,16 @@ public class LambdaService implements ResourceProvider { } public LambdaFunction updateFunctionConfiguration(String region, String functionName, Map request) { + Map environment = structureMember(request, "Environment"); + Map environmentVariables = environmentVariables(environment); + Map ephemeralStorage = structureMember(request, "EphemeralStorage"); + Map tracingConfig = structureMember(request, "TracingConfig"); + Map deadLetterConfig = structureMember(request, "DeadLetterConfig"); + Map requestedVpcConfigUpdate = structureMember(request, "VpcConfig"); + Map snapStart = structureMember(request, "SnapStart"); + Map loggingConfig = structureMember(request, "LoggingConfig"); + Map imageConfig = structureMember(request, "ImageConfig"); + LambdaFunction fn = getFunction(region, functionName); List architectures = validateArchitectures(request.get("Architectures")); @@ -611,10 +622,10 @@ public class LambdaService implements ResourceProvider { validateLayersResolvable(layerList); } if (request.containsKey("SnapStart")) { - validateSnapStart(request.get("SnapStart")); + validateSnapStart(snapStart); } if (request.containsKey("LoggingConfig")) { - validateLoggingConfig(request.get("LoggingConfig")); + validateLoggingConfig(loggingConfig); } if (request.containsKey("Role")) { validateRoleArn((String) request.get("Role")); @@ -624,10 +635,8 @@ public class LambdaService implements ResourceProvider { } Map requestedVpcConfig = fn.getVpcConfig(); - if (request.get("VpcConfig") instanceof Map) { - @SuppressWarnings("unchecked") - Map vpc = (Map) request.get("VpcConfig"); - requestedVpcConfig = new java.util.HashMap<>(vpc); + if (requestedVpcConfigUpdate != null) { + requestedVpcConfig = new java.util.HashMap<>(requestedVpcConfigUpdate); } List requestedFileSystemConfigs = fn.getFileSystemConfigs(); if (request.containsKey("FileSystemConfigs")) { @@ -656,12 +665,8 @@ public class LambdaService implements ResourceProvider { fn.setTimeout(((Number) request.get("Timeout")).intValue()); } if (request.containsKey("Environment")) { - @SuppressWarnings("unchecked") - Map envBlock = (Map) request.get("Environment"); - if (envBlock != null && envBlock.containsKey("Variables")) { - @SuppressWarnings("unchecked") - Map vars = (Map) envBlock.get("Variables"); - fn.setEnvironment(vars != null ? vars : new java.util.HashMap<>()); + if (environment != null && environment.containsKey("Variables")) { + fn.setEnvironment(environmentVariables != null ? environmentVariables : new java.util.HashMap<>()); } } @@ -681,21 +686,21 @@ public class LambdaService implements ResourceProvider { } if (request.containsKey("EphemeralStorage")) { - if (request.get("EphemeralStorage") instanceof Map es) { - fn.setEphemeralStorageSize(toInt(es.get("Size"), 512)); + if (ephemeralStorage != null) { + fn.setEphemeralStorageSize(toInt(ephemeralStorage.get("Size"), 512)); } } if (request.containsKey("TracingConfig")) { - if (request.get("TracingConfig") instanceof Map tc) { - Object mode = tc.get("Mode"); + if (tracingConfig != null) { + Object mode = tracingConfig.get("Mode"); fn.setTracingMode(mode != null ? mode.toString() : "PassThrough"); } } if (request.containsKey("DeadLetterConfig")) { - if (request.get("DeadLetterConfig") instanceof Map dlq) { - fn.setDeadLetterTargetArn((String) dlq.get("TargetArn")); + if (deadLetterConfig != null) { + fn.setDeadLetterTargetArn((String) deadLetterConfig.get("TargetArn")); } } @@ -708,18 +713,18 @@ public class LambdaService implements ResourceProvider { } if (request.containsKey("VpcConfig")) { - if (request.get("VpcConfig") instanceof Map) { + if (requestedVpcConfigUpdate != null) { fn.setVpcConfig(requestedVpcConfig); fn.setVpcId(resolveVpcId(region, requestedVpcConfig)); } } if (request.containsKey("SnapStart")) { - applySnapStart(fn, request.get("SnapStart")); + applySnapStart(fn, snapStart); } if (request.containsKey("LoggingConfig")) { - applyLoggingConfig(fn, request.get("LoggingConfig")); + applyLoggingConfig(fn, loggingConfig); } if (request.containsKey("FileSystemConfigs")) { @@ -727,9 +732,7 @@ public class LambdaService implements ResourceProvider { } if (request.containsKey("ImageConfig")) { - if (request.get("ImageConfig") instanceof Map ic) { - @SuppressWarnings("unchecked") - Map imageConfig = (Map) ic; + if (imageConfig != null) { if (imageConfig.containsKey("Command")) { List cmd = imageConfig.get("Command") instanceof List ? ((List) imageConfig.get("Command")).stream().map(Object::toString).toList() : null; @@ -1034,6 +1037,8 @@ public class LambdaService implements ResourceProvider { // ──────────────────────────── Event Source Mapping (SQS) ──────────────────────────── public EventSourceMapping createEventSourceMapping(String region, Map request) { + validateEventSourceMappingStructures(request); + String functionName = (String) request.get("FunctionName"); String eventSourceArn = (String) request.get("EventSourceArn"); @@ -1120,23 +1125,16 @@ public class LambdaService implements ResourceProvider { } private EventSourceMapping.DestinationConfig parseDestinationConfig(Map request) { - Object raw = request.get("DestinationConfig"); - if (raw == null) { + Map destinationConfigMember = structureMember(request, "DestinationConfig"); + if (destinationConfigMember == null) { return null; } - if (!(raw instanceof Map destMap)) { - throw new AwsException("InvalidParameterValueException", - "DestinationConfig must be a JSON object", 400); - } - Object rawOnFailure = destMap.get("OnFailure"); - if (rawOnFailure == null) { + Map onFailureMap = structureValue( + destinationConfigMember.get("OnFailure"), "DestinationConfig.OnFailure"); + if (onFailureMap == null) { return null; } - if (!(rawOnFailure instanceof Map onFailureMap)) { - throw new AwsException("InvalidParameterValueException", - "DestinationConfig.OnFailure must be a JSON object", 400); - } Object destination = onFailureMap.get("Destination"); if (destination == null) { @@ -1208,16 +1206,11 @@ public class LambdaService implements ResourceProvider { * an empty ScalingConfig as "clear the cap"). */ private ScalingConfig parseScalingConfig(Map request, String eventSourceArn) { - Object raw = request.get("ScalingConfig"); - if (raw == null) { + Map map = structureMember(request, "ScalingConfig"); + if (map == null) { return null; } - if (!(raw instanceof Map)) { - throw new AwsException("InvalidParameterValueException", - "ScalingConfig must be a JSON object", 400); - } boolean isSqs = eventSourceArn != null && eventSourceArn.contains(":sqs:"); - Map map = (Map) raw; Object mc = map.get("MaximumConcurrency"); if (mc == null) { if (!isSqs) { @@ -1284,6 +1277,8 @@ public class LambdaService implements ResourceProvider { } public EventSourceMapping updateEventSourceMapping(String uuid, Map request) { + validateEventSourceMappingStructures(request); + EventSourceMapping esm = getEventSourceMapping(uuid); boolean wasEnabled = esm.isEnabled(); @@ -1632,6 +1627,39 @@ public class LambdaService implements ResourceProvider { return null; } + private static Map structureMember(Map request, String member) { + return structureValue(request.get(member), member); + } + + @SuppressWarnings("unchecked") + private static Map environmentVariables(Map environment) { + if (environment == null) { + return null; + } + return (Map) (Map) structureValue( + environment.get("Variables"), "Environment.Variables"); + } + + private static void validateEventSourceMappingStructures(Map request) { + structureMember(request, "ScalingConfig"); + Map destinationConfig = structureMember(request, "DestinationConfig"); + if (destinationConfig != null) { + structureValue(destinationConfig.get("OnFailure"), "DestinationConfig.OnFailure"); + } + } + + @SuppressWarnings("unchecked") + private static Map structureValue(Object value, String member) { + if (value == null) { + return null; + } + if (value instanceof Map map) { + return (Map) map; + } + throw new AwsException("SerializationException", + member + " must be a JSON object or null", 400); + } + private static void validateSnapStart(Object value) { if (!(value instanceof Map snapStart)) { return; diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaStructureSerializationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaStructureSerializationIntegrationTest.java new file mode 100644 index 0000000000..c15966bf3d --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaStructureSerializationIntegrationTest.java @@ -0,0 +1,200 @@ +package io.github.hectorvent.floci.services.lambda; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; + +@QuarkusTest +class LambdaStructureSerializationIntegrationTest { + + private static final String BASE_PATH = "/2015-03-31"; + private static final String QUEUE_ARN = "arn:aws:sqs:us-east-1:000000000000:structure-queue"; + private static final List FUNCTION_STRUCTURE_MEMBERS = List.of( + "Environment", "EphemeralStorage", "TracingConfig", "DeadLetterConfig", + "VpcConfig", "SnapStart", "LoggingConfig", "ImageConfig"); + + @Test + void createFunctionRejectsScalarStructureMembers() { + List members = new java.util.ArrayList<>(FUNCTION_STRUCTURE_MEMBERS); + members.add("Code"); + + for (String member : members) { + Map request = functionRequest("create-scalar-" + member.toLowerCase()); + request.put(member, 5); + + given() + .contentType("application/json") + .body(request) + .when() + .post(BASE_PATH + "/functions") + .then() + .statusCode(400) + .body("__type", equalTo("SerializationException")); + } + } + + @Test + void updateFunctionConfigurationRejectsScalarStructureMembers() { + createFunction("update-scalar-members"); + + for (String member : FUNCTION_STRUCTURE_MEMBERS) { + given() + .contentType("application/json") + .body(Map.of(member, 5)) + .when() + .put(BASE_PATH + "/functions/update-scalar-members/configuration") + .then() + .statusCode(400) + .body("__type", equalTo("SerializationException")); + } + } + + @Test + void environmentVariablesRejectsScalarBeforeUpdateMutation() { + Map createRequest = functionRequest("nested-environment-create"); + createRequest.put("Environment", Map.of("Variables", 5)); + given() + .contentType("application/json") + .body(createRequest) + .when() + .post(BASE_PATH + "/functions") + .then() + .statusCode(400) + .body("__type", equalTo("SerializationException")); + + Map initialRequest = functionRequest("nested-environment-update"); + initialRequest.put("Description", "original description"); + given() + .contentType("application/json") + .body(initialRequest) + .when() + .post(BASE_PATH + "/functions") + .then() + .statusCode(201); + + given() + .contentType("application/json") + .body(Map.of( + "Description", "must not be applied", + "Environment", Map.of("Variables", 5))) + .when() + .put(BASE_PATH + "/functions/nested-environment-update/configuration") + .then() + .statusCode(400) + .body("__type", equalTo("SerializationException")); + + given() + .when() + .get(BASE_PATH + "/functions/nested-environment-update/configuration") + .then() + .statusCode(200) + .body("Description", equalTo("original description")); + } + + @Test + void explicitNullStructureMembersRemainOptional() { + Map request = functionRequest("null-structure-members"); + FUNCTION_STRUCTURE_MEMBERS.forEach(member -> request.put(member, null)); + request.put("Code", null); + + given() + .contentType("application/json") + .body(request) + .when() + .post(BASE_PATH + "/functions") + .then() + .statusCode(201); + + Map update = new HashMap<>(); + FUNCTION_STRUCTURE_MEMBERS.forEach(member -> update.put(member, null)); + given() + .contentType("application/json") + .body(update) + .when() + .put(BASE_PATH + "/functions/null-structure-members/configuration") + .then() + .statusCode(200); + } + + @Test + void eventSourceMappingRejectsScalarStructureMembers() { + createFunction("esm-structure-members"); + + assertEventSourceMappingSerializationError(Map.of("ScalingConfig", 5)); + assertEventSourceMappingSerializationError(Map.of("DestinationConfig", 5)); + assertEventSourceMappingSerializationError(Map.of( + "DestinationConfig", Map.of("OnFailure", 5))); + + String uuid = given() + .contentType("application/json") + .body(Map.of( + "FunctionName", "esm-structure-members", + "EventSourceArn", QUEUE_ARN, + "Enabled", false)) + .when() + .post(BASE_PATH + "/event-source-mappings") + .then() + .statusCode(202) + .body("UUID", notNullValue()) + .extract() + .path("UUID"); + + List> updates = List.of( + Map.of("ScalingConfig", 5), + Map.of("DestinationConfig", 5), + Map.of("DestinationConfig", Map.of("OnFailure", 5))); + for (Map update : updates) { + given() + .contentType("application/json") + .body(update) + .when() + .put(BASE_PATH + "/event-source-mappings/" + uuid) + .then() + .statusCode(400) + .body("__type", equalTo("SerializationException")); + } + } + + private static void assertEventSourceMappingSerializationError(Map structureMember) { + Map request = new HashMap<>(); + request.put("FunctionName", "esm-structure-members"); + request.put("EventSourceArn", QUEUE_ARN); + request.put("Enabled", false); + request.putAll(structureMember); + + given() + .contentType("application/json") + .body(request) + .when() + .post(BASE_PATH + "/event-source-mappings") + .then() + .statusCode(400) + .body("__type", equalTo("SerializationException")); + } + + private static void createFunction(String name) { + given() + .contentType("application/json") + .body(functionRequest(name)) + .when() + .post(BASE_PATH + "/functions") + .then() + .statusCode(201); + } + + private static Map functionRequest(String name) { + Map request = new HashMap<>(); + request.put("FunctionName", name); + request.put("Runtime", "nodejs20.x"); + request.put("Role", "arn:aws:iam::000000000000:role/lambda-role"); + request.put("Handler", "index.handler"); + return request; + } +}