-
Notifications
You must be signed in to change notification settings - Fork 10
/
policyset.go
481 lines (427 loc) · 13.2 KB
/
policyset.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// © 2023 Snyk Limited All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package engine
import (
"context"
"fmt"
"runtime"
"sort"
"sync"
"github.com/hashicorp/go-multierror"
"github.com/open-policy-agent/opa/ast"
"github.com/snyk/policy-engine/pkg/data"
"github.com/snyk/policy-engine/pkg/metrics"
"github.com/snyk/policy-engine/pkg/models"
"github.com/snyk/policy-engine/pkg/policy"
"github.com/snyk/policy-engine/pkg/rego"
)
type PolicySource string
const (
POLICY_SOURCE_DATA PolicySource = "data"
POLICY_SOURCE_BUNDLE_ARCHIVE PolicySource = "bundle_archive"
POLICY_SOURCE_BUNDLE_DIRECTORY PolicySource = "bundle_directory"
)
type policySet struct {
PolicyConsumer
instrumentation *policySetInstrumentation
rego *rego.State
policies []policy.Policy
name string
source PolicySource
checksum string
}
type policySetOptions struct {
providers []data.Provider
source PolicySource
instrumentation instrumentation
name string
checksum string
}
type RuleBundleError struct {
ruleBundle *models.RuleBundle
err error
}
func (p *RuleBundleError) Error() string {
return p.err.Error()
}
func (p *RuleBundleError) ToModel() models.RuleBundleInfo {
return models.RuleBundleInfo{
RuleBundle: p.ruleBundle,
Errors: []string{p.Error()},
}
}
func newRuleBundleError(ruleBundle *models.RuleBundle, err error) error {
return &RuleBundleError{
ruleBundle: ruleBundle,
err: err,
}
}
func newPolicySet(ctx context.Context, options policySetOptions) (*policySet, error) {
s := &policySet{
instrumentation: &policySetInstrumentation{
instrumentation: options.instrumentation,
},
PolicyConsumer: *NewPolicyConsumer(),
name: options.name,
source: options.source,
checksum: options.checksum,
}
s.instrumentation.startInitialization(ctx)
defer s.instrumentation.finishInitialization(ctx, s)
if err := s.loadRegoAPI(ctx); err != nil {
return nil, newRuleBundleError(s.ruleBundle(), fmt.Errorf("%w: %v", FailedToLoadRegoAPI, err))
}
if err := s.consumeProviders(ctx, options.providers); err != nil {
return nil, newRuleBundleError(s.ruleBundle(), fmt.Errorf("%w: %v", FailedToLoadRules, err))
}
s.extractPolicies(ctx)
if err := s.compile(ctx); err != nil {
return nil, newRuleBundleError(s.ruleBundle(), fmt.Errorf("%w: %v", FailedToCompile, err))
}
return s, nil
}
func (s *policySet) loadRegoAPI(ctx context.Context) error {
s.instrumentation.startLoadRegoAPI(ctx)
defer s.instrumentation.finishLoadRegoAPI(ctx)
if err := policy.RegoAPIProvider(ctx, s); err != nil {
return err
}
return data.PureRegoLibProvider()(ctx, s)
}
func (s *policySet) consumeProviders(ctx context.Context, providers []data.Provider) error {
s.instrumentation.startConsumeProviders(ctx)
defer s.instrumentation.finishConsumeProviders(ctx)
var result *multierror.Error
for _, p := range providers {
if err := p(ctx, s); err != nil {
result = multierror.Append(result, err)
}
}
return result.ErrorOrNil()
}
func (s *policySet) extractPolicies(ctx context.Context) {
s.instrumentation.startExtractPolicies(ctx)
defer s.instrumentation.finishExtractPolicies(ctx)
tree := ast.NewModuleTree(s.Modules)
policies := []policy.Policy{}
for _, moduleSet := range policy.ExtractModuleSets(tree) {
p, err := policy.PolicyFactory(moduleSet)
if err != nil {
// This can happen if customers include non-rule code in the rules
// package, so we just log a warning.
s.instrumentation.extractPoliciesError(ctx, err)
} else if p != nil {
policies = append(policies, p)
}
}
s.policies = policies
}
func (s *policySet) compile(ctx context.Context) error {
s.instrumentation.startCompile(ctx)
defer s.instrumentation.finishCompile(ctx)
var err error
s.rego, err = rego.NewState(rego.Options{
Modules: s.Modules,
Document: s.Document,
Capabilities: policy.Capabilities(),
})
if err != nil {
return err
}
return nil
}
type evalPolicyOptions struct {
resourcesResolver policy.ResourcesResolver
policy policy.Policy
input *models.State
}
func (s *policySet) evalPolicy(ctx context.Context, options *evalPolicyOptions) policyResults {
pol := options.policy
instrumentation := s.instrumentation.policyEvalInstrumentation(pol)
instrumentation.startEval(ctx)
ruleResults, err := pol.Eval(ctx, policy.EvalOptions{
RegoState: s.rego,
Logger: instrumentation.logger,
ResourcesResolver: options.resourcesResolver,
Input: options.input,
})
totalResults := 0
for idx, r := range ruleResults {
ruleResults[idx].RuleBundle = s.ruleBundle()
totalResults += len(r.Results)
}
instrumentation.finishEval(ctx, totalResults)
return policyResults{
ruleResults: ruleResults,
err: err,
}
}
type policyFilter func(pol policy.Policy) bool
func (s *policySet) selectPolicies(ctx context.Context, filters []policyFilter) []policy.Policy {
s.instrumentation.startPolicySelection(ctx)
subset := []policy.Policy{}
for _, pol := range s.policies {
matches := true
for _, filter := range filters {
if !filter(pol) {
matches = false
}
}
if matches {
subset = append(subset, pol)
}
}
s.instrumentation.finishPolicySelection(ctx, len(subset))
return subset
}
func (s *policySet) ruleIDFilter(ctx context.Context, ruleIDs []string) policyFilter {
ids := map[string]bool{}
for _, r := range ruleIDs {
ids[r] = true
}
return func(pol policy.Policy) bool {
id, err := pol.ID(ctx, s.rego)
if err != nil {
s.instrumentation.policyIDError(ctx, pol.Package(), err)
return false
}
return ids[id]
}
}
type parallelEvalOptions struct {
resourcesResolver policy.ResourcesResolver
workers int
input *models.State
ruleIDs []string
loggerFields []loggerOption
}
func (s *policySet) eval(ctx context.Context, options *parallelEvalOptions) []models.RuleResults {
// Get list of policies to evaluate
filters := []policyFilter{
func(pol policy.Policy) bool {
return pol.InputTypeMatches(options.input.InputType)
},
}
if len(options.ruleIDs) > 0 {
filters = append(filters, s.ruleIDFilter(ctx, options.ruleIDs))
}
policies := s.selectPolicies(ctx, filters)
// Spin off N workers to go through the list
numWorkers := options.workers
if numWorkers < 1 {
numWorkers = runtime.NumCPU() + 1
}
loggerFields := []loggerOption{withField("workers", numWorkers)}
loggerFields = append(loggerFields, options.loggerFields...)
s.instrumentation.startEval(ctx, loggerFields)
defer s.instrumentation.finishEval(ctx, loggerFields)
allRuleResults := []models.RuleResults{}
policyChan := make(chan policy.Policy)
resultsChan := make(chan policyResults)
var wg sync.WaitGroup
go func() {
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
p, ok := <-policyChan
if !ok {
return
}
resultsChan <- s.evalPolicy(ctx, &evalPolicyOptions{
resourcesResolver: options.resourcesResolver,
policy: p,
input: options.input,
})
}
}()
}
for _, p := range policies {
policyChan <- p
}
close(policyChan)
wg.Wait()
close(resultsChan)
}()
for {
policyResults, ok := <-resultsChan
if !ok {
break
}
s.instrumentation.countPolicyEval(ctx)
if policyResults.err != nil {
s.instrumentation.countPolicyEvalError(ctx)
}
allRuleResults = append(allRuleResults, policyResults.ruleResults...)
}
return allRuleResults
}
func (s *policySet) metadata(ctx context.Context) []MetadataResult {
// Ensure a consistent ordering for policies to make our output
// deterministic.
policies := make([]policy.Policy, len(s.policies))
copy(policies, s.policies)
sort.Slice(policies, func(i, j int) bool {
return policies[i].Package() < policies[j].Package()
})
metadata := make([]MetadataResult, len(policies))
for idx, p := range policies {
m, err := p.Metadata(ctx, s.rego)
result := MetadataResult{
Package: p.Package(),
}
if err != nil {
result.Error = err.Error()
} else {
result.Metadata = m
}
metadata[idx] = result
}
return metadata
}
// QueryOptions contain options for Engine.Query
type QueryOptions struct {
// Query is a rego query
Query string
// Input is an optional state to query against
Input *models.State
// ResourceResolver is an optional function that returns a resource state
// for the given ResourceRequest. Multiple ResourcesResolvers can be
// composed with And() and Or().
ResourcesResolver policy.ResourcesResolver
// ResultProcessor is a function that is run on every result returned by the
// query.
ResultProcessor func(ast.Value) error
}
func (s *policySet) query(ctx context.Context, options *QueryOptions) error {
input := options.Input
if input == nil {
input = &models.State{}
}
builtins := policy.NewBuiltins(input, options.ResourcesResolver)
return s.rego.Query(ctx, rego.Query{
Query: options.Query,
Builtins: builtins.Implementations(),
// TODO: remove once we're no longer looking at input.resources in
// Snyk rules
Input: ast.NewObject(
[2]*ast.Term{ast.StringTerm("resources"), ast.ObjectTerm()},
),
}, options.ResultProcessor)
}
func (s *policySet) ruleBundle() *models.RuleBundle {
return &models.RuleBundle{
Name: s.name,
Source: string(s.source),
Checksum: s.checksum,
}
}
type policySetInstrumentation struct {
instrumentation
}
func (i *policySetInstrumentation) startInitialization(ctx context.Context) {
i.startPhase(ctx, "initialize_policy_set")
}
func (i *policySetInstrumentation) finishInitialization(ctx context.Context, s *policySet) {
modules := len(s.Modules)
docs := s.NumDocuments
policies := len(s.policies)
i.finishPhase(ctx, "initialize_policy_set",
withField("modules_loaded", modules),
withField("data_documents_loaded", docs),
withField("policies_loaded", policies),
)
}
func (i *policySetInstrumentation) startLoadRegoAPI(ctx context.Context) {
i.startPhase(ctx, "load_rego_api")
}
func (i *policySetInstrumentation) finishLoadRegoAPI(ctx context.Context) {
i.finishPhase(ctx, "load_rego_api")
}
func (i *policySetInstrumentation) startConsumeProviders(ctx context.Context) {
i.startPhase(ctx, "consume_providers")
}
func (i *policySetInstrumentation) finishConsumeProviders(ctx context.Context) {
i.finishPhase(ctx, "consume_providers")
}
func (i *policySetInstrumentation) startExtractPolicies(ctx context.Context) {
i.startPhase(ctx, "extract_policies")
}
func (i *policySetInstrumentation) finishExtractPolicies(ctx context.Context) {
i.finishPhase(ctx, "extract_policies")
}
func (i *policySetInstrumentation) extractPoliciesError(ctx context.Context, err error) {
// Using WithField here because we don't want a stack trace in this situation
i.logger.
WithField("error", err.Error()).
Warn(ctx, "Error while parsing policy. It will still be loaded and accessible via data.")
}
func (i *policySetInstrumentation) startCompile(ctx context.Context) {
i.startPhase(ctx, "compile")
}
func (i *policySetInstrumentation) finishCompile(ctx context.Context) {
i.finishPhase(ctx, "compile")
}
func (i *policySetInstrumentation) startPolicySelection(ctx context.Context) {
i.startPhase(ctx, "policy_selection")
}
func (i *policySetInstrumentation) finishPolicySelection(ctx context.Context, policies int) {
i.finishPhase(ctx, "policy_selection",
withField("policies", policies),
)
}
func (i *policySetInstrumentation) policyIDError(ctx context.Context, pkg string, err error) {
i.logger.
WithField("package", pkg).
WithError(err).
Error(ctx, "failed to extract rule ID")
}
func (i *policySetInstrumentation) startEval(ctx context.Context, fields []loggerOption) {
i.startPhase(ctx, "evaluate_policy_set", fields...)
}
func (i *policySetInstrumentation) finishEval(ctx context.Context, fields []loggerOption) {
i.finishPhase(ctx, "evaluate_policy_set", fields...)
}
func (i *policySetInstrumentation) countPolicyEval(ctx context.Context) {
i.metrics.
Counter(ctx, "policies_evaluated", "", i.labels).
Inc()
}
func (i *policySetInstrumentation) countPolicyEvalError(ctx context.Context) {
i.metrics.
Counter(ctx, "policy_evaluation_errors", "", i.labels).
Inc()
}
func (i *policySetInstrumentation) policyEvalInstrumentation(p policy.Policy) *policyEvalInstrumentation {
pkg := p.Package()
return &policyEvalInstrumentation{
instrumentation: i.child(
metrics.Labels{"package": pkg},
debug,
withField("package", pkg),
),
}
}
type policyEvalInstrumentation struct {
instrumentation
}
func (i *policyEvalInstrumentation) startEval(ctx context.Context) {
i.startPhase(ctx, "evaluate_policy")
}
func (i *policyEvalInstrumentation) finishEval(ctx context.Context, results int) {
i.finishPhase(ctx, "evaluate_policy",
withField("results", results),
)
}