Skip to main content

How to integrate Enterprise OPA in a Go application with the Rego package

This how-to guide shows how to embed Enterprise OPA into a Go application and enable Enterprise 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

Then add the following lines to your application's go.mod file:

require github.com/styrainc/enterprise-opa-private <VERSION>
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 how-to guide 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.