mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* s3: add the bucket notification target registry Notification destinations are defined by the operator in /etc/s3/notification/targets.json and referred to by name from bucket configurations, so a bucket owner cannot make the server connect anywhere new. The registry parses and validates that file: unique names that fit in an ARN segment and an MQ topic name, known types (topic and webhook now, kafka, aws_sqs, google_pub_sub and pubsub accepted for later), the fields each type needs, and no unknown keys. A target serves only the buckets its allowed_buckets globs match, and none when the list is empty. Parse errors never carry credential values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: hot-reload the notification target registry Every gateway reads /etc/s3/notification/targets.json at startup and reloads it from the /etc metadata subscription it already runs for the IAM and circuit breaker files. A write reloads it, a delete or rename away empties it, and a file that does not parse leaves the previous registry in place so a bad edit does not cut every bucket off its destinations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: parse and validate bucket notification configurations The full PutBucketNotificationConfiguration document is parsed: topic, queue and cloud function configurations with their Id, destination ARN, events and key filter. Event names are checked against the AWS list, wildcards and names for things that never happen here included, plus the multipart and retention events this server emits. A filter takes at most one prefix and one suffix rule, a document at most 100 configurations, and overlapping rules are allowed. EventBridge stays NotImplemented. A destination resolves by the last segment of its ARN to a target, which must exist, be enabled and allow the bucket; the error does not say which of those failed. A rule without an Id gets one derived from its content, so every gateway reports the same configurationId for it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: match an event against a bucket's notification rules Configuration.Match takes an event name such as s3:ObjectCreated:Put and an object key and returns each rule that covers them, with its configurationId and target, in configuration order. Category wildcards cover every event in the category, prefix and suffix are case-sensitive and checked independently, and an event that several rules cover matches each of them, as AWS delivers one record per rule. Publishing calls this; it is a pure function of the stored configuration. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: store bucket notification configurations PutBucketNotificationConfiguration used to accept only an empty document and answer NotImplemented for any destination. It now parses the full document, checks every destination against the target registry for this bucket, and stores the XML exactly as sent in one Extended key on the bucket entry through updateBucketConfig, which patches that key alone on the bucket's owner filer. An empty document removes the key. GET returns the stored bytes unchanged, so tools comparing ARNs see no diff, and the bucket config cache carries the parsed rules for publishing to match. x-amz-skip-destination-validation is parsed and must be true or false. It only skips the test event, which publishing will send; the registry check always runs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3notification: reject content after the configuration's root element The stored document is served back unchanged by GET, so anything after the root other than whitespace, comments or processing instructions is refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3notification: reject unknown elements inside rules and filters A misspelled Filter, S3Key, FilterRule or Value was dropped by decoding and left an unfiltered rule; it is now MalformedXML like an unknown root child. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3notification: URL-decode prefix and suffix filter values AWS has special characters in filter values percent-encoded and a space written as +, so reports+2026/ selects reports 2026/a. A value that is not valid encoding is still matched as written. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: refuse a notification targets file stored in chunks Only inline content is read, so a targets.json kept in chunks (written by a mount, for one) read as an empty registry and dropped every target. It is now refused with a warning and the loaded registry stays. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3notification: refuse overlapping rules as AWS does AWS answers InvalidArgument ("Configuration is ambiguously defined") when two rules share an event type and both their prefixes and suffixes overlap, so an event for a key matches at most one rule. Such configurations were accepted and matched every rule. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
5.1 KiB
Go
131 lines
5.1 KiB
Go
package s3notification
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestEventMatches(t *testing.T) {
|
|
cases := []struct {
|
|
configured, event string
|
|
want bool
|
|
}{
|
|
{"s3:ObjectCreated:*", "s3:ObjectCreated:Put", true},
|
|
{"s3:ObjectCreated:*", "s3:ObjectCreated:CompleteMultipartUpload", true},
|
|
{"s3:ObjectCreated:*", "s3:ObjectRemoved:Delete", false},
|
|
{"s3:ObjectCreated:*", "s3:ObjectCreated:", false},
|
|
{"s3:ObjectCreated:*", "s3:ObjectCreatedX:Put", false},
|
|
{"s3:ObjectRemoved:*", "s3:ObjectRemoved:DeleteMarkerCreated", true},
|
|
{"s3:LifecycleExpiration:*", "s3:LifecycleExpiration:Delete", true},
|
|
{"s3:MultipartUpload:*", "s3:MultipartUpload:PartUploaded", true},
|
|
{"s3:ObjectCreated:Put", "s3:ObjectCreated:Put", true},
|
|
{"s3:ObjectCreated:Put", "s3:ObjectCreated:Post", false},
|
|
{"s3:ObjectCreated:Put", "ObjectCreated:Put", false},
|
|
{"s3:ObjectAcl:Put", "s3:ObjectAcl:Put", true},
|
|
{"s3:ObjectTagging:*", "s3:ObjectTagging:Delete", true},
|
|
}
|
|
for _, tc := range cases {
|
|
require.Equal(t, tc.want, eventMatches(tc.configured, tc.event), "%s vs %s", tc.configured, tc.event)
|
|
}
|
|
}
|
|
|
|
func TestMatch(t *testing.T) {
|
|
doc := `<NotificationConfiguration>
|
|
<TopicConfiguration>
|
|
<Id>jpg-created</Id>
|
|
<Topic>arn:aws:sns:::images</Topic>
|
|
<Event>s3:ObjectCreated:*</Event>
|
|
<Filter><S3Key>
|
|
<FilterRule><Name>prefix</Name><Value>photos/</Value></FilterRule>
|
|
<FilterRule><Name>suffix</Name><Value>.jpg</Value></FilterRule>
|
|
</S3Key></Filter>
|
|
</TopicConfiguration>
|
|
<QueueConfiguration>
|
|
<Id>all-removals</Id>
|
|
<Queue>arn:aws:sqs:::audit</Queue>
|
|
<Event>s3:ObjectRemoved:*</Event>
|
|
</QueueConfiguration>
|
|
<QueueConfiguration>
|
|
<Id>logs</Id>
|
|
<Queue>arn:aws:sqs:::audit</Queue>
|
|
<Event>s3:ObjectCreated:*</Event>
|
|
<Filter><S3Key><FilterRule><Name>Prefix</Name><Value>logs/</Value></FilterRule></S3Key></Filter>
|
|
</QueueConfiguration>
|
|
<CloudFunctionConfiguration>
|
|
<Id>csv</Id>
|
|
<CloudFunction>arn:aws:lambda:us-east-1:1:function:etl</CloudFunction>
|
|
<Event>s3:ObjectCreated:CompleteMultipartUpload</Event>
|
|
<Filter><S3Key>
|
|
<FilterRule><Name>prefix</Name><Value>exports/</Value></FilterRule>
|
|
<FilterRule><Name>suffix</Name><Value>.csv</Value></FilterRule>
|
|
</S3Key></Filter>
|
|
</CloudFunctionConfiguration>
|
|
</NotificationConfiguration>`
|
|
config, err := ParseConfiguration([]byte(doc))
|
|
require.NoError(t, err)
|
|
|
|
cases := []struct {
|
|
name, event, key string
|
|
want []Match
|
|
}{
|
|
{"prefix and suffix", "s3:ObjectCreated:Put", "photos/a.jpg",
|
|
[]Match{{"jpg-created", "images"}}},
|
|
{"wildcard covers copy", "s3:ObjectCreated:Copy", "photos/a.jpg",
|
|
[]Match{{"jpg-created", "images"}}},
|
|
{"suffix misses", "s3:ObjectCreated:Copy", "photos/a.png", nil},
|
|
{"prefix misses", "s3:ObjectCreated:Copy", "other/photos/a.jpg", nil},
|
|
{"prefix is case sensitive", "s3:ObjectCreated:Copy", "Photos/a.jpg", nil},
|
|
{"removal", "s3:ObjectRemoved:DeleteMarkerCreated", "photos/a.jpg",
|
|
[]Match{{"all-removals", "audit"}}},
|
|
{"second rule to one target", "s3:ObjectCreated:Put", "logs/app.log",
|
|
[]Match{{"logs", "audit"}}},
|
|
{"multipart csv", "s3:ObjectCreated:CompleteMultipartUpload", "exports/x.csv",
|
|
[]Match{{"csv", "etl"}}},
|
|
{"csv put is not multipart", "s3:ObjectCreated:Put", "exports/x.csv", nil},
|
|
{"prefix equals key", "s3:ObjectCreated:Post", "logs/",
|
|
[]Match{{"logs", "audit"}}},
|
|
{"no rule for event", "s3:ObjectTagging:Put", "photos/a.jpg", nil},
|
|
{"unknown event", "s3:Nope", "photos/a.jpg", nil},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
require.Equal(t, tc.want, config.Match(tc.event, tc.key))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMatchEdgeCases(t *testing.T) {
|
|
var none *Configuration
|
|
require.Nil(t, none.Match("s3:ObjectCreated:Put", "k"))
|
|
|
|
empty, err := ParseConfiguration([]byte(`<NotificationConfiguration/>`))
|
|
require.NoError(t, err)
|
|
require.Nil(t, empty.Match("s3:ObjectCreated:Put", "k"))
|
|
|
|
// Prefix and suffix are checked independently, so they may overlap in a short key.
|
|
overlap, err := ParseConfiguration([]byte(filterDoc(filterRule("prefix", "ab"), filterRule("suffix", "bc"))))
|
|
require.NoError(t, err)
|
|
require.Len(t, overlap.Match("s3:ObjectCreated:Put", "abc"), 1)
|
|
require.Empty(t, overlap.Match("s3:ObjectCreated:Put", "ab"))
|
|
|
|
// A rule without an Id still reports one.
|
|
unnamed, err := ParseConfiguration([]byte(queueDoc(`<Queue>arn:aws:sqs:::t</Queue><Event>s3:ObjectCreated:*</Event>`)))
|
|
require.NoError(t, err)
|
|
matches := unnamed.Match("s3:ObjectCreated:Put", "k")
|
|
require.Len(t, matches, 1)
|
|
require.NotEmpty(t, matches[0].ID)
|
|
require.Equal(t, "t", matches[0].Target)
|
|
}
|
|
|
|
func TestMatchDecodesFilterValues(t *testing.T) {
|
|
config, err := ParseConfiguration([]byte(filterDoc(filterRule("prefix", "reports+2026%2Fq1/"), filterRule("suffix", "%2B.csv"))))
|
|
require.NoError(t, err)
|
|
require.Len(t, config.Match("s3:ObjectCreated:Put", "reports 2026/q1/a+.csv"), 1)
|
|
require.Empty(t, config.Match("s3:ObjectCreated:Put", "reports+2026%2Fq1/a+.csv"))
|
|
|
|
literal, err := ParseConfiguration([]byte(filterDoc(filterRule("prefix", "100%/"))))
|
|
require.NoError(t, err)
|
|
require.Len(t, literal.Match("s3:ObjectCreated:Put", "100%/a"), 1)
|
|
}
|