Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/matrix-org/rust-opa-wasm
Open Policy Agent WebAssembly Rust SDK
https://github.com/matrix-org/rust-opa-wasm
Last synced: 6 days ago
JSON representation
Open Policy Agent WebAssembly Rust SDK
- Host: GitHub
- URL: https://github.com/matrix-org/rust-opa-wasm
- Owner: matrix-org
- License: apache-2.0
- Created: 2022-05-31T15:25:59.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-23T15:27:02.000Z (19 days ago)
- Last Synced: 2024-12-29T11:09:24.364Z (13 days ago)
- Language: Rust
- Size: 492 KB
- Stars: 50
- Watchers: 8
- Forks: 12
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-opa - Rust - A crate to use OPA policies compiled to Wasm. (WebAssembly (Wasm) / Typescript)
README
# Rust Open Policy Agent SDK
A crate to use OPA policies compiled to WASM.
## Try it out
This includes a CLI tool to try out the SDK implementation.
```text
cargo run --features=cli -- \
--module ./policy.wasm \
--data-path ./data.json \
--input '{"hello": "world"}' \
--entrypoint 'hello/world'
```Set the `RUST_LOG` environment variable to `info` to show timings informations about the execution.
```text
opa-wasm
Evaluates OPA policies compiled as WASM modulesUSAGE:
opa-eval [OPTIONS] --entrypoint <--module |--bundle >OPTIONS:
-m, --module Path to the WASM module
-b, --bundle Path to the OPA bundle
-e, --entrypoint Entrypoint to use
-d, --data JSON literal to use as data
-D, --data-path Path to a JSON file to load as data
-i, --input JSON literal to use as input
-I, --input-path Path to a JSON file to load as data
-h, --help Print help information
```## As a library
```rust,no_run
use std::collections::HashMap;use anyhow::Result;
use opa_wasm::{wasmtime, Runtime};
#[tokio::main]
async fn main() -> Result<()> {
// Configure the WASM runtime
let mut config = wasmtime::Config::new();
config.async_support(true);let engine = wasmtime::Engine::new(&config)?;
// Load the policy WASM module
let module = tokio::fs::read("./policy.wasm").await?;
let module = wasmtime::Module::new(&engine, module)?;// Create a store which will hold the module instance
let mut store = wasmtime::Store::new(&engine, ());let data = HashMap::from([("hello", "world")]);
let input = HashMap::from([("message", "world")]);// Instantiate the module
let runtime = Runtime::new(&mut store, &module).await?;let policy = runtime.with_data(&mut store, &data).await?;
// Evaluate the policy
let res: serde_json::Value = policy.evaluate(&mut store, "hello/world", &input).await?;println!("{}", res);
Ok(())
}
```