Skip to content

API Reference parrot.tools.dataset_manager.sources.rls

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

parrot.tools.dataset_manager.sources.rls

View source

RLS Predicate Injection for FEAT-228 Data-Plane Authorization.

Provides per-source-type injection functions that take a list of rendered :class:~parrot.auth.rls_registry.RlsPredicate objects and apply them to the outbound query. Each injection method is a pure function (or returns a modified source/DataFrame) — no network I/O.

Injection strategies by source type (Spec §2 Module 5):

  • :class:~parrot.tools.dataset_manager.sources.sql.SQLQuerySource: wrap the original SQL with a SELECT * FROM (...) AS _rls WHERE <pred> so the predicate is applied after all table joins.
  • :class:~parrot.tools.dataset_manager.sources.table.TableSource: extend the existing _permanent_filter dict with RLS column conditions.
  • :class:~parrot.tools.dataset_manager.sources.query_slug.QuerySlugSource: merge predicates into the slug's _permanent_filter.
  • :class:~parrot.tools.dataset_manager.sources.mongo.MongoSource: merge predicate conditions into the Mongo query filter via $and.
  • Post-fetch (Airtable/Smartsheet): filter the returned :class:pandas.DataFrame by the bound parameter values.

Security note: SQLQuerySource.fetch() uses str.format() style substitution and cannot bind named :p0 parameters via a driver. Values are therefore inlined (escaped) directly into the WHERE clause at injection time. inject_rls_sql always returns an empty bound_params dict — callers do not need to pass parameters to the driver separately.

Usage::

from parrot.auth.rls_registry import RlsPredicate
from parrot.tools.dataset_manager.sources.rls import inject_rls_sql

wrapped_sql, params = inject_rls_sql(
    "SELECT * FROM sales.orders", "postgres", [pred]
)
# params is always {} — values are inlined into wrapped_sql

Alias MongoSource

Re-exported from parrot.tools.dataset_manager.sources.mongo.MongoSource.

Alias QuerySlugSource

Re-exported from parrot.tools.dataset_manager.sources.query_slug.QuerySlugSource.

Alias RlsPredicate

Re-exported from parrot.auth.rls_registry.RlsPredicate.

Alias TableSource

Re-exported from parrot.tools.dataset_manager.sources.table.TableSource.

Function inject_rls_mongo

Build a Mongo $and filter dict from RLS predicates.

Translates each predicate's bound parameters into Mongo $in filter expressions and wraps them in a {"$and": [...]} structure.

inject_rls_mongo(source: 'MongoSource', predicates: 'list[RlsPredicate]') -> dict[str, Any]

Parameter Type Default Description
source 'MongoSource' The :class:~parrot.tools.dataset_manager.sources.mongo.MongoSource (used to read existing required_filter if set).
predicates 'list[RlsPredicate]' List of rendered predicates to apply.

Returns: dict[str, Any] — A Mongo query filter dict ready for use as the filter argument.

Function inject_rls_postfetch

Apply RLS predicates as post-fetch row filtering on a DataFrame.

Used for API-backed sources (Airtable, Smartsheet) where server-side filtering is not available or not reliable. Filters the DataFrame to retain only rows whose column values appear in the allowed lists.

For each predicate, the bound parameter values are collected and used as an allow-list for the corresponding column. The column name is inferred from the predicate's SQL expression using the pattern col IN (...) or col = .... If the column name cannot be extracted, a :class:ValueError is raised.

.. note:: TODO: FEAT-228 post-fetch RLS deferred — AuthorizingDataSource does not yet call this function for Airtable/Smartsheet sources. Wiring it in requires a pre/post split of fetch() that is deferred. This function is fully implemented and tested; the missing piece is the callsite in _apply_rls.

Security note: post-fetch means data entered the process before filtering. This is weaker than server-side filtering but still enforces the restriction at the process boundary. Sensitive-classed sources should block post-fetch RLS (handled by AuthorizingDataSource, not here).

inject_rls_postfetch(df: pd.DataFrame, predicates: 'list[RlsPredicate]') -> pd.DataFrame

Parameter Type Default Description
df pd.DataFrame The :class:pandas.DataFrame returned by the source.
predicates 'list[RlsPredicate]' List of rendered :class:~parrot.auth.rls_registry.RlsPredicate objects.

Returns: pd.DataFrame — A filtered :class:pandas.DataFrame.

Function inject_rls_query_slug

Merge RLS predicates into a QuerySlugSource's permanent_filter.

Adds the actual column name (extracted from pred.sql_predicate) and all bound parameter values into source._permanent_filter. The existing permanent_filter mechanism merges these conditions at fetch time.

The column name is parsed from the predicate SQL using the pattern col IN (...) or col = .... Predicates that do not match this pattern are skipped.

inject_rls_query_slug(source: 'QuerySlugSource', predicates: 'list[RlsPredicate]') -> 'QuerySlugSource'

Parameter Type Default Description
source 'QuerySlugSource' The :class:~parrot.tools.dataset_manager.sources.query_slug.QuerySlugSource to modify.
predicates 'list[RlsPredicate]' List of rendered predicates to apply.

Returns: 'QuerySlugSource' — The same (mutated) source instance.

Function inject_rls_sql

Inject RLS predicates into a SQL query via wrapping.

Wraps the original query as a subquery and appends a WHERE clause composed of all predicate expressions joined with AND::

SELECT * FROM (<original>) AS _rls WHERE (<pred1>) AND (<pred2>)

Bound parameter values are inlined (safely escaped via single-quote doubling) directly into the WHERE clause. SQLQuerySource.fetch() uses str.format() style substitution and cannot bind named :p0 parameters via a driver, so inlining is the correct approach here.

Multiple predicates are deduplicated by index-prefixing their parameter names before inlining to prevent collisions between predicates that share the same placeholder names (e.g. both use :p0).

inject_rls_sql(sql: str, dialect: str, predicates: 'list[RlsPredicate]') -> tuple[str, dict[str, list[str]]]

Parameter Type Default Description
sql str Original SQL query string.
dialect str sqlglot dialect name (unused in the wrapping strategy but kept for API consistency with future push-down strategies).
predicates 'list[RlsPredicate]' List of rendered :class:~parrot.auth.rls_registry.RlsPredicate objects to inject.

Returns: tuple[str, dict[str, list[str]]] — A (rewritten_sql, {}) tuple. bound_params is always empty because values are inlined into the SQL string.

Function inject_rls_table_source

Extend a TableSource's permanent_filter with RLS conditions.

Modifies source._permanent_filter in place by adding the actual column name (extracted from pred.sql_predicate) as the filter key and all bound parameter values as the allow-list.

The column name is parsed from the predicate SQL using the pattern col IN (...) or col = .... Predicates that do not match this pattern are skipped with a warning.

inject_rls_table_source(source: 'TableSource', predicates: 'list[RlsPredicate]') -> 'TableSource'

Parameter Type Default Description
source 'TableSource' The :class:~parrot.tools.dataset_manager.sources.table.TableSource to modify.
predicates 'list[RlsPredicate]' List of rendered predicates to apply.

Returns: 'TableSource' — The same (mutated) source instance.

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