forked from aquasecurity/trivy-iac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
176 lines (147 loc) · 4.1 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
package kubernetes
import (
"context"
"io"
"io/fs"
"path/filepath"
"sort"
"sync"
"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/scanners"
"github.com/nordcloud/trivy-iac/pkg/scanners/kubernetes/parser"
)
var _ scanners.FSScanner = (*Scanner)(nil)
var _ options.ConfigurableScanner = (*Scanner)(nil)
type Scanner struct {
debug debug.Logger
options []options.ScannerOption
policyDirs []string
policyReaders []io.Reader
regoScanner *rego.Scanner
parser *parser.Parser
skipRequired bool
sync.Mutex
loadEmbeddedPolicies bool
frameworks []framework.Framework
spec string
loadEmbeddedLibraries bool
}
func (s *Scanner) SetSpec(spec string) {
s.spec = spec
}
func (s *Scanner) SetRegoOnly(bool) {}
func (s *Scanner) SetFrameworks(frameworks []framework.Framework) {
s.frameworks = frameworks
}
func (s *Scanner) SetUseEmbeddedPolicies(b bool) {
s.loadEmbeddedPolicies = b
}
func (s *Scanner) SetUseEmbeddedLibraries(b bool) {
s.loadEmbeddedLibraries = b
}
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, "kubernetes", "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) Name() string {
return "Kubernetes"
}
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.SourceKubernetes, 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) ScanReader(ctx context.Context, filename string, reader io.Reader) (scan.Results, error) {
memfs := memoryfs.New()
if err := memfs.MkdirAll(filepath.Base(filename), 0o700); err != nil {
return nil, err
}
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
if err := memfs.WriteFile(filename, data, 0o644); err != nil {
return nil, err
}
return s.ScanFS(ctx, memfs, ".")
}
func (s *Scanner) ScanFS(ctx context.Context, target fs.FS, dir string) (scan.Results, error) {
k8sFilesets, err := s.parser.ParseFS(ctx, target, dir)
if err != nil {
return nil, err
}
if len(k8sFilesets) == 0 {
return nil, nil
}
var inputs []rego.Input
for path, k8sFiles := range k8sFilesets {
for _, content := range k8sFiles {
inputs = append(inputs, rego.Input{
Path: path,
FS: target,
Contents: content,
})
}
}
regoScanner, err := s.initRegoScanner(target)
if err != nil {
return nil, err
}
s.debug.Log("Scanning %d files...", len(inputs))
results, err := regoScanner.ScanInput(ctx, inputs...)
if err != nil {
return nil, err
}
results.SetSourceAndFilesystem("", target, false)
sort.Slice(results, func(i, j int) bool {
return results[i].Rule().AVDID < results[j].Rule().AVDID
})
return results, nil
}