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

Remove URLDecodedKey from S3Object #335

Merged
merged 1 commit into from Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions events/s3.go
Expand Up @@ -3,6 +3,8 @@
package events

import (
"encoding/json"
"net/url"
"time"
)

Expand Down Expand Up @@ -54,6 +56,20 @@ type S3Object struct {
Sequencer string `json:"sequencer"`
}

func (o *S3Object) UnmarshalJSON(data []byte) error {
type rawS3Object S3Object
if err := json.Unmarshal(data, (*rawS3Object)(o)); err != nil {
return err
}
key, err := url.QueryUnescape(o.Key)
if err != nil {
return err
}
o.URLDecodedKey = key

return nil
}

type S3TestEvent struct {
Service string `json:"Service"`
Bucket string `json:"Bucket"`
Expand Down
7 changes: 5 additions & 2 deletions events/s3_test.go
Expand Up @@ -26,8 +26,11 @@ func TestS3EventMarshaling(t *testing.T) {
t.Errorf("could not marshal event. details: %v", err)
}

// 4. check result
assert.JSONEq(t, string(inputJSON), string(outputJSON))
// 4. read expected output JSON from file
exepectedOutputJSON := test.ReadJSONFromFile(t, "./testdata/s3-event-with-decoded.json")

// 5. check result
assert.JSONEq(t, string(exepectedOutputJSON), string(outputJSON))
}

func TestS3TestEventMarshaling(t *testing.T) {
Expand Down
40 changes: 40 additions & 0 deletions events/testdata/s3-event-with-decoded.json
@@ -0,0 +1,40 @@
{
"Records": [
{
"eventVersion": "2.0",
"eventSource": "aws:s3",
"awsRegion": "us-east-1",
"eventTime": "1970-01-01T00:00:00.123Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"responseElements": {
"x-amz-request-id": "C3D13FE58DE4C810",
"x-amz-id-2": "FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD"
},
"s3": {
"s3SchemaVersion": "1.0",
"configurationId": "testConfigRule",
"bucket": {
"name": "sourcebucket",
"ownerIdentity": {
"principalId": "EXAMPLE"
},
"arn": "arn:aws:s3:::mybucket"
},
"object": {
"key": "Happy%20Face.jpg",
"urlDecodedKey": "Happy Face.jpg",
"size": 1024,
"versionId": "version",
"eTag": "d41d8cd98f00b204e9800998ecf8427e",
"sequencer": "Happy Sequencer"
}
}
}
]
}
5 changes: 2 additions & 3 deletions events/testdata/s3-event.json
Expand Up @@ -12,7 +12,7 @@
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"responseElements": {
"responseElements": {
"x-amz-request-id": "C3D13FE58DE4C810",
"x-amz-id-2": "FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD"
},
Expand All @@ -27,9 +27,8 @@
"arn": "arn:aws:s3:::mybucket"
},
"object": {
"key": "HappyFace.jpg",
"key": "Happy%20Face.jpg",
"size": 1024,
"urlDecodedKey": "HappyFace.jpg",
"versionId": "version",
"eTag": "d41d8cd98f00b204e9800998ecf8427e",
"sequencer": "Happy Sequencer"
Expand Down