microsoft / regorus

Regorus - A fast, lightweight Rego (OPA policy language) interpreter written in Rust.
MIT License
130 stars 31 forks source link

Provide ability to get JSON representation of policy AST #266

Closed anakrish closed 3 months ago

anakrish commented 4 months ago

closes #265

anakrish commented 4 months ago

@thedavemarshall Can you try out this PR to see if it helps your usecase?

thedavemarshall commented 3 months ago

@anakrish this is super useful, thank you!

This unblocks the use case of deconstructing REGO documents into a format that can be easily be manipulated by a Ruby application!

Is there a recommended way load a JSON AST representation directly into the engine? Else I can make a simple parser in Ruby to turn the AST JSON representation into a REGO document and then load that into the engine. Either way, this is great!

I sketched out a couple tests for the ruby biniding get_ast_as_json_ast . If you think the AST representation is stable enough to assert against, I can open a PR to add https://github.com/microsoft/regorus/commit/595cb2eed0e7818aaeda55ce5573248eda60b292 , or feel free to copy/paste those tests into this PR!

anakrish commented 3 months ago

@thedavemarshall Thanks for trying out the PR. Great that it helps with the Ruby use case.

I have also added a version field. I will go ahead and merge it to make progress. We can refine it in subsequent PRs.

Is there a recommended way load a JSON AST representation directly into the engine?

Currently no. Many AST nodes store a Span and the span holds a reference to the Source. To avoid duplication, we don't serialize the Source with each span. To handle this, we need a special deserializer than can set the missing Source reference correctly.

Also, I'm not sure if the Span can be created accurately unless the AST is created via parsing.

Else I can make a simple parser in Ruby to turn the AST JSON representation into a REGO document and then load that into the engine.

Generating Rego from the AST and then supplying it back to the engine is a good approach.

I sketched out a couple tests for the ruby biniding get_ast_as_json_ast . If you think the AST representation is stable enough to assert against, I can open a PR to add https://github.com/microsoft/regorus/commit/595cb2eed0e7818aaeda55ce5573248eda60b292

Yes, please submit a PR.