Skip to content

Commit 99b35f2

Browse files
authored
Adding ECR Image Scan Event (#378)
* Add ECR Scan Event Type * Add ECR Scan Event Type Unit Test * Consistency for type names Co-authored-by: Alvin Siew <[email protected]>
1 parent cd5ccc1 commit 99b35f2

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

events/ecr_scan.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package events
2+
3+
type ECRScanEvent struct {
4+
Version string `json:"version"`
5+
ID string `json:"id"`
6+
DetailType string `json:"detail-type"`
7+
Source string `json:"source"`
8+
Time string `json:"time"`
9+
Region string `json:"region"`
10+
Resources []string `json:"resources"`
11+
Account string `json:"account"`
12+
Detail ECRScanEventDetailType `json:"detail"`
13+
}
14+
15+
type ECRScanEventDetailType struct {
16+
ScanStatus string `json:"scan-status"`
17+
RepositoryName string `json:"repository-name"`
18+
FindingSeverityCounts ECRScanEventFindingSeverityCounts `json:"finding-severity-counts"`
19+
ImageDigest string `json:"image-digest"`
20+
ImageTags []string `json:"image-tags"`
21+
}
22+
23+
type ECRScanEventFindingSeverityCounts struct {
24+
Critical int64 `json:"CRITICAL"`
25+
High int64 `json:"HIGH"`
26+
Medium int64 `json:"MEDIUM"`
27+
Low int64 `json:"LOW"`
28+
Informational int64 `json:"INFORMATIONAL"`
29+
Undefined int64 `json:"UNDEFINED"`
30+
}

events/ecr_scan_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
package events
3+
4+
import (
5+
"encoding/json"
6+
"testing"
7+
8+
"github.com/aws/aws-lambda-go/events/test"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestECRScanEventMarshaling(t *testing.T) {
13+
// 1. read JSON from file
14+
inputJson := test.ReadJSONFromFile(t, "./testdata/ecr-image-scan-event.json")
15+
16+
// 2. de-serialize into Go object
17+
var inputEvent ECRScanEvent
18+
if err := json.Unmarshal(inputJson, &inputEvent); err != nil {
19+
t.Errorf("could not unmarshal event. details: %v", err)
20+
}
21+
22+
// 3. Verify values populated into Go Object, at least one validation per data type
23+
assert.Equal(t, "0", inputEvent.Version)
24+
assert.Equal(t, "01234567-0123-0123-0123-012345678901", inputEvent.ID)
25+
assert.Equal(t, "ECR Image Scan", inputEvent.DetailType)
26+
assert.Equal(t, "aws.ecr", inputEvent.Source)
27+
assert.Equal(t, "123456789012", inputEvent.Account)
28+
assert.Equal(t, "2019-10-30T21:32:27Z", inputEvent.Time)
29+
assert.Equal(t, "eu-north-1", inputEvent.Region)
30+
assert.Equal(t, "arn:aws:ecr:eu-north-1:123456789012:repository/tribble-image-scan-test", inputEvent.Resources[0])
31+
32+
var detail = inputEvent.Detail
33+
assert.Equal(t, "COMPLETE", detail.ScanStatus)
34+
assert.Equal(t, "tribble-image-scan-test", detail.RepositoryName)
35+
assert.Equal(t, "sha256:d4a96ee9443e641fc100e763a0c10928720b50c6e3ea3342d05d7c3435fc5355", detail.ImageDigest)
36+
assert.Equal(t, "1572471135", detail.ImageTags[0])
37+
assert.Equal(t, int64(10), detail.FindingSeverityCounts.Critical)
38+
assert.Equal(t, int64(2), detail.FindingSeverityCounts.High)
39+
assert.Equal(t, int64(9), detail.FindingSeverityCounts.Medium)
40+
assert.Equal(t, int64(3), detail.FindingSeverityCounts.Low)
41+
assert.Equal(t, int64(0), detail.FindingSeverityCounts.Informational)
42+
assert.Equal(t, int64(0), detail.FindingSeverityCounts.Undefined)
43+
44+
// 4. serialize to JSON
45+
outputJson, err := json.Marshal(inputEvent)
46+
if err != nil {
47+
t.Errorf("could not marshal event. details: %v", err)
48+
}
49+
50+
// 5. check result
51+
assert.JSONEq(t, string(inputJson), string(outputJson))
52+
}
53+
54+
func TestECRScanMarshalingMalformedJson(t *testing.T) {
55+
test.TestMalformedJson(t, ECRScanEvent{})
56+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": "0",
3+
"id": "01234567-0123-0123-0123-012345678901",
4+
"detail-type": "ECR Image Scan",
5+
"source": "aws.ecr",
6+
"account": "123456789012",
7+
"time": "2019-10-30T21:32:27Z",
8+
"region": "eu-north-1",
9+
"resources": ["arn:aws:ecr:eu-north-1:123456789012:repository/tribble-image-scan-test"],
10+
"detail": {
11+
"scan-status": "COMPLETE",
12+
"repository-name": "tribble-image-scan-test",
13+
"finding-severity-counts": {
14+
"CRITICAL": 10,
15+
"HIGH": 2,
16+
"MEDIUM": 9,
17+
"LOW": 3,
18+
"INFORMATIONAL": 0,
19+
"UNDEFINED": 0
20+
},
21+
"image-digest": "sha256:d4a96ee9443e641fc100e763a0c10928720b50c6e3ea3342d05d7c3435fc5355",
22+
"image-tags": ["1572471135"]
23+
}
24+
}

0 commit comments

Comments
 (0)