opa

package
v0.0.0-...-54f4ef6 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package opa provides OPA-based HTTP request authorization middleware.

Index

Constants

View Source
const DefaultPolicy = `` /* 145-byte string literal not displayed */

DefaultPolicy allows all requests from localhost and restricts remote access to non-admin paths.

Variables

This section is empty.

Functions

func Middleware

func Middleware(ctx context.Context, cfg Config, next http.Handler) (http.Handler, error)

Middleware returns an http.Handler that evaluates OPA policy before delegating to next. The policy must define a boolean "allow" rule under package cachew.authz. If allow is true the request proceeds; otherwise it is rejected with 403.

func RunTests

func RunTests(ctx context.Context, cfg Config) (int, error)

RunTests compiles the configured policy together with the Rego test module in cfg.Test and executes every test_* rule. It returns the number of tests that passed and an error enumerating any that failed or errored. When cfg.Test is empty it is a no-op. The policy under test is loaded the same way as Middleware, so an empty policy config exercises DefaultPolicy.

Types

type Config

type Config struct {
	Policy     string `hcl:"policy,optional" help:"Inline Rego policy."`
	PolicyFile string `hcl:"policy-file,optional" help:"Path to a Rego policy file."`
	Data       string `hcl:"data,optional" help:"Inline JSON object loaded as OPA data.*"`
	DataFile   string `hcl:"data-file,optional" help:"Path to a JSON file loaded as OPA data.*"`
	Test       string `hcl:"test,optional" help:"Inline Rego test module run against the policy when cachewd starts."`
}

Config for OPA policy evaluation. If neither Policy nor PolicyFile is set, a default policy allowing only GET and HEAD requests is used.

Jump to

Keyboard shortcuts

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