mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix: resolve CloudFormation Lambda Code.S3Key base64 decode error (#62)
* fix: resolve CloudFormation Lambda Code.S3Key base64 decode error * fix: conflicts with main + 2 tests failing Signed-off-by: Hector Ventura <hectorvent@gmail.com> Co-authored-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
020a54642d
commit
78be5232e2
5 files changed
+307
-13
No files matched your search
@@ -21,7 +21,7 @@ import java.util.regex.Pattern;
|
||||
@PreMatching
|
||||
public class SqsQueueUrlRouterFilter implements ContainerRequestFilter {
|
||||
|
||||
private static final Pattern QUEUE_PATH = Pattern.compile("^/([^/]+)/([^/]+)$");
|
||||
private static final Pattern QUEUE_PATH = Pattern.compile("^/(\\d+)/([^/]+)$");
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext ctx) {
|
||||
|
||||
+11
-2
@@ -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)
|
||||
|
||||
+61
-8
@@ -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);
|
||||
|
||||
+3
-2
@@ -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 ───────────────────────────────────────────────────────
|
||||
|
||||
Reference in new issue
Block a user