-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner.go
241 lines (206 loc) · 5.87 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
package scanner
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"runtime"
"sync"
"github.com/khulnasoft-lab/defsec/pkg/debug"
"github.com/khulnasoft-lab/defsec/pkg/framework"
"github.com/khulnasoft-lab/defsec/pkg/scan"
"github.com/khulnasoft-lab/defsec/pkg/scanners/options"
"github.com/khulnasoft-lab/defsec/pkg/state"
defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types"
"github.com/khulnasoft-lab/vul-policies/pkg/rego"
"github.com/khulnasoft-lab/vul-policies/pkg/rules"
"github.com/khulnasoft-lab/vul-policies/pkg/types"
"github.com/khulnasoft-lab/vul-aws/internal/adapters"
"github.com/khulnasoft-lab/vul-aws/pkg/concurrency"
"github.com/khulnasoft-lab/vul-aws/pkg/errs"
"github.com/khulnasoft-lab/vul-aws/pkg/progress"
)
var _ ConfigurableAWSScanner = (*Scanner)(nil)
type Scanner struct {
sync.Mutex
regoScanner *rego.Scanner
debug debug.Logger
options []options.ScannerOption
progressTracker progress.Tracker
region string
endpoint string
services []string
frameworks []framework.Framework
spec string
concurrencyStrategy concurrency.Strategy
policyDirs []string
policyReaders []io.Reader
policyFS fs.FS
dataFs fs.FS
loadEmbeddedPolicies bool
loadEmbeddedLibraries bool
regoOnly bool
}
func (s *Scanner) SetRegoOnly(value bool) {
s.regoOnly = value
}
func (s *Scanner) SetFrameworks(frameworks []framework.Framework) {
s.frameworks = frameworks
}
func (s *Scanner) SetSpec(spec string) {
s.spec = spec
}
func (s *Scanner) Name() string {
return "AWS API"
}
func (s *Scanner) SetDebugWriter(writer io.Writer) {
s.debug = debug.New(writer, "aws-api", "scanner")
}
func (s *Scanner) SetProgressTracker(t progress.Tracker) {
s.progressTracker = t
}
func (s *Scanner) SetPolicyReaders(readers []io.Reader) {
s.policyReaders = readers
}
func (s *Scanner) SetPolicyDirs(dirs ...string) {
s.policyDirs = dirs
}
func (s *Scanner) SetPolicyFilesystem(fs fs.FS) {
s.policyFS = fs
}
func (s *Scanner) SetDataFilesystem(fs fs.FS) {
s.dataFs = fs
}
func (s *Scanner) SetUseEmbeddedPolicies(b bool) {
s.loadEmbeddedPolicies = b
}
func (s *Scanner) SetUseEmbeddedLibraries(b bool) {
s.loadEmbeddedLibraries = b
}
func (s *Scanner) SetTraceWriter(io.Writer) {}
func (s *Scanner) SetPerResultTracingEnabled(bool) {}
func (s *Scanner) SetDataDirs(...string) {}
func (s *Scanner) SetPolicyNamespaces(...string) {}
func (s *Scanner) SetSkipRequiredCheck(bool) {}
func (s *Scanner) SetRegoErrorLimit(int) {}
func AllSupportedServices() []string {
return adapters.AllServices()
}
func (s *Scanner) SetAWSRegion(region string) {
s.region = region
}
func (s *Scanner) SetAWSEndpoint(endpoint string) {
s.endpoint = endpoint
}
func (s *Scanner) SetAWSServices(services []string) {
s.services = services
}
func (s *Scanner) SetConcurrencyStrategy(strategy concurrency.Strategy) {
s.concurrencyStrategy = strategy
}
func New(opts ...options.ScannerOption) *Scanner {
s := &Scanner{
options: opts,
progressTracker: progress.NoProgress,
concurrencyStrategy: concurrency.DefaultStrategy,
}
for _, opt := range opts {
opt(s)
}
return s
}
func (s *Scanner) CreateState(ctx context.Context) (*state.State, error) {
cloudState, err := adapters.Adapt(ctx, adapters.Options{
ProgressTracker: s.progressTracker,
Region: s.region,
Endpoint: s.endpoint,
Services: s.services,
DebugWriter: s.debug,
ConcurrencyStrategy: s.concurrencyStrategy,
})
if err != nil {
var adaptionError errs.AdapterError
if errors.As(err, &adaptionError) {
s.debug.Log("There were %d errors during adaption process: %s", len(adaptionError.Errors()), adaptionError)
} else {
return nil, err
}
}
return cloudState, nil
}
func (s *Scanner) ScanWithStateRefresh(ctx context.Context) (results scan.Results, err error) {
cloudState, err := s.CreateState(ctx)
if err != nil {
return nil, err
}
return s.Scan(ctx, cloudState)
}
func (s *Scanner) Scan(ctx context.Context, cloudState *state.State) (results scan.Results, err error) {
if cloudState == nil {
return nil, fmt.Errorf("cloud state is nil")
}
// evaluate go rules
if !s.regoOnly {
for _, rule := range s.getRules() {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
if rule.GetRule().RegoPackage != "" {
continue
}
ruleResults := rule.Evaluate(cloudState)
if len(ruleResults) > 0 {
s.debug.Log("Found %d results for %s", len(ruleResults), rule.GetRule().AVDID)
results = append(results, ruleResults...)
}
}
}
// evaluate rego rules
regoScanner, err := s.initRegoScanner()
if err != nil {
return nil, err
}
regoResults, err := regoScanner.ScanInput(ctx, rego.Input{
Contents: cloudState.ToRego(),
})
if err != nil {
return nil, err
}
return append(results, regoResults...), nil
}
func (s *Scanner) getRules() []types.RegisteredRule {
if len(s.frameworks) > 0 { // Only for maintaining backwards compat
return rules.GetRegistered(s.frameworks...)
}
return rules.GetSpecRules(s.spec)
}
func (s *Scanner) initRegoScanner() (*rego.Scanner, error) {
s.Lock()
defer s.Unlock()
if s.regoScanner != nil {
return s.regoScanner, nil
}
srcFS := s.policyFS
if srcFS == nil {
if runtime.GOOS == "windows" {
homeDrive := os.Getenv("HOMEDRIVE")
if homeDrive == "" {
homeDrive = "C:"
}
srcFS = os.DirFS(homeDrive + "\\")
} else {
srcFS = os.DirFS("/")
}
}
regoScanner := rego.NewScanner(defsecTypes.SourceCloud, 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
}