Sub topic 14
Granting and auditing access
This chapter assumes you can read SQL, know what a catalog, schema, and table are in Unity Catalog, and have run a query against a Delta table by its three-level name. It covers how Unity Catalog decides who can see what, how to grant and revoke access, and how to check afterward that the grants match your intent.
The model: securable objects, principals, privileges
Unity Catalog access control has three parts.
- A securable object is something you can protect: a catalog, a schema, a table, a view, a volume, a function, and so on. Objects form a hierarchy: metastore, catalog, schema, then table or view.
- A principal is who receives access: a user, a service principal (an identity for automation), or a group. Grant to groups where you can. Adding a person to a group is easier to review than editing grants on many objects.
- A privilege is a permitted action, such as
SELECT,MODIFY,CREATE TABLE,USE CATALOG, orUSE SCHEMA.
Privileges are inherited downward. A privilege granted on a schema applies to the tables in it, including tables created later. A grant on a catalog applies to every schema in it. Access is denied by default, and Unity Catalog has no deny rule. If a principal has no grant at any level, the request fails.
Why SELECT alone is not enough
To read a table, a principal needs three things:
USE CATALOGon the parent catalog.USE SCHEMAon the parent schema.SELECTon the table, or on the schema or catalog that contains it.
The two USE privileges let the principal traverse the hierarchy. They do not expose any data. Forgetting them is the most common cause of a permission error for a user who "has SELECT".
Granting access
The following statements give a group read access to every table in one schema.
GRANT USE CATALOG ON CATALOG main TO `data-analysts`;
GRANT USE SCHEMA, SELECT ON SCHEMA main.tutorial TO `data-analysts`;Because SELECT is granted on the schema, it covers main.tutorial.orders and any table added to the schema later. Granting at the schema level keeps the number of grants small. Granting on each table gives finer control but creates more to maintain.
To remove access, use REVOKE with the same shape:
REVOKE SELECT ON SCHEMA main.tutorial FROM `data-analysts`;Revoking a privilege at one level does not remove a grant made at another level. If the group also has SELECT on the catalog, it can still read the table. Check every level before you conclude that access is gone.
Who is allowed to grant
Only an object's owner, a metastore admin, or a principal with the MANAGE privilege on the object can grant privileges on it. The user who creates an object becomes its owner. Ownership gives full control, so prefer a group as owner for shared schemas. Then a departing employee does not leave the objects owned by a deactivated account.
Seeing current grants
Use SHOW GRANTS to see what is granted on an object.
SHOW GRANTS ON SCHEMA main.tutorial;
SHOW GRANTS `data-analysts` ON SCHEMA main.tutorial;
SHOW GRANTS ON TABLE main.tutorial.orders;By default the output lists grants made directly on that object. It does not always show privileges inherited from a parent. When you debug "why can this user read the table?", run SHOW GRANTS on the table, its schema, and its catalog, and also check the user's group memberships.
Limiting what a reader sees: views and dynamic views
Sometimes a group needs part of a table, not all of it. Two approaches are common.
- A view that selects only the permitted columns or rows. Grant
SELECTon the view and not on the base table. The reader can query the view without having access to the underlying table. - Row filters and column masks, which are functions attached to a table. They run every time the table is queried and change the result depending on who is asking.
This example exposes order totals without showing customer_id:
CREATE OR REPLACE VIEW main.tutorial.order_totals AS
SELECT order_id, amount, order_ts
FROM main.tutorial.orders;
GRANT SELECT ON VIEW main.tutorial.order_totals TO `finance-readers`;The group finance-readers also needs USE CATALOG on main and USE SCHEMA on main.tutorial. It does not need any privilege on main.tutorial.orders.
Auditing access
Granting is half the job. You also need to answer two questions later: who has access now, and who actually used it.
Who has access now
The information_schema views in each catalog expose privilege data that you can query with SQL. For example, to list grants on tables in one schema:
SELECT grantee, table_name, privilege_type
FROM main.information_schema.table_privileges
WHERE table_schema = 'tutorial'
ORDER BY table_name, grantee;This lists table-level grants. Check the equivalent schema and catalog views as well, since inherited grants are recorded at the level where they were made.
Who used it
Unity Catalog records activity in audit logs. When system tables are enabled for your account, the table system.access.audit holds one row per logged event, such as a table read or a permission change. A query for recent permission changes might look like this:
SELECT event_time, user_identity.email, action_name, request_params
FROM system.access.audit
WHERE action_name IN ('updatePermissions')
AND event_date >= current_date() - INTERVAL 7 DAYS
ORDER BY event_time DESC;Reading system tables requires that an account admin has enabled the schema and granted you access. Column and action names can vary by event, so inspect a few rows with SELECT * before you build reports on them.
Lineage
Unity Catalog also captures lineage, which is a record of which tables, notebooks, jobs, and queries read from or wrote to a given table. Lineage is shown in Catalog Explorer for a table and is also available in system tables. It helps you assess the impact of tightening access: before you revoke a group's grant, check which jobs read the table.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| "Table not found" or permission denied for a user with `SELECT` | Missing `USE CATALOG` or `USE SCHEMA` | Grant both on the parents |
| Access remains after `REVOKE` | A grant at a higher level, or through another group | Run `SHOW GRANTS` at each level and check group membership |
| Grants are hard to review | Many grants to individual users | Grant to groups and manage membership |
| A job fails after a team member leaves | The job runs as, and objects are owned by, a deactivated user | Use service principals and group ownership |
| A view returns an error for a reader | The reader lacks `USE SCHEMA` on the view's schema | Grant `USE` privileges; the base table grant is not required |
A review routine
A short routine keeps access under control.
- Grant to groups, at the schema level where that fits.
- Record the owner of each shared catalog and schema, and make that owner a group.
- Query
information_schemaandSHOW GRANTSon a schedule, and compare the result with what you expect. - Query
system.access.auditfor permission changes and unusual reads. - Before revoking, check lineage for jobs that depend on the table.
Exercise
A user in the group data-analysts has SELECT on main.tutorial.orders through a grant on the table, but the query fails with a permission error. Which two privileges are most likely missing, and on which objects?
Show answer
USE CATALOG on the catalog main and USE SCHEMA on the schema main.tutorial. SELECT on the table is not enough without permission to traverse its parents.
Exercise
You run REVOKE SELECT ON SCHEMA main.tutorial FROM data-analysts`, but a member of that group can still read main.tutorial.orders`. Name two possible reasons and one way to investigate.
Show answer
The group may still hold SELECT at the catalog level or directly on the table, or the user may belong to another group that has the grant. Run SHOW GRANTS on the table, schema, and catalog, and check the user's group memberships.
Exercise
You need to let finance-readers see order amounts but not customer IDs. What object do you create, and what do you grant?
Show answer
Create a view that selects only order_id, amount, and order_ts, then grant SELECT on the view to finance-readers, along with USE CATALOG and USE SCHEMA on its parents. Do not grant access to the base table.