Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(lambdaweb): check the code bucket versioning and policy as AWS does (#6479)

TATSUNO “Taz” Yasuhirocd2dac708da3
diff --git a/compatibility-tests/sdk-test-awscli/test/lambda-web.bats b/compatibility-tests/sdk-test-awscli/test/lambda-web.bats
index 6d2b96f0d4..f8563f6d86 100644
--- a/compatibility-tests/sdk-test-awscli/test/lambda-web.bats
+++ b/compatibility-tests/sdk-test-awscli/test/lambda-web.bats
@@ -14,6 +14,11 @@ setup() {
'{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}' \
>/dev/null 2>&1 || true
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null 2>&1 || true
+ # AWS takes code only from a versioned bucket that lets Lambda read it.
+ aws_cmd s3api put-bucket-versioning --bucket "$BUCKET" --versioning-configuration Status=Enabled >/dev/null
+ aws_cmd s3api put-bucket-policy --bucket "$BUCKET" --policy \
+ "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Action\":[\"s3:GetObject\",\"s3:GetObjectVersion\"],\"Resource\":\"arn:aws:s3:::$BUCKET/*\"}]}" \
+ >/dev/null
# A revision whose code is not a zip fails. python3 ships in the CLI image.
python3 -c 'import sys, zipfile
with zipfile.ZipFile(sys.argv[1], "w") as z:
@@ -27,7 +32,10 @@ teardown() {
aws_cmd iam delete-role-policy --role-name "awscli-lambdaweb-$FUNCTION_NAME" --policy-name awscli-lambdaweb-logs >/dev/null 2>&1 || true
aws_cmd iam delete-role --role-name "awscli-lambdaweb-$FUNCTION_NAME" >/dev/null 2>&1 || true
fi
- aws_cmd s3 rb "s3://$BUCKET" --force >/dev/null 2>&1 || true
+ for version in $(aws_cmd s3api list-object-versions --bucket "$BUCKET" --query 'Versions[].VersionId' --output text 2>/dev/null); do
+ aws_cmd s3api delete-object --bucket "$BUCKET" --key app.zip --version-id "$version" >/dev/null 2>&1 || true
+ done
+ aws_cmd s3api delete-bucket --bucket "$BUCKET" >/dev/null 2>&1 || true
aws_cmd iam delete-role --role-name "$ROLE_NAME" >/dev/null 2>&1 || true
}
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaWebTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaWebTest.java
index 25e18f8b4e..ab27669316 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaWebTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaWebTest.java
@@ -32,6 +32,7 @@ import software.amazon.awssdk.services.lambdaweb.model.ServiceConfig;
import software.amazon.awssdk.services.lambdaweb.model.UpdateWebFunctionEndpointResponse;
import software.amazon.awssdk.services.lambdaweb.model.ValidationException;
import software.amazon.awssdk.services.s3.S3Client;
+import software.amazon.awssdk.services.s3.model.BucketVersioningStatus;
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
import java.util.Map;
@@ -46,12 +47,16 @@ class LambdaWebTest {
private static final String FUNCTION_NAME = "sdk-test-web-fn";
private static final String LAMBDA_TRUST = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}";
+ private static final String LAMBDA_READ = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ + "\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},"
+ + "\"Action\":[\"s3:GetObject\",\"s3:GetObjectVersion\"],\"Resource\":\"arn:aws:s3:::%s/*\"}]}";
private static LambdaWebClient lambdaWeb;
private static IamClient iam;
private static String roleName;
private static String role;
private static S3Client s3;
private static String bucket;
+ private static String codeVersion;
private static String functionArn;
private static String firstRevisionId;
private static String secondRevisionId;
@@ -65,7 +70,12 @@ class LambdaWebTest {
role = iam.createRole(r -> r.roleName(roleName).assumeRolePolicyDocument(LAMBDA_TRUST)).role().arn();
bucket = TestFixtures.uniqueName("sdk-test-web-code");
s3.createBucket(r -> r.bucket(bucket));
- s3.putObject(r -> r.bucket(bucket).key("app.zip"), RequestBody.fromBytes(LambdaUtils.minimalZip()));
+ // AWS takes code only from a versioned bucket that lets Lambda read it.
+ s3.putBucketVersioning(r -> r.bucket(bucket)
+ .versioningConfiguration(v -> v.status(BucketVersioningStatus.ENABLED)));
+ s3.putBucketPolicy(r -> r.bucket(bucket).policy(LAMBDA_READ.formatted(bucket)));
+ codeVersion = s3.putObject(r -> r.bucket(bucket).key("app.zip"),
+ RequestBody.fromBytes(LambdaUtils.minimalZip())).versionId();
}
/** Only "already gone" is tolerated; any other failure to clean up fails the run. */
@@ -81,7 +91,7 @@ class LambdaWebTest {
}
if (s3 != null) {
try {
- s3.deleteObject(r -> r.bucket(bucket).key("app.zip"));
+ s3.deleteObject(r -> r.bucket(bucket).key("app.zip").versionId(codeVersion));
s3.deleteBucket(r -> r.bucket(bucket));
} catch (NoSuchBucketException expected) {
// Setup did not get as far as creating the bucket.
diff --git a/docs/services/iam.md b/docs/services/iam.md
index c73fe05883..5bad7292fb 100644
--- a/docs/services/iam.md
+++ b/docs/services/iam.md
@@ -1793,7 +1793,7 @@ When `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED` is active, Floci also queries regi
- SNS topic policies take part the same way for a request that names the topic in a JSON-protocol body; a topic with no policy set has the default one AWS gives a new topic. See [SNS](sns.md).
- Aurora DSQL cluster policies take part the same way for a control plane call on the cluster, and when a client connects to the cluster's endpoint. See [DSQL](dsql.md#authentication).
- SQS queue and API Gateway REST API policies are stored but not evaluated.
-- A call an AWS service makes to another service's resource on its own behalf, such as SES publishing an event or a notification to an SNS topic, has no IAM principal, so the target's resource policy decides it alone and has to allow the calling service, through `{"Service": "<service>.amazonaws.com"}` or `{"AWS": "*"}`. A target with no policy refuses it. Its request context holds `aws:PrincipalIsAWSService` (`true`), `aws:PrincipalServiceName` and `aws:PrincipalServiceNamesList`, `aws:SourceArn` and `aws:SourceAccount` for the resource whose configuration triggered the call, `aws:ResourceAccount` and `aws:RequestedRegion`, and none of the keys that describe an IAM principal. A publish to an SNS topic from SES also carries `aws:SourceOwner`, the key the SNS Developer Guide lists SES among the services setting, which is what the default topic policy checks, so that policy admits SES acting for the topic owner's account and no other. SES is the one service checked this way so far; see [SES IAM enforcement](ses.md#iam-enforcement).
+- A call an AWS service makes to another service's resource on its own behalf, such as SES publishing an event or a notification to an SNS topic, has no IAM principal, so the target's resource policy decides it alone and has to allow the calling service, through `{"Service": "<service>.amazonaws.com"}` or `{"AWS": "*"}`. A target with no policy refuses it. Its request context holds `aws:PrincipalIsAWSService` (`true`), `aws:PrincipalServiceName` and `aws:PrincipalServiceNamesList`, `aws:SourceArn` and `aws:SourceAccount` for the resource whose configuration triggered the call, `aws:ResourceAccount` and `aws:RequestedRegion`, and none of the keys that describe an IAM principal. A publish to an SNS topic from SES also carries `aws:SourceOwner`, the key the SNS Developer Guide lists SES among the services setting, which is what the default topic policy checks, so that policy admits SES acting for the topic owner's account and no other. SES and Lambda, when it reads the code of a Web Function revision from S3, are the services checked this way so far; see [SES IAM enforcement](ses.md#iam-enforcement) and [Lambda Web Functions](lambda-web-functions.md#emulation-behavior).
### Service control policies (SCPs)
diff --git a/docs/services/lambda-web-functions.md b/docs/services/lambda-web-functions.md
index 7e40656d26..64f0b8d68f 100644
--- a/docs/services/lambda-web-functions.md
+++ b/docs/services/lambda-web-functions.md
@@ -36,7 +36,7 @@ The `Lambda Web` service model (apiVersion 2025-03-07, `aws lambda-web` in the A
- **Instant convergence:** a revision and an endpoint are stored `Active`, or `Failed` as below, and an endpoint update is stored `Successful`. The create and update responses still report the transitional state AWS reports (`Pending`, `updateStatus: InProgress`, and the per-region block as it was before the update), so a waiter sees one poll.
- **Failed revisions:** a revision whose code is not a zip, or whose files add up to more than 512 MiB, is stored `Failed`, as on AWS. It cannot be deployed: weights that name it, or `LatestRevision` while it is the newest revision, are `409 ConflictException`, and `LatestRevision` endpoints stay on their revision. The endpoint of a `CreateWebFunction` whose revision fails is `Failed` too. It answers every request with `409` and `Platform.EndpointNotActive` and refuses updates until it is deleted. In a stack, the revision's resource fails with the revision's `stateReason` and the rollback deletes it. An empty object is a `500 InternalServerException` with the body `{}`, and an object over 512 MiB is a `400 ValidationException`, as on AWS.
-- **Code comes from S3 only.** The object named in `buildConfig.codeConfig.s3Object` must exist in Floci's S3, as on AWS. AWS also requires bucket versioning and a bucket policy for the Lambda service principal. Floci does not check them yet.
+- **Code comes from S3 only.** The object named in `buildConfig.codeConfig.s3Object` must exist in Floci's S3, as on AWS. A request without `versionId` is refused when the latest version is the null version, so the object has to be written after the bucket's versioning is enabled, as on AWS. When IAM enforcement is on, the bucket policy must also let `lambda.amazonaws.com` take `s3:GetObjectVersion` on the object. The policy sees the new revision ARN as `aws:SourceArn` and the function's account as `aws:SourceAccount`. As on AWS, `s3:GetObject` is not evaluated, although the error names it. Floci does not check the caller's own S3 permissions or the KMS key policy of an encrypted object.
- **Each revision keeps its own copy of the code**, read at creation from the version the request names or the current one, under the Lambda `code-path`. As on AWS, a later write or delete in the bucket does not change what the revision runs. AWS moves a revision to `Failed` when it later finds its object version gone, which Floci does not do.
- **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. The network connector ARN is checked for shape only.
- **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`. 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 `IamAuth` endpoint evaluates it on requests, as below.
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.java
index 14059e827c..e70dd65cfe 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
@@ -13,9 +13,12 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.RequestContext;
import io.github.hectorvent.floci.core.common.RequestScopes;
import io.github.hectorvent.floci.core.common.Resettable;
+import io.github.hectorvent.floci.core.common.ServiceCallAuthorizer;
+import io.github.hectorvent.floci.core.common.ServicePrincipals;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
+import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
import io.github.hectorvent.floci.services.iam.PublicAccessConditions;
import io.github.hectorvent.floci.services.lambdaweb.model.WebFunction;
import io.github.hectorvent.floci.services.lambdaweb.model.WebFunctionEndpoint;
@@ -140,6 +143,7 @@ public class LambdaWebService implements Resettable {
private final ObjectMapper objectMapper;
private final Instance<RequestContext> requestContext;
private final LambdaWebExecutionRole executionRole;
+ private final ServiceCallAuthorizer serviceCalls;
/** Keyed {@code region/functionName}. The backend adds the caller's account to every key. */
private StorageBackend<String, WebFunction> functions;
@@ -152,7 +156,8 @@ public class LambdaWebService implements Resettable {
@Inject
public LambdaWebService(RegionResolver regionResolver, StorageFactory storageFactory, EmulatorConfig config,
S3Service s3Service, LambdaWebCode code, ObjectMapper objectMapper,
- Instance<RequestContext> requestContext, LambdaWebExecutionRole executionRole) {
+ Instance<RequestContext> requestContext, LambdaWebExecutionRole executionRole,
+ ServiceCallAuthorizer serviceCalls) {
this.regionResolver = regionResolver;
this.storageFactory = storageFactory;
this.config = config;
@@ -161,6 +166,7 @@ public class LambdaWebService implements Resettable {
this.objectMapper = objectMapper;
this.requestContext = requestContext;
this.executionRole = executionRole;
+ this.serviceCalls = serviceCalls;
}
@PostConstruct
@@ -625,11 +631,16 @@ public class LambdaWebService implements Resettable {
WebFunctionRevision.BuildConfig build = revision.buildConfig;
long size;
try (S3Service.ObjectRead object = openCode(build)) {
+ if (build.versionId == null && object.object().getVersionId() == null) {
+ throw new AwsException("ValidationException", "You must enable S3 versioning for your S3 bucket"
+ + " when S3ObjectStorageMode is set to REFERENCE.", 400);
+ }
// AWS accepts an object of exactly this size, despite the wording.
if (LambdaWebCode.MAX_BYTES < object.object().getSize()) {
throw new AwsException("ValidationException", "Zipped size must be smaller than "
+ LambdaWebCode.MAX_BYTES + " bytes", 400);
}
+ requireLambdaCanRead(fn, revision);
size = code.store(accountId, region, fn.functionName, revision.revisionId, object.body());
} catch (IOException e) {
throw new UncheckedIOException("Could not close the code object s3://" + build.bucket + "/" + build.key, e);
@@ -651,6 +662,21 @@ public class LambdaWebService implements Resettable {
}
}
+ /** AWS evaluates only s3:GetObjectVersion, although its message names both actions. */
+ private void requireLambdaCanRead(WebFunction fn, WebFunctionRevision revision) {
+ WebFunctionRevision.BuildConfig build = revision.buildConfig;
+ AwsArnUtils.Arn function = AwsArnUtils.parse(fn.functionArn);
+ String objectArn = AwsArnUtils.Arn.global(function.partition(), "s3", "", build.bucket + "/" + build.key)
+ .toString();
+ if (serviceCalls.authorize("lambda", "s3:GetObjectVersion", objectArn, revision.revisionArn,
+ function.accountId()) == Decision.DENY) {
+ throw new AwsException("ValidationException", "Lambda service principal " + ServicePrincipals.of("lambda")
+ + " does not have s3:GetObject and s3:GetObjectVersion permission on s3://" + build.bucket + "/"
+ + build.key + ". Update the bucket policy to grant the Lambda service principal access and retry.",
+ 400);
+ }
+ }
+
private S3Service.ObjectRead openCode(WebFunctionRevision.BuildConfig build) {
try {
return s3Service.openObject(build.bucket, build.key, build.versionId);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaWebIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaWebIntegrationTest.java
index ffbe923ac2..edbcbc426d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaWebIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaWebIntegrationTest.java
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.services.iam.IamService;
+import io.github.hectorvent.floci.services.lambdaweb.LambdaWebCodeBuckets;
import io.github.hectorvent.floci.services.lambdaweb.LambdaWebRoles;
import io.github.hectorvent.floci.services.lambdaweb.LambdaWebZips;
import io.github.hectorvent.floci.services.s3.S3Service;
@@ -44,7 +45,7 @@ class CloudFormationLambdaWebIntegrationTest {
@Test
void stackProvisionsFunctionRevisionAndEndpoint() {
LambdaWebRoles.trustLambda(iamService, "arn:aws:iam::000000000000:role/cfn-web");
- s3Service.createBucket(BUCKET, "us-east-1");
+ LambdaWebCodeBuckets.create(s3Service, BUCKET);
s3Service.putObject(BUCKET, "app.zip", LambdaWebZips.indexJs(""), "application/zip", Map.of());
String suffix = Long.toString(System.nanoTime(), 36);
String functionName = "cfn-web-" + suffix;
@@ -110,7 +111,7 @@ class CloudFormationLambdaWebIntegrationTest {
@Test
void aRevisionOnCodeThatIsNotAZipFailsItsResource() {
LambdaWebRoles.trustLambda(iamService, "arn:aws:iam::000000000000:role/cfn-web");
- s3Service.createBucket(BUCKET, "us-east-1");
+ LambdaWebCodeBuckets.create(s3Service, BUCKET);
s3Service.putObject(BUCKET, "app.zip", LambdaWebZips.indexJs(""), "application/zip", Map.of());
s3Service.putObject(BUCKET, "notzip.zip", "this is not a zip\n".getBytes(StandardCharsets.UTF_8),
"application/zip", Map.of());
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontWebFunctionOriginServingTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontWebFunctionOriginServingTest.java
index 33bd08aa8f..4fbb4ac13e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontWebFunctionOriginServingTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontWebFunctionOriginServingTest.java
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.services.cloudfront.model.Distribution;
import io.github.hectorvent.floci.services.cloudfront.model.DistributionConfig;
import io.github.hectorvent.floci.services.cloudfront.model.Origin;
import io.github.hectorvent.floci.services.iam.IamService;
+import io.github.hectorvent.floci.services.lambdaweb.LambdaWebCodeBuckets;
import io.github.hectorvent.floci.services.lambdaweb.LambdaWebRoles;
import io.github.hectorvent.floci.services.lambdaweb.LambdaWebZips;
import io.github.hectorvent.floci.services.s3.S3Service;
@@ -89,7 +90,7 @@ class CloudFrontWebFunctionOriginServingTest {
static String createFunction(S3Service s3Service, IamService iamService, PartitionCleanup cleanup,
String functionName, String authType) {
LambdaWebRoles.trustLambda(iamService, ROLE);
- s3Service.createBucket(BUCKET, "us-east-1");
+ LambdaWebCodeBuckets.create(s3Service, BUCKET);
s3Service.putObject(BUCKET, "app.zip", LambdaWebZips.indexJs(""), "application/zip", Map.of());
String domain = given().contentType("application/json")
.body("""
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebCodeBucketIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebCodeBucketIntegrationTest.java
new file mode 100644
index 0000000000..d4fe82709f
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebCodeBucketIntegrationTest.java
@@ -0,0 +1,161 @@
+package io.github.hectorvent.floci.services.lambdaweb;
+
+import io.github.hectorvent.floci.services.iam.IamService;
+import io.github.hectorvent.floci.services.s3.S3Service;
+import io.github.hectorvent.floci.testing.IamEnforcementProfile;
+import io.github.hectorvent.floci.testing.PartitionCleanup;
+import io.quarkus.test.common.http.TestHTTPResource;
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.TestProfile;
+import io.restassured.response.ValidatableResponse;
+import jakarta.inject.Inject;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import java.net.URI;
+import java.util.Map;
+
+import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
+
+/** Under IAM enforcement the code bucket must be versioned and let Lambda read the object version. */
+@QuarkusTest
+@TestProfile(IamEnforcementProfile.class)
+class LambdaWebCodeBucketIntegrationTest {
+
+ private static final String BASE = "/2025-03-07";
+ private static final String ROOT = auth("000000000000", "lambda");
+ private static final String ROLE = "arn:aws:iam::000000000000:role/web";
+ private static final String VERSIONING = "You must enable S3 versioning for your S3 bucket when"
+ + " S3ObjectStorageMode is set to REFERENCE.";
+
+ @TestHTTPResource("/")
+ static URI baseUri;
+
+ @RegisterExtension
+ final PartitionCleanup cleanup = new PartitionCleanup();
+
+ @Inject
+ S3Service s3Service;
+
+ @Inject
+ IamService iamService;
+
+ private String functionName;
+ private String functionArn;
+ private String bucket;
+
+ @BeforeEach
+ void createFunction() {
+ functionName = "code-bucket-" + Long.toString(System.nanoTime(), 36);
+ functionArn = "arn:aws:lambda:us-east-1:000000000000:web-function/" + functionName;
+ bucket = functionName;
+ LambdaWebRoles.trustLambda(iamService, ROLE);
+ LambdaWebCodeBuckets.create(s3Service, "web-code-bucket");
+ put("web-code-bucket");
+ int port = baseUri.getPort();
+ String name = functionName;
+ cleanup.register(() -> given().port(port).header("Authorization", ROOT)
+ .delete(BASE + "/web-functions/" + name));
+ given().header("Authorization", ROOT).contentType("application/json")
+ .body("""
+ {"functionName": "%s",
+ "revisionConfig": {"buildConfig": %s, "serviceConfig": {"executionRoleArn": "%s"}},
+ "endpointConfig": {"endpointName": "dev", "endpointType": "HomeRegion",
+ "authType": "ApplicationManaged"}}
+ """.formatted(functionName, build("web-code-bucket", null), ROLE))
+ .when().put(BASE + "/web-functions")
+ .then().statusCode(202);
+ s3Service.createBucket(bucket, "us-east-1");
+ }
+
+ @Test
+ void theLatestVersionOfTheObjectMustNotBeTheNullVersion() {
+ LambdaWebCodeBuckets.allowLambda(s3Service, bucket);
+ put(bucket);
+ refused(null, VERSIONING);
+ s3Service.putBucketVersioning(bucket, "Enabled");
+ refused(null, VERSIONING);
+ accepted("null");
+
+ put(bucket);
+ accepted(null);
+ s3Service.putBucketVersioning(bucket, "Suspended");
+ accepted(null);
+ put(bucket);
+ refused(null, VERSIONING);
+ }
+
+ @Test
+ void lambdaNeedsGetObjectVersionUnderTheConditionsItSends() {
+ s3Service.putBucketVersioning(bucket, "Enabled");
+ put(bucket);
+ String refusal = "Lambda service principal lambda.amazonaws.com does not have s3:GetObject and"
+ + " s3:GetObjectVersion permission on s3://" + bucket + "/app.zip. Update the bucket policy to grant"
+ + " the Lambda service principal access and retry.";
+ refused(null, refusal);
+
+ policy(allow("\"s3:GetObject\"", null));
+ refused(null, refusal);
+ policy(allow("\"s3:GetObjectVersion\"", null));
+ accepted(null);
+ policy(allow("\"s3:GetObjectVersion\"", "{\"ArnLike\":{\"aws:SourceArn\":\"" + functionArn + "/revision/*\"}}"));
+ accepted(null);
+ policy(allow("\"s3:GetObjectVersion\"", "{\"ArnLike\":{\"aws:SourceArn\":\"" + functionArn + "\"}}"));
+ refused(null, refusal);
+ policy(allow("\"s3:GetObjectVersion\"", "{\"StringEquals\":{\"aws:SourceAccount\":\"000000000000\"}}"));
+ accepted(null);
+ policy(allow("\"s3:GetObjectVersion\"", "{\"StringEquals\":{\"aws:SourceAccount\":\"111111111111\"}}"));
+ refused(null, refusal);
+ policy(allow("[\"s3:GetObject\",\"s3:GetObjectVersion\"]", null) + "," + deny("s3:GetObject"));
+ accepted(null);
+ policy(allow("[\"s3:GetObject\",\"s3:GetObjectVersion\"]", null) + "," + deny("s3:GetObjectVersion"));
+ refused(null, refusal);
+ }
+
+ private void put(String in) {
+ s3Service.putObject(in, "app.zip", LambdaWebZips.indexJs(""), "application/zip", Map.of());
+ }
+
+ private void policy(String statements) {
+ s3Service.putBucketPolicy(bucket, "{\"Version\":\"2012-10-17\",\"Statement\":[" + statements + "]}");
+ }
+
+ private String allow(String actions, String condition) {
+ return "{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Action\":" + actions
+ + ",\"Resource\":\"arn:aws:s3:::" + bucket + "/*\"" + (condition == null ? "" : ",\"Condition\":"
+ + condition) + "}";
+ }
+
+ private String deny(String action) {
+ return "{\"Effect\":\"Deny\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Action\":\"" + action
+ + "\",\"Resource\":\"arn:aws:s3:::" + bucket + "/*\"}";
+ }
+
+ private static String build(String in, String versionId) {
+ return """
+ {"codeConfig": {"s3Object": {"bucket": "%s", "key": "app.zip"%s}}, "runtimeConfig": {"runtime": "nodejs24.x"}}
+ """.formatted(in, versionId == null ? "" : ", \"versionId\": \"" + versionId + "\"");
+ }
+
+ private ValidatableResponse createRevision(String versionId) {
+ return given().header("Authorization", ROOT).contentType("application/json")
+ .body("""
+ {"buildConfig": %s, "serviceConfig": {"executionRoleArn": "%s"}}
+ """.formatted(build(bucket, versionId), ROLE))
+ .when().post(BASE + "/web-functions/" + functionName + "/revisions")
+ .then();
+ }
+
+ private void accepted(String versionId) {
+ createRevision(versionId).statusCode(202);
+ }
+
+ private void refused(String versionId, String message) {
+ createRevision(versionId).statusCode(400)
+ .header("X-Amzn-Errortype", "ValidationException")
+ .body("message", equalTo(message));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebCodeBuckets.java b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebCodeBuckets.java
new file mode 100644
index 0000000000..2637d42a9b
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebCodeBuckets.java
@@ -0,0 +1,26 @@
+package io.github.hectorvent.floci.services.lambdaweb;
+
+import io.github.hectorvent.floci.services.s3.S3Service;
+
+/** Code buckets for tests that create Web Function revisions. */
+public final class LambdaWebCodeBuckets {
+
+ private static final String LAMBDA_READ = """
+ {"Version":"2012-10-17","Statement":[{"Effect":"Allow",
+ "Principal":{"Service":"lambda.amazonaws.com"},"Action":["s3:GetObject","s3:GetObjectVersion"],
+ "Resource":"arn:aws:s3:::%s/*"}]}""";
+
+ private LambdaWebCodeBuckets() {
+ }
+
+ /** Creates a versioned bucket that lets Lambda read the code, as AWS requires. */
+ public static void create(S3Service s3Service, String bucket) {
+ s3Service.createBucket(bucket, "us-east-1");
+ s3Service.putBucketVersioning(bucket, "Enabled");
+ allowLambda(s3Service, bucket);
+ }
+
+ public static void allowLambda(S3Service s3Service, String bucket) {
+ s3Service.putBucketPolicy(bucket, LAMBDA_READ.formatted(bucket));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java
index c3c42c2a09..7e870e530c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java
@@ -631,7 +631,7 @@ class LambdaWebDataPlaneDockerIntegrationTest {
String mode, String authType, String nodeOptions) throws IOException {
functionName = "dp-docker-" + Long.toString(System.nanoTime(), 36);
LambdaWebRoles.trustLambda(iamService, ROLE);
- s3Service.createBucket(BUCKET, "us-east-1");
+ LambdaWebCodeBuckets.create(s3Service, BUCKET);
s3Service.putObject(BUCKET, functionName + ".zip", zip(APP), "application/zip", Map.of());
return given().contentType("application/json")
.body("""
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneIntegrationTest.java
index dca520e42d..a4baeb7bea 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneIntegrationTest.java
@@ -383,7 +383,7 @@ class LambdaWebDataPlaneIntegrationTest {
private String createFunction(String region, String functionName, String authType, byte[] code, String role) {
LambdaWebRoles.trustLambda(iamService, role);
- s3Service.createBucket(BUCKET, "us-east-1");
+ LambdaWebCodeBuckets.create(s3Service, BUCKET);
s3Service.putObject(BUCKET, "app.zip", code, "application/zip", Map.of());
return given().header("Authorization", auth("test", region, "lambda")).contentType("application/json")
.body("""
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIamAuthIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIamAuthIntegrationTest.java
index 8b6a6b312e..e84087c06b 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIamAuthIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIamAuthIntegrationTest.java
@@ -70,7 +70,7 @@ class LambdaWebIamAuthIntegrationTest {
void createFunction() throws IOException {
functionName = "iam-auth-" + Long.toString(System.nanoTime(), 36);
LambdaWebRoles.trustLambda(iamService, "arn:aws:iam::000000000000:role/web");
- s3Service.createBucket(BUCKET, "us-east-1");
+ LambdaWebCodeBuckets.create(s3Service, BUCKET);
s3Service.putObject(BUCKET, functionName + ".zip", zip(), "application/zip", Map.of());
int port = baseUri.getPort();
String name = functionName;
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIntegrationTest.java
index 1d4ee09a9b..2120dbf0fc 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIntegrationTest.java
@@ -56,7 +56,7 @@ class LambdaWebIntegrationTest {
@BeforeAll
void seedCode() {
LambdaWebRoles.trustLambda(iamService, ROLE);
- s3Service.createBucket(BUCKET, "us-east-1");
+ LambdaWebCodeBuckets.create(s3Service, BUCKET);
s3Service.putObject(BUCKET, KEY, LambdaWebZips.indexJs(""), "application/zip", Map.of());
}
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.java
index 9c9a49f3f2..01bea1e94f 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
@@ -9,10 +9,12 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
+import io.github.hectorvent.floci.core.common.ServiceCallAuthorizer;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.PersistentStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
+import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
import io.github.hectorvent.floci.services.lambdaweb.model.WebFunction;
import io.github.hectorvent.floci.services.lambdaweb.model.WebFunctionEndpoint;
import io.github.hectorvent.floci.services.lambdaweb.model.WebFunctionRevision;
@@ -40,6 +42,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -53,6 +57,7 @@ class LambdaWebServiceTest {
private static final byte[] NOT_A_ZIP = "this is not a zip\n".getBytes(StandardCharsets.UTF_8);
private final ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
+ private final ServiceCallAuthorizer serviceCalls = mock(ServiceCallAuthorizer.class);
@TempDir
Path storageDir;
@@ -78,7 +83,7 @@ class LambdaWebServiceTest {
EmulatorConfig config = mock(EmulatorConfig.class);
when(config.effectiveBaseUrl()).thenReturn(baseUrl);
LambdaWebService service = new LambdaWebService(new RegionResolver(REGION, ACCOUNT_ID), storage, config,
- s3Service, new LambdaWebCode(storageDir.resolve("code")), mapper, null, executionRole);
+ s3Service, new LambdaWebCode(storageDir.resolve("code")), mapper, null, executionRole, serviceCalls);
service.initializeStorage();
return service;
}
@@ -915,6 +920,55 @@ class LambdaWebServiceTest {
}
}
+ @Test
+ void theNullVersionAsTheLatestIsRefusedBeforeTheSize() {
+ S3Service s3 = mock(S3Service.class);
+ LambdaWebService service = service(new MemoryStorageFactory(), "http://localhost:4566", s3);
+ byte[] zip = LambdaWebZips.indexJs("");
+ when(s3.openObject("code", "app.zip", null)).thenReturn(nullVersion(object(zip, LambdaWebCode.MAX_BYTES + 1)));
+
+ AwsException create = error(() -> service.createFunction(REGION, createRequest("app")));
+ assertEquals("ValidationException", create.getErrorCode());
+ assertEquals(400, create.getHttpStatus());
+ assertEquals("You must enable S3 versioning for your S3 bucket when S3ObjectStorageMode is set to REFERENCE.",
+ create.getMessage());
+ assertEquals("The function arn:aws:lambda:us-east-1:000000000000:web-function/app does not exist.",
+ message(() -> service.getFunction(REGION, "app")));
+
+ ObjectNode pinned = createRequest("app");
+ ((ObjectNode) pinned.path("revisionConfig").path("buildConfig").path("codeConfig").path("s3Object"))
+ .put("versionId", "null");
+ when(s3.openObject("code", "app.zip", "null")).thenReturn(nullVersion(object(zip)));
+ assertNull(service.createFunction(REGION, pinned).revisions.values().iterator().next().failureReason);
+ }
+
+ @Test
+ void lambdaMustBeAllowedToReadTheObjectVersionAfterTheSizeCheck() throws IOException {
+ S3Service s3 = mock(S3Service.class);
+ LambdaWebService service = service(new MemoryStorageFactory(), "http://localhost:4566", s3);
+ when(serviceCalls.authorize(eq("lambda"), eq("s3:GetObjectVersion"), eq("arn:aws:s3:::code/app.zip"),
+ startsWith(ARN + "/revision/rev-"), eq(ACCOUNT_ID))).thenReturn(Decision.DENY);
+ when(s3.openObject("code", "app.zip", null)).thenReturn(object(new byte[0]));
+
+ AwsException create = error(() -> service.createFunction(REGION, createRequest("app")));
+ assertEquals("ValidationException", create.getErrorCode());
+ assertEquals(400, create.getHttpStatus());
+ assertEquals("Lambda service principal lambda.amazonaws.com does not have s3:GetObject and s3:GetObjectVersion"
+ + " permission on s3://code/app.zip. Update the bucket policy to grant the Lambda service principal"
+ + " access and retry.", create.getMessage());
+ assertEquals("The function arn:aws:lambda:us-east-1:000000000000:web-function/app does not exist.",
+ message(() -> service.getFunction(REGION, "app")));
+ Files.createDirectories(storageDir.resolve("code"));
+ try (Stream<Path> copies = Files.walk(storageDir.resolve("code"))) {
+ assertEquals(0, copies.filter(Files::isRegularFile).count());
+ }
+
+ when(s3.openObject("code", "app.zip", null))
+ .thenReturn(object(LambdaWebZips.indexJs(""), LambdaWebCode.MAX_BYTES + 1));
+ assertEquals("Zipped size must be smaller than 536870912 bytes",
+ message(() -> service.createFunction(REGION, createRequest("app"))));
+ }
+
private ObjectNode stateFilter(String state) {
ObjectNode request = mapper.createObjectNode();
request.putArray("filters").addObject().put("name", "state").putArray("values").add(state);
@@ -922,14 +976,22 @@ class LambdaWebServiceTest {
}
private static S3Service.ObjectRead object(byte[] bytes) {
- return new S3Service.ObjectRead(new S3Object("code", "app.zip", bytes, "application/zip"),
- new ByteArrayInputStream(bytes));
+ return versioned(new S3Object("code", "app.zip", bytes, "application/zip"), bytes);
}
/** An object S3 reports as {@code size} bytes, whatever its body. */
private static S3Service.ObjectRead object(byte[] bytes, long size) {
- return new S3Service.ObjectRead(new S3Object("code", "app.zip", size, "application/zip", "etag"),
- new ByteArrayInputStream(bytes));
+ return versioned(new S3Object("code", "app.zip", size, "application/zip", "etag"), bytes);
+ }
+
+ private static S3Service.ObjectRead versioned(S3Object object, byte[] bytes) {
+ object.setVersionId("v1");
+ return new S3Service.ObjectRead(object, new ByteArrayInputStream(bytes));
+ }
+
+ private static S3Service.ObjectRead nullVersion(S3Service.ObjectRead read) {
+ read.object().setVersionId(null);
+ return read;
}
private static final class MemoryStorageFactory extends StorageFactory {