sentinel-agent-lua-0.1.0 is not a library.
sentinel-agent-lua
Lua scripting agent for Sentinel reverse proxy. Write custom request/response processing logic in Lua.
Features
- Execute Lua scripts on request/response lifecycle events
- Sandboxed execution with resource limits (memory, CPU, time)
- Hot-reload of scripts without restart
- Rich standard library (JSON, crypto, HTTP utilities, regex)
- Script metadata for routing and prioritization
- VM pooling for performance
Installation
From crates.io
From source
Usage
Command Line Options
| Option | Environment Variable | Description | Default |
|---|---|---|---|
--socket |
AGENT_SOCKET |
Unix socket path | /tmp/sentinel-lua.sock |
--scripts |
LUA_SCRIPTS_DIR |
Scripts directory | /etc/sentinel/scripts |
--config |
LUA_CONFIG |
Configuration file | - |
--log-level |
RUST_LOG |
Log level | info |
Writing Scripts
Script Structure
-- name: my-script
-- version: 1.0.0
-- hook: request_headers
-- paths: /api/*
-- methods: GET, POST
-- priority: 100
Available Hooks
| Hook | Description |
|---|---|
on_request_headers(request) |
Called when request headers are received |
on_request_body(request, body) |
Called when full request body is buffered |
on_request_body_chunk(request, chunk, is_last) |
Called for each body chunk (streaming) |
on_response_headers(response) |
Called when response headers are received |
on_response_body(response, body) |
Called when full response body is buffered |
Request/Response API
-- Request object
request: -- Get header value
request: -- Add header
request: -- Remove header
request: -- Get request path
request: -- Get HTTP method
request: -- Get query string
request: -- Get client IP
-- Response object
response: -- Get status code
response: -- Get header value
response: -- Add header
response: -- Remove header
Standard Library
JSON
local obj = json.
local str = json.
local pretty = json.
Crypto
local hash = crypto.
local hash384 = crypto.
local hash512 = crypto.
local mac = crypto.
local bytes = crypto.
local hex = crypto.
HTTP Utilities
local encoded = http.
local decoded = http.
local params = http.
local query = http.
local cookies = http.
local text = http. -- "OK"
Encoding
local b64 = encoding.
local data = encoding.
local hex = encoding.
local data = encoding.
local compressed = encoding.
local data = encoding.
Regex
local matched = regex.
local found = regex.
local all = regex.
local replaced = regex.
Time
local now = time. -- Unix timestamp
local now_ms = time. -- Milliseconds
local formatted = time.
local ts = time.
Sentinel
sentinel.
sentinel. -- or "block", "redirect"
sentinel.
sentinel. -- Agent version
Configuration
Sentinel Proxy Configuration
agents {
agent "lua" {
type "custom"
transport "unix_socket" {
path "/var/run/sentinel/lua.sock"
}
events ["request_headers", "response_headers"]
timeout-ms 100
failure-mode "open"
}
}
Agent Configuration (KDL)
socket-path "/var/run/sentinel/lua.sock"
scripts {
directory "/etc/sentinel/scripts"
hot-reload true
watch-interval 5
timeout 100
cache-size 200
}
vm-pool {
size 20
max-age 600
max-executions 5000
}
resource-limits {
max-memory 52428800 // 50MB
max-instructions 10000000
max-execution-time 200
allow-filesystem false
allow-network false
}
safety {
fail-open true
debug-scripts false
max-concurrent 200
}
Resource Limits
The agent enforces strict resource limits on Lua execution:
| Limit | Default | Description |
|---|---|---|
| Memory | 50MB | Maximum memory per VM |
| Instructions | 10M | Maximum CPU instructions |
| Execution time | 100ms | Maximum script runtime |
| String length | 10MB | Maximum string size |
| Table size | 10,000 | Maximum table entries |
Security
Scripts run in a sandboxed environment with:
- No filesystem access (by default)
- No network access (by default)
- No process spawning
- Dangerous functions removed (dofile, loadfile, etc.)
- Limited libraries (string, table, math, utf8, coroutine)
Development
# Run with debug logging
RUST_LOG=debug
# Run tests
License
MIT OR Apache-2.0