Sub topic 14
Granting and auditing access
This chapter assumes you know basic SQL and have met Unity Catalog's three-level names (catalog.schema.table) from the previous chapters. It covers how permissions are granted and how you check who has access to what.
How Unity Catalog permissions work
Unity Catalog is the governance layer for the workspace. Access is deny-by-default. A principal (a user, a group, or a service principal) can use an object only if a privilege was granted on it or inherited from a parent.
Privileges inherit downward through the namespace. A grant on a catalog applies to every schema and table inside it. A grant on a schema applies to its tables.
Reading a table needs three privileges along its path:
| Privilege | Granted on | Purpose |
|---|---|---|
| `USE CATALOG` | Catalog | Lets the principal see into the catalog |
| `USE SCHEMA` | Schema | Lets the principal see into the schema |
| `SELECT` | Table or schema | Lets the principal read rows |
Granting access to a group
Grant to groups, not to individuals. When someone changes teams, you change their group membership and leave the grants alone.
GRANT USE CATALOG ON CATALOG main TO `data-analysts`;
GRANT USE SCHEMA, SELECT ON SCHEMA main.tutorial TO `data-analysts`;The second statement grants SELECT on the schema. Members of data-analysts can therefore read every current and future table in main.tutorial. Group names that contain a hyphen need backticks.
Checking and removing grants
Use SHOW GRANTS to see what a principal or an object has.
SHOW GRANTS ON SCHEMA main.tutorial;
SHOW GRANTS `data-analysts` ON SCHEMA main.tutorial;Use REVOKE to take a privilege back. It undoes a grant made at the same level.
REVOKE SELECT ON SCHEMA main.tutorial FROM `data-analysts`;Revoking at the table level does not cancel a grant inherited from the schema. If access persists after a revoke, check the parent objects.
Auditing access
Granting is half of governance. You also need to see who actually used the data. Databricks records account activity in system tables, which are read-only tables in the system catalog. An account admin must enable the schemas you want before they can be queried.
The audit log is system.access.audit. This query lists the last day of actions on the workspace:
SELECT event_time, user_identity.email, service_name, action_name
FROM system.access.audit
WHERE event_time > current_timestamp() - INTERVAL 1 DAY
ORDER BY event_time DESC
LIMIT 50;Unity Catalog also tracks lineage, which shows which tables and notebooks read from or wrote to a table. Lineage is available on a table's page in the catalog browser.
Common errors
- Granting
SELECTwithoutUSE CATALOGandUSE SCHEMA. - Granting to individual users, which leaves grants behind after people move teams.
- Forgetting backticks around group names that contain hyphens.
- Revoking on a table while a schema-level grant still applies.
- Querying
system.access.auditbefore an admin has enabled the schema.
Exercise
A new analyst is in the group data-analysts. The group has SELECT on main.tutorial.orders but not USE SCHEMA on main.tutorial. Can the analyst query the table? How would you fix it?
Show answer
No. Reading a table needs USE CATALOG on the catalog, USE SCHEMA on the schema, and SELECT on the table. The fix is to run GRANT USE SCHEMA ON SCHEMA main.tutorial TO data-analysts;, and to confirm that USE CATALOG on main is also granted.
Exercise
Which statement shows every privilege currently granted on the schema main.tutorial, and where do you look to see who actually queried its tables?
Show answer
SHOW GRANTS ON SCHEMA main.tutorial; lists the grants. The audit log in system.access.audit shows who used the data, and lineage on the table page shows which tables and notebooks read from or wrote to it.