OpaDotNet.Wasm 1.1.0-preview0078

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 1.1.0-preview0078
                    
NuGet\Install-Package OpaDotNet.Wasm -Version 1.1.0-preview0078
                    
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="1.1.0-preview0078" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpaDotNet.Wasm" Version="1.1.0-preview0078" />
                    
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 1.1.0-preview0078
                    
#r "nuget: OpaDotNet.Wasm, 1.1.0-preview0078"
                    
#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@1.1.0-preview0078
                    
#: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=1.1.0-preview0078&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=OpaDotNet.Wasm&version=1.1.0-preview0078&prerelease
                    
Install as a Cake Tool

CI NuGet Coverage Status

Open Policy Agent (OPA) WebAssembly dotnet core SDK

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

Initial implementation was based on Open Policy Agent WebAssemby NPM Module

For more information check out the guide.

Supported ABI

Version Status
1.0 ✔️
1.2 ✔️
1.3 ✔️

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;

var factory = new OpaEvaluatorFactory();

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

using var engine = factory.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)
{
    // 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. See ./samples for a more comprehensive example.

See opa build --help for more details.

With OpaDotNet.Wasm.Compilation

You can use SDK to do compilation for you.

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

using OpaDotNet.Wasm;
using OpaDotNet.Wasm.Compilation;

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

// Use compiled policy.
var factory = new OpaEvaluatorFactory();

using var engine = factory.CreateFromBundle(policyStream);

3rd Party Libraries and Contributions

  • OPA - An open source, general-purpose policy engine that unifies policy enforcement across the stack.
  • Moq - The most popular and friendly mocking library for .NET.
  • xUnit.net - Free, open source, community-focused unit testing tool for the .NET Framework.
  • wasmtime-dotnet - .NET embedding of Wasmtime.
  • IPNetwork2 - Utility classes take care of complex network, IPv4, IPv6, CIDR calculation for .NET developers.
  • BenchmarkDotNet - Powerful .NET library for benchmarking.
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,101 3/11/2025
3.0.0-preview-0040 234 3/5/2025
3.0.0-preview-0027 95 2/26/2025
3.0.0-preview-0004 124 11/10/2024
3.0.0-preview-0001 115 10/25/2024
3.0.0-preview-0000 102 10/25/2024
2.5.1 996 11/8/2024
2.5.0 1,114 2/12/2024
2.4.1 184 1/26/2024
2.4.1-pre0004 115 1/23/2024
2.4.0 221 1/10/2024
2.3.0 264 11/21/2023
2.2.0 234 10/11/2023
2.2.0-preview0010 183 10/9/2023
2.2.0-preview0008 185 10/3/2023
2.1.1 203 9/29/2023
2.1.0 210 9/28/2023
2.0.0 345 8/18/2023
2.0.0-preview0012 173 8/18/2023
2.0.0-preview 182 8/17/2023
1.4.0 278 8/15/2023
1.3.0 225 8/9/2023
1.3.0-preview0010 193 8/7/2023
1.2.1 255 7/27/2023
1.2.0 266 7/26/2023
1.2.0-preview0033 194 7/25/2023
1.2.0-preview0028 180 7/24/2023
1.2.0-preview0012 210 7/20/2023
1.2.0-preview0009 192 7/19/2023
1.2.0-preview0007 192 7/19/2023
1.1.0 241 7/13/2023
1.1.0-preview0078 213 7/13/2023
1.1.0-preview0076 165 7/13/2023
1.1.0-preview0073 169 7/13/2023
1.1.0-preview0068 201 7/12/2023
1.1.0-preview0067 222 7/12/2023
1.1.0-preview0065 220 7/12/2023
1.1.0-preview0063 197 7/12/2023
1.1.0-preview0061 218 7/11/2023
1.1.0-preview0057 182 7/11/2023
1.1.0-preview0053 176 7/11/2023
1.1.0-preview0049 165 7/10/2023
1.1.0-preview0047 177 7/10/2023
1.1.0-preview0018 205 7/6/2023
1.1.0-preview0015 182 7/6/2023
1.1.0-preview0012 191 7/6/2023
1.1.0-preview0011 193 7/5/2023
1.1.0-preview0010 199 7/5/2023
1.1.0-preview0009 184 7/5/2023
1.1.0-preview0007 172 7/3/2023
1.0.0 230 6/27/2023
1.0.0-preview0059 198 6/27/2023
1.0.0-preview0058 190 6/27/2023
1.0.0-preview0056 187 6/27/2023
1.0.0-preview0049 180 6/23/2023
1.0.0-preview0048 181 6/23/2023
1.0.0-preview0047 175 6/23/2023
1.0.0-preview0045 189 6/23/2023
1.0.0-preview0043 186 6/23/2023
1.0.0-preview0041 143 6/22/2023
1.0.0-preview0040 191 6/22/2023
1.0.0-preview0037 167 6/22/2023
1.0.0-preview0036 184 6/22/2023
1.0.0-preview0032 195 6/22/2023
1.0.0-preview0031 188 6/22/2023
1.0.0-preview0029 175 6/21/2023
1.0.0-preview0027 191 6/21/2023
1.0.0-preview0025 183 6/21/2023
1.0.0-preview0023 198 6/21/2023
1.0.0-preview0021 153 6/20/2023
1.0.0-preview0012 155 6/20/2023
1.0.0-preview0011 191 6/20/2023