Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Only raise problematic if error when rule has no name set #935

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions policy/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ func TestProblematicIf(t *testing.T) {
desc: "No rules",
body: "",
},
{
desc: "Bare deny",
body: "deny { true }\n",
},
{
desc: "Rule not using if statement",
body: "deny[msg] {\n 1 == 1\nmsg := \"foo\"\n}\n",
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
Loading