Skip to content

API Reference parrot.auth.rls_registry

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.auth.rls_registry

View source

Row-Level Security (RLS) Registry for FEAT-228 Data-Plane Authorization.

Maps (driver, table) pairs to predicate templates with subject-attribute placeholders. At query time the registry renders a :class:RlsPredicate that the injection layer (:mod:parrot.tools.dataset_manager.sources.rls) injects into the outbound query.

Design constraints (see spec §2 and §4):

  • Predicates are never string-interpolated. Values are always returned as bound parameters (bound_params dict) for the driver to bind safely.
  • Empty attribute lists (e.g. no programs on the subject) produce a deny-all predicate (1=0) rather than an open predicate.
  • The registry is in-memory and loaded from config at startup (config loading is outside this module's responsibility).

Usage::

from parrot.auth.rls_registry import RlsRegistry, RlsRule, RlsPredicate

registry = RlsRegistry()
registry.register(RlsRule(
    driver="pg",
    table="sales.orders",
    predicate_template="region IN (:subject.programs)",
    subject_attribute="programs",
    description="Regional managers see only their region",
))

# At query time:
predicates = registry.lookup("pg", {"pg:sales.orders"})
rendered = registry.render(predicates[0], pctx)

Alias PermissionContext

Re-exported from parrot.auth.permission.PermissionContext.

Class RlsPredicate

A rendered RLS predicate ready for injection.

Fields:

Field Type Default Description
table str The physical table this predicate applies to.
sql_predicate str SQL WHERE clause fragment with parameter placeholders (e.g. "region IN (:p0, :p1)"). Subject values are never interpolated into this string — they live in bound_params.
bound_params dict[str, list[str]] Mapping from placeholder name to list of string values to bind (e.g. {"p0": ["northeast"], "p1": ["southeast"]}).

Class RlsRegistry

In-memory registry mapping (driver, table) to predicate templates.

Rules are keyed by (driver, table). The lookup method strips the driver: prefix from table resource strings ("pg:sales.orders" → looks up ("pg", "sales.orders")).

Example::

registry = RlsRegistry()
registry.register(RlsRule(
    driver="pg",
    table="sales.orders",
    predicate_template="region IN (:subject.programs)",
    subject_attribute="programs",
))
rules = registry.lookup("pg", {"pg:sales.orders"})
assert len(rules) == 1

Method RlsRegistry.register

Add a predicate template rule to the registry.

register(rule: RlsRule) -> None

Parameter Type Default Description
rule RlsRule The :class:RlsRule to register. If a rule for the same (driver, table) already exists it is replaced.

Method RlsRegistry.lookup

Return matching rules for the given driver and table resource strings.

lookup(driver: str, tables: set[str]) -> list[RlsRule]

Parameter Type Default Description
driver str Canonical driver name (e.g. "pg").
tables set[str] Set of table resource strings in "driver:schema.table" form (as produced by the physical-resource resolver).

Returns: list[RlsRule] — List of :class:RlsRule objects whose (driver, table) pair matches any of the supplied table resources.

Method RlsRegistry.render

Render a rule into a bound :class:RlsPredicate.

Subject attribute values are read from ctx.session.metadata[rule.subject_attribute]. Values are never interpolated into the SQL string — they are placed in bound_params for safe parameterised binding.

If the subject has no values for the required attribute (empty list), the predicate is set to 1=0 (deny-all tautology) to prevent data exposure when a required restriction cannot be established.

render(rule: RlsRule, ctx: PermissionContext) -> RlsPredicate

Parameter Type Default Description
rule RlsRule The :class:RlsRule template to render.
ctx PermissionContext The current :class:PermissionContext from which subject attribute values are extracted.

Returns: RlsPredicate — class:RlsPredicate with parameter placeholders in sql_predicate and actual values in bound_params.

Class RlsRule

Registry entry: template predicate keyed by (driver, table).

Fields:

Field Type Default Description
driver str Canonical ai-parrot driver name (output of normalize_driver).
table str Fully-qualified table name in schema.table form.
predicate_template str SQL WHERE fragment with :subject.<attr> placeholders, e.g. "region IN (:subject.programs)".
subject_attribute str Name of the UserSession.metadata key whose values are bound as parameters, e.g. "programs" or "groups".
description str '' Human-readable description of the rule.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally