feat(lambda): add AddPermission, GetPolicy, ListTags, ListLayerVersions, etc endpoints (#223)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-04-06 00:21:59 -05:00
1 parent d4ebc8e820
commit c79f02f26f
7 files changed
+594 -20

No files matched your search

@@ -370,6 +370,60 @@ public class LambdaController {
return Response.noContent().build();
}
// ──────────────────────────── Permissions (Policy) ────────────────────────────
@POST
@Path("/functions/{functionName}/policy")
public Response addPermission(@Context HttpHeaders headers,
@PathParam("functionName") String functionName,
String body) {
String region = regionResolver.resolveRegion(headers);
try {
@SuppressWarnings("unchecked")
Map<String, Object> request = objectMapper.readValue(body, Map.class);
Map<String, Object> statement = lambdaService.addPermission(region, functionName, request);
String statementJson = objectMapper.writeValueAsString(statement);
ObjectNode root = objectMapper.createObjectNode();
root.put("Statement", statementJson);
return Response.status(201).entity(root).build();
} catch (AwsException e) {
throw e;
} catch (Exception e) {
throw new AwsException("InvalidParameterValueException", e.getMessage(), 400);
}
}
@GET
@Path("/functions/{functionName}/policy")
public Response getPolicy(@Context HttpHeaders headers,
@PathParam("functionName") String functionName) {
String region = regionResolver.resolveRegion(headers);
try {
Map<String, Object> data = lambdaService.getPolicy(region, functionName);
@SuppressWarnings("unchecked")
Map<String, Object> policy = (Map<String, Object>) data.get("policy");
String policyJson = objectMapper.writeValueAsString(policy);
ObjectNode root = objectMapper.createObjectNode();
root.put("Policy", policyJson);
root.put("RevisionId", (String) data.get("revisionId"));
return Response.ok(root).build();
} catch (AwsException e) {
throw e;
} catch (Exception e) {
throw new AwsException("ServiceException", e.getMessage(), 500);
}
}
@DELETE
@Path("/functions/{functionName}/policy/{statementId}")
public Response removePermission(@Context HttpHeaders headers,
@PathParam("functionName") String functionName,
@PathParam("statementId") String statementId) {
String region = regionResolver.resolveRegion(headers);
lambdaService.removePermission(region, functionName, statementId);
return Response.noContent().build();
}
// ──────────────────────────── Helper ────────────────────────────
private Map<String, Object> buildAliasResponse(LambdaAlias alias) {
@@ -0,0 +1,47 @@
package io.github.hectorvent.floci.services.lambda;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
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.MediaType;
import jakarta.ws.rs.core.Response;
/**
* Lambda layer endpoints — use the /2018-10-31 API version prefix.
*
* ListLayers: GET /2018-10-31/layers
* ListLayerVersions: GET /2018-10-31/layers/{LayerName}/versions
*/
@Path("/2018-10-31")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class LambdaLayerController {
private final ObjectMapper objectMapper;
@Inject
public LambdaLayerController(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@GET
@Path("/layers")
public Response listLayers() {
ObjectNode root = objectMapper.createObjectNode();
root.putArray("Layers");
return Response.ok(root).build();
}
@GET
@Path("/layers/{layerName}/versions")
public Response listLayerVersions(@PathParam("layerName") String layerName) {
ObjectNode root = objectMapper.createObjectNode();
root.putArray("LayerVersions");
return Response.ok(root).build();
}
}
@@ -649,6 +649,100 @@ public class LambdaService {
extractZipCode(fn, Base64.getEncoder().encodeToString(obj.getData()));
}
// ──────────────────────────── Permissions (Policy) ────────────────────────────
public Map<String, Object> addPermission(String region, String functionName, Map<String, Object> request) {
LambdaFunction fn = getFunction(region, functionName);
String statementId = (String) request.get("StatementId");
if (statementId == null || statementId.isBlank()) {
throw new AwsException("InvalidParameterValueException", "StatementId is required", 400);
}
fn.getPolicies().stream()
.filter(s -> statementId.equals(s.get("Sid")))
.findFirst()
.ifPresent(s -> {
throw new AwsException("ResourceConflictException",
"The statement id (" + statementId + ") already exists. Please try again with a new Statement Id.", 409);
});
String principal = (String) request.get("Principal");
String action = (String) request.get("Action");
String sourceArn = (String) request.get("SourceArn");
String sourceAccount = (String) request.get("SourceAccount");
Map<String, Object> statement = new java.util.LinkedHashMap<>();
statement.put("Sid", statementId);
statement.put("Effect", "Allow");
if (principal != null && principal.contains(".")) {
statement.put("Principal", Map.of("Service", principal));
} else if (principal != null && principal.startsWith("arn:")) {
statement.put("Principal", Map.of("AWS", principal));
} else {
statement.put("Principal", principal);
}
statement.put("Action", action);
statement.put("Resource", fn.getFunctionArn());
if (sourceArn != null) {
statement.put("Condition", Map.of("ArnLike", Map.of("AWS:SourceArn", sourceArn)));
} else if (sourceAccount != null) {
statement.put("Condition", Map.of("StringEquals", Map.of("AWS:SourceAccount", sourceAccount)));
}
fn.getPolicies().add(statement);
functionStore.save(region, fn);
LOG.infov("Added permission {0} to function {1}", statementId, functionName);
return statement;
}
public Map<String, Object> getPolicy(String region, String functionName) {
LambdaFunction fn = getFunction(region, functionName);
if (fn.getPolicies().isEmpty()) {
throw new AwsException("ResourceNotFoundException",
"Function not found: " + functionName, 404);
}
Map<String, Object> policy = new java.util.LinkedHashMap<>();
policy.put("Version", "2012-10-17");
policy.put("Id", "default");
policy.put("Statement", fn.getPolicies());
return Map.of("policy", policy, "revisionId", fn.getRevisionId());
}
public void removePermission(String region, String functionName, String statementId) {
LambdaFunction fn = getFunction(region, functionName);
boolean removed = fn.getPolicies().removeIf(s -> statementId.equals(s.get("Sid")));
if (!removed) {
throw new AwsException("ResourceNotFoundException",
"Statement " + statementId + " not found in function " + functionName, 404);
}
functionStore.save(region, fn);
LOG.infov("Removed permission {0} from function {1}", statementId, functionName);
}
// ──────────────────────────── Tags ────────────────────────────
public Map<String, String> listTags(String functionArn) {
String[] parts = functionArn.split(":");
LambdaFunction fn = getFunction(parts[3], parts[6]);
return fn.getTags() != null ? fn.getTags() : Map.of();
}
public void tagResource(String functionArn, Map<String, String> tags) {
String[] parts = functionArn.split(":");
LambdaFunction fn = getFunction(parts[3], parts[6]);
if (fn.getTags() == null) fn.setTags(new java.util.HashMap<>());
fn.getTags().putAll(tags);
functionStore.save(parts[3], fn);
}
public void untagResource(String functionArn, List<String> tagKeys) {
String[] parts = functionArn.split(":");
LambdaFunction fn = getFunction(parts[3], parts[6]);
if (fn.getTags() != null) {
tagKeys.forEach(fn.getTags()::remove);
}
functionStore.save(parts[3], fn);
}
private int toInt(Object value, int defaultValue) {
if (value == null) return defaultValue;
if (value instanceof Number n) return n.intValue();
@@ -0,0 +1,79 @@
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.AwsException;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.List;
import java.util.Map;
/**
* Lambda tag endpoints — use the /2017-03-31 API version prefix.
*
* TagResource: POST /2017-03-31/tags/{ARN}
* ListTags: GET /2017-03-31/tags/{ARN}
* UntagResource: DELETE /2017-03-31/tags/{ARN}?tagKeys=...
*/
@Path("/2017-03-31")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class LambdaTagController {
private final LambdaService lambdaService;
private final ObjectMapper objectMapper;
@Inject
public LambdaTagController(LambdaService lambdaService, ObjectMapper objectMapper) {
this.lambdaService = lambdaService;
this.objectMapper = objectMapper;
}
@GET
@Path("/tags/{arn}")
public Response listTags(@PathParam("arn") String arn) {
Map<String, String> tags = lambdaService.listTags(arn);
ObjectNode root = objectMapper.createObjectNode();
ObjectNode tagsNode = root.putObject("Tags");
tags.forEach(tagsNode::put);
return Response.ok(root).build();
}
@POST
@Path("/tags/{arn}")
public Response tagResource(@PathParam("arn") String arn, String body) {
try {
@SuppressWarnings("unchecked")
Map<String, Object> request = objectMapper.readValue(body, Map.class);
@SuppressWarnings("unchecked")
Map<String, String> tags = (Map<String, String>) request.get("Tags");
if (tags == null) {
throw new AwsException("InvalidParameterValueException", "Tags is required", 400);
}
lambdaService.tagResource(arn, tags);
return Response.noContent().build();
} catch (AwsException e) {
throw e;
} catch (Exception e) {
throw new AwsException("InvalidParameterValueException", e.getMessage(), 400);
}
}
@DELETE
@Path("/tags/{arn}")
public Response untagResource(@PathParam("arn") String arn,
@QueryParam("tagKeys") List<String> tagKeys) {
lambdaService.untagResource(arn, tagKeys);
return Response.noContent().build();
}
}
@@ -4,7 +4,9 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RegisterForReflection
@@ -28,6 +30,7 @@ public class LambdaFunction {
private String codeLocalPath;
private Map<String, String> environment = new HashMap<>();
private Map<String, String> tags = new HashMap<>();
private List<Map<String, Object>> policies = new ArrayList<>();
private long lastModified;
private String revisionId;
private String version = "$LATEST";
@@ -90,6 +93,9 @@ public class LambdaFunction {
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
public List<Map<String, Object>> getPolicies() { return policies; }
public void setPolicies(List<Map<String, Object>> policies) { this.policies = policies; }
public long getLastModified() { return lastModified; }
public void setLastModified(long lastModified) { this.lastModified = lastModified; }