Light, fluffy, and always free - The AWS Local Emulator alternative
fix(lambdaweb): check the endpoint quota after the revisions an endpoint routes to (#6888)
TATSUNO “Taz” Yasuhiroa682400d4583
diff --git a/docs/services/lambda-web-functions.md b/docs/services/lambda-web-functions.mdindex 051fcad9ab..9fac7d55df 100644--- a/docs/services/lambda-web-functions.md+++ b/docs/services/lambda-web-functions.md@@ -42,6 +42,7 @@ The `Lambda Web` service model (apiVersion 2025-03-07, `aws lambda-web` in the A- **Execution role:** a role in another account is `403 AccessDeniedException` "Cross-account pass role is not allowed.". A role that does not exist in IAM, or whose trust policy does not let `lambda.amazonaws.com` assume it, is `400 ValidationException` "The execution role <arn> cannot be assumed by lambda.amazonaws.com. Verify its trust policy. If the role was just created, retry in a few seconds.". As on AWS, this check sets no `aws:SourceAccount` or `aws:SourceArn`, so a trust policy that requires either refuses the role.- **Egress network connectors:** `egressNetworkConnectorArn` names a connector version by the connector id, in the function's region, as on AWS. A version that does not exist, a connector name, or another region is `404 ResourceNotFoundException` "The egress network connector <arn> does not exist.". A connector without `WebFunction` in its `AssociatedComputeResourceTypes` is `400 ValidationException`. An unknown region, a partition that is not the region's, or a version such as `0` or `01` is `400 ValidationException` "The egress network connector ARN: '<arn>' is not valid. ...", and a connector in another account is `403 AccessDeniedException`. A version past the 64-bit range is `500 InternalServerException` with the body `{}`, as on AWS. Only a `HomeRegion` endpoint can serve such a revision: `CreateWebFunction` with another endpoint type, and weights of a `PerRegion` or `MultiRegion` endpoint that name such a revision, are `400 ValidationException`. Floci settles a connector at once, so the AWS refusal of a `PENDING` version never happens. When IAM enforcement is on, the caller also needs `lambda:PassNetworkConnector` on the version ARN as the request gives it, refused as on AWS (see [IAM](iam.md)). Floci checks it before the request reaches Lambda, so a refusal comes before the ARN checks above, which AWS runs first. The connector does not change the network of the function's container.- **Resource policies** are checked the way AWS checks them before storing: the document grammar, a `lambda:` action, a `Resource` that names the function, a valid principal, and no public `Allow`. An action is `*` or `<service>:<action>`, and each one names `lambda` in any case unless the list also holds `*`, as on AWS. AWS reads only the service and the resource of a `Resource`, so `lambda` with `web-function/<name>`, `web-function/<name>*` or `web-function/<name>/...` passes in any partition, region or account, as on AWS. A wildcard principal needs a positive condition (`StringEquals`, `StringLike`, `ArnLike`, `Bool`) on a trusted key such as `aws:PrincipalAccount` or `aws:PrincipalArn`, with no wildcard in the account it names. An `Allow` for a `Federated` principal is public in the same way, unless its values include `*`, as on AWS. AWS checks no pattern on `Federated` and `CanonicalUser` values. Every principal has to exist, as on AWS. A role or user is looked up by its ARN in the account the ARN names, or by its unique id (`AROA...`, `AIDA...`) in the function's account, and a missing one, an assumed-role session, a `CanonicalUser` or an ARN in another partition is `400 ValidationException` "The provided principal was invalid. Please check the principal and try again.". A role or user named under a path other than its own is "The principal arn provided does not match the actual principal arn. Please check your input and try again.". Each check runs on every statement before the next check starts, in the order AWS reports them, so an `Effect` other than `Allow` or `Deny` ("Invalid effect: <value>") in any statement comes before a principal error in another. The principals are looked up once every statement passes its other checks, and before any statement is refused as public. Floci holds every account, so any account id or root is taken to exist, and it keeps no list of service principals, so an unknown one is accepted. An `IamAuth` endpoint evaluates the policy on requests, as below.+- **Endpoint quota:** a function holds up to 10 endpoints, as on AWS. Another one is `402 ServiceQuotaExceededException` "The function <arn> has reached the maxEndpointsPerFunction quota of 10.", checked after the revisions it routes to, so a missing, `Failed` or connector revision is refused first, as on AWS. A deleted endpoint frees its place at once.- **Function names** are unique across the regions of an account, as on AWS. A name that another region already has is `409 ConflictException` "A function named <name> already exists in the account. Function names must be unique across all regions in an account.".- **ARNs** given as identifiers must name the request's region and the caller's account, as on AWS. A revision or endpoint ARN resolves by its id or name only.- **Page tokens** are base64 cursors that carry the request and the position of the page, not the UUIDs AWS issues. A token reused with other parameters gets the AWS "does not match the request" error, and any other token the "invalid or has expired" error.diff --git a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.javaindex 68fd7bf288..49ee34305c 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java@@ -1052,10 +1052,6 @@ public class LambdaWebService implements Resettable {if (fn.endpoints.containsKey(endpoint.endpointName)) {throw new AwsException("ConflictException", "The endpoint " + endpoint.endpointName + " already exists.", 409);}- if (fn.endpoints.size() >= MAX_ENDPOINTS_PER_FUNCTION) {- throw new AwsException("ServiceQuotaExceededException", "The function " + fn.functionArn- + " has reached the maxEndpointsPerFunction quota of " + MAX_ENDPOINTS_PER_FUNCTION + ".", 402);- }if (initialRevision != null) {endpoint.revisionWeights = List.of(weight(initialRevision.revisionId, 100));} else if (!endpoint.revisionWeights.isEmpty()) {@@ -1063,6 +1059,10 @@ public class LambdaWebService implements Resettable {} else {endpoint.revisionWeights = latestRevision(fn).map(id -> List.of(weight(id, 100))).orElse(List.of());}+ if (fn.endpoints.size() >= MAX_ENDPOINTS_PER_FUNCTION) {+ throw new AwsException("ServiceQuotaExceededException", "The function " + fn.functionArn+ + " has reached the maxEndpointsPerFunction quota of " + MAX_ENDPOINTS_PER_FUNCTION + ".", 402);+ }endpoint.endpointArn = fn.functionArn + "/endpoint/" + endpoint.endpointName;endpoint.functionArn = fn.functionArn;String host = URI.create(config.effectiveBaseUrl()).getAuthority();diff --git a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebServiceTest.javaindex 26bdf127c5..1945cfb470 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebServiceTest.java@@ -474,6 +474,14 @@ class LambdaWebServiceTest {assertEquals(notServed.replace("PerRegion", "MultiRegion"),message(() -> service.updateEndpoint(REGION, "app", "mr", update)));assertEquals(plain, service.getEndpoint(REGION, "app", "pr").revisionWeights.get(0).revisionId);++ for (int i = 5; i <= 10; i++) {+ service.createEndpoint(REGION, "app", weighted("ep" + i, "HomeRegion", plain));+ }+ assertEquals(notServed, message(() -> service.createEndpoint(REGION, "app",+ weighted("pr2", "PerRegion", connected))), "before the quota");+ assertEquals("ServiceQuotaExceededException", error(() -> service.createEndpoint(REGION, "app",+ weighted("home2", "HomeRegion", connected))).getErrorCode());}private ObjectNode weighted(String name, String endpointType, String... revisionIds) {@@ -664,6 +672,12 @@ class LambdaWebServiceTest {assertEquals("ServiceQuotaExceededException", quota.getErrorCode());assertEquals(402, quota.getHttpStatus());assertEquals("The function " + ARN + " has reached the maxEndpointsPerFunction quota of 10.", quota.getMessage());+ assertEquals("The endpoint dev already exists.", message(() -> service.createEndpoint(REGION, "app",+ endpointConfig("dev"))));+ ObjectNode missing = endpointConfig("ep11").put("autoDeploymentMode", "Disabled");+ missing.putArray("revisionWeights").addObject().put("revisionId", "rev-aaaaa").put("weight", 100);+ assertEquals("The revision rev-aaaaa does not exist.", message(() -> service.createEndpoint(REGION, "app",+ missing)));String revisionId = service.listRevisions(REGION, "app", mapper.createObjectNode()).items().get(0).revisionId;assertEquals("The revision " + revisionId + " cannot be deleted because the endpoints [dev, ep10, ep2, ep3, ep4,"@@ -1180,6 +1194,17 @@ class LambdaWebServiceTest {assertEquals(latestRefusal, message(() -> service.updateEndpoint(REGION, "app", "dev",mapper.createObjectNode().put("autoDeploymentMode", "LatestRevision"))));assertEquals(latestRefusal, message(() -> service.createEndpoint(REGION, "app", endpointConfig("auto"))));+ for (int i = 2; i <= 10; i++) {+ ObjectNode pinned = endpointConfig("ep" + i).put("autoDeploymentMode", "Disabled");+ pinned.set("revisionWeights", disabled.get("revisionWeights"));+ service.createEndpoint(REGION, "app", pinned);+ }+ assertEquals(refusal, message(() -> service.createEndpoint(REGION, "app", weighted)), "before the quota");+ assertEquals(latestRefusal, message(() -> service.createEndpoint(REGION, "app", endpointConfig("auto"))),+ "before the quota");+ for (int i = 2; i <= 10; i++) {+ service.deleteEndpoint(REGION, "app", "ep" + i);+ }service.deleteRevision(REGION, "app", bad.revisionId);assertEquals(List.of(good), List.copyOf(service.getFunction(REGION, "app").revisions.keySet()));