Open Policy Agent (OPA) Compilation Tools
March 13, 2025 ยท View on GitHub
Caution
This repository moved. Please use the new location: OpaDotNet
Open Policy Agent (OPA) Compilation Tools
Backend for packaging OPA policy and data files into bundles for OpaDotNet project.
NuGet Packages
| Package | |
|---|---|
| OpaDotNet.Compilation.Abstractions | |
| OpaDotNet.Compilation.Cli | |
| OpaDotNet.Compilation.Interop |
Getting Started
Which one you should be using?
Use OpaDotNet.Compilation.Cli if you have opa CLI tool installed or you need functionality besides compilation (running tests, syntax checking etc.). Suitable for web applications and/or applications running in Docker containers. See README for more details.
Use OpaDotNet.Compilation.Interop if you need compilation only and want to avoid having external dependencies. Suitable for libraries, console application etc. See README for more details.
For more information you can check the guide.
Cli
Install OpaDotNet.Compilation.Cli nuget package
dotnet add package OpaDotNet.Compilation.Cli
Usage
Important
You will need opa cli tool v0.20.0+ to be in your PATH or provide full path in RegoCliCompilerOptions.
using OpaDotNet.Compilation.Abstractions;
using OpaDotNet.Compilation.Cli;
IRegoCompiler compiler = new RegoCliCompiler();
var bundleStream = await compiler.CompileFile("example.rego", new[] { "example/hello" });
// Use compiled policy bundle.
...
Interop
Install OpaDotNet.Compilation.Interop nuget package
dotnet add package OpaDotNet.Compilation.Interop
Usage
using OpaDotNet.Compilation.Abstractions;
using OpaDotNet.Compilation.Interop;
IRegoCompiler compiler = new RegoInteropCompiler();
var bundleStream = await compiler.CompileFile("example.rego", new[] { "example/hello" });
// Use compiled policy bundle.
...
Building
Prerequisites
- go lang v1.20
- dotnet SDK 7.0
- opa cli
Linux (WSL)
gccto compileOpa.Interop.sogcc-mingw-w64to compileOpa.Interop.dll
Windows
Note
WSL 2.0 is required to compile Opa.Interop.so on windows.
- Powershell Core 7.0+
- WSL 2.0
Build and Test
- Run
build.ps1Compile Opa.Interop libraries - Run
dotnet buildto build the project or use Visual Studio to buildOpaDotNet.Compilation.sln - Run
dotnet testto test the project or use Visual Studio test explorer.
3rd Party Libraries and Contributions
- OPA SDK - High-level API for embedding OPA inside of Go programs.