Skip to content

Conversation

@dev-rice
Copy link
Contributor

@dev-rice dev-rice commented May 6, 2025

Continuing my trend of shoving more validation into OPA.Engine.prepareForEvaluation, this method now checks that the provided query can be evaluated. This should help catch misconfigurations earlier in application bootstrapping, without necessarily having to evaluate something first.

I think it's a bit of a deviation from how query preparation works with the Go implementation. And I could see this check becoming much more difficult or impossible to implement as query capabilities expand, so I won't be upset if this is rejected.

There are already some tests checking for this behavior, but they were expecting it to happen on evaluate. Now the error pops up during prepareForEvaluation, but the tests pass either way.

@dev-rice dev-rice force-pushed the check_query_during_preparation branch from adaeb74 to f6cea42 Compare May 6, 2025 23:57
patrick-east
patrick-east previously approved these changes May 8, 2025
Copy link
Contributor

@patrick-east patrick-east left a comment

Choose a reason for hiding this comment

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

I think it's a bit of a deviation from how query preparation works with the Go implementation. And I could see this check becoming much more difficult or impossible to implement as query capabilities expand

Yeah, things get a little blurry with the different evaluator types. The aim was to keep the top level APIs a little more agnostic of how a query got evaluated (IR interpreter, topdown evaluator, etc). There was a point in time on this where it would just give back "undefined" if a query that wasn't in the plan was specified, which is maybe more in line with the Go rego APIs.

That being said, if we are going to give back an error saying it isn't a query that can be supported, returning that in the prepare step makes a lot of sense to me.

}

// The above guard has already ensured that the policy has a plan for the given entrypoint, so force unwrap is safe.
return (policy, policy.plans[entrypoint]!)
Copy link
Contributor

Choose a reason for hiding this comment

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

⛏️ reworking this as a for loop to find and return in-line can eliminate the need for a force unwrap and reasoning about whether it is safe in favor of compile-time checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternate impl in 2cbbcea

self.policies = policies.map { IndexedIRPolicy(policy: $0) }
}

func findPlanAndPolicy(entrypoint: String) -> (IndexedIRPolicy, Plan)? {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this change warrant a dedicated test?

Copy link
Contributor Author

@dev-rice dev-rice May 9, 2025

Choose a reason for hiding this comment

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

It's still covered by IREvaluatorTests.testInvalidEvaluations because those test cases both prepare a query, then try to evaluate it. Before, the evaluation would fail, but now the query preparation step fails. I considered splitting out a distinct set of tests (for the Engine) around only query preparation to put tighter bounds around where the errors are thrown, but ultimately decided against it since it'd be duplicating most of what's covered in IREvaluatorTests.

What do you think?

Continuing my trend of shoving more validation into
`OPA.Engine.prepareForEvaluation`, this method now checks that the
provided query can be evaluated. This should help catch
misconfigurations earlier in application bootstrapping, without
necessarily having to evaluate something first.

I think it's a bit of a deviation from how query preparation works with
the Go implementation. And I could see this check becoming much more
difficult or impossible to implement as query capabilities expand, so I
won't be upset if this is rejected.

There are already some tests checking for this behavior, but they were
expecting it to happen on `evaluate`. Now the error pops up during
`prepareForEvaluation`, but the tests pass either way.

Signed-off-by: Chris Rice <[email protected]>
@dev-rice dev-rice force-pushed the check_query_during_preparation branch from f6cea42 to 2cbbcea Compare May 9, 2025 16:46
@dev-rice dev-rice requested review from patrick-east and shomron May 9, 2025 16:47
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