regobrick

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

RegoBrick

RegoBrick provides a simple way to parse and transform Rego modules without modifying the OPA engine.
It applies certain transformations based on special import markers, such as import data.regobrick.default_false.


Overview

  • Default False: If your Rego module imports data.regobrick.default_false, RegoBrick will automatically insert a default rule that evaluates to false for any “if” or boolean rules.
  • Additional features may be introduced in the future.

Installation

go get github.com/sky1core/regobrick

Ensure you have OPA as well, and that your go.mod is properly set up.


Usage

In your Go code, use RegoBrick’s module-transform function before running queries with OPA:

import (
    "github.com/open-policy-agent/opa/v1/rego"
    "github.com/sky1core/regobrick"
)

func main() {
    // Your Rego source
    code := `
        package example

        import data.regobrick.default_false

        allow if {
            input.user == "admin"
        }
    `

    // Apply transformations (e.g. default_false) via RegoBrick,
    // then pass the resulting module to OPA.
    query, err := rego.New(
        regobrick.Module("example.rego", code),
        rego.Query("data.example.allow"),
    ).PrepareForEval(ctx)
    
    // ...
}
  • If import data.regobrick.default_false is present, RegoBrick injects a default rule that sets the if or boolean rule to false if conditions aren’t met.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Module

func Module(filename, input string) func(r *rego.Rego)

Module returns a function conforming to `func(*rego.Rego)`. Internally, it tries to parse & transform the Rego code via ParseModule. If parsing fails, it falls back to providing the **original code** to OPA, so that OPA will raise a compile error at evaluation time.

TODO: If you prefer to detect errors earlier, call ParseModule(...) directly and handle them before using Module(...). This design just defers the error to OPA's compile phase instead of returning it immediately.

func ParseModule

func ParseModule(filename, input string) (*ast.Module, error)

ParseModule parses and transforms the given Rego source.

  • If parsing fails, returns an error.
  • If an import marker like "data.regobrick.default_false" exists, applies the corresponding AST transformation (e.g. addDefaultFalse).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL