How to Enforce Fine-Grained Authorization in Elasticsearch RAG with SpiceDB

· Original article ↗

Summary

A practical guide to using SpiceDB relationship-based access control in Elasticsearch RAG pipelines, with a deterministic authorization step that filters retrieved documents before they reach an LLM.

Key points

  • The proposed flow retrieves relevant chunks from Elasticsearch, then checks each document ID against SpiceDB before passing authorized content to the LLM.
  • Relationship-based access control (ReBAC) represents permissions as relationships between users, groups, and documents, supporting document sharing and individual exceptions without reindexing.
  • A deterministic LangGraph authorization node must run before generation; missing document IDs, denied permissions, or individual check errors result in documents being dropped.
  • If SpiceDB is unreachable, the pipeline stops before generation, preventing the LLM from receiving unchecked documents.
  • Post-filtering can reduce the number of usable results, so the article recommends overfetching and using coarse metadata filters where appropriate.
  • Production guidance includes synchronizing relationships with the access-control source of truth and deploying SpiceDB with a persistent datastore.

Article Details

Defense Focus
Prevent unauthorized documents from reaching the LLM in Elasticsearch RAG pipelines by enforcing fine-grained, relationship-based authorization outside the model.
Detection Methods
  • Check each retrieved document's doc_id against the user's view permission in SpiceDB before passing it to the LLM.
  • Log authorization outcomes, including authorized and denied counts and denied document IDs, to support review of access decisions.
  • Fail closed: drop documents with missing doc_id or an individual authorization error, and stop the pipeline if SpiceDB is unreachable.
Data Sources
  • Retrieved document metadata, including doc_id
  • SpiceDB relationship and permission-check results
  • Authorization logs containing subject_id, document_count, authorized count, denied count, and denied document IDs
Rule Types
  • SpiceDB schema and relationship tuples defining document view permissions
  • SpiceDB CheckPermission and CheckBulkPermissions API calls
  • Elasticsearch kNN search with an optional metadata terms pre-filter
Defensive Actions
  • Place a deterministic authorization node between retrieval and generation, with no route from retrieval directly to the LLM.
  • Pass only documents confirmed as authorized by SpiceDB into the generation prompt; do not expose details about denied documents.
  • Keep access relationships synchronized with the source of truth, writing relationships for grants and deleting them for revocations.
  • Use overfetching to compensate for documents removed by post-filter authorization, while accounting for Elasticsearch candidate limits.
  • Use coarse metadata filters during kNN retrieval when appropriate, while retaining SpiceDB for fine-grained authorization.
  • Avoid reindexing when document-sharing relationships change; keep the relationship graph in SpiceDB.
  • Use a persistent datastore for production SpiceDB deployments.

Vendors

Products

Industries

Related Articles