-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
scanner.go
165 lines (139 loc) · 3.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
package yaml
import (
"context"
"io"
"io/fs"
"sync"
"github.com/aquasecurity/trivy/pkg/iac/debug"
"github.com/aquasecurity/trivy/pkg/iac/framework"
"github.com/aquasecurity/trivy/pkg/iac/rego"
"github.com/aquasecurity/trivy/pkg/iac/scan"
"github.com/aquasecurity/trivy/pkg/iac/scanners/options"
"github.com/aquasecurity/trivy/pkg/iac/scanners/yaml/parser"
"github.com/aquasecurity/trivy/pkg/iac/types"
)
var _ options.ConfigurableScanner = (*Scanner)(nil)
type Scanner struct { // nolint: gocritic
options []options.ScannerOption
debug debug.Logger
policyDirs []string
policyReaders []io.Reader
parser *parser.Parser
regoScanner *rego.Scanner
skipRequired bool
sync.Mutex
frameworks []framework.Framework
spec string
loadEmbeddedLibraries bool
loadEmbeddedPolicies bool
}
func (s *Scanner) SetRegoOnly(bool) {}
func (s *Scanner) SetFrameworks(frameworks []framework.Framework) {
s.frameworks = frameworks
}
func (s *Scanner) SetSpec(spec string) {
s.spec = spec
}
func (s *Scanner) SetUseEmbeddedPolicies(b bool) {
s.loadEmbeddedPolicies = b
}
func (s *Scanner) SetUseEmbeddedLibraries(b bool) {
s.loadEmbeddedLibraries = b
}
func (s *Scanner) Name() string {
return "YAML"
}
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, "yaml", "scanner")
}
func (s *Scanner) SetTraceWriter(_ io.Writer) {}
func (s *Scanner) SetPerResultTracingEnabled(_ bool) {}
func (s *Scanner) SetPolicyDirs(dirs ...string) {
s.policyDirs = dirs
}
func (s *Scanner) SetDataDirs(_ ...string) {}
func (s *Scanner) SetPolicyNamespaces(_ ...string) {}
func (s *Scanner) SetPolicyFilesystem(_ fs.FS) {
// handled by rego when option is passed on
}
func (s *Scanner) SetDataFilesystem(_ fs.FS) {
// handled by rego when option is passed on
}
func (s *Scanner) SetRegoErrorLimit(_ int) {}
func NewScanner(opts ...options.ScannerOption) *Scanner {
s := &Scanner{
options: opts,
}
for _, opt := range opts {
opt(s)
}
s.parser = parser.New(options.ParserWithSkipRequiredCheck(s.skipRequired))
return s
}
func (s *Scanner) ScanFS(ctx context.Context, fsys fs.FS, path string) (scan.Results, error) {
fileset, err := s.parser.ParseFS(ctx, fsys, path)
if err != nil {
return nil, err
}
if len(fileset) == 0 {
return nil, nil
}
var inputs []rego.Input
for path, files := range fileset {
for _, file := range files {
inputs = append(inputs, rego.Input{
Path: path,
Contents: file,
FS: fsys,
})
}
}
results, err := s.scanRego(ctx, fsys, inputs...)
if err != nil {
return nil, err
}
return results, nil
}
func (s *Scanner) ScanFile(ctx context.Context, fsys fs.FS, path string) (scan.Results, error) {
parsed, err := s.parser.ParseFile(ctx, fsys, path)
if err != nil {
return nil, err
}
s.debug.Log("Scanning %s...", path)
return s.scanRego(ctx, fsys, rego.Input{
Path: path,
Contents: parsed,
})
}
func (s *Scanner) initRegoScanner(srcFS fs.FS) (*rego.Scanner, error) {
s.Lock()
defer s.Unlock()
if s.regoScanner != nil {
return s.regoScanner, nil
}
regoScanner := rego.NewScanner(types.SourceYAML, s.options...)
regoScanner.SetParentDebugLogger(s.debug)
if err := regoScanner.LoadPolicies(s.loadEmbeddedLibraries, s.loadEmbeddedPolicies, srcFS, s.policyDirs, s.policyReaders); err != nil {
return nil, err
}
s.regoScanner = regoScanner
return regoScanner, nil
}
func (s *Scanner) scanRego(ctx context.Context, srcFS fs.FS, inputs ...rego.Input) (scan.Results, error) {
regoScanner, err := s.initRegoScanner(srcFS)
if err != nil {
return nil, err
}
results, err := regoScanner.ScanInput(ctx, inputs...)
if err != nil {
return nil, err
}
results.SetSourceAndFilesystem("", srcFS, false)
return results, nil
}