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

feat: Raise error when problematic use of the if keyword is encountered #902

Merged
merged 1 commit into from
Jan 15, 2024

Conversation

jalseth
Copy link
Member

@jalseth jalseth commented Jan 7, 2024

Using the if keyword without also using the contains keyword makes the rule name in the OPA AST an emptry string, which causes conftest to inadvertently skip over tests leading to inaccurate results.

open-policy-agent/opa#6509

@jalseth jalseth requested a review from boranx January 10, 2024 03:18
@anderseknert
Copy link
Member

I guess this works, but since you have parsed the modules already, you could also iterate over the rules and check each rule head either for the presence of a name, or whether the ref contained has more than one element.

The AST representation of a policy like you use for testing:

package main

import future.keywords.if

deny[msg] if {
    msg := "foo"
}

would look something like this for the rule head:

"head": {
        "value": {
          "type": "boolean",
          "value": true
        },
        "ref": [
          {
            "type": "var",
            "value": "deny"
          },
          {
            "type": "var",
            "value": "msg"
          }
        ]
}

policy/engine.go Outdated Show resolved Hide resolved
@jalseth
Copy link
Member Author

jalseth commented Jan 13, 2024

@anderseknert thanks for the insight to the AST, it made it easy to set up without regexp.

Using the if keyword without also using the contains keyword makes the
rule name in the OPA AST an emptry string, which causes conftest to
inadvertently skip over tests leading to inaccurate results.

open-policy-agent/opa#6509

Signed-off-by: James Alseth <[email protected]>
Copy link
Member

@boranx boranx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@jalseth jalseth merged commit 50c3df5 into master Jan 15, 2024
7 checks passed
@jalseth jalseth deleted the warn-empty-rule-name branch January 15, 2024 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants