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

feat(opa): allow importing custom OPA modules in OPA policies #826

Merged
merged 4 commits into from
Dec 12, 2024

Conversation

nacx
Copy link
Contributor

@nacx nacx commented Dec 2, 2024

Description

This change allows importing existing OPA packages to be used in the main OPA policies.

In scenarios where several controls are implemented, it is desirable to have a way to define common functions that can be reused across them, to keep the main policies understandable and maintainable. OPA already provides a nice way of packaging modules, and this PR adds a modules configuration knob to the OPA provider to allow importing existing rego modules.

Related Issue

N/A

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Other (security config, docs update, etc)

Checklist before merging

@nacx nacx requested a review from a team as a code owner December 2, 2024 15:45
@brandtkeller brandtkeller added the open-source Contributed by non-maintainers label Dec 2, 2024
@nacx nacx changed the title Allow importing custom OPA modules in OPA policies feat(opa): allow importing custom OPA modules in OPA policies Dec 2, 2024
Copy link
Member

@brandtkeller brandtkeller left a comment

Choose a reason for hiding this comment

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

very minor schema adjustment (I'll file an issue to integrate schema validation more natively) otherwise this looks good.

src/pkg/common/schemas/validation.json Outdated Show resolved Hide resolved
brandtkeller
brandtkeller previously approved these changes Dec 3, 2024
Copy link
Member

@brandtkeller brandtkeller left a comment

Choose a reason for hiding this comment

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

Looks great. Appreciate the minor adjustments. Will get an additional set of eyes on this so that we can merge.

@meganwolf0
Copy link
Collaborator

Love this! Shout out to the first PR I put up for Lula that was attempting to do this (your implementation is much nicer!)

Pulled this down, tested, all lgtm, the only thing I might call out in the docs (or could probably be added in the code) is that validate.rego should be a protected key, I think it will override the policy if someone happens to specify a module key with the same text.

@nacx
Copy link
Contributor Author

nacx commented Dec 4, 2024

Thanks!
You're right. I can change the code to add the user modules first, then the validate.rego to make sure it is not messed up. WDYT?
I'm out until 11th, though, I'll do the changes once I'm back.

@meganwolf0
Copy link
Collaborator

You're right. I can change the code to add the user modules first, then the validate.rego to make sure it is not messed up.

Sounds great, plus the doc point that zirain pointed out should cover that edge case. Thank you!

Had another thought, and this certainly shouldn't block the current PR but more of a future point of expansion perhaps - if you are executing many validations that are all using the same common module it'll be repeatedly downloaded; might be nice to look at caching that data across validations somehow. Might add as a follow-on issue once this is merged 😄

@brandtkeller
Copy link
Member

You're right. I can change the code to add the user modules first, then the validate.rego to make sure it is not messed up.

Sounds great, plus the doc point that zirain pointed out should cover that edge case. Thank you!

Had another thought, and this certainly shouldn't block the current PR but more of a future point of expansion perhaps - if you are executing many validations that are all using the same common module it'll be repeatedly downloaded; might be nice to look at caching that data across validations somehow. Might add as a follow-on issue once this is merged 😄

I'd agree - I think we can look at accomplishing this from multiple levels. One is general file caching within Lula - or more appropriately how do we "package" everything for portability. The other is the module references which are not retained across validations. Feels like a few issues worth scoping separately.

@brandtkeller brandtkeller dismissed their stale review December 4, 2024 21:02

Impending changes required

nacx and others added 3 commits December 11, 2024 09:49
Signed-off-by: Ignasi Barrera <ignasi@tetrate.io>
Signed-off-by: Ignasi Barrera <nacx@apache.org>
Signed-off-by: Ignasi Barrera <nacx@apache.org>
@nacx
Copy link
Contributor Author

nacx commented Dec 11, 2024

Hi! I'm back and have addressed the review comments :)
I've opted to return a clear error if the validate.rego is used as a custom module name, to make sure validations don't run with an unexpected configuration leading to inaccurate results.

I agree caching would be great, but I'd like to keep that out of the scope of this PR.

Copy link
Member

@brandtkeller brandtkeller left a comment

Choose a reason for hiding this comment

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

Great catch on the reserved module name @meganwolf0

@brandtkeller brandtkeller merged commit d3a8690 into defenseunicorns:main Dec 12, 2024
10 checks passed
@nacx nacx deleted the opa-import branch December 12, 2024 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open-source Contributed by non-maintainers
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

4 participants