-
Couldn't load subscription status.
- Fork 49
Use rule short_failure_message as fallback for evaluation failure message #5933
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
base: main
Are you sure you want to change the base?
Use rule short_failure_message as fallback for evaluation failure message #5933
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this needs a rebase, but I like the idea, and I was wondering why we didn't do it earlier. 😁
I was also going to ask about whether we should do this for violations, but that already collects messages as part of its operation, so we don't need this kind of fallback, though we could replace "Evaluation failures:" with the short failure message if we wanted.
…sage In deny-by-default rego evaluations, when no custom message is provided by the rule writer, the evaluator now falls back to the rule's short_failure_message if available, instead of always defaulting to "denied". This provides better error messages to users when rule evaluations fail, as they will see the descriptive short_failure_message from the rule definition rather than a generic "denied" message. Changes: - Added shortFailureMessage field to denyByDefaultEvaluator struct - Modified parseResult to use cmp.Or() with short_failure_message as fallback before "denied" - Created WithShortFailureMessage() Option function to pass the message to the evaluator - Updated NewRuleEvaluator to pass short_failure_message option when creating rego evaluators Fixes mindersec#4718 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Add three new test functions to thoroughly test the short_failure_message fallback behavior in deny-by-default rego evaluations: - TestDenyByDefaultWithShortFailureMessage: Tests the complete fallback chain (custom message > short_failure_message > "denied") with 4 scenarios - TestDenyByDefaultShortFailureMessageOnlyAppliedToDenyByDefault: Verifies that the option is silently ignored for constraints evaluator - TestDenyByDefaultShortFailureMessageWithEntityName: Tests that entity names are properly included in error details Also enhanced the godoc for WithShortFailureMessage() to clearly document the fallback priority, when the option applies, and its type-specific behavior. All tests pass and linting is clean.
fce50a2 to
ceaca8c
Compare
| } | ||
| return jq.NewJQEvaluator(e.GetJq(), opts...) | ||
| case rego.RegoEvalType: | ||
| // Add short_failure_message as an option if available |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can actually store this always, since you use cmp.Or to combine this and the "denied".
Summary
In deny-by-default rego evaluations, when no custom message is provided by the rule writer, the evaluator now falls back to the rule's
short_failure_messageif available, instead of always defaulting to "denied".This provides better error messages to users when rule evaluations fail, as they will see the descriptive
short_failure_messagefrom the rule definition rather than a generic "denied" message.Changes
shortFailureMessagefield todenyByDefaultEvaluatorstructparseResultto usecmp.Or()withshort_failure_messageas fallback before "denied"WithShortFailureMessage()Option function to pass the message to the evaluatorNewRuleEvaluatorto passshort_failure_messageoption when creating rego evaluatorsExample
Before: Rule failures would show
"denied"After: Rule failures show the descriptive message from the rule type, e.g.:
"License file does not match the expected license type""Malicious package found""denied"only if noshort_failure_messageis definedTest Plan
Related Issues
Fixes #4718
🤖 Generated with Claude Code