RBAC and Audit Logging
SketchLog's role-based access control (RBAC) module enforces per-namespace permissions on every API call and records every access decision to a tamper-evident audit log (designed to support SOC2 audit requirements).
Quick start
Sample output:
Roles
| Role | Ingest | Query | Admin |
|---|---|---|---|
admin |
✅ | ✅ | ✅ |
read_write |
✅ | ✅ | ❌ |
ingest |
✅ | ❌ | ❌ |
read |
❌ | ✅ | ❌ |
Configuration
{
"grants": [
{"token_id": "prod-admin", "role": "admin", "namespaces": []},
{"token_id": "svc-ingest", "role": "ingest", "namespaces": ["prod"]},
{"token_id": "svc-reader", "role": "read", "namespaces": ["prod","staging"]},
{"token_id": "svc-rw", "role": "read_write", "namespaces": ["staging"]}
],
"audit_file": "/var/log/sketchlog/audit.jsonl",
"audit_hmac_key": "YOUR_SECRET_KEY",
"enabled": true
}
Audit log format
Each line is a JSON object:
{
"ts": 1720000000.123,
"token_id": "svc-reader",
"action": "query",
"namespace": "prod",
"stream": "latency",
"result": "ALLOW",
"reason": "ok",
"role": "read",
"hmac_tag": "e3b0c44298fc1c149afb..."
}
result—ALLOWorDENYhmac_tag— HMAC-SHA256 of the event payload for tamper detection
Python API
import os
from sketchlog.rbac import Role, Permission, TokenGrant, RBACConfig, RBACEnforcer
config = RBACConfig(
grants=[
TokenGrant("my-token", Role.READ_WRITE, ["prod"]),
],
audit_file="/var/log/sketchlog/audit.jsonl",
audit_hmac_key=os.environ["SKETCHLOG_HMAC_KEY"],
)
enforcer = RBACEnforcer(config)
if enforcer.check("my-token", Permission.INGEST, "prod", "latency"):
# proceed
pass
Wiring into server.py
Pass the RBACEnforcer instance into your request middleware or startup hook:
import os
from sketchlog.rbac import RBACConfig, TokenGrant, Role, RBACEnforcer
config = RBACConfig(
grants=[TokenGrant("prod-token", Role.READ_WRITE, ["prod"])],
audit_hmac_key=os.environ["SKETCHLOG_HMAC_KEY"],
)
enforcer = RBACEnforcer(config)
# In your FastAPI middleware or dependency:
# enforcer.check(token_id, action, namespace, stream)
Note: Full
server.pymiddleware integration is tracked in #250. Thesketchlog-rbac-checkCLI validates your config independently.
CLI reference
| Flag | Default | Description |
|---|---|---|
--config |
— | Path to JSON config file |
--format |
text |
Output format: text or json |
--demo |
off | Run with built-in demo grants |
Exit codes: 0 = PASS/WARN, 1 = FAIL, 2 = bad config.
Syslog output (Unix only)
Enable syslog audit output by setting audit_syslog=True in RBACConfig:
config = RBACConfig(
grants=[...],
audit_hmac_key=os.environ["SKETCHLOG_HMAC_KEY"],
audit_syslog=True, # sends LOG_INFO events to syslog (Linux/macOS)
)
On Windows, audit_syslog=True is silently ignored and output falls back to stdout.
Caveats
- Tokens are validated by
token_idstring only — rotate tokens regularly. - HMAC integrity requires keeping
audit_hmac_keysecret; rotate if compromised. - Audit log is append-only — implement external rotation (logrotate, fluentd).
- RBAC does not replace network-level controls (TLS, firewall).
- Empty
namespaceslist means the token has access to all namespaces. enabled: falsedisables all checks — use only for development.