-
Notifications
You must be signed in to change notification settings - Fork 10
/
engine.go
324 lines (309 loc) · 9.95 KB
/
engine.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
// Copyright 2022 Snyk Ltd
//
// 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"
"sync"
"time"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/storage/inmem"
"github.com/open-policy-agent/opa/util"
"github.com/snyk/policy-engine/pkg/data"
"github.com/snyk/policy-engine/pkg/logging"
"github.com/snyk/policy-engine/pkg/metrics"
"github.com/snyk/policy-engine/pkg/models"
"github.com/snyk/policy-engine/pkg/policy"
)
// Engine is responsible for evaluating some States with a given set of rules.
type Engine struct {
logger logging.Logger
metrics metrics.Metrics
policies []policy.Policy
compiler *ast.Compiler
store storage.Store
ruleIDs map[string]bool
runAllRules bool
}
// EngineOptions contains options for initializing an Engine instance
type EngineOptions struct {
// Providers contains functions that produce parsed OPA modules or data documents.
Providers []data.Provider
// RuleIDs determines which rules are executed. When this option is empty or
// unspecified, all rules will be run.
RuleIDs map[string]bool
// Logger is an optional instance of the logger.Logger interface
Logger logging.Logger
// Metrics is an optional instance of the metrics.Metrics interface
Metrics metrics.Metrics
}
// NewEngine constructs a new Engine instance.
func NewEngine(ctx context.Context, options *EngineOptions) (*Engine, error) {
logger := options.Logger
if logger == nil {
logger = logging.DefaultLogger
}
m := options.Metrics
if m == nil {
m = metrics.NewLocalMetrics(logger)
}
logger.Info(ctx, "Initializing engine")
consumer := NewPolicyConsumer()
if err := policy.RegoAPIProvider(ctx, consumer); err != nil {
logger.Error(ctx, "Failed to load rego API")
return nil, fmt.Errorf("%w: %v", FailedToLoadRegoAPI, err)
}
providersStart := time.Now()
for _, p := range options.Providers {
if err := p(ctx, consumer); err != nil {
logger.Error(ctx, "Failed to consume rule and data providers")
return nil, fmt.Errorf("%w: %v", FailedToLoadRules, err)
}
}
m.Timer(ctx, metrics.PROVIDERS_LOAD_TIME, "", metrics.Labels{}).
Record(time.Now().Sub(providersStart))
logger.WithField(logging.MODULES, len(consumer.Modules)).
WithField(logging.DATA_DOCUMENTS, consumer.NumDocuments).
Info(ctx, "Finished consuming providers")
tree := ast.NewModuleTree(consumer.Modules)
policies := []policy.Policy{}
for _, moduleSet := range policy.ExtractModuleSets(tree) {
l := logger.WithField(logging.PACKAGE, moduleSet.Path.String())
p, err := policy.PolicyFactory(moduleSet)
if err != nil {
l.WithField(logging.ERROR, err.Error()).
Warn(ctx, "Error while parsing policy. It will still be loaded and accessible via data.")
} else if p == nil {
l.Debug(ctx, "Module did not contain a policy. It will still be loaded and accessible via data.")
} else {
policies = append(policies, p)
}
}
compiler := ast.NewCompiler().WithCapabilities(policy.Capabilities())
compilationStart := time.Now()
compiler.Compile(consumer.Modules)
m.Timer(ctx, metrics.COMPILATION_TIME, "", metrics.Labels{}).
Record(time.Now().Sub(compilationStart))
if len(compiler.Errors) > 0 {
err := compiler.Errors.Error()
logger.Error(ctx, "Failed during compilation")
return nil, fmt.Errorf("%w: %v", FailedToCompile, err)
}
logger.Info(ctx, "Finished initializing engine")
m.Counter(ctx, metrics.MODULES_LOADED, "", metrics.Labels{}).
Add(float64(len(consumer.Modules)))
m.Counter(ctx, metrics.DATA_DOCUMENTS_LOADED, "", metrics.Labels{}).
Add(float64(consumer.NumDocuments))
m.Counter(ctx, metrics.POLICIES_LOADED, "", metrics.Labels{}).
Add(float64(len(policies)))
return &Engine{
logger: logger,
metrics: m,
compiler: compiler,
policies: policies,
store: inmem.NewFromObject(consumer.Document),
ruleIDs: options.RuleIDs,
runAllRules: len(options.RuleIDs) < 1,
}, nil
}
type policyResults struct {
pkg string
err error
ruleResults []models.RuleResults
}
// EvalOptions contains options for Engine.Eval
type EvalOptions struct {
// Inputs are the State instances that the engine should evaluate.
Inputs []models.State
Workers int
// ResourceResolver is a function that returns a resource state for the given
// ResourceRequest.
// Multiple ResourcesResolvers can be composed with And() and Or().
ResourcesResolver policy.ResourcesResolver
}
// Eval evaluates the given states using the rules that the engine was initialized with.
func (e *Engine) Eval(ctx context.Context, options *EvalOptions) *models.Results {
e.logger.Debug(ctx, "Beginning evaluation")
regoOptions := []func(*rego.Rego){
rego.Compiler(e.compiler),
rego.Store(e.store),
rego.StrictBuiltinErrors(true),
}
policies := e.policies
if !e.runAllRules {
ruleSelectionStart := time.Now()
policies = []policy.Policy{}
for _, p := range e.policies {
id, err := p.ID(ctx, regoOptions)
if err != nil {
e.logger.WithField("package", p.Package()).
Warn(ctx, "Failed to extract ID from policy")
continue
}
if !e.ruleIDs[id] {
continue
}
policies = append(policies, p)
}
e.metrics.Timer(ctx, metrics.RULE_SELECTION_TIME, "", metrics.Labels{}).
Record(time.Now().Sub(ruleSelectionStart))
}
results := []models.Result{}
for idx, state := range options.Inputs {
value, err := stateToAstValue(&state)
if err != nil {
e.logger.WithError(err).Error(ctx, "Failed to pre-parse input")
continue
}
policyOpts := policy.EvalOptions{
RegoOptions: regoOptions,
Input: &state,
InputValue: value,
ResourcesResolver: options.ResourcesResolver,
}
allRuleResults := []models.RuleResults{}
policyChan := make(chan policy.Policy)
resultsChan := make(chan policyResults)
var wg sync.WaitGroup
ruleEvalCounter := e.metrics.Counter(ctx, metrics.RULES_EVALUATED, "", metrics.Labels{
metrics.INPUT_IDX: fmt.Sprint(idx),
})
totalEvalStart := time.Now()
go func() {
numWorkers := options.Workers
if numWorkers < 1 {
numWorkers = runtime.NumCPU() + 1
}
e.logger.WithField("workers", numWorkers).Debug(ctx, "Starting workers")
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
p, ok := <-policyChan
if !ok {
return
}
pkg := p.Package()
evalStart := time.Now()
ruleResults, err := p.Eval(ctx, policyOpts)
labels := metrics.Labels{
metrics.PACKAGE: pkg,
// TODO: Do we need a better way to identify inputs?
metrics.INPUT_IDX: fmt.Sprint(idx),
}
e.metrics.Timer(ctx, metrics.RULE_EVAL_TIME, "", labels).
Record(time.Now().Sub(evalStart))
for _, r := range ruleResults {
e.metrics.Counter(ctx, metrics.RESULTS_PRODUCED, "", labels).
Add(float64(len(r.Results)))
}
resultsChan <- policyResults{
pkg: pkg,
err: err,
ruleResults: ruleResults,
}
}
}()
}
for _, p := range policies {
if !p.InputTypeMatches(state.InputType) {
continue
}
policyChan <- p
ruleEvalCounter.Inc()
}
close(policyChan)
wg.Wait()
close(resultsChan)
}()
errCounter := e.metrics.Counter(ctx, metrics.POLICY_ERRORS, "", metrics.Labels{})
for {
select {
case policyResults, ok := <-resultsChan:
if !ok {
resultsChan = nil
break
}
if policyResults.err != nil {
e.logger.WithField(logging.PACKAGE, policyResults.pkg).
WithError(policyResults.err).
Warn(ctx, "Failed to evaluate policy")
errCounter.Inc()
allRuleResults = append(allRuleResults, policyResults.ruleResults...)
} else {
e.logger.WithField(logging.PACKAGE, policyResults.pkg).
Debug(ctx, "Completed policy evaluation")
allRuleResults = append(allRuleResults, policyResults.ruleResults...)
}
}
if resultsChan == nil {
break
}
}
e.metrics.Timer(ctx, metrics.TOTAL_RULE_EVAL_TIME, "", metrics.Labels{
metrics.INPUT_IDX: fmt.Sprint(idx),
}).Record(time.Now().Sub(totalEvalStart))
results = append(results, models.Result{
Input: state,
RuleResults: allRuleResults,
})
}
return &models.Results{
Format: "results",
FormatVersion: "1.0.0",
Results: results,
}
}
type MetadataResult struct {
Package string `json:"package"`
Metadata policy.Metadata `json:"metadata"`
Error string `json:"error,omitempty"`
}
// Metadata returns the metadata of all Policies that have been loaded into this
// Engine instance.
func (e *Engine) Metadata(ctx context.Context) []MetadataResult {
opts := []func(*rego.Rego){
rego.Compiler(e.compiler),
rego.Store(e.store),
}
metadata := make([]MetadataResult, len(e.policies))
for idx, p := range e.policies {
m, err := p.Metadata(ctx, opts)
result := MetadataResult{
Package: p.Package(),
}
if err != nil {
result.Error = err.Error()
} else {
result.Metadata = m
}
metadata[idx] = result
}
return metadata
}
// Pre-parsing the input saves a significant number of cycles for large inputs
// and multi-resource policies.
func stateToAstValue(state *models.State) (ast.Value, error) {
rawPtr := util.Reference(state)
// roundtrip through json: this turns slices (e.g. []string, []bool) into
// []interface{}, the only array type ast.InterfaceToValue can work with
if err := util.RoundTrip(rawPtr); err != nil {
return nil, err
}
return ast.InterfaceToValue(*rawPtr)
}