-
Notifications
You must be signed in to change notification settings - Fork 30
Add support for embedding custom Rego rules (library and CLI) #355
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
Add support for embedding custom Rego rules (library and CLI) #355
Conversation
This enhancement allows library consumers (like pkg-supply and spicy-poutine) to embed their own custom Rego rules directly into their binaries alongside Poutine's built-in rules, creating fully self-contained deployments without filesystem dependencies. Changes: - Add NewOpaWithEmbeddedRules() constructor that accepts embed.FS containing custom rules - Add AddEmbeddedRules() method for adding rules to existing Opa instances - Modify Compile() to load custom embedded rules alongside built-in rules - Custom rules respect skip and allowed filters like filesystem-based rules - Fully backward compatible with existing NewOpa() usage Usage example: //go:embed rules/*.rego var CustomRules embed.FS opa, err := poutineOpa.NewOpaWithEmbeddedRules(ctx, config, CustomRules, "rules") 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for embedding custom Rego rules directly into binaries when consuming Poutine as a library, eliminating the need for filesystem dependencies and enabling truly self-contained deployments.
- Introduces
NewOpaWithEmbeddedRules()constructor for creating OPA instances with custom embedded rules - Adds
AddEmbeddedRules()method for adding custom rules to existing OPA instances - Modifies the
Compile()method to load custom embedded rules alongside built-in Poutine rules
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| opa/opa.go | Core implementation with new constructor, method, and compilation logic |
| opa/opa_test.go | Comprehensive test suite covering new functionality and rule filtering |
| opa/testdata/embedded/custom_rule.rego | Test rule for validating custom embedded rule functionality |
| opa/testdata/embedded/rules/skippable_rule.rego | Test rule for validating skip/allowed rule filtering with embedded rules |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Wrap errors from embed.FS.ReadFile() and fs.WalkDir() with context to satisfy wrapcheck linter for new code. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
- Remove AddEmbeddedRules() method (no clear use case) - Add CustomEmbeddedRules and CustomEmbeddedRulesRoot exported variables to cmd package - Update newOpa() and newOpaWithConfig() to use NewOpaWithEmbeddedRules when set - CLI extensions can now set poutineCmd.CustomEmbeddedRules before Execute() 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
The customRoot parameter was unnecessary implementation detail. Custom embedded rules are now always loaded from "." root. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Remove unnecessary index from prefix - just use "custom/" like "poutine/opa/" for built-in and "include/" for filesystem rules. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Summary
Adds support for embedding custom Rego rules when consuming Poutine as a library or extending it as a CLI. This allows projects like pkg-supply and spicy-poutine to bundle their own rules alongside Poutine's built-in rules in a single self-contained binary without filesystem dependencies.
Problem
Library consumers and CLI extensions who want to add custom Rego rules currently must:
This prevents truly self-contained binary deployments.
Solution
For Library Consumers
New constructor in
opa/opa.go:For CLI Extensions
New exported variable in
cmd/root.go:All commands (built-in and custom) automatically use the embedded rules.
Changes
opa/opa.go:
NewOpaWithEmbeddedRules()constructor for library usageCompile()to load custom embedded rules alongside built-in rulesskipandallowedrule filterscmd/root.go:
CustomEmbeddedRulesexported variablenewOpa()andnewOpaWithConfig()to use embedded rules when setTests:
TestNewOpaWithEmbeddedRules- verifies custom rules load and executeTestEmbeddedRulesWithSkipAndAllowed- confirms rule filtering worksBackward Compatibility
✅ Fully backward compatible - existing usage works unchanged
🤖 Generated with Claude Code