Skip to content
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

Provide ability to get JSON representation of policy AST #266

Merged
merged 1 commit into from
Jun 9, 2024

Conversation

anakrish
Copy link
Collaborator

@anakrish anakrish commented Jun 6, 2024

closes #265

@anakrish
Copy link
Collaborator Author

anakrish commented Jun 6, 2024

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

@anakrish anakrish force-pushed the ast-json branch 3 times, most recently from 75b058d to ef45c50 Compare June 6, 2024 00:42
@thedavemarshall
Copy link
Contributor

@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 595cb2e , or feel free to copy/paste those tests into this PR!

@anakrish
Copy link
Collaborator Author

anakrish commented Jun 9, 2024

@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 595cb2e

Yes, please submit a PR.

@anakrish anakrish merged commit df98c8d into microsoft:main Jun 9, 2024
15 checks passed
@anakrish anakrish deleted the ast-json branch June 9, 2024 01:58
@github-actions github-actions bot mentioned this pull request Jun 19, 2024
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.

Allow ability to introspect AST of policies
2 participants