sentinel-agent-wasm
WebAssembly agent for Sentinel reverse proxy. Execute custom Wasm modules for request/response processing.
Features
- Execute WebAssembly modules for request/response lifecycle events
- Fast, secure wasmtime runtime with instance pooling
- Language-agnostic: write modules in Rust, Go, C, AssemblyScript, or any language that compiles to Wasm
- JSON-based data exchange between host and module
- Header manipulation (add/remove request and response headers)
- Audit tags for logging and analytics
- Fail-open mode for graceful error handling
Installation
From crates.io
From source
Usage
Command Line Options
| Option | Environment Variable | Description | Default |
|---|---|---|---|
--socket |
AGENT_SOCKET |
Unix socket path | /tmp/sentinel-wasm.sock |
--module |
WASM_MODULE |
Wasm module file (.wasm) | (required) |
--pool-size |
WASM_POOL_SIZE |
Instance pool size | 4 |
--verbose |
WASM_VERBOSE |
Enable debug logging | false |
--fail-open |
FAIL_OPEN |
Allow requests on module errors | false |
Writing Wasm Modules
Required ABI
Modules must export the following functions:
// Memory allocation (required)
alloc(size: i32) -> i32 // Allocate `size` bytes, return pointer
dealloc(ptr: i32, size: i32) // Free memory at `ptr`
// Request/Response handlers (at least one required)
on_request_headers(ptr: i32, len: i32) -> i64 // Returns (result_ptr << 32) | result_len
on_response_headers(ptr: i32, len: i32) -> i64 // Returns (result_ptr << 32) | result_len
Data Exchange
The host passes JSON data to handlers and expects JSON back.
Request Object (on_request_headers)
Response Object (on_response_headers)
Result Object (return value)
Decision Values
| Decision | Description |
|---|---|
allow |
Allow the request/response to proceed |
block |
Block with given status (default: 403) and body |
deny |
Same as block |
redirect |
Redirect to URL in body field (default: 302) |
Example: Rust Module
See examples/wasm-module/ for a complete example. Key parts:
use ;
use ;
pub extern "C"
pub extern "C"
pub extern "C"
Build with:
Building the Example Module
The module will be at examples/wasm-module/target/wasm32-unknown-unknown/release/example_wasm_module.wasm.
Instance Pooling
The agent maintains a pool of pre-initialized Wasm instances for performance. Configure with --pool-size:
- Pool size 1: Minimum memory, sequential processing
- Pool size 4 (default): Good balance for most workloads
- Pool size 8+: High-concurrency scenarios
Sentinel Proxy Configuration
agents {
agent "wasm" {
type "custom"
transport "unix_socket" {
path "/var/run/sentinel/wasm.sock"
}
events ["request_headers", "response_headers"]
timeout-ms 50
failure-mode "open"
}
}
Error Handling
When --fail-open is enabled, module errors will:
- Log the error
- Allow the request to proceed
- Add
wasm-errorandfail-opentags to audit metadata
When --fail-open is disabled (default), module errors will:
- Log the error
- Block the request with 500 status
- Add
wasm-errortag to audit metadata
Comparison with Other Agents
| Feature | sentinel-agent-wasm | sentinel-agent-js | sentinel-agent-lua |
|---|---|---|---|
| Language | Any (Rust, Go, C, etc.) | JavaScript | Lua |
| Runtime | wasmtime | QuickJS | mlua |
| Performance | Fastest | Fast | Fast |
| Sandboxing | Strong (Wasm isolation) | Basic | Comprehensive |
| Ecosystem | Wasm-compatible libraries | Limited | Lua libraries |
| Complexity | Higher (compilation required) | Lower | Lower |
Use sentinel-agent-wasm for:
- Maximum performance requirements
- Existing Rust/Go/C code that needs minimal porting
- Strong isolation between modules
- Memory-safe execution
Development
# Run tests (requires example module)
&& &&
# Run with debug logging
RUST_LOG=debug
License
Apache-2.0