mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix: Implement S3 Lambda notifications (#278)
* feat: Implement S3 Lambda notifications Add support for S3 event notifications to AWS Lambda functions. - Introduce `LambdaNotification` model to represent Lambda configurations. - Update S3 controller to parse and serialize `CloudFunctionConfiguration` in notification XML. - Implement logic in `S3Service` to extract Lambda ARN details and invoke functions upon matching S3 events. - Add unit and integration tests for Lambda notification configuration persistence and event triggering. * test(s3-notifications): Add S3 bucket notification compatibility tests Introduces `S3NotificationsTest` to verify S3 event notifications for SQS, SNS, and Lambda targets. Includes tests for notification configuration round-trip and Lambda invocation with object filters. Adds `s3NotificationLoggerZip` to `LambdaUtils` for creating a test Lambda function. Co-authored-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
ae49bfb378
commit
04cbeb542f
9 files changed
+585
-10
No files matched your search
@@ -1002,6 +1002,55 @@ class S3IntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(93)
|
||||
void putLambdaNotificationConfigWithFilterIsPersisted() {
|
||||
String xml = """
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<CloudFunctionConfiguration>
|
||||
<Id>lambda-notif</Id>
|
||||
<CloudFunction>arn:aws:lambda:us-east-1:000000000000:function:s3-notif-test</CloudFunction>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>uploads/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>.json</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
</CloudFunctionConfiguration>
|
||||
</NotificationConfiguration>
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.queryParam("notification", "")
|
||||
.body(xml)
|
||||
.when()
|
||||
.put("/notif-test-bucket")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.queryParam("notification", "")
|
||||
.when()
|
||||
.get("/notif-test-bucket")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("CloudFunctionConfiguration"))
|
||||
.body(containsString("arn:aws:lambda:us-east-1:000000000000:function:s3-notif-test"))
|
||||
.body(containsString("s3:ObjectCreated:Put"))
|
||||
.body(containsString("<Name>prefix</Name>"))
|
||||
.body(containsString("<Value>uploads/</Value>"))
|
||||
.body(containsString("<Name>suffix</Name>"))
|
||||
.body(containsString("<Value>.json</Value>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(94)
|
||||
void cleanupNotificationBucket() {
|
||||
given().delete("/notif-test-bucket");
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.services.s3.model.FilterRule;
|
||||
import io.github.hectorvent.floci.services.s3.model.LambdaNotification;
|
||||
import io.github.hectorvent.floci.services.s3.model.QueueNotification;
|
||||
import io.github.hectorvent.floci.services.s3.model.TopicNotification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -22,6 +23,10 @@ class S3NotificationModelTest {
|
||||
var tn = new TopicNotification("id", "arn:aws:sns:us-east-1:000000000000:t",
|
||||
List.of("s3:ObjectCreated:*"), null);
|
||||
assertTrue(tn.matchesKey("anything"));
|
||||
|
||||
var ln = new LambdaNotification("id", "arn:aws:lambda:us-east-1:000000000000:function:test",
|
||||
List.of("s3:ObjectCreated:*"), null);
|
||||
assertTrue(ln.matchesKey("anything"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -37,6 +42,12 @@ class S3NotificationModelTest {
|
||||
assertTrue(qn.matchesKey("images/photo.jpg"));
|
||||
assertFalse(qn.matchesKey("images/photo.png"));
|
||||
assertFalse(qn.matchesKey("docs/photo.jpg"));
|
||||
|
||||
var ln = new LambdaNotification("id", "arn", List.of("s3:ObjectCreated:*"),
|
||||
List.of(new FilterRule("prefix", "images/"), new FilterRule("suffix", ".jpg")));
|
||||
assertTrue(ln.matchesKey("images/photo.jpg"));
|
||||
assertFalse(ln.matchesKey("images/photo.png"));
|
||||
assertFalse(ln.matchesKey("docs/photo.jpg"));
|
||||
}
|
||||
|
||||
// --- FilterRule.matches ---
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.s3.model.FilterRule;
|
||||
import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesResult;
|
||||
import io.github.hectorvent.floci.services.s3.model.LambdaNotification;
|
||||
import io.github.hectorvent.floci.services.s3.model.NotificationConfiguration;
|
||||
import io.github.hectorvent.floci.services.s3.model.ObjectAttributeName;
|
||||
import io.github.hectorvent.floci.services.s3.model.Bucket;
|
||||
import io.github.hectorvent.floci.services.s3.model.S3Object;
|
||||
@@ -375,6 +380,69 @@ class S3ServiceTest {
|
||||
assertArrayEquals("image-data".getBytes(), retrieved.getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObjectTriggersLambdaNotificationWhenKeyMatches() {
|
||||
RecordingLambdaInvoker lambdaInvoker = new RecordingLambdaInvoker();
|
||||
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
|
||||
|
||||
S3Service service = new S3Service(new InMemoryStorage<>(), new InMemoryStorage<>(), tempDir.resolve("notif-s3"),
|
||||
false, lambdaInvoker, regionResolver);
|
||||
service.createBucket("test-bucket", "ap-northeast-1");
|
||||
service.putBucketNotificationConfiguration("test-bucket", lambdaNotificationConfig("uploads/", ".json"));
|
||||
|
||||
service.putObject("test-bucket", "uploads/test.json", "{\"ok\":true}".getBytes(StandardCharsets.UTF_8),
|
||||
"application/json", null);
|
||||
|
||||
assertEquals("ap-northeast-1", lambdaInvoker.region);
|
||||
assertEquals("s3-notif-test", lambdaInvoker.functionName);
|
||||
assertEquals(InvocationType.Event, lambdaInvoker.type);
|
||||
assertNotNull(lambdaInvoker.payload);
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObjectDoesNotTriggerLambdaNotificationWhenKeyDoesNotMatch() {
|
||||
RecordingLambdaInvoker lambdaInvoker = new RecordingLambdaInvoker();
|
||||
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
|
||||
|
||||
S3Service service = new S3Service(new InMemoryStorage<>(), new InMemoryStorage<>(), tempDir.resolve("notif-s3-no-match"),
|
||||
false, lambdaInvoker, regionResolver);
|
||||
service.createBucket("test-bucket", "ap-northeast-1");
|
||||
service.putBucketNotificationConfiguration("test-bucket", lambdaNotificationConfig("uploads/", ".json"));
|
||||
|
||||
service.putObject("test-bucket", "incoming/test.txt", "ignored".getBytes(StandardCharsets.UTF_8),
|
||||
"text/plain", null);
|
||||
|
||||
assertNull(lambdaInvoker.functionName);
|
||||
}
|
||||
|
||||
private static NotificationConfiguration lambdaNotificationConfig(String prefix, String suffix) {
|
||||
NotificationConfiguration config = new NotificationConfiguration();
|
||||
config.getLambdaFunctionConfigurations().add(new LambdaNotification(
|
||||
"lambda-notif",
|
||||
"arn:aws:lambda:ap-northeast-1:000000000000:function:s3-notif-test",
|
||||
List.of("s3:ObjectCreated:Put"),
|
||||
List.of(
|
||||
new FilterRule("prefix", prefix),
|
||||
new FilterRule("suffix", suffix)
|
||||
)));
|
||||
return config;
|
||||
}
|
||||
|
||||
private static final class RecordingLambdaInvoker implements S3Service.LambdaInvoker {
|
||||
private String region;
|
||||
private String functionName;
|
||||
private byte[] payload;
|
||||
private InvocationType type;
|
||||
|
||||
@Override
|
||||
public void invoke(String region, String functionName, byte[] payload, InvocationType type) {
|
||||
this.region = region;
|
||||
this.functionName = functionName;
|
||||
this.payload = payload;
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listObjectsWithStartAfterFiltersResults() {
|
||||
s3Service.createBucket("test-bucket", "us-east-1");
|
||||
|
||||
Reference in new issue
Block a user