Skip to content

Commit 2251f80

Browse files
moredhelHamish Hutchings
authored andcommitted
Clean up the engine code to be a bit clearer.
- Refactor the terraform example Signed-off-by: Hamish Hutchings <hamish@drybrough.nl>
1 parent 4486092 commit 2251f80

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

examples/excludes/policy/deny.rego

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package main
22

33
exceptions = {"exception-name"}
44

5-
func_name_msg(name) = ret {
6-
ret := sprintf("Resource Name '%s' contains dashes", [name])
7-
}
8-
9-
deny_name[msg] {
5+
deny_name[result] {
106
input.resource[_][name]
117
contains(name, "-")
12-
msg := func_name_msg(name)
8+
msg := sprintf("Resource Name '%s' contains dashes", [name])
9+
result := {
10+
"msg": msg,
11+
"resource-name": name,
12+
}
1313
}
1414

1515
deny_resource_type[msg] {
@@ -18,10 +18,9 @@ deny_resource_type[msg] {
1818
msg := sprintf("Resource Type '%s' is invalid", [type])
1919
}
2020

21-
exclude_name[rules] {
22-
input.resource[_][name]
21+
exclude_name[attrs] {
2322
exceptions[name]
24-
rules := [func_name_msg(name)]
23+
attrs := [{"resource-name": name}]
2524
}
2625

2726
exception[rules] {

policy/engine.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -316,21 +316,29 @@ func (e *Engine) check(ctx context.Context, path string, config interface{}, nam
316316
}
317317

318318
result, err := json.Marshal(ruleResult.Metadata)
319-
localExcludeQuery := fmt.Sprintf("data.%s.exclude_%s[_][_] = %s", namespace, removeRulePrefix(rule), result)
320-
localExcludeQueryResult, err := e.query(ctx, config, localExcludeQuery)
321319
if err != nil {
322-
return output.CheckResult{}, fmt.Errorf("query exception: %w", err)
320+
return output.CheckResult{}, fmt.Errorf("json marshal: %w", err)
323321
}
324322

325-
// If the query was a failure, let's have a look & see if an exception was written for it.
326-
if len(localExcludeQueryResult.Results) > 0 {
327-
// append an exception & continue
328-
localExcludeResult := localExcludeQueryResult.Results[0]
329-
localExcludeResult.Message = localExcludeQuery
330-
excludes = append(excludes, localExcludeResult)
331-
continue
332-
}
323+
// If we have a non-null metadata response, then we are eligible to exclude the policy.
324+
// Otherwise we can just skip & process the policy violation
325+
if string(result) != "null" {
326+
localExcludeQuery := fmt.Sprintf("data.%s.exclude_%s[_][_] = %s", namespace, removeRulePrefix(rule), result)
327+
localExcludeQueryResult, err := e.query(ctx, config, localExcludeQuery)
328+
if err != nil {
329+
return output.CheckResult{}, fmt.Errorf("query exception: %w", err)
330+
}
333331

332+
// If the query was a failure, let's have a look & see if an exception was written for it.
333+
if len(localExcludeQueryResult.Results) > 0 {
334+
// append an exception & continue
335+
localExcludeResult := localExcludeQueryResult.Results[0]
336+
localExcludeResult.Message = localExcludeQuery
337+
excludes = append(excludes, localExcludeResult)
338+
continue
339+
}
340+
341+
}
334342
if isFailure(rule) {
335343
failures = append(failures, ruleResult)
336344
} else {

0 commit comments

Comments
 (0)