RBAC, Policies, Masks & Audit

Deny-by-default roles, row filters, role-scoped field masks, ABAC attributes, and SHA-256 hash-chained audit logs.

3 min read Security & Governance

SpydDB enforces deny-by-default authorization inside the query planner and storage executor across SpydWire, PostgreSQL wire, HTTP/1.1, gRPC, and Embedded Web Studio.

1. Declarative RBAC Roles (create role)#

No session may read, insert, update, delete, or run DDL on a collection unless a bound role grants that privilege explicitly (and no explicit deny rule overrides it):

spydql
create or replace role support_agent {
  grant select, update(status, notes) on collection (tickets, orders)
  grant select on collection (customers)
  deny delete on collection (orders)
}

Supported Object Privileges#

  • Collection & table privileges: select, insert, update, delete, create, alter, drop, index, manage, all
  • Field-scoped grants: grant select(id, customer, status), update(status) on collection (orders)
  • Database & object kinds: collection, view, materialized_view, stream, database, system
  • Least-privilege database scopes:
    • database(maintenance) — run maintain and maintain purge without full security or DDL privileges
    • database(extensions) — install, update, and drop signed WebAssembly extensions without security privileges
    • database(security) — administer roles, policies, backups, encryption keys, and cluster control
    • database(*) — all collections and database administrative scopes
  • Precedence rule: explicit deny always wins over grant.
spydql
create or replace role maintainer {
  grant manage on database(maintenance)
}

Bind roles directly to authentication credentials in tokens, scram, or external OIDC/LDAP/JWT claim_groups_to_roles mappings.

2. Row-Level Security, Field Masking & ABAC (create policy)#

Protect sensitive rows and columns declaratively per collection — evaluated before user predicates and projections so unauthorized users cannot probe masked or filtered values:

spydql
create or replace policy tenant_and_pii_guard on customers {
  -- Row-level security filter using ABAC session claims
  filter: region == session.region

  -- Explicit deny rule evaluated strictly alongside filters
  deny where suspended == true and session.role != "auditor"

  -- Role-scoped field masking
  mask email: "***@redacted" for role("support_agent")
  mask phone: tokenize(phone) for role("analyst")
}

Key Properties of Policy Guards#

  • Write-path safety: Masks apply purely on read projections; write planning strips read masks so redacted strings like "***@redacted" are never written back to storage.
  • Tokenization: tokenize(value) produces a consistent, tenant-scoped pseudonym per session without exposing plaintext.
  • ABAC Attributes: Authenticate sessions carry immutable session.<attribute> attributes (e.g., session.region, session.clearance, session.user) evaluated directly inside filter: and deny where predicates.

Rule Policies (when ... then ...)#

Define cross-cutting governance guards on actions or workloads:

spydql
create or replace policy bulk_export_guard {
  when action == "export" and session.clearance != "high" then deny
  log: true
}

3. SHA-256 Hash-Chained Audit Log#

With auditing on, every statement a user runs is recorded, with its start and its outcome, in a cryptographically hash-chained audit ledger (audit.log + sealed segments + verified manifest). Statements that write are recorded durably before they are acknowledged.

To flag the decisions that need review, add a rule policy with an audit action:

spydql
create or replace policy review_customer_access {
  when session.role != "support" then audit
}

A per-collection audit on <collection> { ... } statement is not supported; the ledger already covers every statement, and rule policies select what to flag.

Verifying & Querying Audit Logs (HTTP & Embedded Studio)#

Verify the complete chain integrity across the manifest, every sealed segment join, and the active segment via REST API or the Studio → Security tab:

sh
# 1. Verify tamper-evident SHA-256 chain
curl -fsS --cacert tls/ca.crt https://localhost:8080/v1/admin/audit/verify \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN"
json
{
  "verified": true,
  "chain": {
    "segments": 3,
    "events": 14820,
    "head_hash": "e3b0c44298fc1c14..."
  }
}
sh
# 2. Fetch recent audit events
curl -fsS --cacert tls/ca.crt "https://localhost:8080/v1/admin/audit/events?limit=50" \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN"

Configure segment rotation size (audit_segment_bytes), retention window (audit_retention_secs), and minimum retained sealed files (audit_min_sealed_segments) in server.json, and replicate sealed segments off-host to immutable WORM object storage for regulatory compliance.

4. Multi-Node & Cross-Cluster Security Sync#

In a distributed cluster, every shard node evaluates its own local catalog roles and row policies. SpydDB prevents nodes from ever serving queries under stale or mismatched security rules:

  • Always-on fingerprint guard (X-Spyd-Security): Coordinators stamp every forwarded shard statement with the cryptographic fingerprint of the relevant roles, rule policies, and collection row policies (security_fingerprint_for). Any shard node whose local fingerprint differs fails closed with 409 security_mismatch.
  • Opt-in synchronization (PUT /v1/cluster/security/sync): Cluster administrators can opt specific nodes ({"node_id": "...", "enabled": true}) or outbound linked clusters ({"link": "...", "enabled": true}) into sharing roles, policies, and rule_policies per database. Role or policy changes push synchronously in parallel to opted-in cluster nodes before statement acknowledgment, queue coalesced updates to opted-in links, heal via 5 s jittered gossip (GET /v1/cluster/security/state + POST /v1/cluster/security/apply), and converge deterministically by (version, updated_us, fingerprint).

For full API details, auto-repair on mismatch, and NAT/dial-in behavior, see Multi-Cluster Fleet & Security Sync.