What you will be able to do
- Choose between ACCOUNT_USAGE views and the Information Schema based on dropped objects, latency and retention
- Give a non-admin role audit access through IMPORTED PRIVILEGES or the narrower SNOWFLAKE database roles
- Write login-history audit queries and explain who can access the ORGANIZATION_USAGE schema
1.ACCOUNT_USAGE: the long-term audit record
The shared SNOWFLAKE database contains an ACCOUNT_USAGE schema with object metadata and historical usage for your account. A READER_ACCOUNT_USAGE schema does the same for any reader accounts you have created. ACCOUNT_USAGE views have the same structure and names as their Information Schema counterparts. Three differences decide which one an audit question needs.
| Difference | ACCOUNT_USAGE | Information Schema |
|---|---|---|
| Includes dropped objects | Yes | No |
| Latency of data | From 45 minutes to 3 hours (varies by view) | None |
| Retention of historical data | 1 year | From 7 days to 6 months (varies by view/table function) |
Because dropped objects stay in the views, an object name can repeat. ID columns tell apart records that share a name, and many object views have a DELETED timestamp. When an object-name column is NULL, the object has been dropped. These views cover the user and query activity this objective asks about.
| View | Type | Latency | SNOWFLAKE database role |
|---|---|---|---|
| QUERY_HISTORY | Historical | 45 minutes | GOVERNANCE_VIEWER |
| ACCESS_HISTORY (Enterprise Edition or higher) | Historical | 3 hours | GOVERNANCE_VIEWER |
| LOGIN_HISTORY | Historical | 2 hours | SECURITY_VIEWER |
| SESSIONS | Historical | 3 hours | SECURITY_VIEWER |
| USERS / ROLES | Object | 2 hours | SECURITY_VIEWER |
| GRANTS_TO_ROLES / GRANTS_TO_USERS | Object | 2 hours | SECURITY_VIEWER |
Checkpoint 1 of 4· Check yourself
An auditor must find which users ran queries against a table that was dropped four months ago. Where should they look?
The Information Schema excludes dropped objects and keeps between 7 days and 6 months of history. ACCOUNT_USAGE includes dropped objects and keeps historical data for a year.
“Certain account usage views provide historical usage metrics. The retention period for these views is 1 year (365 days).”Source: docs.snowflake.com
Sources1
2.Giving an auditor role access to ACCOUNT_USAGE
Every user can see the SNOWFLAKE database, but its schemas need a grant from a user with ACCOUNTADMIN. There are two ways to give that grant. IMPORTED PRIVILEGES on the SNOWFLAKE database gives broad access in a single statement. SNOWFLAKE database roles are narrower: each one has SELECT on a defined set of ACCOUNT_USAGE views, and you grant it to an account role. Snowflake recommends database roles so that you do not expose organization-level data by accident.
USE ROLE ACCOUNTADMIN; GRANT IMPORTED PRIVILEGES ON DATABASE SNOWFLAKE TO ROLE SYSADMIN; GRANT IMPORTED PRIVILEGES ON DATABASE SNOWFLAKE TO ROLE customrole1;USE ROLE customrole1; SELECT database_name, database_owner FROM SNOWFLAKE.ACCOUNT_USAGE.DATABASES;| Database role | Visibility into | Example views |
|---|---|---|
| OBJECT_VIEWER | Object metadata | DATABASES, TABLES, VIEWS |
| USAGE_VIEWER | Historical usage information | WAREHOUSE_METERING_HISTORY, STORAGE_USAGE |
| GOVERNANCE_VIEWER | Data-governance-related information | QUERY_HISTORY, ACCESS_HISTORY, MASKING_POLICIES |
| SECURITY_VIEWER | Security-based information | LOGIN_HISTORY, USERS, GRANTS_TO_ROLES, SESSIONS |
For an auditor, which database role to grant depends on the question being asked. A role that has SECURITY_VIEWER can read LOGIN_HISTORY, USERS and the GRANTS_TO_* views, but it cannot read QUERY_HISTORY, which belongs to GOVERNANCE_VIEWER. If the auditor needs both logins and queries, grant both database roles. There is also READER_USAGE_VIEWER, which covers the READER_ACCOUNT_USAGE views for reader accounts.
Checkpoint 2 of 4· Put it in order
Put the steps for letting customrole1 query ACCOUNT_USAGE in order
- 1.USE ROLE customrole1
- 2.GRANT IMPORTED PRIVILEGES ON DATABASE SNOWFLAKE TO ROLE customrole1
- 3.SELECT database_name, database_owner FROM SNOWFLAKE.ACCOUNT_USAGE.DATABASES
- 4.USE ROLE ACCOUNTADMIN
The grant has to come from ACCOUNTADMIN. Only after that can a user acting as customrole1 query the view.
“USE ROLE ACCOUNTADMIN; GRANT IMPORTED PRIVILEGES ON DATABASE SNOWFLAKE TO ROLE SYSADMIN;”Source: docs.snowflake.com
Sources1
3.Auditing login activity
LOGIN_HISTORY is the starting point for user-activity audits. The documented examples calculate failed logins per user and the login failure rate for the month so far. A variant groups by reported_client_type as well, which shows which client the failures came from. Write the query to select only the columns you need. Snowflake may change these views, so avoid SELECT *.
select user_name, sum(iff(is_success = 'NO', 1, 0)) as failed_logins, count(*) as logins, sum(iff(is_success = 'NO', 1, 0)) / nullif(count(*), 0) as login_failure_rate from login_history where event_timestamp > date_trunc(month, current_date) group by 1 order by 4 desc;Checkpoint 3 of 4· Fill the gap
Which ACCOUNT_USAGE view completes this failed-login audit query?
select user_name, sum(iff(is_success = 'NO', 1, 0)) as failed_logins, count(*) as logins, sum(iff(is_success = 'NO', 1, 0)) / nullif(count(*), 0) as login_failure_rate from ? where event_timestamp > date_trunc(month, current_date) group by 1 order by 4 desc;Each login attempt, including its is_success outcome and event_timestamp, is recorded in LOGIN_HISTORY.
Source: docs.snowflake.comSources1
4.ORGANIZATION_USAGE: auditing across accounts
ACCOUNT_USAGE covers a single account. ORGANIZATION_USAGE, also in the shared SNOWFLAKE database, provides historical usage data for every account in the organization. The schema exists in the organization account and in any regular account where the ORGADMIN role is enabled. By default, only users with GLOBALORGADMIN can query its views in the organization account. To give access to other users, the organization administrator grants an application role, such as SNOWFLAKE.ORG_USAGE_ADMIN, which opens every view in the schema.
USE ROLE GLOBALORGADMIN;
GRANT APPLICATION ROLE SNOWFLAKE.ORG_USAGE_ADMIN TO ROLE custom_role;
GRANT ROLE custom_role TO USER joe;When you compare cost-related ACCOUNT_USAGE views with the matching ORGANIZATION_USAGE views, set the session time zone to UTC before querying the ACCOUNT_USAGE view, using ALTER SESSION SET TIMEZONE = UTC;. The sources behind this lesson do not list the individual ORGANIZATION_USAGE views. The per-user login and query views described above are documented in ACCOUNT_USAGE, so audit those there.
Checkpoint 4 of 4· Fill the gap
Which application role gives custom_role access to every ORGANIZATION_USAGE view?
USE ROLE GLOBALORGADMIN;
GRANT APPLICATION ROLE SNOWFLAKE. ? TO ROLE custom_role;
GRANT ROLE custom_role TO USER joe;SNOWFLAKE.ORG_USAGE_ADMIN opens all ORGANIZATION_USAGE views in the organization account. USAGE_VIEWER and SECURITY_VIEWER are database roles for ACCOUNT_USAGE.
Source: docs.snowflake.comExam traps
Each one states something that sounds right. Open it to see what is actually true.
1.ACCOUNT_USAGE views show activity in real time, just like the Information Schema.Why is that wrong?
ACCOUNT_USAGE views lag by between 45 minutes and 3 hours depending on the view. For activity from the last few minutes, use the Information Schema.
Covered in ACCOUNT_USAGE: the long-term audit record
2.Granting IMPORTED PRIVILEGES on SNOWFLAKE is the least-privilege way to give an auditor ACCOUNT_USAGE access.Why is that wrong?
Snowflake recommends SNOWFLAKE database roles such as SECURITY_VIEWER, which limit access to particular views and avoid exposing organization-level data.
3.Any ACCOUNTADMIN can query ORGANIZATION_USAGE in the organization account by default.Why is that wrong?
By default, only users with GLOBALORGADMIN can access those views. Anyone else needs an application role such as ORG_USAGE_ADMIN.
Covered in ORGANIZATION_USAGE: auditing across accounts
Practise it for real
Give a custom role ACCOUNT_USAGE access and run a month-to-date failed-login audit
1.Run USE ROLE ACCOUNTADMIN; GRANT IMPORTED PRIVILEGES ON DATABASE SNOWFLAKE TO ROLE customrole1;
Why: SNOWFLAKE schemas can only be queried after a grant from ACCOUNTADMIN.
You should see: The statements succeed and customrole1 can now read ACCOUNT_USAGE.
2.Switch with USE ROLE customrole1; and run SELECT database_name, database_owner FROM SNOWFLAKE.ACCOUNT_USAGE.DATABASES;
Why: This confirms the grant works before you run audit queries.
You should see: Rows listing databases and the roles that own them.
3.Run the failed-logins-by-user query against login_history.
Why: LOGIN_HISTORY records each login attempt along with whether it succeeded.
You should see: One row per user showing failed_logins, logins and login_failure_rate, highest failure rate first. Activity from the last two hours may not appear yet.
Stuck? Get a nudge
If you would rather not grant IMPORTED PRIVILEGES, grant the SNOWFLAKE database role SECURITY_VIEWER to the role instead. It covers LOGIN_HISTORY.
Sources
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“In the SNOWFLAKE database, the ACCOUNT_USAGE and READER_ACCOUNT_USAGE schemas enable querying object metadata, as well as historical usage data”
↩︎ ACCOUNT_USAGE: the long-term audit record“If a column for an object name (for example, the TABLE_NAME column) is NULL, that object has been dropped.”
↩︎ ACCOUNT_USAGE: the long-term audit record“By default, the SNOWFLAKE database is visible to all users”
↩︎ Giving an auditor role access to ACCOUNT_USAGE“The SECURITY_VIEWER role provides visibility into security-based information.”
↩︎ Giving an auditor role access to ACCOUNT_USAGE“Avoid selecting all columns from these views.”
↩︎ Auditing login activity“you must first set the timezone of the session to UTC.”
↩︎ ORGANIZATION_USAGE: auditing across accounts“For most of the views, the latency is 2 hours (120 minutes).”
↩︎ Exam trap 1“To avoid unintentionally granting access to organization-level data, consider using SNOWFLAKE database roles to grant access to views in the ACCOUNT_USAGE schema.”
↩︎ Exam trap 2“In contrast, views/table functions in the Snowflake Information Schema do not have any latency.”
↩︎ Prediction“Certain account usage views provide historical usage metrics. The retention period for these views is 1 year (365 days).”
↩︎ Checkpoint“USE ROLE ACCOUNTADMIN; GRANT IMPORTED PRIVILEGES ON DATABASE SNOWFLAKE TO ROLE SYSADMIN;”
↩︎ Checkpoint - 2.
“Snowflake provides historical usage data for all accounts in your organization via the ORGANIZATION_USAGE schema in a shared database named SNOWFLAKE.”
↩︎ ORGANIZATION_USAGE: auditing across accounts“The ORGANIZATION_USAGE schema is available in the organization account and a regular account that has the ORGADMIN role enabled.”
↩︎ ORGANIZATION_USAGE: auditing across accounts“By default, only users granted the GLOBALORGADMIN role can access ORGANIZATION_USAGE views in the organization account.”
↩︎ Exam trap 3