Skip to content

Commit

Permalink
fix: Only raise problematic if error when rule has no name set
Browse files Browse the repository at this point in the history
If the rule has a name set, that means there isn't problematic
use of the 'if' keyword without 'contains' so we can skip those.

Signed-off-by: James Alseth <[email protected]>
  • Loading branch information
jalseth committed Mar 31, 2024
1 parent a12c536 commit eb96ee5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion policy/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ func problematicIf(modules map[string]*ast.Module) error {
// https://github.com/open-policy-agent/opa/issues/6509
for _, module := range modules {
for _, rule := range module.Rules {
if rule.Head == nil || rule.Head.Value == nil || len(rule.Head.Reference) == 0 {
if rule.Head == nil || rule.Head.Name != "" || rule.Head.Value == nil || len(rule.Head.Reference) == 0 {
continue
}
refName := rule.Head.Reference[0].Value.String()
Expand Down
5 changes: 5 additions & 0 deletions tests/problematic-if/policy/valid_bare_deny.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

deny {
true
}
8 changes: 8 additions & 0 deletions tests/problematic-if/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
[[ "$output" =~ "1 test, 0 passed, 0 warnings, 1 failure, 0 exceptions" ]]
}

@test "Bare deny rule can be used without contains or if" {
run $CONFTEST test --policy=policy/valid_bare_deny.rego data.yaml

[ "$status" -eq 0 ]
echo $output
[[ "$output" =~ "1 test, 1 passed, 0 warnings, 0 failures, 0 exceptions" ]]
}

@test "Error is raised when if is used without contains" {
run $CONFTEST test --policy=policy/invalid.rego data.yaml

Expand Down

0 comments on commit eb96ee5

Please sign in to comment.