OpaDotNet.Wasm 2.5.0

dotnet add package OpaDotNet.Wasm --version 2.5.0                
NuGet\Install-Package OpaDotNet.Wasm -Version 2.5.0                
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.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpaDotNet.Wasm --version 2.5.0                
#r "nuget: OpaDotNet.Wasm, 2.5.0"                
#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.
// Install OpaDotNet.Wasm as a Cake Addin
#addin nuget:?package=OpaDotNet.Wasm&version=2.5.0

// Install OpaDotNet.Wasm as a Cake Tool
#tool nuget:?package=OpaDotNet.Wasm&version=2.5.0                

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

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. See ./samples for a more comprehensive example.

See opa build --help for more details.

With OpaDotNet.Compilation

You can use SDK to do compilation for you. For more information see OpaDotNet.Compilation.

OpaDotNet.Compilation.Cli
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 is compatible.  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. 
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

OpaDotNet.Wasm for AspNetCore applications

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.5.0 289 2/12/2024
2.4.1 119 1/26/2024
2.4.1-pre0004 76 1/23/2024
2.4.0 145 1/10/2024
2.3.0 212 11/21/2023
2.2.0 163 10/11/2023
2.2.0-preview0010 125 10/9/2023
2.2.0-preview0008 104 10/3/2023
2.1.1 128 9/29/2023
2.1.0 136 9/28/2023
2.0.0 238 8/18/2023
2.0.0-preview0012 103 8/18/2023
2.0.0-preview 99 8/17/2023
1.4.0 180 8/15/2023
1.3.0 135 8/9/2023
1.3.0-preview0010 116 8/7/2023
1.2.1 156 7/27/2023
1.2.0 163 7/26/2023
1.2.0-preview0033 117 7/25/2023
1.2.0-preview0028 109 7/24/2023
1.2.0-preview0012 125 7/20/2023
1.2.0-preview0009 112 7/19/2023
1.2.0-preview0007 115 7/19/2023
1.1.0 148 7/13/2023
1.1.0-preview0078 121 7/13/2023
1.1.0-preview0076 87 7/13/2023
1.1.0-preview0073 99 7/13/2023
1.1.0-preview0068 115 7/12/2023
1.1.0-preview0067 134 7/12/2023
1.1.0-preview0065 117 7/12/2023
1.1.0-preview0063 118 7/12/2023
1.1.0-preview0061 137 7/11/2023
1.1.0-preview0057 101 7/11/2023
1.1.0-preview0053 95 7/11/2023
1.1.0-preview0049 83 7/10/2023
1.1.0-preview0047 99 7/10/2023
1.1.0-preview0018 117 7/6/2023
1.1.0-preview0015 112 7/6/2023
1.1.0-preview0012 117 7/6/2023
1.1.0-preview0011 109 7/5/2023
1.1.0-preview0010 114 7/5/2023
1.1.0-preview0009 115 7/5/2023
1.1.0-preview0007 101 7/3/2023
1.0.0 134 6/27/2023
1.0.0-preview0059 120 6/27/2023
1.0.0-preview0058 109 6/27/2023
1.0.0-preview0056 105 6/27/2023
1.0.0-preview0049 95 6/23/2023
1.0.0-preview0048 94 6/23/2023
1.0.0-preview0047 85 6/23/2023
1.0.0-preview0045 110 6/23/2023
1.0.0-preview0043 100 6/23/2023
1.0.0-preview0041 80 6/22/2023
1.0.0-preview0040 107 6/22/2023
1.0.0-preview0037 86 6/22/2023
1.0.0-preview0036 96 6/22/2023
1.0.0-preview0032 105 6/22/2023
1.0.0-preview0031 109 6/22/2023
1.0.0-preview0029 93 6/21/2023
1.0.0-preview0027 104 6/21/2023
1.0.0-preview0025 106 6/21/2023
1.0.0-preview0023 117 6/21/2023
1.0.0-preview0021 77 6/20/2023
1.0.0-preview0012 92 6/20/2023
1.0.0-preview0011 111 6/20/2023