-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathdriver.go
400 lines (325 loc) · 11.3 KB
/
driver.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
package local
import (
"bytes"
"context"
"encoding/json"
"fmt"
"sort"
"strings"
"sync"
"github.com/open-policy-agent/frameworks/constraint/pkg/apis/constraints"
"github.com/open-policy-agent/frameworks/constraint/pkg/client/drivers"
clienterrors "github.com/open-policy-agent/frameworks/constraint/pkg/client/errors"
"github.com/open-policy-agent/frameworks/constraint/pkg/core/templates"
"github.com/open-policy-agent/frameworks/constraint/pkg/externaldata"
"github.com/open-policy-agent/frameworks/constraint/pkg/types"
"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/topdown"
"github.com/open-policy-agent/opa/topdown/print"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/utils/pointer"
)
const (
libRoot = "data.lib"
violation = "violation"
)
var _ drivers.Driver = &Driver{}
// Driver is a threadsafe Rego environment for compiling Rego in ConstraintTemplates,
// registering Constraints, and executing queries.
type Driver struct {
// compilers is a store of Rego Compilers for each Template.
compilers Compilers
// mtx guards access to the storage and target maps.
mtx sync.RWMutex
storage storages
// targets is a map from each Template's kind to the targets for that Template.
targets map[string][]string
// traceEnabled is whether tracing is enabled for Rego queries by default.
// If enabled, individual queries cannot disable tracing.
traceEnabled bool
// printEnabled is whether print statements are allowed in Rego. If disabled,
// print statements are removed from modules at compile-time.
printEnabled bool
// printHook specifies where to send the output of Rego print() statements.
printHook print.Hook
// providerCache allows Rego to read from external_data in Rego queries.
providerCache *externaldata.ProviderCache
// sendRequestToProvider allows Rego to send requests to the provider specified in external_data.
sendRequestToProvider externaldata.SendRequestToProvider
}
// AddTemplate adds templ to Driver. Normalizes modules into usable forms for
// use in queries.
func (d *Driver) AddTemplate(ctx context.Context, templ *templates.ConstraintTemplate) error {
var targets []string
for _, target := range templ.Spec.Targets {
// Ensure storage for each of this Template's targets exists.
_, err := d.storage.getStorage(ctx, target.Target)
if err != nil {
return err
}
targets = append(targets, target.Target)
}
kind := templ.Spec.CRD.Spec.Names.Kind
d.mtx.Lock()
defer d.mtx.Unlock()
d.targets[kind] = targets
return d.compilers.addTemplate(templ, d.printEnabled)
}
// RemoveTemplate removes all Compilers and Constraints for templ.
// Returns nil if templ does not exist.
func (d *Driver) RemoveTemplate(ctx context.Context, templ *templates.ConstraintTemplate) error {
kind := templ.Spec.CRD.Spec.Names.Kind
constraintParent := storage.Path{"constraint", kind}
d.mtx.Lock()
defer d.mtx.Unlock()
d.compilers.removeTemplate(kind)
delete(d.targets, kind)
return d.storage.removeDataEach(ctx, constraintParent)
}
// AddConstraint adds Constraint to Rego storage. Future calls to Query will
// be evaluated against Constraint if the Constraint's key is passed.
func (d *Driver) AddConstraint(ctx context.Context, constraint *unstructured.Unstructured) error {
// Note that this discards "status" as we only copy spec.parameters.
params, _, err := unstructured.NestedFieldNoCopy(constraint.Object, "spec", "parameters")
if err != nil {
return fmt.Errorf("%w: %v", constraints.ErrInvalidConstraint, err)
}
// default .spec.parameters so that we don't need to default this in Rego.
if params == nil {
params = make(map[string]interface{})
}
key := drivers.ConstraintKeyFrom(constraint)
path := key.StoragePath()
d.mtx.Lock()
defer d.mtx.Unlock()
targets := d.targets[key.Kind]
for _, target := range targets {
err := d.storage.addData(ctx, target, path, params)
if err != nil {
return err
}
}
return nil
}
// RemoveConstraint removes Constraint from Rego storage. Future calls to Query
// will not be evaluated against the constraint. Queries which specify the
// constraint's key will silently not evaluate the Constraint.
func (d *Driver) RemoveConstraint(ctx context.Context, constraint *unstructured.Unstructured) error {
path := drivers.ConstraintKeyFrom(constraint).StoragePath()
d.mtx.Lock()
defer d.mtx.Unlock()
return d.storage.removeDataEach(ctx, path)
}
// AddData adds data to Rego storage at data.inventory.path.
func (d *Driver) AddData(ctx context.Context, target string, path storage.Path, data interface{}) error {
path = inventoryPath(path)
return d.storage.addData(ctx, target, path, data)
}
// RemoveData deletes data from Rego storage at data.inventory.path.
func (d *Driver) RemoveData(ctx context.Context, target string, path storage.Path) error {
path = inventoryPath(path)
return d.storage.removeData(ctx, target, path)
}
// eval runs a query against compiler.
// path is the path to evaluate.
// input is the already-parsed Rego Value to use as input.
// Returns the Rego results, the trace if requested, or an error if there was
// a problem executing the query.
func (d *Driver) eval(ctx context.Context, compiler *ast.Compiler, target string, path []string, input ast.Value, opts ...drivers.QueryOpt) (rego.ResultSet, *string, error) {
cfg := &drivers.QueryCfg{}
for _, opt := range opts {
opt(cfg)
}
queryPath := strings.Builder{}
queryPath.WriteString("data")
for _, p := range path {
queryPath.WriteString(".")
queryPath.WriteString(p)
}
store, err := d.storage.getStorage(ctx, target)
if err != nil {
return nil, nil, err
}
args := []func(*rego.Rego){
rego.Compiler(compiler),
rego.Store(store),
rego.ParsedInput(input),
rego.Query(queryPath.String()),
rego.EnablePrintStatements(d.printEnabled),
rego.PrintHook(d.printHook),
}
buf := topdown.NewBufferTracer()
if d.traceEnabled || cfg.TracingEnabled {
args = append(args, rego.QueryTracer(buf))
}
r := rego.New(args...)
res, err := r.Eval(ctx)
var t *string
if d.traceEnabled || cfg.TracingEnabled {
b := &bytes.Buffer{}
topdown.PrettyTrace(b, *buf)
t = pointer.StringPtr(b.String())
}
return res, t, err
}
func (d *Driver) Query(ctx context.Context, target string, constraints []*unstructured.Unstructured, review interface{}, opts ...drivers.QueryOpt) ([]*types.Result, *string, error) {
if len(constraints) == 0 {
return nil, nil, nil
}
constraintsByKind := toConstraintsByKind(constraints)
traceBuilder := strings.Builder{}
constraintsMap := drivers.KeyMap(constraints)
path := []string{"hooks", "violation[result]"}
var results []*types.Result
// Round-trip review through JSON so that the review object is round-tripped
// once per call to Query instead of once per compiler.
reviewMap, err := toInterfaceMap(review)
if err != nil {
return nil, nil, err
}
d.mtx.RLock()
defer d.mtx.RUnlock()
for kind, kindConstraints := range constraintsByKind {
compiler := d.compilers.getCompiler(target, kind)
if compiler == nil {
// The Template was just removed, so the Driver is in an inconsistent
// state with Client. Raise this as an error rather than attempting to
// continue.
return nil, nil, fmt.Errorf("missing Template %q for target %q", kind, target)
}
// Parse input into an ast.Value to avoid round-tripping through JSON when
// possible.
parsedInput, err := toParsedInput(target, kindConstraints, reviewMap)
if err != nil {
return nil, nil, err
}
resultSet, trace, err := d.eval(ctx, compiler, target, path, parsedInput, opts...)
if err != nil {
resultSet = make(rego.ResultSet, 0, len(kindConstraints))
for _, constraint := range kindConstraints {
resultSet = append(resultSet, rego.Result{
Bindings: map[string]interface{}{
"result": map[string]interface{}{
"msg": err.Error(),
"key": map[string]interface{}{
"kind": constraint.GetKind(),
"name": constraint.GetName(),
},
},
},
})
}
}
if trace != nil {
traceBuilder.WriteString(*trace)
}
kindResults, err := drivers.ToResults(constraintsMap, resultSet)
if err != nil {
return nil, nil, err
}
results = append(results, kindResults...)
}
traceString := traceBuilder.String()
if len(traceString) != 0 {
return results, &traceString, nil
}
return results, nil, nil
}
func (d *Driver) Dump(ctx context.Context) (string, error) {
dt := make(map[string]map[string]rego.ResultSet)
compilers := d.compilers.list()
for targetName, targetCompilers := range compilers {
targetData := make(map[string]rego.ResultSet)
for kind, compiler := range targetCompilers {
rs, _, err := d.eval(ctx, compiler, targetName, []string{"data"}, nil)
if err != nil {
return "", err
}
targetData[kind] = rs
}
dt[targetName] = targetData
}
resp := map[string]interface{}{
"data": dt,
}
b, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return "", err
}
return string(b), nil
}
// rewriteModulePackage rewrites the module's package path to path.
func rewriteModulePackage(module *ast.Module) error {
pathParts := ast.Ref([]*ast.Term{ast.VarTerm(templatePath)})
packageRef := ast.Ref([]*ast.Term{ast.VarTerm("data")})
newPath := packageRef.Extend(pathParts)
module.Package.Path = newPath
return nil
}
// requireModuleRules makes sure the module contains all of the specified
// requiredRules.
func requireModuleRules(module *ast.Module, requiredRules map[string]struct{}) error {
ruleSets := make(map[string]struct{}, len(module.Rules))
for _, rule := range module.Rules {
ruleSets[string(rule.Head.Name)] = struct{}{}
}
var missing []string
for name := range requiredRules {
_, ok := ruleSets[name]
if !ok {
missing = append(missing, name)
}
}
sort.Strings(missing)
if len(missing) > 0 {
return fmt.Errorf("%w: missing required rules: %v",
clienterrors.ErrInvalidModule, missing)
}
return nil
}
func toInterfaceMap(obj interface{}) (map[string]interface{}, error) {
jsn, err := json.Marshal(obj)
if err != nil {
return nil, err
}
result := make(map[string]interface{})
err = json.Unmarshal(jsn, &result)
if err != nil {
return nil, err
}
return result, nil
}
func toKeySlice(constraints []*unstructured.Unstructured) []interface{} {
var keys []interface{}
for _, constraint := range constraints {
key := drivers.ConstraintKeyFrom(constraint)
keys = append(keys, map[string]interface{}{
"kind": key.Kind,
"name": key.Name,
})
}
return keys
}
func toConstraintsByKind(constraints []*unstructured.Unstructured) map[string][]*unstructured.Unstructured {
constraintsByKind := make(map[string][]*unstructured.Unstructured)
for _, constraint := range constraints {
kind := constraint.GetKind()
constraintsByKind[kind] = append(constraintsByKind[kind], constraint)
}
return constraintsByKind
}
func toParsedInput(target string, constraints []*unstructured.Unstructured, review map[string]interface{}) (ast.Value, error) {
// Store constraint keys in a format InterfaceToValue does not need to
// round-trip through JSON.
constraintKeys := toKeySlice(constraints)
input := map[string]interface{}{
"target": target,
"constraints": constraintKeys,
"review": review,
}
// Parse input into an ast.Value to avoid round-tripping through JSON when
// possible.
return ast.InterfaceToValue(input)
}