Skip to content

me-viper/OpaDotNet.Compilation

Repository files navigation

CI Coverage Status Static Badge

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 NuGet
OpaDotNet.Compilation.Cli NuGet
OpaDotNet.Compilation.Interop NuGet

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)

  • gcc to compile Opa.Interop.so
  • gcc-mingw-w64 to compile Opa.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.ps1 Compile Opa.Interop libraries
  • Run dotnet build to build the project or use Visual Studio to build OpaDotNet.Compilation.sln
  • Run dotnet test to 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.