-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathscanner.go
419 lines (358 loc) · 11.9 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package misconf
import (
"context"
_ "embed"
"fmt"
"io"
"io/fs"
"path/filepath"
"sort"
"strings"
"github.com/samber/lo"
"golang.org/x/xerrors"
"github.com/aquasecurity/defsec/pkg/detection"
"github.com/aquasecurity/defsec/pkg/scan"
"github.com/aquasecurity/defsec/pkg/scanners"
"github.com/aquasecurity/defsec/pkg/scanners/azure/arm"
cfscanner "github.com/aquasecurity/defsec/pkg/scanners/cloudformation"
cfparser "github.com/aquasecurity/defsec/pkg/scanners/cloudformation/parser"
dfscanner "github.com/aquasecurity/defsec/pkg/scanners/dockerfile"
"github.com/aquasecurity/defsec/pkg/scanners/helm"
k8sscanner "github.com/aquasecurity/defsec/pkg/scanners/kubernetes"
"github.com/aquasecurity/defsec/pkg/scanners/options"
tfscanner "github.com/aquasecurity/defsec/pkg/scanners/terraform"
tfpscanner "github.com/aquasecurity/defsec/pkg/scanners/terraformplan"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/mapfs"
)
var enabledDefsecTypes = map[detection.FileType]string{
detection.FileTypeAzureARM: types.AzureARM,
detection.FileTypeCloudFormation: types.CloudFormation,
detection.FileTypeTerraform: types.Terraform,
detection.FileTypeDockerfile: types.Dockerfile,
detection.FileTypeKubernetes: types.Kubernetes,
detection.FileTypeHelm: types.Helm,
detection.FileTypeTerraformPlan: types.TerraformPlan,
}
type ScannerOption struct {
Trace bool
RegoOnly bool
Namespaces []string
PolicyPaths []string
DataPaths []string
DisableEmbeddedPolicies bool
HelmValues []string
HelmValueFiles []string
HelmFileValues []string
HelmStringValues []string
TerraformTFVars []string
K8sVersion string
}
func (o *ScannerOption) Sort() {
sort.Strings(o.Namespaces)
sort.Strings(o.PolicyPaths)
sort.Strings(o.DataPaths)
}
type Scanner struct {
fileType detection.FileType
scanner scanners.FSScanner
hasFilePattern bool
}
func NewAzureARMScanner(filePatterns []string, opt ScannerOption) (*Scanner, error) {
return newScanner(detection.FileTypeAzureARM, filePatterns, opt)
}
func NewCloudFormationScanner(filePatterns []string, opt ScannerOption) (*Scanner, error) {
return newScanner(detection.FileTypeCloudFormation, filePatterns, opt)
}
func NewDockerfileScanner(filePatterns []string, opt ScannerOption) (*Scanner, error) {
return newScanner(detection.FileTypeDockerfile, filePatterns, opt)
}
func NewHelmScanner(filePatterns []string, opt ScannerOption) (*Scanner, error) {
return newScanner(detection.FileTypeHelm, filePatterns, opt)
}
func NewKubernetesScanner(filePatterns []string, opt ScannerOption) (*Scanner, error) {
return newScanner(detection.FileTypeKubernetes, filePatterns, opt)
}
func NewTerraformScanner(filePatterns []string, opt ScannerOption) (*Scanner, error) {
return newScanner(detection.FileTypeTerraform, filePatterns, opt)
}
func NewTerraformPlanScanner(filePatterns []string, opt ScannerOption) (*Scanner, error) {
return newScanner(detection.FileTypeTerraformPlan, filePatterns, opt)
}
func newScanner(t detection.FileType, filePatterns []string, opt ScannerOption) (*Scanner, error) {
opts, err := scannerOptions(t, opt)
if err != nil {
return nil, err
}
var scanner scanners.FSScanner
switch t {
case detection.FileTypeAzureARM:
scanner = arm.New(opts...)
case detection.FileTypeCloudFormation:
scanner = cfscanner.New(opts...)
case detection.FileTypeDockerfile:
scanner = dfscanner.NewScanner(opts...)
case detection.FileTypeHelm:
scanner = helm.New(opts...)
case detection.FileTypeKubernetes:
scanner = k8sscanner.NewScanner(opts...)
case detection.FileTypeTerraform:
scanner = tfscanner.New(opts...)
case detection.FileTypeTerraformPlan:
scanner = tfpscanner.New(opts...)
}
return &Scanner{
fileType: t,
scanner: scanner,
hasFilePattern: hasFilePattern(t, filePatterns),
}, nil
}
func (s *Scanner) Scan(ctx context.Context, fsys fs.FS) ([]types.Misconfiguration, error) {
newfs, err := s.filterFS(fsys)
if err != nil {
return nil, xerrors.Errorf("fs filter error: %w", err)
} else if newfs == nil {
// Skip scanning if no relevant files are found
return nil, nil
}
log.Logger.Debugf("Scanning %s files for misconfigurations...", s.scanner.Name())
results, err := s.scanner.ScanFS(ctx, newfs, ".")
if err != nil {
if _, ok := err.(*cfparser.InvalidContentError); ok {
log.Logger.Errorf("scan %q was broken with InvalidContentError: %v", s.scanner.Name(), err)
return nil, nil
}
return nil, xerrors.Errorf("scan config error: %w", err)
}
configType := enabledDefsecTypes[s.fileType]
misconfs := ResultsToMisconf(configType, s.scanner.Name(), results)
// Sort misconfigurations
for _, misconf := range misconfs {
sort.Sort(misconf.Successes)
sort.Sort(misconf.Warnings)
sort.Sort(misconf.Failures)
}
return misconfs, nil
}
func (s *Scanner) filterFS(fsys fs.FS) (fs.FS, error) {
mfs, ok := fsys.(*mapfs.FS)
if !ok {
// Unable to filter this filesystem
return fsys, nil
}
var foundRelevantFile bool
filter := func(path string, d fs.DirEntry) (bool, error) {
file, err := fsys.Open(path)
if err != nil {
return false, err
}
rs, ok := file.(io.ReadSeeker)
if !ok {
return false, xerrors.Errorf("type assertion error: %w", err)
}
defer file.Close()
if !s.hasFilePattern && !detection.IsType(path, rs, s.fileType) {
return true, nil
}
foundRelevantFile = true
return false, nil
}
newfs, err := mfs.FilterFunc(filter)
if err != nil {
return nil, xerrors.Errorf("fs filter error: %w", err)
}
if !foundRelevantFile {
return nil, nil
}
return newfs, nil
}
func scannerOptions(t detection.FileType, opt ScannerOption) ([]options.ScannerOption, error) {
opts := []options.ScannerOption{
options.ScannerWithSkipRequiredCheck(true),
options.ScannerWithEmbeddedPolicies(!opt.DisableEmbeddedPolicies),
}
policyFS, policyPaths, err := createPolicyFS(opt.PolicyPaths)
if err != nil {
return nil, err
}
if policyFS != nil {
opts = append(opts, options.ScannerWithPolicyFilesystem(policyFS))
}
dataFS, dataPaths, err := createDataFS(opt.DataPaths, opt.K8sVersion)
if err != nil {
return nil, err
}
opts = append(opts, options.ScannerWithDataDirs(dataPaths...))
opts = append(opts, options.ScannerWithDataFilesystem(dataFS))
if opt.Trace {
opts = append(opts, options.ScannerWithPerResultTracing(true))
}
if opt.RegoOnly {
opts = append(opts, options.ScannerWithRegoOnly(true))
}
if len(policyPaths) > 0 {
opts = append(opts, options.ScannerWithPolicyDirs(policyPaths...))
}
if len(opt.DataPaths) > 0 {
opts = append(opts, options.ScannerWithDataDirs(opt.DataPaths...))
}
if len(opt.Namespaces) > 0 {
opts = append(opts, options.ScannerWithPolicyNamespaces(opt.Namespaces...))
}
switch t {
case detection.FileTypeHelm:
return addHelmOpts(opts, opt), nil
case detection.FileTypeTerraform:
return addTFOpts(opts, opt), nil
default:
return opts, nil
}
}
func hasFilePattern(t detection.FileType, filePatterns []string) bool {
for _, pattern := range filePatterns {
if strings.HasPrefix(pattern, fmt.Sprintf("%s:", t)) {
return true
}
}
return false
}
func addTFOpts(opts []options.ScannerOption, scannerOption ScannerOption) []options.ScannerOption {
if len(scannerOption.TerraformTFVars) > 0 {
opts = append(opts, tfscanner.ScannerWithTFVarsPaths(scannerOption.TerraformTFVars...))
}
return opts
}
func addHelmOpts(opts []options.ScannerOption, scannerOption ScannerOption) []options.ScannerOption {
if len(scannerOption.HelmValueFiles) > 0 {
opts = append(opts, helm.ScannerWithValuesFile(scannerOption.HelmValueFiles...))
}
if len(scannerOption.HelmValues) > 0 {
opts = append(opts, helm.ScannerWithValues(scannerOption.HelmValues...))
}
if len(scannerOption.HelmFileValues) > 0 {
opts = append(opts, helm.ScannerWithFileValues(scannerOption.HelmFileValues...))
}
if len(scannerOption.HelmStringValues) > 0 {
opts = append(opts, helm.ScannerWithStringValues(scannerOption.HelmStringValues...))
}
return opts
}
func createPolicyFS(policyPaths []string) (fs.FS, []string, error) {
if len(policyPaths) == 0 {
return nil, nil, nil
}
mfs := mapfs.New()
for _, p := range policyPaths {
abs, err := filepath.Abs(p)
if err != nil {
return nil, nil, xerrors.Errorf("failed to derive absolute path from '%s': %w", p, err)
}
if err = mfs.CopyFilesUnder(abs); err != nil {
return nil, nil, xerrors.Errorf("mapfs file copy error: %w", err)
}
}
// policy paths are no longer needed as fs.FS contains only needed files now.
policyPaths = []string{"."}
return mfs, policyPaths, nil
}
func createDataFS(dataPaths []string, k8sVersion string) (fs.FS, []string, error) {
fsys := mapfs.New()
// Create a virtual file for Kubernetes scanning
if k8sVersion != "" {
if err := fsys.MkdirAll("system", 0700); err != nil {
return nil, nil, err
}
data := []byte(fmt.Sprintf(`{"k8s": {"version": "%s"}}`, k8sVersion))
if err := fsys.WriteVirtualFile("system/k8s-version.json", data, 0600); err != nil {
return nil, nil, err
}
}
for _, path := range dataPaths {
if err := fsys.CopyFilesUnder(path); err != nil {
return nil, nil, err
}
}
// data paths are no longer needed as fs.FS contains only needed files now.
dataPaths = []string{"."}
return fsys, dataPaths, nil
}
// ResultsToMisconf is exported for trivy-plugin-aqua purposes only
func ResultsToMisconf(configType string, scannerName string, results scan.Results) []types.Misconfiguration {
misconfs := map[string]types.Misconfiguration{}
for _, result := range results {
flattened := result.Flatten()
query := fmt.Sprintf("data.%s.%s", result.RegoNamespace(), result.RegoRule())
ruleID := result.Rule().AVDID
if result.RegoNamespace() != "" && len(result.Rule().Aliases) > 0 {
ruleID = result.Rule().Aliases[0]
}
cause := NewCauseWithCode(result)
misconfResult := types.MisconfResult{
Namespace: result.RegoNamespace(),
Query: query,
Message: flattened.Description,
PolicyMetadata: types.PolicyMetadata{
ID: ruleID,
AVDID: result.Rule().AVDID,
Type: fmt.Sprintf("%s Security Check", scannerName),
Title: result.Rule().Summary,
Description: result.Rule().Explanation,
Severity: string(flattened.Severity),
RecommendedActions: flattened.Resolution,
References: flattened.Links,
},
CauseMetadata: cause,
Traces: result.Traces(),
}
filePath := flattened.Location.Filename
misconf, ok := misconfs[filePath]
if !ok {
misconf = types.Misconfiguration{
FileType: configType,
FilePath: filePath,
}
}
if flattened.Warning {
misconf.Warnings = append(misconf.Warnings, misconfResult)
} else {
switch flattened.Status {
case scan.StatusPassed:
misconf.Successes = append(misconf.Successes, misconfResult)
case scan.StatusIgnored:
misconf.Exceptions = append(misconf.Exceptions, misconfResult)
case scan.StatusFailed:
misconf.Failures = append(misconf.Failures, misconfResult)
}
}
misconfs[filePath] = misconf
}
return types.ToMisconfigurations(misconfs)
}
func NewCauseWithCode(underlying scan.Result) types.CauseMetadata {
flat := underlying.Flatten()
cause := types.CauseMetadata{
Resource: flat.Resource,
Provider: flat.RuleProvider.DisplayName(),
Service: flat.RuleService,
StartLine: flat.Location.StartLine,
EndLine: flat.Location.EndLine,
}
if code, err := underlying.GetCode(); err == nil {
cause.Code = types.Code{
Lines: lo.Map(code.Lines, func(l scan.Line, i int) types.Line {
return types.Line{
Number: l.Number,
Content: l.Content,
IsCause: l.IsCause,
Annotation: l.Annotation,
Truncated: l.Truncated,
Highlighted: l.Highlighted,
FirstCause: l.FirstCause,
LastCause: l.LastCause,
}
}),
}
}
return cause
}