Skip to main content

Module backend_context

Module backend_context 

Source
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 $jsonSchema validators 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§

AppliedContext
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§

ContextEffect
How strongly the backend applied the context.
SqlDialect
SQL dialect selector for render_sql_session_settings.

Traits§

BackendContextEnforcer
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::enforce shape: an empty context is Advisory { recorded_in: "no_context_to_apply" }, and a non-empty context is Enforced { 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) implement enforce directly 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 in render_sql_session_settings don’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_id and 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 None when 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 SET statements the executor prepends to the user SQL inside a request-scoped transaction.