Sub topic 12
Common audit queries: access, changes, and denied requests
This chapter shows three audit query patterns on system.access.audit: who accessed an object, what changed, and which requests failed. It assumes you can write basic SQL. It also assumes you know the Unity Catalog three-level namespace and have read the previous chapter on the audit table schema.
Prerequisites and setup
You need SELECT on system.access.audit, plus USE CATALOG on system and USE SCHEMA on system.access. Ordinary users do not have these by default. Grant them to a governance group rather than to everyone.
GRANT USE CATALOG ON CATALOG system TO `data-governance`;
GRANT USE SCHEMA ON SCHEMA system.access TO `data-governance`;
GRANT SELECT ON TABLE system.access.audit TO `data-governance`;The audit table is large. Always filter on event_date first. The table is partitioned by date, so this filter limits how much data a query scans. Then filter on service_name and action_name.
Pattern 1: who accessed an object
Unity Catalog logs metadata requests under service_name = 'unityCatalog'. The getTable action records that a user requested metadata for a table. The table name is in the request_params map.
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;Each row is one event. user_identity.email is the person or service principal that made the request. response.status_code tells you whether the request worked.
Pattern 2: who changed an object
Change events use different action names, such as createTable, deleteTable, and updateTable. You can list the action names that exist in your own account before you pick filters.
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;Once you know the names, filter on the ones that modify objects. Use the same request_params field to restrict the result to one table.
Pattern 3: denied and failed requests
A status code of 400 or higher means the request failed. Repeated 403 responses for one user can mean a missing grant. They can also mean someone is probing objects they should not see.
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;Common errors
- Scanning without a date filter. The query runs slowly and costs more. Add an
event_datebound. - Wrong parameter key.
request_paramskeys differ by action.full_name_argworks forgetTable, but other actions may use other keys. Inspect a sample row withSELECT request_params FROM ... LIMIT 10first. - Treating
getTableas a data read. It records a metadata request. It does not prove that rows were read. Use lineage or other audit actions for data movement questions. - Missing results. If you get an empty result or a permission error, check your grants on the
systemcatalog.
Exercise
Write a filter condition that restricts audit results to failed Unity Catalog requests in the last 14 days.
Show answer
Use service_name = 'unityCatalog' AND event_date >= current_date() - INTERVAL 14 DAYS AND response.status_code >= 400.
Exercise
Why should you check request_params on a sample row before filtering on a key such as full_name_arg?
Show answer
The keys inside request_params vary by action. A key that works for getTable may not exist for another action, so a filter on it would silently return no rows.