Skip to main content

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

This how-to guide shows how to embed Enterprise OPA into a Go application that uses OPA's SDK 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 SDK package

Import Enterprise OPA into the application with the following import and then override the options that your application provides when instantiating the SDK.

import eopa_sdk "github.com/styrainc/enterprise-opa-private/pkg/sdk"
       opts := eopa_sdk.DefaultOptions()
opts.ID = "eopa-test-1"
opts.Config = bytes.NewReader(config)
// create an instance of the OPA object
opa, err := sdk.New(ctx, sdk.Options{
ID: "opa-test-1",
Config: bytes.NewReader(config),
})
opa, err := sdk.New(ctx, opts)

The DefaultOptions enables the Enterprise OPA VM target globally as a side-effect.

Since Enterprise OPA's data source and decision log sink features are implemented as standard OPA plugins, there is no additional implementation required. To enable specific sources and sinks edit the configuration passed to the SDK. See the Configuration documentation for details.

Wrap up

This how-to guide showed how you can embed Enterprise OPA into a Go application that uses the SDK package. The sample code is hosted on GitHub. For additional examples see the examples/sdk directory from the tarball.