A lightweight, high-performance safety coprocessor for AI workflows and infrastructure. Guardian Agent is a drop-in sidecar that validates, reasons, and logs actions in real-time, enforcing the principle:
"Only do what you're allowed to do, and always explain why."
Ultra-fast, tiny binary (4MB), production-ready, runs everywhere!
- π 10-100x faster than Python implementations
- π¦ 4MB binary (50x smaller than Python containers)
- πΎ 5-10MB memory (10x lower than Python)
- β‘ 50ms startup (10x faster than Python)
- π Cross-platform - Linux, macOS, Windows, ARM, x86_64
- π Memory safe - No GC, compile-time safety guarantees
Currently in beta. Suitable for evaluation, development, and non-critical deployments.
- Production: Use behind a reverse proxy (nginx, Caddy, Traefik) for TLS (HTTPS). Native TLS is not yet implemented; the server runs HTTP only.
- Limitations: Per-route RBAC is planned (any authenticated user can call any protected route today). Audit log and MFA secrets are in-memory and do not persist across restarts. See docs/FULL_SYSTEM_ANALYSIS.md for a full analysis and docs/DEPLOYMENT.md for deployment guidance.
Guardian Agent is a safety coprocessor β a lightweight runtime that sits beside any system (AI workflow, API, infrastructure pipeline) and continuously validates, reasons, and logs what that system is doing.
"Only do what you're allowed to do, and always explain why."
ββββββββββββββββββββββββββββββββ
β Your App / Agent / Model β
β (e.g. LLM, API, CI runner) β
ββββββββββββββββ¬ββββββββββββββββ
β API / Hook
ββββββββββββββββ΄ββββββββββββββββ
β Guardian Agent Sidecar β
β β’ Policy validator (OPA) β
β β’ LLM reasoner (Ollama) β
β β’ Token gate / capability β
β β’ Immutable local log β
ββββββββββββββββ¬ββββββββββββββββ
β
Local or Remote
Audit Collector
- π Policy Validation: OPA (Open Policy Agent) integration for rule-based validation
- π§ LLM Reasoning: Local LLM (Ollama) for gray-area policy decisions
- π« Capability Gates: Short-lived JWT tokens for privileged actions
- π Immutable Logging: Cryptographically signed, append-only audit logs
- β‘ High Performance: Async I/O, 10000+ req/s throughput
- π Observability: Built-in metrics collection
- π Hot Reload: Zero-downtime policy updates
- π Cross-Platform: Linux, macOS, Windows, ARM, x86_64
- π³ Docker Ready: Multi-architecture Docker images
- π¦ Single Binary: No dependencies, easy deployment
- π Data Retention Policies: Automatic log archival and deletion with legal hold support (HIPAA, GDPR, PCI-DSS)
- π PII Detection & Redaction: Automatic detection and redaction of SSN, email, credit cards, etc. (HIPAA, GDPR, PCI-DSS)
- π Encryption at Rest: AES-256-GCM encryption with KMS integration (AWS KMS, Azure Key Vault, HashiCorp Vault) (HIPAA, PCI-DSS)
- π₯ RBAC for Log Access: Role-based access control with audit logging (SOC 2, HIPAA, PCI-DSS, GDPR)
- π Compliance Reporting: Automated reports for SOC 2, HIPAA, GDPR, PCI-DSS, ISO 27001
- βοΈ Legal Hold: Litigation hold with log preservation (SOC 2, HIPAA, GDPR)
- π Forensic Capabilities: Log querying, search, and timeline generation (SOC 2, HIPAA, PCI-DSS)
- π’ Multi-Tenancy Isolation: Tenant isolation with separate configs and keys (SOC 2, HIPAA, GDPR)
- π Chain of Custody: RFC 3161 timestamping for tamper-proof evidence
- π Regulatory Framework Mappings: Gap analysis and requirement tracking
- π Advanced Analytics: Anomaly detection and compliance risk scoring
- π MCP Protocol Monitoring: Monitor all MCP communications between AI agents
- π‘οΈ Tool Call Validation: Validate and block unauthorized tool calls
- π Resource Access Control: Control access to resources (files, APIs, etc.)
- π MCP Audit Logging: Complete audit trail of all MCP interactions
- βοΈ Policy Enforcement: Apply governance policies to MCP traffic
- Rust (1.75+) - Install Rust
- OPA (Optional) - Install OPA
- Ollama (Optional, for LLM reasoning) - Install Ollama
# Clone the repository
git clone https://github.com/srex-dev/Guardian-Agent.git
cd Guardian-Agent
# Build (takes ~1 minute first time)
cargo build --release --features server
# Binary is ready: target/release/guardian-server (or guardian-server.exe on Windows)# Run the server
./target/release/guardian-server
# Or with configuration
GUARDIAN_CONFIG=guardian.yaml PORT=8080 ./target/release/guardian-server# Health check
curl http://localhost:8080/health
# Validate an action
curl -X POST http://localhost:8080/validate \
-H "Content-Type: application/json" \
-d '{
"action": {
"type": "file_write",
"resource": "/tmp/test.txt"
},
"context": {
"user_id": "test_user"
}
}'Create a guardian.yaml file:
# Policy rules (Rego format or plain text descriptions)
policies:
- "no external http post except approved domains"
- "mask pii fields before log write"
- "require human review for model retrain"
# Paths to Rego policy files
policy_paths:
- "./policies/base.rego"
- "./policies/security.rego"
# OPA server URL (optional - for OPA server mode)
# opa_url: "http://localhost:8181"
# LLM Reasoner configuration (optional)
reasoner:
model: "local:mistral-7b" # Ollama model name
temperature: 0.0
max_tokens: 512
base_url: "http://localhost:11434" # Ollama default
# Capability/token gate configuration
capabilities:
ttl_seconds: 300 # 5 minutes
algorithm: "HS256"
# Immutable logging configuration
logging:
log_path: "/var/lib/.log.jsonl"
rotate_size_mb: 100
# Performance optimizations
cache:
enable: true
ttl_seconds: 300
max_size: 1000
# Hot reload configuration
enable_hot_reload: true
# Rate limiting configuration (optional)
enable_rate_limiting: false
rate_limits:
"action:file_write":
capacity: 100
refill_rate: 10 # requests per second-
Policy Validator (
src/policy_validator.rs)- Integrates with OPA for policy evaluation
- Supports OPA server mode (HTTP) or CLI mode
- Fallback validation when OPA unavailable
- Fast: 1-5ms per validation
-
Immutable Logger (
src/logger.rs)- Zero-copy logging with HMAC-SHA256 signatures
- Append-only JSONL format
- Automatic log rotation
- Device key management
-
Capability Gate (
src/capability_gate.rs)- JWT token issuance and validation
- Short-lived tokens (configurable TTL)
- Fast: 0.1-0.5ms per operation
-
Guardian Agent (
src/agent.rs)- Main orchestrator
- Coordinates validation, reasoning, logging
- Manages policy reloading
-
HTTP Server (
src/server.rs)- Axum-based async HTTP server
- High throughput: 10000+ req/s
- RESTful API endpoints
-
Metrics Collector (
src/metrics.rs)- Thread-safe atomic counters
- Performance tracking
- Prometheus-compatible
1. Action Request β Guardian Agent
2. Policy Validation (OPA) β Allow/Deny/Ambiguous
3. If Ambiguous β LLM Reasoning (Ollama)
4. Generate Verdict β Allow/Deny + Reason
5. Issue Capability Token (if needed)
6. Log Event (signed, immutable)
7. Return Response
Validate an action.
Request:
{
"action": {
"type": "file_write",
"resource": "/tmp/test.txt",
"method": "write"
},
"input_data": {},
"context": {
"user_id": "user123",
"environment": "production"
}
}Response:
{
"allowed": true,
"reason": "Policy evaluation completed",
"verdict_id": "abc123",
"device_id": "def456",
"confidence": 1.0,
"latency_ms": 2.5
}Validate multiple actions in batch (parallel processing).
Request:
{
"actions": [
{
"action": {"type": "file_write", "resource": "/tmp/test1.txt"}
},
{
"action": {"type": "file_write", "resource": "/tmp/test2.txt"}
}
],
"parallel": true
}Response:
{
"results": [...],
"total_latency_ms": 25.5,
"success_count": 2,
"denied_count": 0
}Validate a capability token.
Request:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"action": "file_write",
"resource": "/tmp/test.txt"
}Response:
{
"valid": true,
"reason": "Token is valid",
"payload": {
"action": "file_write",
"resource": "/tmp/test.txt",
"exp": 1234567890
}
}Health check with metrics.
Response:
{
"status": "healthy",
"device_id": "abc123",
"uptime_seconds": 3600.0,
"metrics": {
"validations_total": 1000,
"validations_allowed": 950,
"validations_denied": 50,
"cache_hits": 800,
"cache_misses": 200
}
}Prometheus metrics endpoint.
Reload policies without restart (hot reload).
Get current retention policies.
Update retention policies.
Manually trigger retention processing.
List all legal holds.
Add a legal hold to preserve logs.
Test PII detection on sample data.
Get encryption key information.
Rotate encryption keys.
List all RBAC users.
Create a new RBAC user.
Get RBAC access audit log.
List available compliance frameworks.
Generate compliance report for a framework.
Get requirements for a compliance framework.
Query logs with filters and date ranges.
Generate timeline of events.
List all tenants (multi-tenancy).
Create a new tenant.
Get chain of custody for a log entry.
Get regulatory framework mappings.
Perform gap analysis for a framework.
Detect anomalies in logs.
Calculate compliance risk score.
cargo build --release --target x86_64-unknown-linux-gnu --features servercargo build --release --target aarch64-unknown-linux-gnu --features servercargo build --release --target x86_64-apple-darwin --features servercargo build --release --target aarch64-apple-darwin --features servercargo build --release --features serverBuild a static binary with no dependencies:
# Install musl target
rustup target add x86_64-unknown-linux-musl
# Build static binary
cargo build --release --target x86_64-unknown-linux-musl --features server
# Result: Single binary, works on any Linux!Use the cross tool for easy cross-compilation:
# Install cross
cargo install cross
# Build for Linux from Windows/macOS
cross build --release --target x86_64-unknown-linux-gnu --features server# Build
docker build -t guardian:latest .
# Run
docker run -p 8080:8080 \
-v $(pwd)/guardian.yaml:/app/guardian.yaml:ro \
-v $(pwd)/policies:/app/policies:ro \
-v guardian-logs:/var/lib/guardian \
guardian:latest# Build for multiple platforms
docker buildx build --platform linux/amd64,linux/arm64 \
-t guardian:latest \
--push \
.
# Docker automatically selects correct architecture
docker run -p 8080:8080 guardian:latestversion: '3.8'
services:
guardian:
build: .
ports:
- "8080:8080"
volumes:
- ./guardian.yaml:/app/guardian.yaml:ro
- ./policies:/app/policies:ro
- guardian-logs:/var/lib/guardian
environment:
- GUARDIAN_CONFIG=/app/guardian.yaml
- PORT=8080| Metric | Value |
|---|---|
| Throughput | 10000+ req/s |
| Latency (cached) | 0.1-1ms |
| Latency (uncached) | 1-5ms |
| Memory Usage | 5-10MB |
| Startup Time | 50ms |
| Binary Size | 4MB |
| Metric | Python | Rust | Improvement |
|---|---|---|---|
| Policy Validation | 10-50ms | 1-5ms | 10x |
| JWT Operations | 1-5ms | 0.1-0.5ms | 10x |
| Log Writing | 2-10ms | 0.2-1ms | 10x |
| HTTP Throughput | 1000 req/s | 10000+ req/s | 10x+ |
| Memory Usage | 50-100MB | 5-10MB | 10x |
| Startup Time | 500ms | 50ms | 10x |
| Binary Size | 200MB | 4MB | 50x |
# Debug build
cargo build
# Release build
cargo build --release --features server
# With optimizations
RUSTFLAGS="-C target-cpu=native" cargo build --release --features server# Run all tests
cargo test
# Run with output
cargo test -- --nocapture
# Run specific test
cargo test test_policy_validator# Run directly
cargo run --release --features server
# Run with config
GUARDIAN_CONFIG=guardian.yaml cargo run --release --features server# Format code
cargo fmt
# Lint
cargo clippy
# Lint with fixes
cargo clippy --fixCreate policies/example.rego:
package guardian
default allow = false
# Allow file writes to /tmp
allow {
input.action.type == "file_write"
input.action.resource = startswith("/tmp/")
}
# Allow file writes to home directories
allow {
input.action.type == "file_write"
input.action.resource = startswith("/home/")
}
# Deny writes to system directories
deny {
input.action.type == "file_write"
input.action.resource = startswith("/etc/")
}
# Final decision
allow {
not deny
}In guardian.yaml:
policy_paths:
- "./policies/example.rego"
# Or use OPA server
opa_url: "http://localhost:8181"- β Memory safety - Rust's compile-time guarantees
- β Cryptographic signatures - HMAC-SHA256 for log integrity
- β Capability tokens - Short-lived JWTs for privileged actions
- β Immutable logs - Append-only, tamper-evident
- β Input validation - Type-safe API
- β Rate limiting - Prevent abuse
- Secure key storage - Store signing keys securely
- Policy review - Review policies before deployment
- Log monitoring - Monitor audit logs regularly
- Rate limiting - Enable rate limiting in production
- HTTPS - Use HTTPS in production (reverse proxy)
apiVersion: apps/v1
kind: Deployment
metadata:
name: guardian-agent
spec:
replicas: 1
selector:
matchLabels:
app: guardian
template:
metadata:
labels:
app: guardian
spec:
containers:
- name: guardian
image: guardian:latest
ports:
- containerPort: 8080
volumeMounts:
- name: config
mountPath: /app/guardian.yaml
subPath: guardian.yaml
- name: policies
mountPath: /app/policies
- name: logs
mountPath: /var/lib/guardian
volumes:
- name: config
configMap:
name: guardian-config
- name: policies
configMap:
name: guardian-policies
- name: logs
emptyDir: {}Create /etc/systemd/system/guardian.service:
[Unit]
Description=Guardian Agent
After=network.target
[Service]
Type=simple
User=guardian
WorkingDirectory=/opt/guardian
ExecStart=/opt/.guardian
Environment="GUARDIAN_CONFIG=/etc/.guardian.yaml"
Environment="PORT=8080"
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.targetversion: '3.8'
services:
guardian:
image: guardian:latest
ports:
- "8080:8080"
volumes:
- ./guardian.yaml:/app/guardian.yaml:ro
- ./policies:/app/policies:ro
- guardian-logs:/var/lib/guardian
environment:
- GUARDIAN_CONFIG=/app/guardian.yaml
- PORT=8080
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3
volumes:
guardian-logs:Access Prometheus metrics at /metrics:
curl http://localhost:8080/metricsguardian_validations_total- Total validationsguardian_validation_latency_seconds- Validation latencyguardian_cache_size- Cache sizeguardian_errors_total- Error count
curl http://localhost:8080/healthReturns:
- Status
- Device ID
- Uptime
- Metrics snapshot
cargo test# Start server
./target/release/guardian-server &
# Test endpoints
curl http://localhost:8080/health
curl -X POST http://localhost:8080/validate ...# Using Apache Bench
ab -n 10000 -c 100 http://localhost:8080/health
# Using wrk
wrk -t12 -c400 -d30s http://localhost:8080/health# Check if port is in use
netstat -an | grep 8080
# Check logs
RUST_LOG=debug ./target/release/guardian-server# Install OPA
# See: https://www.openpolicyagent.org/docs/latest/#running-opa
# Or use OPA server mode
opa_url: "http://localhost:8181"# Start Ollama
ollama serve
# Check connection
curl http://localhost:11434/api/tags# Check policy file paths
ls -la policies/
# Check configuration
cat guardian.yaml
# Enable debug logging
RUST_LOG=debug ./target/release/guardian-server# Reload policies without restart
curl -X POST http://localhost:8080/policies/reloadcurl -X POST http://localhost:8080/validate/batch \
-H "Content-Type: application/json" \
-d '{
"actions": [
{"action": {"type": "file_write", "resource": "/tmp/test1.txt"}},
{"action": {"type": "file_write", "resource": "/tmp/test2.txt"}}
],
"parallel": true
}'# Validate capability token
curl -X POST http://localhost:8080/capability \
-H "Content-Type: application/json" \
-d '{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"action": "file_write",
"resource": "/tmp/test.txt"
}'- SIDECAR_INTEGRATION_GUIDE.md - Complete guide for Kubernetes, Docker, and Systemd sidecar deployments
- COMPLIANCE_CONFIGURATION_GUIDE.md - Guide to configuring enterprise compliance features
- COMPLIANCE_ROADMAP.md - Full implementation roadmap and todo list
- COMPLIANCE_AND_GOVERNANCE_ANALYSIS.md - Compliance framework analysis and gaps
- INTEGRATION_ANALYSIS.md - Integration patterns and performance analysis
- EPHEMERAL_NANOSERVICE_ANALYSIS.md - Event-driven, serverless architecture patterns
- docs/MCP_MONITORING.md - Complete guide to MCP (Model Context Protocol) monitoring and governance
use guardian_rs::{GuardianAgent, GuardianConfig};
// Initialize agent
let config = GuardianConfig::from_file("guardian.yaml").await?;
let agent = GuardianAgent::new(config).await?;
// Validate LLM prompt
let action = serde_json::json!({
"type": "llm_inference",
"prompt": user_prompt,
"model": "gpt-4"
});
let result = agent.validate_action(&action, None, None).await?;
if result.allowed {
// Make LLM API call
} else {
// Deny request
}#!/bin/bash
# Pre-deploy validation
RESULT=$(curl -s -X POST http://localhost:8080/validate \
-H "Content-Type: application/json" \
-d "{
\"action\": {
\"type\": \"infrastructure_change\",
\"resource\": \"$TERRAFORM_PLAN\"
}
}")
if echo "$RESULT" | jq -e '.allowed == true' > /dev/null; then
echo "Deployment approved"
exit 0
else
echo "Deployment denied: $(echo $RESULT | jq -r '.reason')"
exit 1
fi// Middleware example
async fn guardian_middleware(request: Request, next: Next) -> Response {
let action = extract_action(&request);
let result = agent.validate_action(&action, None, None).await?;
if !result.allowed {
return Response::new(StatusCode::FORBIDDEN);
}
next.run(request).await
}.
βββ Cargo.toml # Rust project configuration
βββ Dockerfile # Docker build
βββ Makefile # Build automation
βββ src/
β βββ lib.rs # Library entry point
β βββ main.rs # Binary entry point
β βββ agent.rs # Main orchestrator
β βββ policy_validator.rs # OPA integration
β βββ logger.rs # Immutable logging
β βββ capability_gate.rs # JWT operations
β βββ server.rs # HTTP server
β βββ metrics.rs # Metrics collection
β βββ config.rs # Configuration
β βββ error.rs # Error handling
βββ policies/ # Policy files (Rego)
β βββ example.rego
βββ guardian.yaml # Configuration file
- β Validate LLM prompts before execution
- β Block harmful completions
- β Enforce content policies
- β Log all AI interactions
- β Validate infrastructure changes
- β Enforce compliance policies
- β Audit infrastructure actions
- β Prevent unauthorized changes
- β Validate API requests
- β Enforce rate limits
- β Audit API usage
- β Block malicious requests
- β Pre-deploy validation
- β Policy compliance checks
- β Security scanning
- β Audit deployment actions
# Policy configuration
policies:
- "policy rule 1"
- "policy rule 2"
policy_paths:
- "./policies/base.rego"
opa_url: "http://localhost:8181" # Optional
# LLM reasoner
reasoner:
model: "local:mistral-7b"
temperature: 0.0
max_tokens: 512
base_url: "http://localhost:11434"
# Capabilities
capabilities:
ttl_seconds: 300
algorithm: "HS256"
# Logging
logging:
log_path: "/var/lib/.log.jsonl"
rotate_size_mb: 100
# Cache
cache:
enable: true
ttl_seconds: 300
max_size: 1000
# Hot reload
enable_hot_reload: true
# Rate limiting
enable_rate_limiting: false
rate_limits:
"action:file_write":
capacity: 100
refill_rate: 10cargo build --release --features server \
-- -C opt-level=z -C lto=thinRUSTFLAGS="-C target-cpu=native" \
cargo build --release --features servercargo build --release \
--target x86_64-unknown-linux-musl \
--features serverdocker build -t guardian:latest .docker buildx build \
--platform linux/amd64,linux/arm64 \
-t guardian:latest \
--push \
.- Use static binary - No dependencies
- Enable rate limiting - Prevent abuse
- Configure hot reload - Zero downtime updates
- Set up monitoring - Track metrics
- Use HTTPS - Reverse proxy (nginx, Traefik)
- Secure keys - Store signing keys securely
- Rotate logs - Configure log rotation
- Backup logs - Regular backups
- CPU: 1 core (2+ recommended)
- Memory: 10MB minimum (50MB recommended)
- Disk: 100MB for binary + logs
- Network: Any (HTTP/HTTPS)
Logs are written in JSONL format with cryptographic signatures:
{
"timestamp": "2024-01-01T12:00:00Z",
"event_type": "action_validated",
"action": {
"type": "file_write",
"resource": "/tmp/test.txt"
},
"verdict": {
"allowed": true,
"reason": "Policy evaluation completed",
"device_id": "abc123"
},
"metadata": {},
"signature": "hmac-sha256-signature"
}-
Port already in use
# Use different port PORT=8081 ./target/release/guardian-server -
Policy not found
# Check policy paths ls -la policies/ -
OPA connection failed
# Check OPA server curl http://localhost:8181/health -
Permission denied
# Check file permissions chmod +x target/release/guardian-server
Contributions welcome! Please see CONTRIBUTING.md for guidelines. To report a security vulnerability, see SECURITY.md.
MIT License - see LICENSE for details.
- Open Policy Agent - Policy engine
- Ollama - Local LLM runtime
- Axum - HTTP framework
- Tokio - Async runtime
- Documentation: This README and docs/
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Guardian Agent - Safety coprocessor for AI workflows and infrastructure.
Built with Rust - Fast, safe, reliable.