Sub topic 14
Auditing access with audit logs
This chapter assumes you can run SQL in a Databricks workspace and that you have already granted and revoked privileges in Unity Catalog. Granting access is only half of governance. You also need to check afterward who used that access and what changed.
What the audit log records
Databricks writes account activity to the system.access.audit system table. Each row is one event. For Unity Catalog, events include permission changes, object creation and deletion, and data access requests.
The columns you will use most often are:
| Column | Meaning |
|---|---|
| `event_time` | When the event happened. |
| `user_identity.email` | The user or service principal that performed the action. |
| `service_name` | The service that produced the event. Unity Catalog events use `unityCatalog`. |
| `action_name` | The operation, such as `createTable` or `updatePermissions`. |
| `request_params` | A map of details about the request, such as the object name. |
| `response.status_code` | Whether the request succeeded. A value of 200 means success. |
Reading system tables requires access to them. A metastore admin, or a user who has been granted SELECT on the table together with USE privileges on the system catalog and its access schema, can query it. Treat that access as sensitive, because the log shows what other people did.
Query recent Unity Catalog activity
Start with the latest events.
SELECT event_time, user_identity.email, action_name
FROM system.access.audit
WHERE service_name = 'unityCatalog'
ORDER BY event_time DESC
LIMIT 20;This returns the 20 most recent Unity Catalog events. Always add a time filter on large workspaces, because the table grows quickly.
Answer common questions
The pattern is the same each time: filter on service_name, then on action_name or a time window.
Who changed permissions in the last week?
SELECT event_time, user_identity.email, action_name, request_params
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND action_name = 'updatePermissions'
AND event_time >= current_timestamp() - INTERVAL 7 DAYS
ORDER BY event_time DESC;Which requests were denied?
SELECT event_time, user_identity.email, action_name, response.status_code
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND response.status_code = 403
ORDER BY event_time DESC
LIMIT 50;A status code of 403 means the request was forbidden. Repeated 403 events from one principal can show a missing grant, or someone probing for data they should not see.
Where auditing fits in the workflow
Auditing closes the loop on the permission model. You grant, then you verify.
Review the log on a schedule. Typical checks are unexpected permission changes, access to sensitive tables by principals outside the expected groups, and spikes in denied requests.
Common errors
- Querying without a time filter. The query scans a large table and is slow. Filter on
event_time. - Assuming the log is instant. System table data can arrive with a delay. Do not rely on it for real-time alerts.
- Matching the wrong field. The user is in
user_identity.email. For service principals this field holds the application ID, not a person's email address. - Expecting grants to reveal usage.
SHOW GRANTStells you who can access an object. Only the audit log tells you who did.
Exercise
Write a query that lists the Unity Catalog events from the last 24 hours that were denied with status code 403, newest first. Which two columns tell you who tried and when?
Show answer
SELECT eventtime, useridentity.email, actionname FROM system.access.audit WHERE servicename = 'unityCatalog' AND response.statuscode = 403 AND eventtime >= currenttimestamp() - INTERVAL 1 DAY ORDER BY eventtime DESC; The columns are useridentity.email (who) and eventtime (when).