-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
(*Rego).Partial()
with default values
#812
(*Rego).Partial()
with default values
#812
Conversation
These are not currently part of the results, so this "example test" should still pass just fine. Signed-off-by: Stephan Renatus <srenatus@chef.io>
Signed-off-by: Stephan Renatus <srenatus@chef.io>
Signed-off-by: Stephan Renatus <srenatus@chef.io>
@@ -538,6 +538,8 @@ func ExampleRego_Partial() { | |||
// Define a simple policy for example purposes. | |||
module := `package test | |||
|
|||
default allow = false |
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.
The default
keyword implies ordering. I.e., the default rule is only defined if none of the other rules with the same name are defined. In the general case, the partial evaluation has to generate support rules to handle this.
For the initial Compile API (and rego.Rego#Partial
) use cases where we're returning the partial evaluation result to the client, I think it's better if we do not have to deal with support rules.
In the short term, I think we can introduce a special case into partial evaluation where expressions like data.example.allow = true
do not require a support rule when data.example.allow
refers to a rule with a default value.
rego/example_test.go
Outdated
input["path"] = []string{"reviews", "alice"} | ||
|
||
r := rego.New( | ||
rego.Query("data.test.allow"), |
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.
In this case, OPA is going to have to generate support rules because this query is asking for the value of data.test.allow which implicitly says data.test.allow = x
--so we can't omit the default value like I described above.
Signed-off-by: Stephan Renatus <srenatus@chef.io>
input["path"] = []string{"reviews", "alice"} | ||
|
||
r := rego.New( | ||
rego.Query("data.test.allow == true"), |
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.
In this case, OPA is going to have to generate support rules because this query is asking for the value of data.test.allow which implicitly says data.test.allow = x--so we can't omit the default value like I described above.
Thanks, I wasn't aware of that distinction for a moment. However, having changed it, the output goes from
Query #1: neq(data.partial.test.allow, false)
Support #1: package partial.test
allow = true { true }
default allow = false
to
Query #1: equal(data.partial.test.allow, true)
Support #1: package partial.test
allow = true { true }
default allow = false
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.
Given the current implementation, that's what I would expect in this case--since allow
has a default rule, OPA blindly generates support rules. I'll file an issue to improve the behaviour for this relatively common case. I think we can improve this for the next release (v0.9) the Compile API.
For now, I would rather leave ExampleRego_Partial as is.
Filed #820 to track the improvements. Closing this for now, if you think we should revisit, let me know. |
Hi, sorry to bother on an old issue. I've searched around but I'm still a bit confused about this issue. Is it possible to add |
@ovidius72 it's possible but you will receive "support" rules from the Compile API depending on how you reference
The Compile API has to generate a support rule if the reference to |
Thanks @tsandall |
The result structure is described in this section of the docs: https://www.openpolicyagent.org/docs/latest/rest-api/#unconditional-results-from-partial-evaluation The result you posted ( |
This result seems a bit odd to me. First pushing the commit to show that it works now, and then the change to trigger the odd behaviour.
Concretely, when adding
to the test module, the output that was
becomes
So, it seems to be adding a layer of indirection.
To illustrate why this seems odd, I've added an example that is closer to our real-world usage, and its output is
it seems like this could be further reduced -- am I wrong? I'm not sure if this really is a variant of #709 .