Skip to content

Refinement of Output for Mequal and Automated Policy Bundle Documentatation Generation #9

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

Merged
merged 32 commits into from
May 15, 2025

Conversation

mertbugrabicak
Copy link
Contributor

@mertbugrabicak mertbugrabicak commented May 7, 2025

First off sorry for the gigantic PR!

The output we eventually wanted to get required doing some data processing before runtime, where we use OPA to generate objects containing the policy annotations in the source code, map them to a clean JSON, and then include them in the Mequal bundle for policies to make use of.

So the main changes:

  • Script to generate annotation objects using OPA inspect -a
  • Script to map these annotation object fields to one clean json file to show all metadata
  • Create and inject this json into the bundle upon container build
  • Add a "main" bundle to dynamically collect and show results for all bundles included in Mequal
  • eval.rego to get evaluations of all the policies for a given input
  • full.rego to get both the evaluations and a list of all policies ran on said input
  • Add possibility to define severity, level, and any other custom fields in policies and rules via metadata annotations
  • Edit policies to reflect/use the additional metadata fields
  • Edit policies to remove the leveling at the directory level, since no longer will be done this way.
  • Define external policy bundles included into Mequal using a json config file
  • Script to download all external policy bundles to put into Mequal
  • Script to generate AsciiDocs of the Mequal policies to be published in the documentation
  • Github action to automatically generate and publish Mequal policy bundle documentation to be put on the Mequal website

mertbugrabicak and others added 25 commits April 11, 2025 13:09
@mertbugrabicak mertbugrabicak marked this pull request as ready for review May 12, 2025 12:35
&& mkdir ./bundle

RUN adduser -u 1000 -U mequal -m

USER root
COPY ./policy/ ./policy/
COPY ./hack/ ./hack/
Copy link
Contributor

Choose a reason for hiding this comment

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

I think of hack as scripts to support building and expect to run locally, these are ran as part of the container image build, we should perhaps re-purpose local-opa-run.sh to be ran at container build time, rather than maintain two seperate approaches to building the policy bundles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you're absolutely right about this. I'll move the scripts that the image depends on to a scripts folder, and in hack I can create scripts to prep the development environment by running those scripts locally. It currently relies on building and running the image for development and testing, since inside the image it generates that metadata object to be used.

With something like dev-setup.sh, we generate the metadata object and put it into where it would be in the directory, so we can then use OPA locally to do our development. This script can also include downloading the helper functions to be used, so we can remove them from the code at least and let it live in another repo. (currently each policy repo has them copied over as a temporary solution, which is not good at all)

Both can then be marked by gitignore so they don't end up making it into the source code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Separated them into separate scripts folder

@@ -0,0 +1,222 @@
# Generated with assistance from a large language model trained by Google.
Copy link
Contributor

Choose a reason for hiding this comment

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

Add #!/bin/env python and set executable, I'm not sure about the current header, it doesn't give much information or attribution about what was used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I can do that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done as #!/usr/bin/env python3

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Forgot to address the LLM comment: Gemini by default isn't really attaching any info to the code it generates, and I just asked how to credit it. And it said to do this way since versioning may differ in the background if a specific model etc is specified. So I just kept it broad like this

@@ -0,0 +1,4 @@
{
"revision": "v1.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't need fixing just yet as the container image contains the git sha but I created a PR for a temporary solution to having this also set in the policy manifest as I think we will forget to update this and it may catch us out if we distribute policies by alternative methods - mertbugrabicak#2 but feel free to ignore

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yes, this was something I wanted to do but forgot! We can definitely inject it during image build.

For the mequal bundle versioning, we can use our git sha, and for the external policies we define in our config json, I can create a revision field where we can concretely define what sha we are fetching the external policy from, and then inject those shas to their respective manifests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will take in your changes and work from there

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Merged your changes, will now expand it to include external policies

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will leave this for now, to think of a better solution if possible for the external policy revisions

import rego.v1

# Main evaluation rule to generate the desired "evaluation" output object
evaluation := {
Copy link
Contributor

Choose a reason for hiding this comment

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

opa fmt changes this but as far as I can see there are no functional or syntax changes, only formatting, given we want to use regal in future I don't think we need to apply the changes but feel free

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I should be able to do this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Merged fine

jonathanchristison and others added 7 commits May 13, 2025 10:12
In this commit:

* Added Gemini generated script to use current git revision as .manifest
  version
* Call this script before building policy bundle in container
In this commit:

* Allow overrides to the manifest revision
* Update readme for building
* Update github ci to override manifest revision
In this commit:

* `opa fmt policy --v1-compatible -w`
@mertbugrabicak
Copy link
Contributor Author

  • Moved the scripts used during image build to a ./scripts/ directory
  • Added dev-setup.sh and dev-cleanup.sh to automatically get local dev environment ready.
  • Moved helper functions to own directory where it is fetched from there now:
    https://github.com/project-ncl/mequal-helpers
  • .gitignore for dev related stuff, external bundles pulled, etc
  • Added shebang to python script
  • Optimized the container for Mequal to reduce to half its size

@mertbugrabicak
Copy link
Contributor Author

@jonathanchristison if you can have another look tomorrow, hopefully it should be mergeable now. it's it a lot better shape

@mertbugrabicak
Copy link
Contributor Author

There is also a TODO to adapt the local opa run script to our latest bundle structure, i might also work on that tomorrow, but that can also wait

@jonathanchristison
Copy link
Contributor

@mertbugrabicak looks good to me and some nice additions with local scripts and reduced runtime image size 👍

@jonathanchristison jonathanchristison merged commit f3ba37a into project-ncl:main May 15, 2025
3 checks passed
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.

2 participants