Expand description
Backend request-context enforcement (NW-deep parity).
Pre-NW-deep, only Postgres enforced RequestContext — its executor
wrapped every dispatched SQL in a transaction and called
set_request_local_settings so RLS policies could see
app.current_tenant_id / app.current_project_id / etc. Every other
executor (MySQL, SQLite, ClickHouse, MongoDB, Neo4j, Redis, Qdrant,
S3) silently dropped the context. Multi-tenancy on those backends was
therefore application-trust, not broker-enforced.
This module supplies a uniform contract so every executor can apply the active context in its own native idiom:
- SQL backends (PG/MySQL/SQLite/CH): SET session variables that policies/views can read.
- Document / graph backends (Mongo/Neo4j): emit a context predicate (filter prefix or Cypher parameter binding) that callers AND together with their own filters.
- KV / object backends (Redis/Qdrant/S3): emit a key namespace prefix that callers prepend to keys / bucket paths.
The trait’s outputs are structured, not free-form SQL — each
executor lowers AppliedContext to the right shape during dispatch.
That keeps the enforcement decision in one place per backend.
§Effect levels
Every enforce call returns a ContextEffect so the operator can
see how strongly the context was applied:
Enforced— the broker actively constrains backend operations (SQL session vars set; key prefix prepended; filter ANDed in).Advisory— the context is recorded for audit / observability but not enforced at the protocol layer (e.g. Mongo without$jsonSchemavalidators on the collection).Unsupported { reason }— backend can’t carry the context meaningfully. Surfaces in metrics so the operator knows when their RLS posture is application-trust.
The capability matrix’s supports_rls flag now reflects whether
the executor implements Enforced (true) or stops at Advisory
(still true if context is recorded somewhere) or Unsupported
(false).
Structs§
- Applied
Context - The structured context that each backend’s enforcer lowers to its native form. Compiled once per request and reused across multiple operations against the same backend.
Enums§
- Context
Effect - How strongly the backend applied the context.
- SqlDialect
- SQL dialect selector for
render_sql_session_settings.
Traits§
- Backend
Context Enforcer - Backend-specific request-context applicator. Implementations live
next to each executor (e.g.
runtime/executors/postgres_context.rs) so the SQL/JSON/Cypher emission stays close to the dispatch path.
Functions§
- enforce_
with_ mechanism - Shared body for the common
BackendContextEnforcer::enforceshape: an empty context isAdvisory { recorded_in: "no_context_to_apply" }, and a non-empty context isEnforced { mechanism }with the backend’s own per-backend mechanism string. Backends with extra conditions (e.g. MSSQL, which keys on whether a request context is bound) implementenforcedirectly instead of delegating here. - escape_
sql_ string - Escape a string literal for inlining into a SQL statement for the given
dialect. Every dialect doubles the single-quote (
'→''); this is the single source of that rule so the per-dialect arms inrender_sql_session_settingsdon’t each open-code it. - render_
cypher_ context_ parameters - Built-in default for Cypher (Neo4j) — render a parameter map the
executor includes in every Cypher request. Cypher itself can read
parameters via
$ctx_tenant_idand AND them into MATCH clauses. - render_
document_ filter_ prefix - Built-in default for document backends — render a JSON filter
fragment the executor ANDs together with the caller’s query filter.
Returns
Nonewhen the context is empty (no constraint to add). - render_
kv_ key_ prefix - Built-in default for KV / object stores — render a key namespace prefix the executor prepends to keys / object paths. Returns an empty string when the context is empty so prefix-less callers still work.
- render_
sql_ session_ settings - Built-in default for SQL backends that talk to a session-aware
driver (PG, MySQL, ClickHouse) — emits a list of
SETstatements the executor prepends to the user SQL inside a request-scoped transaction.