Skip to main content

Masked derived tables (Enterprise)

Ranger masks and row filters apply in the SQL engines. Notebooks and Spark jobs read storage directly with the key of their workspace and pass no engine, so they cannot get a masked view of a governed table. They read a masked derived table instead: a scheduled job signs in to Trino as a service account, reads the governed table with that account's masks and row filters applied, and writes the result into the published catalog. The copy holds exactly what the service account may see, and it is a normal table in the published bucket.

How it works​

StepWhereEnforced by
The job signs in to Trino as svc-publishedTrino, HTTPS on port 8443Trino password authentication
It reads hr.hr.employeescatalog hr: governed metastore, governed bucketRanger: select, the masks and the row filter of svc-published
It writes published.hr_published.employees_maskedcatalog published: bundled metastore, published bucketRanger: create and drop in published.hr_published; the storage key of the catalog reaches only the published bucket
A notebook reads hr_published.employees_maskedbundled metastore, published bucketthe workspace's storage key (read on the published bucket)
  • The masking rules stay in Ranger. Give the service account the masks and row filters of the consumers the copy is for, for example by making it a member of their group, so the copy holds what those consumers would see in Trino.
  • Trino applies masks and row filters to the source of CREATE TABLE ... AS SELECT and INSERT ... SELECT as to any other query.
  • The copy is as fresh as the job's schedule. A change of a mask or filter reaches the copy with the next run.

Requirements​

  • Trino with Ranger and Trino authentication (values-governed.yaml, see Trino authentication).
  • The catalogs hr and published (values-governed-catalogs.yaml, see Catalogs on their own identities), with a published bucket that the key in ilum-trino-published-storage may read and write.
  • The bundled metastore can reach the published bucket. A metastore creates the directory of a new schema with its own key, so a published bucket on another object store needs the metastore settings described in Catalogs on their own identities.

Setting up​

Service account​

Create the service account as a user of Trino's password file (see Users in the password file) or in the directory. Keep its password and Trino's CA in a namespace of their own, where the job runs, and admit that namespace to Trino:

kubectl create namespace ilum-publishing
kubectl -n ilum-publishing create secret generic svc-published-trino --from-literal=password='<password>'
kubectl -n <release namespace> get secret ilum-trino-tls -o jsonpath='{.data.ca\.crt}' | base64 -d > ilum-trino-ca.crt
kubectl -n ilum-publishing create secret generic ilum-trino-ca --from-file=ca.crt=ilum-trino-ca.crt
global:
trinoAuth:
clients:
namespaces: [ilum-publishing]

Do not grant the account unmasked access to the source table in any other policy, and do not make it a member of a group that has such access: Ranger then applies the unmasked grant, and the job publishes raw data.

Ranger policies​

The account needs these policies in the ilum_trino service, in addition to the base policies that the ilum-ranger bootstrap creates:

PolicyResourceAccess
Catalogscatalog hr, publisheduse, show
Sourcecatalog hr, schema hr, table employees, column *select
Maskshr.hr.employees, columns national_id and salarymasking: show last 4 characters, null
Row filterhr.hr.employeesrow filter department <> 'Executive'
Target schemacatalog published, schema hr_publishedcreate, show
Target tablescatalog published, schema hr_published, table *, column *select, insert, delete, create, drop

Ranger keeps one policy per resource set and refuses a second one with Another policy already exists for matching resource. The source table usually has such policies already, for example the masks and the row filter that business users get on hr.hr.employees: add the account as an item of those policies (edit the policy in the Ranger UI, or send PUT /service/public/v2/api/policy/{id} with the item appended) instead of creating new ones. Where no policy covers a resource yet, create it; the requests below do that through Ranger's REST API (POST /service/public/v2/api/policy), signed in as a Ranger administrator:

{"service": "ilum_trino", "name": "published: catalogs of svc-published",
"resources": {"catalog": {"values": ["hr", "published"]}},
"policyItems": [{"users": ["svc-published"], "accesses": [{"type": "use", "isAllowed": true}, {"type": "show", "isAllowed": true}]}]}

{"service": "ilum_trino", "name": "published: svc-published reads hr.employees",
"resources": {"catalog": {"values": ["hr"]}, "schema": {"values": ["hr"]}, "table": {"values": ["employees"]}, "column": {"values": ["*"]}},
"policyItems": [{"users": ["svc-published"], "accesses": [{"type": "select", "isAllowed": true}]}]}

{"service": "ilum_trino", "name": "published: national_id masked", "policyType": 1,
"resources": {"catalog": {"values": ["hr"]}, "schema": {"values": ["hr"]}, "table": {"values": ["employees"]}, "column": {"values": ["national_id"]}},
"dataMaskPolicyItems": [{"users": ["svc-published"], "accesses": [{"type": "select", "isAllowed": true}], "dataMaskInfo": {"dataMaskType": "MASK_SHOW_LAST_4"}}]}

{"service": "ilum_trino", "name": "published: salary removed", "policyType": 1,
"resources": {"catalog": {"values": ["hr"]}, "schema": {"values": ["hr"]}, "table": {"values": ["employees"]}, "column": {"values": ["salary"]}},
"dataMaskPolicyItems": [{"users": ["svc-published"], "accesses": [{"type": "select", "isAllowed": true}], "dataMaskInfo": {"dataMaskType": "MASK_NULL"}}]}

{"service": "ilum_trino", "name": "published: no executives", "policyType": 2,
"resources": {"catalog": {"values": ["hr"]}, "schema": {"values": ["hr"]}, "table": {"values": ["employees"]}},
"rowFilterPolicyItems": [{"users": ["svc-published"], "accesses": [{"type": "select", "isAllowed": true}], "rowFilterInfo": {"filterExpr": "department <> 'Executive'"}}]}

{"service": "ilum_trino", "name": "published: svc-published creates published.hr_published",
"resources": {"catalog": {"values": ["published"]}, "schema": {"values": ["hr_published"]}},
"policyItems": [{"users": ["svc-published"], "accesses": [{"type": "create", "isAllowed": true}, {"type": "show", "isAllowed": true}]}]}

{"service": "ilum_trino", "name": "published: svc-published writes published.hr_published",
"resources": {"catalog": {"values": ["published"]}, "schema": {"values": ["hr_published"]}, "table": {"values": ["*"]}, "column": {"values": ["*"]}},
"policyItems": [{"users": ["svc-published"], "accesses": [{"type": "select", "isAllowed": true}, {"type": "insert", "isAllowed": true},
{"type": "delete", "isAllowed": true}, {"type": "create", "isAllowed": true}, {"type": "drop", "isAllowed": true}]}]}

Ranger creates users unknown to it when the request carries ?createPrincipalsIfAbsent=true. Readers of the copy in Trino get select on published.hr_published as an item of the policy on its tables; it needs no masks, because the data is already masked.

Job​

The job runs the Trino command-line client of the Trino image that the cluster already uses, so it needs no image of its own. It replaces the copy in one statement, so readers never find the table missing:

apiVersion: v1
kind: ConfigMap
metadata:
name: publish-employees
namespace: ilum-publishing
data:
publish.sql: |
CREATE SCHEMA IF NOT EXISTS published.hr_published WITH (location = 's3a://<published bucket>/hr_published/');
CREATE OR REPLACE TABLE published.hr_published.employees_masked AS
SELECT employee_id, name, national_id, salary, department FROM hr.hr.employees;
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: publish-employees
namespace: ilum-publishing
spec:
schedule: "0 * * * *"
concurrencyPolicy: Forbid
jobTemplate:
spec:
backoffLimit: 2
template:
spec:
restartPolicy: Never
automountServiceAccountToken: false
containers:
- name: publish
image: trinodb/trino:<version of the cluster's Trino>
command: [trino, --server, https://ilum-trino.<release namespace>.svc.cluster.local:8443,
--truststore-path, /etc/ilum-trino/ca.crt, --user, svc-published, --password, --file, /etc/publish/publish.sql]
env:
- name: TRINO_PASSWORD
valueFrom:
secretKeyRef:
name: svc-published-trino
key: password
volumeMounts:
- name: sql
mountPath: /etc/publish
- name: ca
mountPath: /etc/ilum-trino
volumes:
- name: sql
configMap:
name: publish-employees
- name: ca
secret:
secretName: ilum-trino-ca

With --password, the client reads the password from TRINO_PASSWORD, and --truststore-path takes the CA as a PEM file. A password Secret created from a file must not end with a line break: it becomes part of the password. Any other scheduler works the same way: sign in as the service account over HTTPS and run the two statements.

The schema's location starts with s3a://: the metastore creates the directory through Hadoop, which has no s3 scheme (No FileSystem for scheme "s3"). The published catalog shares the bundled metastore with domains and ilum-delta, and CREATE SCHEMA IF NOT EXISTS keeps the location of a schema that already exists there. Give the copy a schema name of its own and check its location once with SHOW CREATE SCHEMA published.hr_published; a table created in a schema outside the published bucket fails, because the catalog's key reaches only that bucket.

Reading the copy​

The copy is a Delta Lake table of the bundled metastore, hr_published.employees_masked, in the published bucket. A notebook or a Spark job of a domain workspace reads it like any other table of that metastore, once the workspace's storage key may read the published bucket (an ilum_objectstore policy in Ranger, which policy-sync compiles into the object store's policies; see Apache Ranger):

spark.table("hr_published.employees_masked").show()

Keep the published bucket read-only for consumers: only the key of the published catalog writes to it.

Limits​

  • The copy holds what the service account sees at the time of the run. Ranger policies on the source do not apply to it; govern access to the copy with the policies of the published catalog and of the published bucket.
  • The bundled Hive Metastore does not authenticate its clients, so a user who reaches it can change or drop the copy's entry. The next run writes the table again.
  • Deleting a row from the source removes it from the copy only with the next run.