forked from aquasecurity/trivy-iac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
218 lines (178 loc) · 5.42 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
package helm
import (
"context"
"fmt"
"io"
"io/fs"
"path/filepath"
"strings"
"github.com/aquasecurity/defsec/pkg/debug"
"github.com/aquasecurity/defsec/pkg/framework"
"github.com/aquasecurity/defsec/pkg/scan"
"github.com/aquasecurity/defsec/pkg/scanners/options"
"github.com/aquasecurity/defsec/pkg/types"
"github.com/aquasecurity/trivy-policies/pkg/rego"
"github.com/liamg/memoryfs"
"github.com/nordcloud/trivy-iac/pkg/detection"
"github.com/nordcloud/trivy-iac/pkg/scanners"
"github.com/nordcloud/trivy-iac/pkg/scanners/helm/parser"
kparser "github.com/nordcloud/trivy-iac/pkg/scanners/kubernetes/parser"
)
var _ scanners.FSScanner = (*Scanner)(nil)
var _ options.ConfigurableScanner = (*Scanner)(nil)
type Scanner struct {
policyDirs []string
dataDirs []string
debug debug.Logger
options []options.ScannerOption
parserOptions []options.ParserOption
policyReaders []io.Reader
loadEmbeddedLibraries bool
loadEmbeddedPolicies bool
policyFS fs.FS
skipRequired bool
frameworks []framework.Framework
spec string
}
func (s *Scanner) SetSpec(spec string) {
s.spec = spec
}
func (s *Scanner) SetRegoOnly(bool) {
}
func (s *Scanner) SetFrameworks(frameworks []framework.Framework) {
s.frameworks = frameworks
}
// New creates a new Scanner
func New(options ...options.ScannerOption) *Scanner {
s := &Scanner{
options: options,
}
for _, option := range options {
option(s)
}
return s
}
func (s *Scanner) AddParserOptions(options ...options.ParserOption) {
s.parserOptions = append(s.parserOptions, options...)
}
func (s *Scanner) SetUseEmbeddedPolicies(b bool) {
s.loadEmbeddedPolicies = b
}
func (s *Scanner) SetUseEmbeddedLibraries(b bool) {
s.loadEmbeddedLibraries = b
}
func (s *Scanner) Name() string {
return "Helm"
}
func (s *Scanner) SetPolicyReaders(readers []io.Reader) {
s.policyReaders = readers
}
func (s *Scanner) SetSkipRequiredCheck(skip bool) {
s.skipRequired = skip
}
func (s *Scanner) SetDebugWriter(writer io.Writer) {
s.debug = debug.New(writer, "helm", "scanner")
}
func (s *Scanner) SetTraceWriter(_ io.Writer) {
// handled by rego later - nothing to do for now...
}
func (s *Scanner) SetPerResultTracingEnabled(_ bool) {
// handled by rego later - nothing to do for now...
}
func (s *Scanner) SetPolicyDirs(dirs ...string) {
s.policyDirs = dirs
}
func (s *Scanner) SetDataDirs(dirs ...string) {
s.dataDirs = dirs
}
func (s *Scanner) SetPolicyNamespaces(namespaces ...string) {
// handled by rego later - nothing to do for now...
}
func (s *Scanner) SetPolicyFilesystem(policyFS fs.FS) {
s.policyFS = policyFS
}
func (s *Scanner) SetDataFilesystem(_ fs.FS) {}
func (s *Scanner) SetRegoErrorLimit(_ int) {}
func (s *Scanner) ScanFS(ctx context.Context, target fs.FS, path string) (scan.Results, error) {
var results []scan.Result
if err := fs.WalkDir(target, path, func(path string, d fs.DirEntry, err error) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if detection.IsArchive(path) {
if scanResults, err := s.getScanResults(path, ctx, target); err != nil {
return err
} else {
results = append(results, scanResults...)
}
}
if strings.HasSuffix(path, "Chart.yaml") {
if scanResults, err := s.getScanResults(filepath.Dir(path), ctx, target); err != nil {
return err
} else {
results = append(results, scanResults...)
}
}
return nil
}); err != nil {
return nil, err
}
return results, nil
}
func (s *Scanner) getScanResults(path string, ctx context.Context, target fs.FS) (results []scan.Result, err error) {
helmParser := parser.New(path, s.parserOptions...)
if err := helmParser.ParseFS(ctx, target, path); err != nil {
return nil, err
}
chartFiles, err := helmParser.RenderedChartFiles()
if err != nil { // not valid helm, maybe some other yaml etc., abort
return nil, nil
}
regoScanner := rego.NewScanner(types.SourceKubernetes, s.options...)
policyFS := target
if s.policyFS != nil {
policyFS = s.policyFS
}
if err := regoScanner.LoadPolicies(s.loadEmbeddedLibraries, s.loadEmbeddedPolicies, policyFS, s.policyDirs, s.policyReaders); err != nil {
return nil, fmt.Errorf("policies load: %w", err)
}
for _, file := range chartFiles {
s.debug.Log("Processing rendered chart file: %s", file.TemplateFilePath)
manifests, err := kparser.New().Parse(strings.NewReader(file.ManifestContent), file.TemplateFilePath)
if err != nil {
return nil, fmt.Errorf("unmarshal yaml: %w", err)
}
for _, manifest := range manifests {
fileResults, err := regoScanner.ScanInput(ctx, rego.Input{
Path: file.TemplateFilePath,
Contents: manifest,
FS: target,
})
if err != nil {
return nil, fmt.Errorf("scanning error: %w", err)
}
if len(fileResults) > 0 {
renderedFS := memoryfs.New()
if err := renderedFS.MkdirAll(filepath.Dir(file.TemplateFilePath), fs.ModePerm); err != nil {
return nil, err
}
if err := renderedFS.WriteLazyFile(file.TemplateFilePath, func() (io.Reader, error) {
return strings.NewReader(file.ManifestContent), nil
}, fs.ModePerm); err != nil {
return nil, err
}
fileResults.SetSourceAndFilesystem(helmParser.ChartSource, renderedFS, detection.IsArchive(helmParser.ChartSource))
}
results = append(results, fileResults...)
}
}
return results, nil
}