Skip to main content

DuckDB with Ranger (Enterprise)

DuckDB has no Ranger plugin. In Ilum Enterprise, the DuckDB engine of the SQL editor is governed by Ilum itself: the DuckDB Ranger guard in ilum-core checks every DuckDB statement against the Ranger policies of Spark SQL (service ilum_spark) before DuckDB runs it, and rewrites it so that the policies' row filters and masks apply. The same policy gives the same result in Spark SQL, Trino and DuckDB: a denied table is refused, a masked column shows masked values, and a row filter hides the same rows.

The guard works like the Ranger plugins of Kyuubi and Trino, but on the statement instead of the engine's plan: every governed table is replaced with a subquery that applies the row filter under the masked columns, so a user's own conditions see only masked values and filtered rows. What the guard cannot check is refused.

The guard is part of governed mode in Ilum Enterprise. It runs on top of the DuckDB session lockdown, which stays on.

How it works​

Each statement of a script is checked and run on its own, in order, so it sees the objects the statements before it created.

  1. Statement shape. Only queries and a few other statements are accepted (see What a guarded session allows). The statement is parsed with DuckDB's own parser, and every table, function, table function and syntax element in it must be known to the guard. A table of a governed catalog is checked with Ranger (select on the table) and replaced with a subquery that applies the row filter, the masks and the column denials.
  2. Second check. DuckDB prints the rewritten statement, the guard parses the printout again and checks it once more: only the guard's subqueries, the session's own objects and allowed functions may remain.
  3. Bound plan. DuckDB binds the printout, with views and macros expanded, and every table and file the plan reads must be one the statement may read.

DuckDB runs only the text the guard printed, never the text the user wrote.

Requirements​

  • Apache Ranger in the installation (ilum-ranger), with the service ilum_spark and, for file functions, ilum_objectstore. Ranger must allow unauthenticated policy downloads (ilum-ranger.admin.unauthenticatedPolicyDownload, the default), as for the Spark SQL and Trino plugins.
  • The DuckDB session lockdown (ilum-core.sql.duckdb.lockdown.enabled), which governed mode turns on.
  • SQL sign-in (global.sqlAuth.enabled), so that policies match the signed-in Ilum user.
  • User and group names in Ranger that match the Ilum users: groups come from Ranger's user store (usersync), as for the Spark SQL engines. See Users and groups from the directory.

Turning it on​

values-governed.yaml of the helm_aio chart turns the guard on, together with Ranger and the governed SQL engines (see Governed engines). On another installation, set:

ilum-core:
sql:
duckdb:
lockdown:
enabled: true
guard:
enabled: true
# Ships ilum-core's log, with the guard's audit records, to Loki
podLabels:
ilum.logAggregation: "true"
note

Without Ranger in the installation, keep the guard off (ilum-core.sql.duckdb.guard.enabled: false after values-governed.yaml). DuckDB then refuses restricted catalogs, as described in Querying a restricted catalog.

Policies​

The guard reads the policies of the Ranger service ranger.sparkService (ilum_spark), the service of the governed Spark SQL engines. A DuckDB session attaches the Hive catalog of its cluster (hms for the default catalog), and the DuckDB schema is the database of the Ranger policy: one policy on hr.employees covers hr.employees in Spark SQL and hms.hr.employees in DuckDB.

PolicyIn DuckDB
Access (select) on a tableRequired for any read of the table, count(*) included
Deny on a tableThe statement is refused
Deny on a columnRefused when a statement uses the column; SELECT * uses every column. DESCRIBE still lists the column, and the refusal names it
Row filterApplied under the masks: the user's conditions see only the rows the filter keeps
Mask MASK, MASK_SHOW_LAST_4, MASK_SHOW_FIRST_4Letters become X or x, digits n, as in Spark SQL; a column that is not text becomes NULL
Mask MASK_HASH, MASK_NULL, MASK_DATE_SHOW_YEAR, MASK_NONEAs in Spark SQL
Mask CUSTOMThe expression, with {col} replaced by the column
Condition of a policy item (Policy Conditions)Evaluated as in Spark SQL and Trino: the item applies only while its condition holds

Row filters and CUSTOM masks are SQL text written for Spark SQL. The guard runs the same text in DuckDB, with current_user() replaced by the Ilum user, so it must be valid in both dialects. A row filter or mask that DuckDB cannot read, or one with a subquery, makes the guard refuse the table instead of reading it without the policy.

Policy changes reach the guard within ranger.pollInterval (30 seconds). When Ranger admin cannot be reached at startup, the guard starts from the policies it saved last (ranger.cacheDir); without any, it refuses every governed table.

What a guarded session allows​

StatementAllowed
SELECT and its forms: CTEs, joins, subqueries, UNION, PIVOT, DESCRIBE, SUMMARIZE, EXPLAINYes
CREATE TABLE ... AS, CREATE VIEW, CREATE MACRO, INSERT ... SELECT, DROPYes, in the session's own memory or temp catalog
PREPARE, EXECUTE, DEALLOCATE, SET VARIABLE, RESET VARIABLEYes
read_parquet, read_csv, read_json, read_text, read_blobFor files the object-storage policies allow, see Files
COPY, EXPORT, ATTACH, DETACH, USE, SET, PRAGMA, CALL, INSTALL, LOAD, CREATE SECRET, UPDATE, DELETE, transactionsNo
Catalog metadata: information_schema, duckdb_tables(), duckdb_columns(), SHOW TABLESNo
Other table functions, and functions outside the guard's allow-listNo

A table needs its full name (hms.<schema>.<table>); the session's own objects can be named without a catalog. Only the Hive catalog of the cluster is governed: DuckLake and every other catalog are refused in a guarded session.

The function allow-list is DuckDB's own functions, without the few that read settings, statistics or the rewritten statement. Functions of an extension the sessions preload (sql.duckdb.lockdown.preloadExtensions) can be added with sql.duckdb.guard.extraFunctions.

Views, macros and prepared statements keep the policies they were created under. When the policies of a table behind one of them change, or the user may no longer read the table or file, the object is refused until it is created again. A table created with CREATE TABLE ... AS is a copy of what the user could read at that time.

Files​

With sql.duckdb.guard.fileFunctions (on by default), read_parquet, parquet_scan, read_csv, read_json, read_ndjson, read_text and read_blob read the s3:// objects that the object-storage policies of Ranger (service ilum_objectstore) let the user read: the same policies that policy-sync compiles into the store. The bucket is the policy's bucket and the object key its path.

  • A path must be a string or a list of strings: an s3://, s3a:// or s3n:// URL with a plain object key (no .., ?, % or #). Local files and other schemes are refused.
  • A glob (*, **, [...]) needs list on its directory. The guard expands the glob itself, checks read on every file, and passes DuckDB the list of files it checked. A glob may match at most sql.duckdb.guard.maxFiles files.
  • Options of the function (hive_partitioning = true, union_by_name := true) must be constants.
CSV, JSON, Delta and Hive Metastore tables

DuckDB 1.5 cannot hand the guard the plan of a CSV, JSON or Delta scan, and the Hive Metastore extension of this release marks the scans of its tables as not serializable. While the plan check (sql.duckdb.guard.planCheck) is on, read_csv and read_json are refused, and so are the tables of the Hive Metastore catalog. read_parquet, read_text and read_blob are not affected.

-- needs list on /curated/sales in bucket finance, and read on every file the glob matches
SELECT region, sum(amount) FROM read_parquet('s3://finance/curated/sales/*.parquet', hive_partitioning = true) GROUP BY region;

The session reads the files with the storage keys of its cluster: a file needs both a Ranger policy and a storage of the cluster that reaches its bucket.

Restricted catalogs​

With the guard on, DuckDB may query a restricted catalog under the same rule as Spark SQL and Trino: only on a cluster that is governed for the catalog. The session then attaches the catalog, and the guard applies its Ranger policies. sql.duckdb.refuseRestrictedCatalogs applies only while the guard is off.

Audit​

The guard writes Ranger's audit records (the JSON of AuthzAuditEvent) as lines of the logger ranger_audit in the ilum-core log: table reads (allowed and denied), applied row filters and masks, denied columns when a statement uses them, and file decisions of the object-storage service. Each record names the user (reqUser), the service (repo), the resource, the result, the statement (reqData), the DuckDB session (sess), the cluster (cluster_name) and the client type ilum-duckdb. With the label ilum.logAggregation: "true" on the ilum-core pod (set by values-governed.yaml), Promtail ships them to Loki, where they are the ilum-core log lines that contain ranger_audit.

Refusals that are not Ranger decisions (an unknown statement or function) are logged by ilum-core at WARN and returned to the user.

Upgrading DuckDB​

The guard reads DuckDB's syntax tree, so a new DuckDB release can bring syntax or functions the guard has never seen. Unknown syntax and functions are refused, and each DuckDB build is verified before it ships: the guard's test matrix (the cases of the access-control spike and the cases added in Ilum, including the bypass attempts) and its function allow-list run on every change of DuckDB in Ilum's build. ilum-core refuses to start the guard on a DuckDB build the matrix did not pass on.

Configuration​

NameDescriptionDefault
ilum-core.sql.duckdb.guard.enabledCheck every DuckDB statement against the Spark SQL policies of Rangerfalse, true with values-governed.yaml
ilum-core.sql.duckdb.guard.planCheckThird check of every statement, on DuckDB's bound plantrue
ilum-core.sql.duckdb.guard.extraFunctionsFunctions allowed besides the built-in allow-list[]
ilum-core.sql.duckdb.guard.catalogTtlHow long a session keeps the columns and file locations of a governed table1m
ilum-core.sql.duckdb.guard.fileFunctionsLet file functions read the objects the object-storage policies allowtrue
ilum-core.sql.duckdb.guard.maxFilesMost files a glob may match10000
ilum-core.ranger.urlRanger admin URL of ilum-core's Ranger pluginhttp://ilum-ranger:6080
ilum-core.ranger.sparkServiceRanger service with the Spark SQL policiesilum_spark
ilum-core.ranger.objectStoreServiceRanger service with the object-storage policiesilum_objectstore
ilum-core.ranger.pollIntervalHow often ilum-core asks Ranger admin for changes30s
ilum-core.ranger.cacheDirPolicies saved for a start while Ranger admin is down/tmp/ranger-policy-cache
ilum-core.podLabelsExtra labels of the ilum-core pod{}, ilum.logAggregation: "true" with values-governed.yaml

Troubleshooting​

MessageMeaning
Permission denied: user [alice] does not have [select] privilege on [hr/employees]No Ranger policy allows the table, or a deny policy applies
... privilege on [hr/employees/salary]The statement used a column the user may not read
unknown table ...The table does not exist, or it was named without its catalog (hms.hr.employees)
the catalog ... is not governed by RangerDuckLake or another catalog that is not the cluster's Hive catalog
statement not allowed in a guarded DuckDB sessionThe statement is not one of the allowed ones
the function ... is not availableThe function is not on the allow-list; see sql.duckdb.guard.extraFunctions
the row filter or a mask of ... cannot be read by DuckDB, ... is not a plain expressionThe policy's expression is not valid in DuckDB, or has a subquery
the Ranger policies ... changed since the view ... was createdCreate the view, macro or prepared statement again
the Ranger policies are not loaded yetilum-core has not downloaded the policies since its start, and has none saved
the guard cannot check DuckDB's plan of this statementThe statement reads CSV, JSON or Delta data or a Hive Metastore table, see Files