open-policy-agent / opa

Open Policy Agent (OPA) is an open source, general-purpose policy engine.
https://www.openpolicyagent.org
Apache License 2.0
9.75k stars 1.35k forks source link

Add `rego.Data` function to simply add data from map #5961

Open anderseknert opened 1 year ago

anderseknert commented 1 year ago

Having to create a storage object, and possibly a transaction, in order to just add some data for evaluation using the Go API is a chore, and not intuitive unless you happen to know about it. It would be great if we added a rego.Data function that would allow passing a map to populate the data object.

(There's a longer story around issues with transactions and concurrency, but I'll save that for another issue)

ashutosh-narkar commented 1 year ago

The proposed rego.Data similar to rego.Load, rego.LoadBundle and rego.ParsedBundle would require a transaction to be passed as well. Then should be just a write call on the store as part of preparing the query.

anderseknert commented 1 year ago

I'm using both rego.Load and rego.ParsedBundle without passing any transaction. I'm not sure I follow :)

ashutosh-narkar commented 1 year ago

I was referring to this comment where you set the store and that this would apply to rego.Data as well.

anderseknert commented 1 year ago

Ah, I see. Yes, that'd be the same, but the most important point here is that you shouldn't need to set the store or transactions unless you have special requirements that necessitate that, and just throwing some random data in for evaluation should not be one.

ashutosh-narkar commented 1 year ago

But then this would make it different than rego.ParsedBundle for instance since in the rego.ParsedBundle case you'd actually write the bundle contents to the store. But that's not how you imagine rego.Data working, correct?

anderseknert commented 1 year ago

No, I want it to be written to the store, just like when using those other helper functions 🙂

I just want to do so without the ceremony of having to provide storage and/or a transaction, just like the other helpers allow me to avoid. Example:

r := rego.New(
    rego.Query("data.example.users[0].likes"),
    rego.Data(map[string]any{"foo": "bar"}),
    rego.ParsedBundle(&b),
)
stale[bot] commented 1 year ago

This issue has been automatically marked as inactive because it has not had any activity in the last 30 days. Although currently inactive, the issue could still be considered and actively worked on in the future. More details about the use-case this issue attempts to address, the value provided by completing it or possible solutions to resolve it would help to prioritize the issue.

stale[bot] commented 6 months ago

This issue has been automatically marked as inactive because it has not had any activity in the last 30 days. Although currently inactive, the issue could still be considered and actively worked on in the future. More details about the use-case this issue attempts to address, the value provided by completing it or possible solutions to resolve it would help to prioritize the issue.