Sub topic 12
Common audit queries: access, changes, and denied requests
This chapter turns the structure of system.access.audit into working queries. It assumes you can read the table's columns (event_time, service_name, action_name, user_identity, request_params, response). It also assumes you have SELECT on the table, as granted in the permissions chapter, and a running SQL warehouse or notebook.
Start from a question
Most audit work falls into three questions. Each one maps to a different filter on the same table.
| Question | Main filter | Columns to read |
|---|---|---|
| Who accessed this object? | `action_name` for reads, plus the object name in `request_params` | `user_identity.email`, `event_time` |
| Who changed this object or its permissions? | `action_name` for create, update, delete, or permission changes | `user_identity.email`, `request_params`, `event_time` |
| Who was refused, and how often? | `response.status_code >= 400` | `user_identity.email`, `action_name`, `response.error_message` |
Always filter by date first
system.access.audit holds events from every service in the account and can be very large. Filtering on event_date lets Databricks skip data that falls outside the window. Add it to every query, even when you also filter on event_time. Then add service_name = 'unityCatalog' so you only see Unity Catalog events.
Discover what is available
Before writing a filter, check which actions your account actually records. Names differ by service, and new ones appear over time. This query lists the Unity Catalog actions seen in the last 30 days and how often each occurred.
SELECT
action_name,
count(*) AS event_count
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND event_date >= current_date() - INTERVAL 30 DAYS
GROUP BY action_name
ORDER BY event_count DESC;Use the result to pick the exact strings for later filters. Do not rely on names from memory.
To see what a given action puts in request_params, list its keys. request_params is a map from string to string, so map_keys works on it.
SELECT DISTINCT
action_name,
map_keys(request_params) AS param_keys
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND action_name = 'getTable'
AND event_date >= current_date() - INTERVAL 7 DAYS
LIMIT 20;Who accessed a table
Unity Catalog records a getTable event when a user or tool requests the metadata of a table. The next query filters on that action and a single table name.
SELECT
event_time,
user_identity.email AS user_email,
action_name,
request_params.full_name_arg AS table_name,
response.status_code
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND action_name = 'getTable'
AND request_params.full_name_arg = 'main.sales.daily_revenue'
AND event_date >= current_date() - INTERVAL 7 DAYS
ORDER BY event_time DESC;Three points matter when you read the output:
- The event is a metadata request. It shows that someone asked Unity Catalog about the table, for example by opening it in Catalog Explorer or running a query that resolves it. It is not a row-by-row record of the data they read.
- The status code matters. A
200means the request succeeded. A403means the user asked and was refused. Both appear in the same table, so keepresponse.status_codein the output. - The key name depends on the action.
full_name_argis the key forgetTable. Other actions use other keys, such as a securable name or a catalog name. Check with themap_keysquery above.
request_params is a map, so you can also write request_params['full_name_arg']. Both forms work in Databricks SQL. Bracket notation makes it clear that the value comes from a map and that a missing key returns NULL.
Counting readers
For an overview, group instead of listing. This answers "who uses this table most?"
SELECT
user_identity.email AS user_email,
count(*) AS requests,
max(event_time) AS last_seen
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND action_name = 'getTable'
AND request_params.full_name_arg = 'main.sales.daily_revenue'
AND response.status_code = 200
AND event_date >= current_date() - INTERVAL 30 DAYS
GROUP BY user_identity.email
ORDER BY requests DESC;Service principals appear here with their application ID in user_identity.email, so a value that does not look like an email address is normal.
Who changed an object
Changes appear as separate actions. Typical Unity Catalog names include createTable, deleteTable, and updatePermissions. Confirm them with the discovery query, then filter with IN.
SELECT
event_time,
user_identity.email AS user_email,
action_name,
request_params,
response.status_code
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND action_name IN ('createTable', 'deleteTable', 'updatePermissions')
AND event_date >= current_date() - INTERVAL 14 DAYS
ORDER BY event_time DESC;Selecting the whole request_params map is useful at first because it shows every key. Once you know which keys you need, pull out only those, such as the securable name, so the output is easier to scan.
A permission change that failed (status 403) is still worth reading. It shows someone tried to grant access they were not allowed to grant.
Denied requests
Denied and failed requests have a status code of 400 or higher. This query counts them per user and action over 30 days.
SELECT
user_identity.email AS user_email,
action_name,
count(*) AS event_count
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND event_date >= current_date() - INTERVAL 30 DAYS
AND response.status_code >= 400
GROUP BY user_identity.email, action_name
ORDER BY event_count DESC;Read the results by pattern:
- A few failures for one user on one action usually means a missing grant. Fix the grant.
- Many failures for one user across many different objects can mean a misconfigured job, or someone probing for data they should not see. Investigate before granting anything.
- A spike from one service principal often points to a job that lost access after a permission change. Compare its first failure time against your change events.
To see why requests failed, add the error message and a sample of target objects:
SELECT
event_time,
user_identity.email AS user_email,
action_name,
response.status_code,
response.error_message
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND event_date >= current_date() - INTERVAL 7 DAYS
AND response.status_code >= 400
ORDER BY event_time DESC
LIMIT 100;Not every status of 400 or higher is a security matter. Status 404 usually means the object did not exist, which happens when a script refers to a dropped table. Separate the codes before you raise an alert:
SELECT
response.status_code,
count(*) AS event_count
FROM system.access.audit
WHERE service_name = 'unityCatalog'
AND event_date >= current_date() - INTERVAL 30 DAYS
AND response.status_code >= 400
GROUP BY response.status_code
ORDER BY event_count DESC;Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Query returns no rows | Wrong `action_name` string, or `request_params` key is different | Run the discovery queries and copy exact values |
| Query is very slow | No `event_date` filter | Add a date window |
| `TABLE_OR_VIEW_NOT_FOUND` on the audit table | Missing `USE CATALOG` or `USE SCHEMA` on `system`, or system schema not enabled | Check the grants and ask a metastore admin to enable the `access` schema |
| User appears as a long ID | Service principal | Map the ID to a name in your own reference table |
| Recent events missing | Audit data arrives with a delay | Widen the window and re-run later |
Exercises
Exercise
A job that ran fine yesterday now fails. You suspect a permission was removed. Which two audit queries would you run, and how do you connect them?
Show answer
First run the denied-requests query, filtered to the job's service principal, to find the time of its first failure with a 403 status. Then run the change query on updatePermissions and related actions for the same window. If a successful permission change on the table happened just before the first failure, that change is the likely cause.
Exercise
Why should a query that counts table readers include response.status_code = 200?
Show answer
Without it, the counts include users who were refused (status 403). Those users asked for the table but did not get access, so counting them overstates who actually used it.