-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.go
More file actions
201 lines (184 loc) · 5.36 KB
/
Copy pathscanner.go
File metadata and controls
201 lines (184 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package audit
import (
"context"
"errors"
"fmt"
"strings"
ecrtypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
smithy "github.com/aws/smithy-go"
"github.com/loilo-inc/canarycage/v6/awsiface"
)
type scanner struct {
ecs awsiface.EcsClient
ecr awsiface.EcrClient
}
type Scanner interface {
Scan(ctx context.Context, cluster string, service string) ([]ScanResult, error)
}
var _ Scanner = (*scanner)(nil)
func NewScanner(ecs awsiface.EcsClient, ecr awsiface.EcrClient) Scanner {
return &scanner{ecs: ecs, ecr: ecr}
}
func (s *scanner) Scan(
ctx context.Context,
cluster string,
service string,
) (results []ScanResult, err error) {
ecsTool := newEcsTool(s.ecs)
ecrTool := newEcrTool(s.ecr)
var imageInfos []ImageInfo
if imageInfos, err = ecsTool.GetServiceImageInfos(ctx, cluster, service); err != nil {
return nil, err
}
findingsList := make([]ScanResult, len(imageInfos))
for i, info := range imageInfos {
if info.IsECRImage() {
findingsList[i] = scanImage(ctx, ecrTool, info)
} else {
findingsList[i] = ScanResult{ImageInfo: info, Err: ErrNonEcrImage}
}
}
return findingsList, nil
}
var ErrNonEcrImage = fmt.Errorf("non-ECR image")
var ErrScanNotFound = fmt.Errorf("scan not found")
func scanImage(ctx context.Context, ecrTool EcrTool, info ImageInfo) ScanResult {
if imageID, err := ecrTool.GetActualImageIdentifier(ctx, &info); err != nil {
return ScanResult{ImageInfo: info, Err: err}
} else if findings, err := ecrTool.GetImageScanFindings(ctx, &info, imageID); err != nil {
return ScanResult{ImageInfo: info, Err: parseError(err)}
} else {
cves := scanFindingsToCVEs(findings)
return ScanResult{ImageInfo: info, Cves: cves}
}
}
func parseError(err error) error {
var awserr smithy.APIError
if errors.As(err, &awserr) && awserr.ErrorCode() == "ScanNotFoundException" {
return ErrScanNotFound
}
return err
}
func scanFindingsToCVEs(findings *ecrtypes.ImageScanFindings) []CVE {
var cves []CVE
if len(findings.Findings) > 0 {
for _, f := range findings.Findings {
cves = append(cves, findingToCVE(f))
}
} else if len(findings.EnhancedFindings) > 0 {
for _, f := range findings.EnhancedFindings {
cves = append(cves, enhancedFindingToCVE(f))
}
}
return cves
}
func findingToCVE(finding ecrtypes.ImageScanFinding) CVE {
cve := CVE{
Name: "unknown",
PackageName: "unknown",
PackageVersion: "unknown",
Severity: finding.Severity,
}
if finding.Name != nil {
cve.Name = *finding.Name
}
attrs := unwrapAttributes(finding.Attributes)
if val, ok := attrs["package_name"]; ok {
cve.PackageName = val
}
if val, ok := attrs["package_version"]; ok {
cve.PackageVersion = val
}
if finding.Uri != nil {
cve.Uri = *finding.Uri
}
if finding.Description != nil {
cve.Description = *finding.Description
}
return cve
}
func enhancedFindingToCVE(finding ecrtypes.EnhancedImageScanFinding) CVE {
analysis := &EnhancedAnalysis{Score: finding.Score}
cve := CVE{
Name: "unknown",
PackageName: "unknown",
PackageVersion: "unknown",
Severity: ecrtypes.FindingSeverityUndefined,
EnhancedAnalysis: analysis,
}
if finding.Description != nil {
cve.Description = *finding.Description
}
if finding.Severity != nil {
cve.Severity = ecrtypes.FindingSeverity(*finding.Severity)
}
if finding.Status != nil {
analysis.Status = *finding.Status
}
if finding.ExploitAvailable != nil {
analysis.ExploitAvailable = *finding.ExploitAvailable
}
if finding.FixAvailable != nil {
analysis.FixAvailable = *finding.FixAvailable
}
if finding.PackageVulnerabilityDetails == nil {
return cve
}
details := finding.PackageVulnerabilityDetails
if details.VulnerabilityId != nil {
cve.Name = *details.VulnerabilityId
}
if cve.Severity == ecrtypes.FindingSeverityUndefined && details.VendorSeverity != nil {
cve.Severity = ecrtypes.FindingSeverity(*details.VendorSeverity)
}
if details.SourceUrl != nil {
cve.Uri = *details.SourceUrl
} else if len(details.ReferenceUrls) > 0 {
cve.Uri = details.ReferenceUrls[0]
}
cve.PackageName, cve.PackageVersion = vulnerablePackagesToNameVersion(details.VulnerablePackages)
analysis.FixedInVersion = strings.Join(uniquePackageValues(details.VulnerablePackages, func(pkg ecrtypes.VulnerablePackage) *string {
return pkg.FixedInVersion
}), ", ")
return cve
}
func unwrapAttributes(attrs []ecrtypes.Attribute) map[string]string {
m := make(map[string]string)
for _, attr := range attrs {
if attr.Key != nil && attr.Value != nil {
m[*attr.Key] = *attr.Value
}
}
return m
}
func vulnerablePackagesToNameVersion(packages []ecrtypes.VulnerablePackage) (string, string) {
names := uniquePackageValues(packages, func(pkg ecrtypes.VulnerablePackage) *string {
return pkg.Name
})
versions := uniquePackageValues(packages, func(pkg ecrtypes.VulnerablePackage) *string {
return pkg.Version
})
return joinOrUnknown(names), joinOrUnknown(versions)
}
func uniquePackageValues(packages []ecrtypes.VulnerablePackage, value func(ecrtypes.VulnerablePackage) *string) []string {
seen := make(map[string]struct{})
var values []string
for _, pkg := range packages {
v := value(pkg)
if v == nil || *v == "" {
continue
}
if _, ok := seen[*v]; ok {
continue
}
seen[*v] = struct{}{}
values = append(values, *v)
}
return values
}
func joinOrUnknown(values []string) string {
if len(values) == 0 {
return "unknown"
}
return strings.Join(values, ", ")
}