Light, fluffy, and always free - The AWS Local Emulator alternative
fix: resolve CloudFormation Lambda Code.S3Key base64 decode error (#62)
Ricardo Lima78be5232e212
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/SqsQueueUrlRouterFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/SqsQueueUrlRouterFilter.javaindex d093e3089d..8c6055a45d 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/SqsQueueUrlRouterFilter.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/SqsQueueUrlRouterFilter.java@@ -21,7 +21,7 @@ import java.util.regex.Pattern;@PreMatchingpublic class SqsQueueUrlRouterFilter implements ContainerRequestFilter {- private static final Pattern QUEUE_PATH = Pattern.compile("^/([^/]+)/([^/]+)$");+ private static final Pattern QUEUE_PATH = Pattern.compile("^/(\\d+)/([^/]+)$");@Overridepublic void filter(ContainerRequestContext ctx) {diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.javaindex 401014faa3..20659c0a7f 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java@@ -19,6 +19,7 @@ import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.UUID;+import java.util.concurrent.Future;/*** Handles CloudFormation Query-protocol API calls (form-encoded POST, XML response).@@ -97,7 +98,7 @@ public class CloudFormationQueryHandler {cfnService.createChangeSet(stackName, "initial-create", "CREATE",templateBody, templateUrl, parameters, capabilities, tags, region);- cfnService.executeChangeSet(stackName, "initial-create", region);+ awaitExecution(cfnService.executeChangeSet(stackName, "initial-create", region));Stack stack = cfnService.describeStacks(stackName, region).get(0);String xml = new XmlBuilder()@@ -122,7 +123,7 @@ public class CloudFormationQueryHandler {ChangeSet cs = cfnService.createChangeSet(stackName, "update-" + UUID.randomUUID().toString().substring(0, 8),"UPDATE", templateBody, templateUrl, parameters, capabilities, Map.of(), region);- cfnService.executeChangeSet(stackName, cs.getChangeSetName(), region);+ awaitExecution(cfnService.executeChangeSet(stackName, cs.getChangeSetName(), region));Stack stack = cfnService.describeStacks(stackName, region).get(0);String xml = new XmlBuilder()@@ -537,6 +538,14 @@ public class CloudFormationQueryHandler {.build();}+ private void awaitExecution(Future<?> future) {+ try {+ future.get();+ } catch (Exception e) {+ LOG.warnv("Stack execution failed: {0}", e.getMessage());+ }+ }+private Response xmlError(String code, String message, int status) {String xml = new XmlBuilder().start("ErrorResponse", CF_NS)diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.javaindex 5776d49340..e57954ed3c 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java@@ -19,7 +19,13 @@ import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.jboss.logging.Logger;+import io.github.hectorvent.floci.services.s3.model.S3Object;++import java.io.ByteArrayOutputStream;+import java.nio.charset.StandardCharsets;import java.util.*;+import java.util.zip.ZipEntry;+import java.util.zip.ZipOutputStream;/*** Provisions individual CloudFormation resource types using Floci's existing service implementations.@@ -272,19 +278,60 @@ public class CloudFormationResourceProvisioner {}Map<String, Object> req = new HashMap<>();req.put("FunctionName", funcName);- req.put("Runtime", props != null && props.has("Runtime") ? engine.resolve(props.get("Runtime")) : "nodejs18.x");- req.put("Handler", props != null && props.has("Handler") ? engine.resolve(props.get("Handler")) : "index.handler");- req.put("Role", props != null && props.has("Role") ? engine.resolve(props.get("Role")) : "arn:aws:iam::" + accountId + ":role/default");- if (props != null && props.has("Code")) {- req.put("Code", Map.of("ZipFile", "exports.handler=async(e)=>({statusCode:200})"));- } else {- req.put("Code", Map.of("ZipFile", "exports.handler=async(e)=>({statusCode:200})"));- }+ req.put("Runtime", resolveOrDefault(props, "Runtime", engine, "nodejs18.x"));+ req.put("Handler", resolveOrDefault(props, "Handler", engine, "index.handler"));+ req.put("Role", resolveOrDefault(props, "Role", engine, "arn:aws:iam::" + accountId + ":role/default"));+ req.put("Code", resolveLambdaCode(props, engine));+var func = lambdaService.createFunction(region, req);r.setPhysicalId(funcName);r.getAttributes().put("Arn", func.getFunctionArn());}+ private Map<String, Object> resolveLambdaCode(JsonNode props, CloudFormationTemplateEngine engine) {+ if (props != null && props.has("Code")) {+ JsonNode codeNode = engine.resolveNode(props.get("Code"));++ String s3Bucket = codeNode.path("S3Bucket").asText(null);+ String s3Key = codeNode.path("S3Key").asText(null);+ if (s3Bucket != null && s3Key != null) {+ try {+ S3Object obj = s3Service.getObject(s3Bucket, s3Key);+ String base64Zip = Base64.getEncoder().encodeToString(obj.getData());+ return Map.of("ZipFile", base64Zip);+ } catch (Exception e) {+ LOG.warnv("S3 code not found for Lambda ({0}/{1}), using default handler: {2}",+ s3Bucket, s3Key, e.getMessage());+ }+ }++ String zipFile = codeNode.path("ZipFile").asText(null);+ if (zipFile != null) {+ return Map.of("ZipFile", zipFile);+ }++ String imageUri = codeNode.path("ImageUri").asText(null);+ if (imageUri != null) {+ return Map.of("ImageUri", imageUri);+ }+ }+ return Map.of("ZipFile", defaultHandlerZipBase64());+ }++ private static String defaultHandlerZipBase64() {+ try {+ var baos = new ByteArrayOutputStream();+ try (var zos = new ZipOutputStream(baos)) {+ zos.putNextEntry(new ZipEntry("index.js"));+ zos.write("exports.handler=async(e)=>({statusCode:200})".getBytes(StandardCharsets.UTF_8));+ zos.closeEntry();+ }+ return Base64.getEncoder().encodeToString(baos.toByteArray());+ } catch (Exception e) {+ throw new RuntimeException("Failed to create default handler zip", e);+ }+ }+// ── IAM Role ──────────────────────────────────────────────────────────────private void provisionIamRole(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,@@ -508,6 +555,12 @@ public class CloudFormationResourceProvisioner {return engine.resolve(props.get(name));}+ private String resolveOrDefault(JsonNode props, String name,+ CloudFormationTemplateEngine engine, String defaultValue) {+ String value = resolveOptional(props, name, engine);+ return (value != null && !value.isBlank()) ? value : defaultValue;+ }+private void deleteRoleSafe(String roleName) {try {var role = iamService.getRole(roleName);diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.javaindex e72f93beaf..597b174b19 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java@@ -18,6 +18,7 @@ import java.util.*;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;+import java.util.concurrent.Future;/*** CloudFormation stack lifecycle management — Create, Update, Delete stacks via ChangeSets.@@ -106,7 +107,7 @@ public class CloudFormationService {// ── ExecuteChangeSet ──────────────────────────────────────────────────────- public void executeChangeSet(String stackName, String changeSetName, String region) {+ public Future<?> executeChangeSet(String stackName, String changeSetName, String region) {Stack stack = getStackOrThrow(stackName, region);ChangeSet cs = stack.getChangeSets().get(changeSetName);if (cs == null) {@@ -125,7 +126,7 @@ public class CloudFormationService {String templateBody = cs.getTemplateBody();Map<String, String> params = cs.getParameters() != null ? cs.getParameters() : Map.of();- executor.submit(() -> executeTemplate(stack, templateBody, params, isCreate, region));+ return executor.submit(() -> executeTemplate(stack, templateBody, params, isCreate, region));}// ── DeleteChangeSet ───────────────────────────────────────────────────────diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.javaindex e9145fd701..46577a8924 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java@@ -7,6 +7,12 @@ import io.restassured.http.ContentType;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.Test;+import java.io.ByteArrayOutputStream;+import java.nio.charset.StandardCharsets;+import java.util.Base64;+import java.util.zip.ZipEntry;+import java.util.zip.ZipOutputStream;+import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.containsString;import static org.hamcrest.Matchers.equalTo;@@ -28,6 +34,20 @@ class CloudFormationIntegrationTest {.encodeContentTypeAs(SSM_CONTENT_TYPE, ContentType.TEXT));}+ private static byte[] buildHandlerZip() {+ try {+ var baos = new ByteArrayOutputStream();+ try (var zos = new ZipOutputStream(baos)) {+ zos.putNextEntry(new ZipEntry("index.js"));+ zos.write("exports.handler=async(e)=>({statusCode:200})".getBytes(StandardCharsets.UTF_8));+ zos.closeEntry();+ }+ return baos.toByteArray();+ } catch (Exception e) {+ throw new RuntimeException(e);+ }+ }+@Testvoid createStack_withS3AndSqs() {String template = """@@ -106,6 +126,217 @@ class CloudFormationIntegrationTest {.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));}+ @Test+ void createStack_lambdaWithS3Code() {+ byte[] zipBytes = buildHandlerZip();++ // Create S3 bucket+ given()+ .when()+ .put("/cfn-lambda-code-bucket")+ .then()+ .statusCode(200);++ // Upload ZIP to S3+ given()+ .contentType("application/zip")+ .body(zipBytes)+ .when()+ .put("/cfn-lambda-code-bucket/handler.zip")+ .then()+ .statusCode(200);++ String template = """+ {+ "Resources": {+ "MyFunction": {+ "Type": "AWS::Lambda::Function",+ "Properties": {+ "FunctionName": "cfn-s3code-func",+ "Runtime": "nodejs20.x",+ "Handler": "index.handler",+ "Code": {+ "S3Bucket": "cfn-lambda-code-bucket",+ "S3Key": "handler.zip"+ },+ "Role": "arn:aws:iam::000000000000:role/cfn-test-lambda-role"+ }+ }+ }+ }+ """;++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateStack")+ .formParam("StackName", "cfn-s3code-stack")+ .formParam("TemplateBody", template)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<StackId>"));++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "DescribeStacks")+ .formParam("StackName", "cfn-s3code-stack")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));++ // Verify Lambda function was created+ given()+ .when()+ .get("/2015-03-31/functions/cfn-s3code-func")+ .then()+ .statusCode(200)+ .body("Configuration.FunctionName", equalTo("cfn-s3code-func"));+ }++ @Test+ void createStack_lambdaWithNoCode() {+ String template = """+ {+ "Resources": {+ "MyFunction": {+ "Type": "AWS::Lambda::Function",+ "Properties": {+ "FunctionName": "cfn-nocode-func",+ "Runtime": "nodejs20.x",+ "Handler": "index.handler",+ "Role": "arn:aws:iam::000000000000:role/cfn-test-lambda-role"+ }+ }+ }+ }+ """;++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateStack")+ .formParam("StackName", "cfn-nocode-stack")+ .formParam("TemplateBody", template)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<StackId>"));++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "DescribeStacks")+ .formParam("StackName", "cfn-nocode-stack")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));++ given()+ .when()+ .get("/2015-03-31/functions/cfn-nocode-func")+ .then()+ .statusCode(200)+ .body("Configuration.FunctionName", equalTo("cfn-nocode-func"));+ }++ @Test+ void createStack_lambdaWithImageUri() {+ String template = """+ {+ "Resources": {+ "MyFunction": {+ "Type": "AWS::Lambda::Function",+ "Properties": {+ "FunctionName": "cfn-image-func",+ "Handler": "index.handler",+ "Code": {+ "ImageUri": "123456789012.dkr.ecr.us-east-1.amazonaws.com/my-repo:latest"+ },+ "Role": "arn:aws:iam::000000000000:role/cfn-test-lambda-role"+ }+ }+ }+ }+ """;++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateStack")+ .formParam("StackName", "cfn-image-stack")+ .formParam("TemplateBody", template)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<StackId>"));++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "DescribeStacks")+ .formParam("StackName", "cfn-image-stack")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));+ }++ @Test+ void createStack_lambdaWithZipFile() {+ String base64Zip = Base64.getEncoder().encodeToString(buildHandlerZip());++ String template = """+ {+ "Resources": {+ "MyFunction": {+ "Type": "AWS::Lambda::Function",+ "Properties": {+ "FunctionName": "cfn-zipfile-func",+ "Runtime": "nodejs20.x",+ "Handler": "index.handler",+ "Code": {+ "ZipFile": "%s"+ },+ "Role": "arn:aws:iam::000000000000:role/cfn-test-lambda-role"+ }+ }+ }+ }+ """.formatted(base64Zip);++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateStack")+ .formParam("StackName", "cfn-zipfile-stack")+ .formParam("TemplateBody", template)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<StackId>"));++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "DescribeStacks")+ .formParam("StackName", "cfn-zipfile-stack")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));++ given()+ .when()+ .get("/2015-03-31/functions/cfn-zipfile-func")+ .then()+ .statusCode(200)+ .body("Configuration.FunctionName", equalTo("cfn-zipfile-func"));+ }+@Testvoid createStack_withDynamoDbGsiAndLsi() {String template = """