-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathrego_check.go
512 lines (419 loc) · 12.2 KB
/
rego_check.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package checks
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/mitchellh/mapstructure"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/topdown/print"
"gopkg.in/yaml.v3"
"github.com/DataDog/datadog-agent/pkg/compliance"
"github.com/DataDog/datadog-agent/pkg/compliance/checks/env"
"github.com/DataDog/datadog-agent/pkg/compliance/eval"
"github.com/DataDog/datadog-agent/pkg/compliance/event"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
const regoEvaluator = "rego"
type regoCheck struct {
ruleID string
ruleScope compliance.RuleScope
inputs []compliance.RegoInput
preparedEvalQuery rego.PreparedEvalQuery
}
func importModule(importPath, parentDir string, required bool) (string, error) {
// look for relative file if we have a source
if parentDir != "" {
importPath = filepath.Join(parentDir, importPath)
}
mod, err := os.ReadFile(importPath)
if err != nil {
if required {
return "", err
}
return "", nil
}
return string(mod), nil
}
func computeRuleModulesAndQuery(rule *compliance.RegoRule, meta *compliance.SuiteMeta) ([]func(*rego.Rego), string, error) {
options := make([]func(*rego.Rego), 0)
options = append(options, rego.Module("datadog_helpers.rego", helpers))
query := rule.Findings
if rule.Module != "" {
mod, err := ast.ParseModule(fmt.Sprintf("__gen__rule_%s.rego", rule.ID), rule.Module)
if err != nil {
return nil, "", err
}
options = append(options, rego.ParsedModule(mod))
if query == "" {
query = fmt.Sprintf("%v.findings", mod.Package.Path)
}
}
if query == "" {
query = "data.datadog.findings"
log.Infof("defaulting rego query to `%s`", query)
}
var parentDir string
if meta.Source != "" {
parentDir = filepath.Dir(meta.Source)
}
alreadyImported := make(map[string]bool)
// import rego file with the same name as the rule id
imp := fmt.Sprintf("%s.rego", rule.ID)
mod, err := importModule(imp, parentDir, false)
if err != nil {
return nil, "", err
}
if mod != "" {
options = append(options, rego.Module(imp, mod))
}
alreadyImported[imp] = true
// import explicitly required imports
for _, imp := range rule.Imports {
if imp == "" || alreadyImported[imp] {
continue
}
mod, err := importModule(imp, parentDir, true)
if err != nil {
return nil, "", err
}
if mod != "" {
options = append(options, rego.Module(imp, mod))
}
alreadyImported[imp] = true
}
return options, query, nil
}
func (r *regoCheck) compileRule(rule *compliance.RegoRule, ruleScope compliance.RuleScope, meta *compliance.SuiteMeta) error {
ctx := context.TODO()
moduleArgs := make([]func(*rego.Rego), 0, 2+len(regoBuiltins))
// rego modules and query
ruleModules, query, err := computeRuleModulesAndQuery(rule, meta)
if err != nil {
return err
}
moduleArgs = append(moduleArgs, ruleModules...)
moduleArgs = append(moduleArgs, rego.Query(query))
log.Debugf("rego query: %v", query)
// rego builtins
moduleArgs = append(moduleArgs, regoBuiltins...)
moduleArgs = append(
moduleArgs,
rego.EnablePrintStatements(true),
rego.PrintHook(®oPrintHook{}),
)
preparedEvalQuery, err := rego.New(
moduleArgs...,
).PrepareForEval(ctx)
if err != nil {
return err
}
r.preparedEvalQuery = preparedEvalQuery
r.ruleScope = ruleScope
return nil
}
func (r *regoCheck) buildNormalInput(env env.Env) (eval.RegoInputMap, error) {
objectsPerTags := make(map[string]interface{})
arraysPerTags := make(map[string][]interface{})
contextInput := r.buildContextInput(env)
objectsPerTags["context"] = contextInput
for _, input := range r.inputs {
resolve, _, err := resourceKindToResolverAndFields(env, r.ruleID, input.Kind())
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
resolved, err := resolve(ctx, env, r.ruleID, input.ResourceCommon, true)
if err != nil {
log.Warnf("failed to resolve input: %v", err)
continue
}
tagName := extractTagName(&input)
inputType, err := input.ValidateInputType()
if err != nil {
return nil, err
}
if _, present := objectsPerTags[tagName]; present {
return nil, fmt.Errorf("already defined tag: `%s`", tagName)
}
switch res := resolved.(type) {
case nil:
switch inputType {
case "array":
r.appendInstance(arraysPerTags, tagName, nil)
case "object":
objectsPerTags[tagName] = &struct{}{}
default:
return nil, fmt.Errorf("internal error, wrong input type `%s`", inputType)
}
case resolvedInstance:
switch inputType {
case "array":
r.appendInstance(arraysPerTags, tagName, res)
case "object":
objectsPerTags[tagName] = res.RegoInput()
default:
return nil, fmt.Errorf("internal error, wrong input type `%s`", inputType)
}
case eval.Iterator:
if inputType != "array" {
return nil, fmt.Errorf("the input kind `%s` does not support the `%s` type", string(input.Kind()), inputType)
}
// create an empty array as a base
// this is useful if the iterator is empty for example, as it will ensure we at least
// export an empty array to the rego input
if _, present := arraysPerTags[tagName]; !present {
arraysPerTags[tagName] = []interface{}{}
}
it := res
for !it.Done() {
instance, err := it.Next()
if err != nil {
return nil, err
}
r.appendInstance(arraysPerTags, tagName, instance)
}
}
}
input := make(map[string]interface{})
for k, v := range objectsPerTags {
input[k] = v
}
for k, v := range arraysPerTags {
if _, present := input[k]; present {
return nil, fmt.Errorf("multiple definitions of tag: `%s`", k)
}
input[k] = v
}
return input, nil
}
func extractTagName(input *compliance.RegoInput) string {
tagName := input.TagName
if tagName == "" {
return string(input.Kind())
}
return tagName
}
func (r *regoCheck) appendInstance(input map[string][]interface{}, key string, instance eval.Instance) {
vars, exists := input[key]
if !exists {
vars = []interface{}{}
}
if instance != nil {
input[key] = append(vars, instance.RegoInput())
}
}
func buildMappedInputs(inputs []compliance.RegoInput) map[string]compliance.RegoInput {
res := make(map[string]compliance.RegoInput)
for _, input := range inputs {
tagName := extractTagName(&input)
if _, present := res[tagName]; present {
log.Warnf("error building mapped input context: duplicated tag")
return nil
}
res[tagName] = input
}
return res
}
func roundTrip(inputs interface{}) (interface{}, error) {
output, err := yaml.Marshal(inputs)
if err != nil {
return nil, err
}
var res interface{}
if err := yaml.Unmarshal(output, &res); err != nil {
return nil, err
}
return res, nil
}
func (r *regoCheck) buildContextInput(env env.Env) eval.RegoInputMap {
context := make(map[string]interface{})
context["ruleID"] = r.ruleID
context["hostname"] = env.Hostname()
if r.ruleScope == compliance.KubernetesClusterScope {
context["kubernetes_cluster"], _ = env.KubeClient().ClusterID()
}
if r.ruleScope == compliance.KubernetesNodeScope {
context["kubernetes_node_labels"] = env.NodeLabels()
}
mappedInputs := buildMappedInputs(r.inputs)
if mappedInputs != nil {
preparedInputs, err := roundTrip(mappedInputs)
if err != nil {
log.Warnf("failed to build mapped inputs in context")
} else {
context["input"] = preparedInputs
}
}
return context
}
func findingsToReports(findings []regoFinding) []*compliance.Report {
var reports []*compliance.Report
for _, finding := range findings {
reportResource := compliance.ReportResource{
ID: finding.ResourceID,
Type: finding.ResourceType,
}
var report *compliance.Report
switch finding.Status {
case "error":
errMsg := finding.Data["error"]
if errMsg == nil {
errMsg = ""
}
err := fmt.Errorf("%v", errMsg)
report = &compliance.Report{
Resource: reportResource,
Passed: false,
Error: err,
Evaluator: regoEvaluator,
}
case "passed":
report = &compliance.Report{
Resource: reportResource,
Passed: true,
Data: finding.Data,
Evaluator: regoEvaluator,
}
case "failing":
report = &compliance.Report{
Resource: reportResource,
Passed: false,
Data: finding.Data,
Evaluator: regoEvaluator,
}
default:
return buildErrorReports(fmt.Errorf("unknown finding status: %s", finding.Status))
}
reports = append(reports, report)
}
return reports
}
func (r *regoCheck) check(env env.Env) []*compliance.Report {
log.Debugf("%s: rego check starting", r.ruleID)
var input eval.RegoInputMap
providedInput := env.ProvidedInput(r.ruleID)
if providedInput != nil {
input = providedInput
} else {
normalInput, err := r.buildNormalInput(env)
if err != nil {
return buildErrorReports(err)
}
input = normalInput
}
log.Debugf("rego eval input: %+v", input)
if path := env.DumpInputPath(); path != "" {
// if the dump failed we pass
_ = dumpInputToFile(r.ruleID, path, input)
}
ctx := context.TODO()
results, err := r.preparedEvalQuery.Eval(ctx, rego.EvalInput(input))
if err != nil {
return buildErrorReports(err)
} else if len(results) == 0 {
return nil
}
log.Debugf("%s: rego evaluation done => %+v\n", r.ruleID, results)
if len(results) == 0 || len(results[0].Expressions) == 0 {
return buildErrorReports(errors.New("failed to collect result expression"))
}
findings, err := parseFindings(results[0].Expressions[0].Value)
if err != nil {
return buildErrorReports(err)
}
reports := findingsToReports(findings)
log.Debugf("reports: %v", reports)
return reports
}
func dumpInputToFile(ruleID, path string, input interface{}) error {
currentData := make(map[string]interface{})
currentContent, err := ioutil.ReadFile(path)
if err == nil {
if len(currentContent) != 0 {
if err := json.Unmarshal(currentContent, ¤tData); err != nil {
return err
}
}
}
currentData[ruleID] = input
jsonData, err := PrettyPrintJSON(currentData, "\t")
if err != nil {
return err
}
return ioutil.WriteFile(path, jsonData, 0644)
}
type regoFinding struct {
Status string `mapstructure:"status"`
ResourceType string `mapstructure:"resource_type"`
ResourceID string `mapstructure:"resource_id"`
Data event.Data `mapstructure:"data"`
}
func parseFindings(regoData interface{}) ([]regoFinding, error) {
arrayData, ok := regoData.([]interface{})
if !ok {
return nil, errors.New("failed to parse array of findings")
}
res := make([]regoFinding, 0)
for _, data := range arrayData {
m, ok := data.(map[string]interface{})
if !ok {
return nil, errors.New("failed to parse finding")
}
var finding regoFinding
var decodeMetadata mapstructure.Metadata
if err := mapstructure.DecodeMetadata(m, &finding, &decodeMetadata); err != nil {
return nil, err
}
if err := checkFindingRequiredFields(&decodeMetadata); err != nil {
return nil, err
}
if err := checkFindingStatus(&finding); err != nil {
return nil, err
}
res = append(res, finding)
}
return res, nil
}
func checkFindingStatus(finding *regoFinding) error {
switch finding.Status {
case "passed", "failing", "error":
return nil
default:
return fmt.Errorf("unknown finding status: %s", finding.Status)
}
}
func checkFindingRequiredFields(metadata *mapstructure.Metadata) error {
requiredFields := make(map[string]bool)
requiredFields["status"] = false
for _, decodedField := range metadata.Keys {
if _, present := requiredFields[decodedField]; present {
requiredFields[decodedField] = true
}
}
for field, present := range requiredFields {
if !present {
return fmt.Errorf("missing field `%s` when decoding rego finding", field)
}
}
return nil
}
func buildErrorReports(err error) []*compliance.Report {
report := compliance.BuildReportForError(err)
report.Evaluator = regoEvaluator
return []*compliance.Report{report}
}
type regoPrintHook struct{}
func (h *regoPrintHook) Print(_ print.Context, value string) error {
log.Infof("Rego print output: %s", value)
return nil
}