feat(lambda): add PutFunctionConcurrency stub (#325)

* feat(lambda): add PutFunctionConcurrency stub

Add PutFunctionConcurrency, GetFunctionConcurrency, and
DeleteFunctionConcurrency endpoints:

  PUT    /2017-10-31/functions/{FunctionName}/concurrency
  DELETE /2017-10-31/functions/{FunctionName}/concurrency
  GET    /2019-09-30/functions/{FunctionName}/concurrency

The stored ReservedConcurrentExecutions value is persisted on
LambdaFunction and echoed back, but it is not enforced at invocation
time — invocation slots in LambdaExecutorService / WarmPool are not
bounded, and the account-level concurrent execution limit is not
validated.

* fix(lambda): tighten PutFunctionConcurrency validation
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-04-11 09:40:12 -05:00
1 parent a32b7f1f3b
commit 3b6b5fb687
5 files changed
+287 -1

No files matched your search

@@ -0,0 +1,114 @@
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 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.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.PUT;
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;
import java.util.Map;
/**
* Lambda reserved concurrency endpoints. These span two API version prefixes:
*
* PutFunctionConcurrency: PUT /2017-10-31/functions/{FunctionName}/concurrency
* DeleteFunctionConcurrency: DELETE /2017-10-31/functions/{FunctionName}/concurrency
* GetFunctionConcurrency: GET /2019-09-30/functions/{FunctionName}/concurrency
*
* The class-level {@code @Path("/")} lets each method declare its own absolute
* version prefix rather than inheriting a single one.
*
* The stored value is not enforced at invocation time — this is a stub so that
* tools and SDKs expecting the endpoint to exist can proceed.
*/
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class LambdaConcurrencyController {
private final LambdaService lambdaService;
private final RegionResolver regionResolver;
private final ObjectMapper objectMapper;
@Inject
public LambdaConcurrencyController(LambdaService lambdaService,
RegionResolver regionResolver,
ObjectMapper objectMapper) {
this.lambdaService = lambdaService;
this.regionResolver = regionResolver;
this.objectMapper = objectMapper;
}
@PUT
@Path("/2017-10-31/functions/{functionName}/concurrency")
public Response putFunctionConcurrency(@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);
if (!request.containsKey("ReservedConcurrentExecutions")
|| request.get("ReservedConcurrentExecutions") == null) {
throw new AwsException("InvalidParameterValueException",
"ReservedConcurrentExecutions is required", 400);
}
Object raw = request.get("ReservedConcurrentExecutions");
// Jackson parses JSON integer literals as Integer or Long. Anything else
// (Double, BigDecimal, String, etc.) must be rejected — AWS does not
// silently truncate 1.5 to 1.
if (!(raw instanceof Integer) && !(raw instanceof Long)) {
throw new AwsException("InvalidParameterValueException",
"ReservedConcurrentExecutions must be an integer", 400);
}
long longValue = ((Number) raw).longValue();
if (longValue < Integer.MIN_VALUE || longValue > Integer.MAX_VALUE) {
throw new AwsException("InvalidParameterValueException",
"ReservedConcurrentExecutions is out of range", 400);
}
LambdaFunction fn = lambdaService.putFunctionConcurrency(
region, functionName, (int) longValue);
ObjectNode root = objectMapper.createObjectNode();
root.put("ReservedConcurrentExecutions", fn.getReservedConcurrentExecutions());
return Response.ok(root).build();
} catch (AwsException e) {
throw e;
} catch (Exception e) {
throw new AwsException("InvalidParameterValueException", e.getMessage(), 400);
}
}
@GET
@Path("/2019-09-30/functions/{functionName}/concurrency")
public Response getFunctionConcurrency(@Context HttpHeaders headers,
@PathParam("functionName") String functionName) {
String region = regionResolver.resolveRegion(headers);
Integer reserved = lambdaService.getFunctionConcurrency(region, functionName);
ObjectNode root = objectMapper.createObjectNode();
if (reserved != null) {
root.put("ReservedConcurrentExecutions", reserved);
}
return Response.ok(root).build();
}
@DELETE
@Path("/2017-10-31/functions/{functionName}/concurrency")
public Response deleteFunctionConcurrency(@Context HttpHeaders headers,
@PathParam("functionName") String functionName) {
String region = regionResolver.resolveRegion(headers);
lambdaService.deleteFunctionConcurrency(region, functionName);
return Response.noContent().build();
}
}
@@ -583,6 +583,28 @@ public class LambdaService {
}
}
public LambdaFunction putFunctionConcurrency(String region, String functionName, Integer reservedConcurrentExecutions) {
if (reservedConcurrentExecutions == null || reservedConcurrentExecutions < 0) {
throw new AwsException("InvalidParameterValueException",
"ReservedConcurrentExecutions must be a non-negative integer", 400);
}
LambdaFunction fn = getFunction(region, functionName);
fn.setReservedConcurrentExecutions(reservedConcurrentExecutions);
functionStore.save(region, fn);
return fn;
}
public Integer getFunctionConcurrency(String region, String functionName) {
LambdaFunction fn = getFunction(region, functionName);
return fn.getReservedConcurrentExecutions();
}
public void deleteFunctionConcurrency(String region, String functionName) {
LambdaFunction fn = getFunction(region, functionName);
fn.setReservedConcurrentExecutions(null);
functionStore.save(region, fn);
}
public LambdaFunction getFunctionByUrlId(String urlId) {
return functionStore.getByUrlId(urlId)
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Function not found for URL ID: " + urlId, 404));
@@ -35,6 +35,7 @@ public class LambdaFunction {
private String revisionId;
private String version = "$LATEST";
private LambdaUrlConfig urlConfig;
private Integer reservedConcurrentExecutions;
@JsonIgnore
private volatile ContainerState containerState = ContainerState.COLD;
@@ -108,6 +109,9 @@ public class LambdaFunction {
public LambdaUrlConfig getUrlConfig() { return urlConfig; }
public void setUrlConfig(LambdaUrlConfig urlConfig) { this.urlConfig = urlConfig; }
public Integer getReservedConcurrentExecutions() { return reservedConcurrentExecutions; }
public void setReservedConcurrentExecutions(Integer reservedConcurrentExecutions) { this.reservedConcurrentExecutions = reservedConcurrentExecutions; }
@JsonIgnore
public ContainerState getContainerState() { return containerState; }
@JsonIgnore