This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
scanner.go
144 lines (130 loc) · 3.47 KB
/
scanner.go
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
package cli
import (
"encoding/json"
"fmt"
"os/exec"
"github.com/aquasecurity/starboard-security-operator/pkg/aqua/client"
"github.com/aquasecurity/starboard/pkg/apis/aquasecurity/v1alpha1"
"github.com/google/go-containerregistry/pkg/name"
)
type Scanner struct {
baseURL string
credentials client.UsernameAndPassword
}
func NewScanner(baseURL string, credentials client.UsernameAndPassword) *Scanner {
return &Scanner{
baseURL: baseURL,
credentials: credentials,
}
}
func (s *Scanner) Scan(imageRef string) (report v1alpha1.VulnerabilityReport, err error) {
args := []string{
"scan",
"--checkonly",
"--dockerless",
fmt.Sprintf("--host=%s", s.baseURL),
fmt.Sprintf("--user=%s", s.credentials.Username),
fmt.Sprintf("--password=%s", s.credentials.Password),
"--local",
imageRef,
}
command := exec.Command("scannercli", args...)
out, err := command.Output()
if err != nil {
err = fmt.Errorf("running scannercli: %w", err)
return
}
var aquaReport ScanReport
err = json.Unmarshal(out, &aquaReport)
if err != nil {
return
}
return s.convert(imageRef, aquaReport)
}
func (s *Scanner) convert(imageRef string, aquaReport ScanReport) (report v1alpha1.VulnerabilityReport, err error) {
items := make([]v1alpha1.VulnerabilityItem, 0)
for _, resourceScan := range aquaReport.Resources {
for _, vln := range resourceScan.Vulnerabilities {
var pkg string
switch resourceScan.Resource.Type {
case Library:
pkg = resourceScan.Resource.Path
case Package:
pkg = resourceScan.Resource.Name
default:
pkg = resourceScan.Resource.Name
}
items = append(items, v1alpha1.VulnerabilityItem{
VulnerabilityID: vln.Name,
Resource: pkg,
InstalledVersion: resourceScan.Resource.Version,
FixedVersion: vln.FixVersion,
Severity: s.toSeverity(vln),
Description: vln.Description,
Links: s.toLinks(vln),
})
}
}
ref, err := name.ParseReference(imageRef)
if err != nil {
return
}
artifact := v1alpha1.Artifact{
Repository: ref.Context().RepositoryStr(),
}
switch t := ref.(type) {
case name.Tag:
artifact.Tag = t.TagStr()
case name.Digest:
artifact.Digest = t.DigestStr()
}
report = v1alpha1.VulnerabilityReport{
Scanner: v1alpha1.Scanner{
Name: "Aqua CSP",
Vendor: "Aqua Security",
//Version: c.config.Version,
},
Registry: v1alpha1.Registry{
URL: ref.Context().RegistryStr(),
},
Artifact: artifact,
Summary: s.toSummary(aquaReport.Summary),
Vulnerabilities: items,
}
return
}
func (s *Scanner) toSeverity(v Vulnerability) v1alpha1.Severity {
switch severity := v.AquaSeverity; severity {
case "critical":
return v1alpha1.SeverityCritical
case "high":
return v1alpha1.SeverityHigh
case "medium":
return v1alpha1.SeverityMedium
case "low":
return v1alpha1.SeverityLow
case "negligible":
// TODO We should have severity None defined in k8s-security-crds
return v1alpha1.SeverityUnknown
default:
return v1alpha1.SeverityUnknown
}
}
func (s *Scanner) toLinks(v Vulnerability) []string {
var links []string
if v.NVDURL != "" {
links = append(links, v.NVDURL)
}
if v.VendorURL != "" {
links = append(links, v.VendorURL)
}
return links
}
func (s *Scanner) toSummary(aquaSummary VulnerabilitySummary) v1alpha1.VulnerabilitySummary {
return v1alpha1.VulnerabilitySummary{
CriticalCount: aquaSummary.Critical,
HighCount: aquaSummary.High,
MediumCount: aquaSummary.Medium,
LowCount: aquaSummary.Low,
}
}