Skip to content

Conversation

@fproulx-boostsecurity
Copy link
Contributor

@fproulx-boostsecurity fproulx-boostsecurity commented Oct 8, 2025

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:

  • Write embedded rules to temporary files at runtime
  • Manage filesystem paths and cleanup
  • Deal with potential permission issues and deployment complexity

This prevents truly self-contained binary deployments.

Solution

For Library Consumers

New constructor in opa/opa.go:

//go:embed rules
var CustomRules embed.FS

opa, err := poutineOpa.NewOpaWithEmbeddedRules(ctx, config, CustomRules)

For CLI Extensions

New exported variable in cmd/root.go:

//go:embed rules
var CustomRules embed.FS

func main() {
    poutineCmd.CustomEmbeddedRules = &CustomRules
    poutineCmd.Execute()
}

All commands (built-in and custom) automatically use the embedded rules.

Changes

opa/opa.go:

  • Added NewOpaWithEmbeddedRules() constructor for library usage
  • Modified Compile() to load custom embedded rules alongside built-in rules
  • Custom rules respect skip and allowed rule filters

cmd/root.go:

  • Added CustomEmbeddedRules exported variable
  • Updated newOpa() and newOpaWithConfig() to use embedded rules when set

Tests:

  • TestNewOpaWithEmbeddedRules - verifies custom rules load and execute
  • TestEmbeddedRulesWithSkipAndAllowed - confirms rule filtering works

Backward Compatibility

✅ Fully backward compatible - existing usage works unchanged

🤖 Generated with Claude Code

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]>
Copy link

Copilot AI left a 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.

fproulx-boostsecurity and others added 3 commits October 8, 2025 15:52
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]>
@fproulx-boostsecurity fproulx-boostsecurity changed the title Add support for embedding custom Rego rules in Poutine library Add support for embedding custom Rego rules (library and CLI) Oct 8, 2025
Copy link

Copilot AI left a 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]>
@fproulx-boostsecurity fproulx-boostsecurity merged commit e604ca1 into main Oct 9, 2025
11 checks passed
@fproulx-boostsecurity fproulx-boostsecurity deleted the feature/embedded-custom-rego-rules branch October 9, 2025 15:23
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.

3 participants