-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathplugin.go
263 lines (232 loc) · 9.03 KB
/
plugin.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package trivy
import (
"encoding/json"
"io"
"path/filepath"
"github.com/aquasecurity/trivy-operator/pkg/exposedsecretreport"
"github.com/aquasecurity/trivy-operator/pkg/sbomreport"
"github.com/aquasecurity/trivy-operator/pkg/utils"
containerimage "github.com/google/go-containerregistry/pkg/name"
"github.com/aquasecurity/trivy-operator/pkg/apis/aquasecurity/v1alpha1"
"github.com/aquasecurity/trivy-operator/pkg/configauditreport"
"github.com/aquasecurity/trivy-operator/pkg/docker"
"github.com/aquasecurity/trivy-operator/pkg/ext"
"github.com/aquasecurity/trivy-operator/pkg/kube"
"github.com/aquasecurity/trivy-operator/pkg/trivyoperator"
"github.com/aquasecurity/trivy-operator/pkg/vulnerabilityreport"
ty "github.com/aquasecurity/trivy/pkg/types"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// Plugin the name of this plugin.
Plugin = "Trivy"
)
const (
SupportedConfigAuditKinds = "Workload,Service,Role,ClusterRole,NetworkPolicy,Ingress,LimitRange,ResourceQuota"
)
const (
DefaultImageRepository = "mirror.gcr.io/aquasec/trivy"
DefaultDBRepository = "mirror.gcr.io/aquasec/trivy-db"
DefaultJavaDBRepository = "mirror.gcr.io/aquasec/trivy-java-db"
DefaultSeverity = "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL"
)
type plugin struct {
clock ext.Clock
idGenerator ext.IDGenerator
objectResolver *kube.ObjectResolver
}
// NewPlugin constructs a new vulnerabilityreport.Plugin, which is using an
// upstream Trivy container image to scan Kubernetes workloads.
//
// The plugin supports Image and Filesystem commands. The Filesystem command may
// be used to scan workload images cached on cluster nodes by scheduling
// scan jobs on a particular node.
//
// The Image command supports both Standalone and ClientServer modes depending
// on the settings returned by Config.GetMode. The ClientServer mode is usually
// more performant, however it requires a Trivy server accessible at the
// configurable Config.GetServerURL.
func NewPlugin(clock ext.Clock, idGenerator ext.IDGenerator, objectResolver *kube.ObjectResolver) vulnerabilityreport.Plugin {
plugin := &plugin{
clock: clock,
idGenerator: idGenerator,
objectResolver: objectResolver,
}
return plugin
}
// NewTrivyConfigAuditPlugin constructs a new configAudit.Plugin, which is using an
// upstream Trivy config audit scanner lib.
func NewTrivyConfigAuditPlugin(clock ext.Clock, idGenerator ext.IDGenerator, objectResolver *kube.ObjectResolver) configauditreport.PluginInMemory {
return &plugin{
clock: clock,
idGenerator: idGenerator,
objectResolver: objectResolver,
}
}
// Init ensures the default Config required by this plugin.
func (p *plugin) Init(ctx trivyoperator.PluginContext) error {
return ctx.EnsureConfig(trivyoperator.PluginConfig{
Data: map[string]string{
keyTrivyImageRepository: DefaultImageRepository,
keyTrivyImageTag: "0.52.2",
KeyTrivySeverity: DefaultSeverity,
keyTrivySlow: "true",
keyTrivyMode: string(Standalone),
keyTrivyTimeout: "5m0s",
keyTrivyDBRepository: DefaultDBRepository,
keyTrivyJavaDBRepository: DefaultJavaDBRepository,
keyTrivyUseBuiltinRegoPolicies: "true",
keyTrivySupportedConfigAuditKinds: SupportedConfigAuditKinds,
keyResourcesRequestsCPU: "100m",
keyResourcesRequestsMemory: "100M",
keyResourcesLimitsCPU: "500m",
keyResourcesLimitsMemory: "500M",
},
})
}
func (p *plugin) GetScanJobSpec(ctx trivyoperator.PluginContext, workload client.Object, credentials map[string]docker.Auth, securityContext *corev1.SecurityContext, sbomClusterReport map[string]v1alpha1.SbomReportData) (corev1.PodSpec, []*corev1.Secret, error) {
config, err := getConfig(ctx)
if err != nil {
return corev1.PodSpec{}, nil, err
}
var podSpec corev1.PodSpec
var secrets []*corev1.Secret
podSpec, secrets, err = NewPodSpecMgr(config).GetPodSpec(ctx, config, workload, credentials, securityContext, p, sbomClusterReport)
// add image pull secret to be used when pulling trivy image fom private registry
podSpec.ImagePullSecrets = config.GetImagePullSecret()
return podSpec, secrets, err
}
const (
tmpVolumeName = "tmp"
ignoreFileVolumeName = "ignorefile"
sslCertDirVolumeName = "ssl-cert-dir"
ignoreFileName = ".trivyignore"
ignoreFileMountPath = "/etc/trivy/" + ignoreFileName
ignorePolicyVolumeName = "ignorepolicy"
ignorePolicyName = "policy.rego"
ignorePolicyMountPath = "/etc/trivy/" + ignorePolicyName
scanResultVolumeName = "scanresult"
FsSharedVolumeName = "trivyoperator"
SharedVolumeLocationOfTrivy = "/var/trivyoperator/trivy"
SslCertDir = "/var/ssl-cert"
)
func (p *plugin) ParseReportData(ctx trivyoperator.PluginContext, imageRef string, logsReader io.ReadCloser) (v1alpha1.VulnerabilityReportData, v1alpha1.ExposedSecretReportData, *v1alpha1.SbomReportData, error) {
var vulnReport v1alpha1.VulnerabilityReportData
var secretReport v1alpha1.ExposedSecretReportData
config, err := getConfig(ctx)
if err != nil {
return vulnReport, secretReport, nil, err
}
cmd := config.GetCommand()
if err != nil {
return vulnReport, secretReport, nil, err
}
compressedLogs := ctx.GetTrivyOperatorConfig().CompressLogs()
if compressedLogs && cmd != Filesystem && cmd != Rootfs {
var errCompress error
logsReader, errCompress = utils.ReadCompressData(logsReader)
if errCompress != nil {
return vulnReport, secretReport, nil, errCompress
}
}
var reports ty.Report
err = json.NewDecoder(logsReader).Decode(&reports)
if err != nil {
return vulnReport, secretReport, nil, err
}
registry, artifact, err := p.parseImageRef(imageRef, reports.Metadata.ImageID)
if err != nil {
return vulnReport, secretReport, nil, err
}
os := p.parseOSRef(reports)
trivyImageRef, err := config.GetImageRef()
if err != nil {
return vulnReport, secretReport, nil, err
}
version, err := trivyoperator.GetVersionFromImageRef(trivyImageRef)
if err != nil {
return vulnReport, secretReport, nil, err
}
var sbomData *v1alpha1.SbomReportData
if ctx.GetTrivyOperatorConfig().GenerateSbomEnabled() {
sbomData, err = sbomreport.BuildSbomReportData(reports, p.clock, registry, artifact, version)
if err != nil {
return vulnReport, secretReport, nil, err
}
}
vulnerabilities := make([]v1alpha1.Vulnerability, 0)
secrets := make([]v1alpha1.ExposedSecret, 0)
for _, report := range reports.Results {
addFields := config.GetAdditionalVulnerabilityReportFields()
vulnerabilities = append(vulnerabilities, vulnerabilityreport.GetVulnerabilitiesFromScanResult(report, addFields)...)
secrets = append(secrets, getExposedSecretsFromScanResult(report)...)
}
vulnerabilitiesData := vulnerabilityreport.BuildVulnerabilityReportData(p.clock, registry, artifact, os, version, vulnerabilities)
exposedSecretsData := exposedsecretreport.BuildExposedSecretsReportData(p.clock, registry, artifact, version, secrets)
return vulnerabilitiesData, exposedSecretsData, sbomData, nil
}
func getExposedSecretsFromScanResult(report ty.Result) []v1alpha1.ExposedSecret {
secrets := make([]v1alpha1.ExposedSecret, 0)
for _, sr := range report.Secrets {
secrets = append(secrets, v1alpha1.ExposedSecret{
Target: report.Target,
RuleID: sr.RuleID,
Title: sr.Title,
Severity: v1alpha1.Severity(sr.Severity),
Category: string(sr.Category),
Match: sr.Match,
})
}
return secrets
}
// NewConfigForConfigAudit and interface which expose related configaudit report configuration
func (p *plugin) NewConfigForConfigAudit(ctx trivyoperator.PluginContext) (configauditreport.ConfigAuditConfig, error) {
return getConfig(ctx)
}
func (p *plugin) parseImageRef(imageRef string, imageID string) (v1alpha1.Registry, v1alpha1.Artifact, error) {
ref, err := containerimage.ParseReference(imageRef)
if err != nil {
return v1alpha1.Registry{}, v1alpha1.Artifact{}, err
}
registry := v1alpha1.Registry{
Server: ref.Context().RegistryStr(),
}
artifact := v1alpha1.Artifact{
Repository: ref.Context().RepositoryStr(),
}
switch t := ref.(type) {
case containerimage.Tag:
artifact.Tag = t.TagStr()
case containerimage.Digest:
artifact.Digest = t.DigestStr()
}
if len(artifact.Digest) == 0 {
artifact.Digest = imageID
}
return registry, artifact, nil
}
func (p *plugin) parseOSRef(reports ty.Report) v1alpha1.OS {
os := v1alpha1.OS{}
if reports.Metadata.OS != nil {
os.Family = reports.Metadata.OS.Family
os.Name = reports.Metadata.OS.Name
eosl := reports.Metadata.OS.Eosl
if eosl {
os.Eosl = eosl
}
}
return os
}
// ExcludeImage checks if the image should be excluded from scanning based on the excludeImagePattern (glob pattern)
func ExcludeImage(excludeImagePattern []string, imageName string) bool {
if len(excludeImagePattern) == 0 {
return false
}
for _, pattern := range excludeImagePattern {
if matched, err := filepath.Match(pattern, imageName); err == nil && matched {
return true
}
}
return false
}