Integrating in Go with the Rego package
Enterprise OPA is a drop-in replacement for OPA that speeds up policy evaluation by 40%, reduces memory usage by a factor of 10, and provides extra built-in functions for fetching data from SQL, DynamoDB, MongoDB, and more. This tutorial shows how to embed Enterprise OPA into a simple Go application and enable Enteprise OPA's VM target with the rego package.
Add the Go module dependency
Extract the Enterprise OPA tarball into your source directory.
mkdir -p eopa
tar xzvf /path/to/eopa.tar.gz –strip-component=1 -C eopa
Add the following lines to your go.mod file.
require github.com/styrainc/enterprise-opa-private v1.8.0
replace github.com/styrainc/enterprise-opa-private => ./eopa
Import the Enterprise OPA VM package
Add the following import to the Go application. You should add this import in the file(s) in your application that integrates with the rego package.
import eopa_vm "github.com/styrainc/enterprise-opa-private/pkg/rego_vm"
Enable the Enterprise OPA VM target
Enable Enterprise OPA's optimized evaluation engine by passing the
eopa_vm.Target
target when constructing the rego.Rego
object.
// Construct a Rego object that can be prepared or evaluated.
r := rego.New(
rego.Query(os.Args[2]),
- rego.Load([]string{os.Args[1]}, nil))
+ rego.Load([]string{os.Args[1]}, nil),
+ rego.Target(eopa_vm.Target),
+ )
// Create a prepared query that can be evaluated.
query, err := r.PrepareForEval(ctx)
Wrap up
This tutorial showed how you can embed Enterprise OPA into a Go application that
uses the rego
package. The sample code is hosted on
GitHub.
For additional examples see the examples/rego
directory from the tarball.