Sub topic 14
Auditing access with audit logs
This chapter shows how to review what happened in Unity Catalog after permissions are in place. It assumes you can write SQL with WHERE, GROUP BY and ORDER BY. It also assumes you know how GRANT, REVOKE, groups and ownership work from the earlier chapters, and that you can run queries in the SQL editor or a notebook.
Why audit logs matter
Permissions describe what is allowed. Audit logs record what was attempted and what happened. You need both to answer questions such as these:
- Who changed the privileges on
main.sales.orderslast week? - Which users were denied access to a catalog?
- Did a service principal read a sensitive table outside business hours?
- Did a grant made for a project get removed afterward?
Unity Catalog writes an audit event for each governed action. Databricks exposes these events through the audit log system table, system.access.audit. Because it is a table, you query it with ordinary SQL and can build dashboards and alerts on it.
Where the audit data comes from
Every request to a Databricks service, such as Unity Catalog, clusters or jobs, produces an event. Events from all workspaces in the account are collected in one place. The system.access.audit table holds them, and it is read-only.
Both allowed and denied requests are recorded. The outcome is stored in the response field, so one table covers successes and failures.
Prerequisites for querying the table
System tables are not available by default. Check these points before you query:
- The metastore must be enabled for system tables. An account admin or metastore admin enables the
accesssystem schema. - The querying principal needs
USE CATALOGonsystem,USE SCHEMAonsystem.access, andSELECTon the table. - You need a compute resource that can run Unity Catalog queries, such as a SQL warehouse.
A reasonable setup gives a security group read access and nothing more:
GRANT USE CATALOG ON CATALOG system TO `security-auditors`;
GRANT USE SCHEMA ON SCHEMA system.access TO `security-auditors`;
GRANT SELECT ON TABLE system.access.audit TO `security-auditors`;Audit records show who touched what, so the table is sensitive. Grant it to a small group, not to everyone.
The important columns
The table has many columns. These are the ones you will use most.
| Column | What it holds |
|---|---|
| `event_time` | When the event happened (a timestamp). |
| `event_date` | The date of the event. Filter on it to scan less data. |
| `workspace_id` | The workspace where the action ran. |
| `service_name` | The service that handled the request, for example `unityCatalog`. |
| `action_name` | The operation, for example `getTable` or `updatePermissions`. |
| `user_identity` | A struct. `user_identity.email` is the user or service principal. |
| `source_ip_address` | The IP address of the request. |
| `request_params` | A map of the request's parameters, such as the object name. |
| `response` | A struct. `response.status_code` is the HTTP-style result. |
The request_params map is the least uniform column. Different actions store different keys, so look at sample rows for an action before you write a filter on a key.
A first query
This query lists the 20 most recent Unity Catalog events:
SELECT event_time, user_identity.email, action_name
FROM system.access.audit
WHERE service_name = 'unityCatalog'
ORDER BY event_time DESC
LIMIT 20;Two habits keep audit queries fast and cheap. Always filter on event_date or event_time to a bounded range. Always filter on service_name so you skip events from other services.
Finding permission changes
Every GRANT and REVOKE is recorded as an updatePermissions action. This query shows who changed privileges in the last seven days:
SELECT
event_time,
user_identity.email AS changed_by,
request_params['securable_type'] AS object_type,
request_params['securable_full_name'] AS object_name,
request_params['changes'] AS changes
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND action_name = 'updatePermissions'
AND event_date >= current_date() - INTERVAL 7 DAYS
ORDER BY event_time DESC;The changes value is a JSON string that lists which principals gained or lost which privileges. You can parse it with from_json or read it by eye. This query answers "who granted ALL PRIVILEGES on this catalog, and when?"
Ownership changes and object creation have their own action names, such as createCatalog, createSchema and createTable. List the distinct names in your own account before you rely on a particular one:
SELECT action_name, count(*) AS events
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND event_date >= current_date() - INTERVAL 30 DAYS
GROUP BY action_name
ORDER BY events DESC;Finding denied requests
A user who lacks a privilege gets an error, and the audit table records it with a failing status code. Denied requests are worth reviewing because they show two things. One is a legitimate user who needs a grant. The other is someone probing for data they should not see.
SELECT
event_time,
user_identity.email AS principal,
action_name,
response.status_code,
response.error_message
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND response.status_code = 403
AND event_date >= current_date() - INTERVAL 7 DAYS
ORDER BY event_time DESC;To spot patterns, group by principal:
SELECT
user_identity.email AS principal,
count(*) AS denied_requests
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND response.status_code = 403
AND event_date >= current_date() - INTERVAL 7 DAYS
GROUP BY user_identity.email
ORDER BY denied_requests DESC
LIMIT 10;A few denials are normal, for example when someone browses a catalog they cannot open. A sudden spike for one principal is worth a closer look.
Seeing who read a table
To see who accessed one table, filter on the table's full name. For table reads, Unity Catalog commonly stores the name under the full_name_arg key:
SELECT
event_time,
user_identity.email AS principal,
action_name,
source_ip_address
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND request_params['full_name_arg'] = 'main.sales.orders'
AND event_date >= current_date() - INTERVAL 30 DAYS
ORDER BY event_time DESC;Metadata events and data reads are different things. A getTable event shows that someone resolved the table's metadata, which happens when a query is planned. It does not tell you which rows or columns were returned. For finer detail, such as which downstream tables were built from a source, use the lineage system tables (system.access.table_lineage and system.access.column_lineage). They record reads and writes between tables and columns.
Auditing the controls from earlier chapters
You can use the audit table to check that the controls in this tutorial still behave as designed.
- Ownership. Search for ownership and permission changes made by people who are not in your administrator groups. Those changes show where the model is being bypassed.
- External storage. Look for credential and external location events to see who creates or changes access to cloud paths.
- Row filters and column masks. Changes to filters and masks appear as table update events. A review that finds an unexpected removal is worth the effort, because the removal exposes data silently.
- Workspace bindings. Binding changes appear as catalog update events. Compare them against your change records.
Turning queries into monitoring
A query you run once finds past problems. A scheduled query finds new ones. A practical routine looks like this:
- Save each query above as a SQL query in the workspace.
- Schedule it as a job or an alert, daily for permission changes and hourly for denial spikes.
- Set an alert condition, such as "more than 50 denied requests from one principal in an hour".
- Send the alert to the security group's channel.
- Review alerts and record the outcome, even when nothing is wrong.
Keep the queries small and bounded by date. A scheduled query that scans a year of events every hour wastes compute.
Common errors
- Table not found or no permission. The
accessschema is not enabled, or you lackUSE SCHEMAonsystem.access. Ask an admin to check both. - Empty
request_paramsvalues. The key name differs for that action. Select the whole map for a few sample rows and read the keys. - Missing old events. The retention window has passed. Archive the data if you need it longer.
- Slow queries. There is no date filter. Add
event_dateandservice_nameconditions. - Misleading counts. Filtering on
action_namealone mixes service types. Always includeservice_name. - Reading metadata events as data reads. See the section on seeing who read a table.
Summary
system.access.auditrecords allowed and denied actions across all workspaces in the account.- Grant it to a small security group with
USE CATALOG,USE SCHEMAandSELECT. - Filter by
service_name,action_nameand a date range, and useresponse.status_codeto find denials. - Use
updatePermissionsevents to trackGRANTandREVOKEactivity. - Schedule queries as alerts so that review is routine.
Exercise
Write a query that lists every permission change in the last 14 days, showing the time, the person who made the change, and the object name. Which table, filters and columns do you use?
Show answer
Query system.access.audit with service_name = 'unityCatalog', action_name = 'updatePermissions' and event_date >= current_date() - INTERVAL 14 DAYS. Select event_time, user_identity.email and request_params['securable_full_name'], and order by event_time DESC.
Exercise
A colleague says a getTable event for main.sales.orders proves a user read customer rows. Is that correct? What would you use to get more detail?
Show answer
No. A getTable event shows that the table's metadata was resolved, for example when a query was planned. It does not show which rows or columns were returned. For more detail, use the lineage system tables, system.access.table_lineage and system.access.column_lineage, and review the other audit events around the same time.
Exercise
Name two reasons an audit query might run slowly or return confusing results, and the fix for each.
Show answer
Without a date filter the query scans the whole retained history, so add an event_date or event_time range. Without a service_name filter the query mixes events from different services, so add service_name = 'unityCatalog' when you want Unity Catalog events.