mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(lambda): add missing runtimes, fix handler validation, long path… (#256)
* feat(lambda): add missing runtimes, fix handler validation, long path extraction, and code signing config * fix: removed unrequired go.sum and package-lock.json from compatibility test
This commit is contained in:
1 parent
48b6ca3475
commit
0ef6f87cba
16 files changed
+477
-4026
No files matched your search
+59
@@ -0,0 +1,59 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
/**
|
||||
* Lambda code-signing endpoints — use the /2020-06-30 API version prefix.
|
||||
*
|
||||
* GetFunctionCodeSigningConfig: GET /2020-06-30/functions/{FunctionName}/code-signing-config
|
||||
*
|
||||
* Floci does not implement code signing config management, so this always returns
|
||||
* an empty CodeSigningConfigArn for existing functions (matching real AWS behaviour
|
||||
* when no signing config is attached). Non-existent functions surface a 404 via
|
||||
* LambdaService, unblocking Terraform and other tools that call this endpoint as
|
||||
* part of their normal Lambda resource lifecycle.
|
||||
*/
|
||||
@Path("/2020-06-30")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class LambdaCodeSigningController {
|
||||
|
||||
private final LambdaService lambdaService;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public LambdaCodeSigningController(LambdaService lambdaService,
|
||||
RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper) {
|
||||
this.lambdaService = lambdaService;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/functions/{functionName}/code-signing-config")
|
||||
public Response getFunctionCodeSigningConfig(@Context HttpHeaders headers,
|
||||
@PathParam("functionName") String functionName) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
LambdaFunction fn = lambdaService.getFunction(region, functionName);
|
||||
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
root.put("CodeSigningConfigArn", "");
|
||||
root.put("FunctionName", fn.getFunctionName());
|
||||
return Response.ok(root).build();
|
||||
}
|
||||
}
|
||||
@@ -126,7 +126,7 @@ public class LambdaService {
|
||||
if (role == null || role.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValueException", "Role is required", 400);
|
||||
}
|
||||
if (handler == null || handler.isBlank()) {
|
||||
if ("Zip".equals(packageType) && (handler == null || handler.isBlank())) {
|
||||
throw new AwsException("InvalidParameterValueException", "Handler is required", 400);
|
||||
}
|
||||
if ("Zip".equals(packageType) && (runtime == null || runtime.isBlank())) {
|
||||
|
||||
+38
-64
@@ -17,9 +17,11 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -137,11 +139,14 @@ public class ContainerLauncher {
|
||||
String containerName = "floci-" + fn.getFunctionName() + "-" + shortId;
|
||||
|
||||
// Create container — CMD must be the handler name (Lambda entrypoint requires it as first arg)
|
||||
// For Image package type without an explicit handler, omit CMD so the image's own CMD is used.
|
||||
CreateContainerCmd createCmd = dockerClient.createContainerCmd(image)
|
||||
.withName(containerName)
|
||||
.withEnv(env)
|
||||
.withCmd(fn.getHandler())
|
||||
.withHostConfig(hostConfig);
|
||||
if (fn.getHandler() != null && !fn.getHandler().isBlank()) {
|
||||
createCmd.withCmd(fn.getHandler());
|
||||
}
|
||||
|
||||
CreateContainerResponse container = createCmd.exec();
|
||||
String containerId = container.getId();
|
||||
@@ -283,15 +288,15 @@ public class ContainerLauncher {
|
||||
java.io.PipedInputStream pis = new java.io.PipedInputStream(pos)) {
|
||||
|
||||
new Thread(() -> {
|
||||
try (pos) {
|
||||
long size = Files.size(sourceFile);
|
||||
writeTarHeader(pos, entryName, size);
|
||||
try (java.io.InputStream fis = Files.newInputStream(sourceFile)) {
|
||||
fis.transferTo(pos);
|
||||
try (TarArchiveOutputStream tar = newTarStream(pos)) {
|
||||
TarArchiveEntry entry = new TarArchiveEntry(entryName);
|
||||
entry.setSize(Files.size(sourceFile));
|
||||
entry.setMode(0755);
|
||||
tar.putArchiveEntry(entry);
|
||||
try (var fis = Files.newInputStream(sourceFile)) {
|
||||
fis.transferTo(tar);
|
||||
}
|
||||
int pad = (int) ((512 - (size % 512)) % 512);
|
||||
if (pad > 0) pos.write(new byte[pad]);
|
||||
pos.write(new byte[1024]); // End-of-archive
|
||||
tar.closeArchiveEntry();
|
||||
} catch (IOException e) {
|
||||
LOG.errorv("Failed to stream file tar for function {0}: {1}", functionName, e.getMessage());
|
||||
}
|
||||
@@ -320,65 +325,34 @@ public class ContainerLauncher {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a minimal POSIX TAR archive from all files in {@code sourceDir}.
|
||||
* Streams content to the OutputStream to avoid loading entire files into memory.
|
||||
* Creates a TAR archive from all files in {@code sourceDir}, streaming to {@code out}.
|
||||
* Uses GNU long-name extension (via Commons Compress) so file paths of any length
|
||||
* are preserved without truncation.
|
||||
*/
|
||||
private static void createTarFromDir(Path sourceDir, java.io.OutputStream out) throws IOException {
|
||||
try (var stream = Files.walk(sourceDir)) {
|
||||
private static void createTarFromDir(Path sourceDir, OutputStream out) throws IOException {
|
||||
try (TarArchiveOutputStream tar = newTarStream(out);
|
||||
var stream = Files.walk(sourceDir)) {
|
||||
for (Path path : (Iterable<Path>) stream::iterator) {
|
||||
if (Files.isDirectory(path)) continue;
|
||||
|
||||
String entryName = sourceDir.relativize(path).toString();
|
||||
long size = Files.size(path);
|
||||
|
||||
writeTarHeader(out, entryName, size);
|
||||
try (java.io.InputStream fis = Files.newInputStream(path)) {
|
||||
fis.transferTo(out);
|
||||
if (Files.isDirectory(path)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Pad data to 512-byte boundary
|
||||
int pad = (int) ((512 - (size % 512)) % 512);
|
||||
if (pad > 0) out.write(new byte[pad]);
|
||||
String entryName = sourceDir.relativize(path).toString();
|
||||
TarArchiveEntry entry = new TarArchiveEntry(entryName);
|
||||
entry.setSize(Files.size(path));
|
||||
entry.setMode(0755);
|
||||
tar.putArchiveEntry(entry);
|
||||
try (var fis = Files.newInputStream(path)) {
|
||||
fis.transferTo(tar);
|
||||
}
|
||||
tar.closeArchiveEntry();
|
||||
}
|
||||
}
|
||||
|
||||
// End-of-archive: two 512-byte zero blocks
|
||||
out.write(new byte[1024]);
|
||||
}
|
||||
|
||||
private static void writeTarHeader(java.io.OutputStream out, String name, long size) throws IOException {
|
||||
byte[] header = new byte[512];
|
||||
|
||||
// Filename (max 100 bytes)
|
||||
byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
|
||||
System.arraycopy(nameBytes, 0, header, 0, Math.min(nameBytes.length, 99));
|
||||
|
||||
// File mode: 0000755
|
||||
putOctal(header, 100, 8, 0755);
|
||||
// UID / GID: 0
|
||||
putOctal(header, 108, 8, 0);
|
||||
putOctal(header, 116, 8, 0);
|
||||
// File size
|
||||
putOctal(header, 124, 12, size);
|
||||
// Modification time
|
||||
putOctal(header, 136, 12, System.currentTimeMillis() / 1000);
|
||||
// Type flag: '0' = regular file
|
||||
header[156] = '0';
|
||||
|
||||
// Checksum: fill checksum field with spaces first, then compute
|
||||
for (int i = 148; i < 156; i++) header[i] = ' ';
|
||||
int checksum = 0;
|
||||
for (byte b : header) checksum += b & 0xFF;
|
||||
putOctal(header, 148, 8, checksum);
|
||||
|
||||
out.write(header);
|
||||
}
|
||||
|
||||
/** Writes {@code value} as a null-terminated octal string into {@code buf[offset..offset+length)}. */
|
||||
private static void putOctal(byte[] buf, int offset, int length, long value) {
|
||||
String octal = String.format("%0" + (length - 1) + "o", value);
|
||||
byte[] bytes = octal.getBytes(StandardCharsets.US_ASCII);
|
||||
System.arraycopy(bytes, 0, buf, offset, Math.min(bytes.length, length - 1));
|
||||
buf[offset + length - 1] = 0;
|
||||
private static TarArchiveOutputStream newTarStream(OutputStream out) {
|
||||
TarArchiveOutputStream tar = new TarArchiveOutputStream(out);
|
||||
tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
|
||||
tar.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
|
||||
return tar;
|
||||
}
|
||||
}
|
||||
+14
-1
@@ -14,21 +14,34 @@ import java.util.Map;
|
||||
public class ImageResolver {
|
||||
|
||||
private static final Map<String, String> RUNTIME_TO_IMAGE = Map.ofEntries(
|
||||
Map.entry("java25", "java:25"),
|
||||
Map.entry("java21", "java:21"),
|
||||
Map.entry("java17", "java:17"),
|
||||
Map.entry("java11", "java:11"),
|
||||
Map.entry("java8.al2", "java:8.al2"),
|
||||
Map.entry("java8", "java:8"),
|
||||
Map.entry("python3.14", "python:3.14"),
|
||||
Map.entry("python3.13", "python:3.13"),
|
||||
Map.entry("python3.12", "python:3.12"),
|
||||
Map.entry("python3.11", "python:3.11"),
|
||||
Map.entry("python3.10", "python:3.10"),
|
||||
Map.entry("python3.9", "python:3.9"),
|
||||
Map.entry("nodejs24.x", "nodejs:24"),
|
||||
Map.entry("nodejs22.x", "nodejs:22"),
|
||||
Map.entry("nodejs20.x", "nodejs:20"),
|
||||
Map.entry("nodejs18.x", "nodejs:18"),
|
||||
Map.entry("nodejs16.x", "nodejs:16"),
|
||||
Map.entry("ruby3.4", "ruby:3.4"),
|
||||
Map.entry("ruby3.3", "ruby:3.3"),
|
||||
Map.entry("ruby3.2", "ruby:3.2"),
|
||||
Map.entry("dotnet10", "dotnet:10"),
|
||||
Map.entry("dotnet9", "dotnet:9"),
|
||||
Map.entry("dotnet8", "dotnet:8"),
|
||||
Map.entry("dotnet6", "dotnet:6"),
|
||||
Map.entry("go1.x", "go:1"),
|
||||
Map.entry("provided.al2023", "provided:al2023"),
|
||||
Map.entry("provided.al2", "provided:al2")
|
||||
Map.entry("provided.al2", "provided:al2"),
|
||||
Map.entry("provided", "provided:latest")
|
||||
);
|
||||
|
||||
private final String baseUri;
|
||||
|
||||
Reference in new issue
Block a user