OpaDotNet.Wasm 2.0.0-preview

This is a prerelease version of OpaDotNet.Wasm.
There is a newer version of this package available.
See the version list below for details.
dotnet add package OpaDotNet.Wasm --version 2.0.0-preview
                    
NuGet\Install-Package OpaDotNet.Wasm -Version 2.0.0-preview
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpaDotNet.Wasm" Version="2.0.0-preview" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpaDotNet.Wasm" Version="2.0.0-preview" />
                    
Directory.Packages.props
<PackageReference Include="OpaDotNet.Wasm" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpaDotNet.Wasm --version 2.0.0-preview
                    
#r "nuget: OpaDotNet.Wasm, 2.0.0-preview"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OpaDotNet.Wasm@2.0.0-preview
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OpaDotNet.Wasm&version=2.0.0-preview&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=OpaDotNet.Wasm&version=2.0.0-preview&prerelease
                    
Install as a Cake Tool

Open Policy Agent (OPA) WebAssembly dotnet core SDK

This is SDK for using WebAssembly (wasm) compiled Open Policy Agent policies with dotnet core.

Initial implementation was based on Open Policy Agent WebAssemby NPM Module

For more information check out the guide.

Key Features

  • Fast in-process OPA policies evaluation.
  • Full ABI support.
  • Additional OPA built-ins.
  • Compilation.

Getting Started

Install nuget package

dotnet add package OpaDotNet.Wasm

Usage

To evaluate OPA policy you need to:

Load compiled policy

using using OpaDotNet.Wasm;

const string data = "{ \"world\": \"world\" }";

using var engine = OpaEvaluatorFactory.CreateFromWasm(
    File.OpenRead("policy.wasm")
    );

engine.SetDataFromRawJson(data);

Evaluate policy

IOpaEvaluator has several APIs for policy evaluation:

  • EvaluatePredicate - Evaluates named policy with specified input. Response interpreted as simple true/false result.
  • Evaluate - Evaluates named policy with specified input.
  • EvaluateRaw - Evaluates named policy with specified raw JSON input.
var policyResult = engine.EvaluatePredicate(inp);

Check result

if (policyResult.Result)
{
    // We've been authorized.
}
else
{
    // Can't do that.
}

Writing policy

See writing policy

Compiling policy

You have several options to compile rego policy into wasm module:

package example

default hello = false

hello {
    x := input.message
    x == data.world
}

Manually

Either use the Compile REST API or opa build CLI tool.

For example, with OPA v0.20.5+:

opa build -t wasm -e example/hello example.rego

Which is compiling the example.rego policy file. The result will be an OPA bundle with the policy.wasm binary included. comprehensive example.

See opa build --help for more details.

With OpaDotNet.Compilation

You can use SDK to do compilation for you.

OpaDotNet.Compilation.Cli

Important. You will need opa cli tool to be in your PATH or provide full path in RegoCliCompilerOptions.

dotnet add package OpaDotNet.Compilation.Cli
using OpaDotNet.Wasm;
using OpaDotNet.Compilation.Cli;

var compiler = new RegoCliCompiler();
var policyStream = await compiler.CompileFile("example.rego", new[] { "example/hello" });

// Use compiled policy.
using var engine = OpaEvaluatorFactory.CreateFromBundle(policyStream);
OpaDotNet.Compilation.Interop
dotnet add package OpaDotNet.Compilation.Interop
using OpaDotNet.Wasm;
using OpaDotNet.Compilation.Interop;

var compiler = new RegoInteropCompiler();
var policyStream = await compiler.CompileFile("example.rego", new[] { "example/hello" });

// Use compiled policy.
using var engine = OpaEvaluatorFactory.CreateFromBundle(policyStream);
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on OpaDotNet.Wasm:

Package Downloads
OpaDotNet.Extensions.AspNetCore

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.1 1,887 3/11/2025
3.0.0-preview-0040 284 3/5/2025
3.0.0-preview-0027 132 2/26/2025
3.0.0-preview-0004 175 11/10/2024
3.0.0-preview-0001 163 10/25/2024
3.0.0-preview-0000 146 10/25/2024
2.5.1 1,087 11/8/2024
2.5.0 1,171 2/12/2024
2.4.1 236 1/26/2024
2.4.1-pre0004 143 1/23/2024
2.4.0 298 1/10/2024
2.3.0 325 11/21/2023
2.2.0 281 10/11/2023
2.2.0-preview0010 241 10/9/2023
2.2.0-preview0008 237 10/3/2023
2.1.1 250 9/29/2023
2.1.0 263 9/28/2023
2.0.0 440 8/18/2023
2.0.0-preview0012 214 8/18/2023
2.0.0-preview 220 8/17/2023
1.4.0 346 8/15/2023
1.3.0 274 8/9/2023
1.3.0-preview0010 232 8/7/2023
1.2.1 314 7/27/2023
1.2.0 333 7/26/2023
1.2.0-preview0033 228 7/25/2023
1.2.0-preview0028 194 7/24/2023
1.2.0-preview0012 256 7/20/2023
1.2.0-preview0009 236 7/19/2023
1.2.0-preview0007 218 7/19/2023
1.1.0 293 7/13/2023
1.1.0-preview0078 245 7/13/2023
1.1.0-preview0076 193 7/13/2023
1.1.0-preview0073 198 7/13/2023
1.1.0-preview0068 235 7/12/2023
1.1.0-preview0067 257 7/12/2023
1.1.0-preview0065 259 7/12/2023
1.1.0-preview0063 240 7/12/2023
1.1.0-preview0061 256 7/11/2023
1.1.0-preview0057 211 7/11/2023
1.1.0-preview0053 200 7/11/2023
1.1.0-preview0049 199 7/10/2023
1.1.0-preview0047 218 7/10/2023
1.1.0-preview0018 236 7/6/2023
1.1.0-preview0015 227 7/6/2023
1.1.0-preview0012 225 7/6/2023
1.1.0-preview0011 229 7/5/2023
1.1.0-preview0010 235 7/5/2023
1.1.0-preview0009 224 7/5/2023
1.1.0-preview0007 205 7/3/2023
1.0.0 276 6/27/2023
1.0.0-preview0059 237 6/27/2023
1.0.0-preview0058 236 6/27/2023
1.0.0-preview0056 227 6/27/2023
1.0.0-preview0049 215 6/23/2023
1.0.0-preview0048 221 6/23/2023
1.0.0-preview0047 215 6/23/2023
1.0.0-preview0045 235 6/23/2023
1.0.0-preview0043 226 6/23/2023
1.0.0-preview0041 186 6/22/2023
1.0.0-preview0040 235 6/22/2023
1.0.0-preview0037 210 6/22/2023
1.0.0-preview0036 229 6/22/2023
1.0.0-preview0032 229 6/22/2023
1.0.0-preview0031 239 6/22/2023
1.0.0-preview0029 219 6/21/2023
1.0.0-preview0027 234 6/21/2023
1.0.0-preview0025 230 6/21/2023
1.0.0-preview0023 233 6/21/2023
1.0.0-preview0021 177 6/20/2023
1.0.0-preview0012 188 6/20/2023
1.0.0-preview0011 222 6/20/2023