Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support in aws-s3 input for s3 notification from SNS to SQS #28800

Merged
merged 6 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions x-pack/filebeat/input/awss3/sqs_s3_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ func nonRetryableErrorWrap(err error) error {
// s3EventsV2 is the notification message that Amazon S3 sends to notify of S3 changes.
// This was derived from the version 2.2 schema.
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/notification-content-structure.html
// If the notification message is sent from SNS to SQS, then Records will be
// replaced by TopicArn and Message fields.
type s3EventsV2 struct {
Records []s3EventV2 `json:"Records"`
TopicArn string `json:"TopicArn"`
Message string `json:"Message"`
Records []s3EventV2 `json:"Records"`
}

// s3EventV2 is a S3 change notification event.
Expand Down Expand Up @@ -189,6 +193,18 @@ func (p *sqsS3EventProcessor) getS3Notifications(body string) ([]s3EventV2, erro
return nil, fmt.Errorf("failed to decode SQS message body as an S3 notification: %w", err)
}

// Check if the notification is from S3 -> SNS -> SQS
if events.TopicArn != "" {
dec := json.NewDecoder(strings.NewReader(events.Message))
if err := dec.Decode(&events); err != nil {
p.log.Debugw("Invalid SQS message body.", "sqs_message_body", body)
return nil, fmt.Errorf("failed to decode SQS message body as an S3 notification: %w", err)
}
}
return p.getS3Info(events)
}

func (p *sqsS3EventProcessor) getS3Info(events s3EventsV2) ([]s3EventV2, error) {
var out []s3EventV2
for _, record := range events.Records {
if !p.isObjectCreatedEvents(record) {
Expand All @@ -211,7 +227,6 @@ func (p *sqsS3EventProcessor) getS3Notifications(body string) ([]s3EventV2, erro

out = append(out, record)
}

return out, nil
}

Expand Down
10 changes: 10 additions & 0 deletions x-pack/filebeat/input/awss3/sqs_s3_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ func TestSqsProcessor_getS3Notifications(t *testing.T) {
require.NoError(t, err)
assert.Len(t, events, 0)
})

t.Run("sns-sqs notification", func(t *testing.T) {
msg := newSNSSQSMessage()
events, err := p.getS3Notifications(*msg.Body)
require.NoError(t, err)
assert.Len(t, events, 1)
assert.Equal(t, "test-object-key", events[0].S3.Object.Key)
assert.Equal(t, "arn:aws:s3:::vpc-flow-logs-ks", events[0].S3.Bucket.ARN)
assert.Equal(t, "vpc-flow-logs-ks", events[0].S3.Bucket.Name)
})
}

func TestNonRecoverableError(t *testing.T) {
Expand Down
22 changes: 22 additions & 0 deletions x-pack/filebeat/input/awss3/sqs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ func newSQSMessage(events ...s3EventV2) sqs.Message {
}
}

func newSNSSQSMessage() sqs.Message {
body, err := json.Marshal(s3EventsV2{
TopicArn: "arn:aws:sns:us-east-1:1234:sns-topic",
Message: "{\"Records\":[{\"eventSource\":\"aws:s3\",\"awsRegion\":\"us-east-1\",\"eventName\":\"ObjectCreated:Put\",\"s3\":{\"configurationId\":\"sns-notification-vpc-flow-logs\",\"bucket\":{\"name\":\"vpc-flow-logs-ks\",\"arn\":\"arn:aws:s3:::vpc-flow-logs-ks\"},\"object\":{\"key\":\"test-object-key\"}}}]}",
})
if err != nil {
panic(err)
}

hash := sha256.Sum256(body)
id, _ := uuid.FromBytes(hash[:16])
messageID := id.String()
receipt := "receipt-" + messageID
bodyStr := string(body)

return sqs.Message{
Body: &bodyStr,
MessageId: &messageID,
ReceiptHandle: &receipt,
}
}

func newS3Event(key string) s3EventV2 {
record := s3EventV2{
AWSRegion: "us-east-1",
Expand Down