Check every role the session can use
Ana opens a fresh session with M17_ANALYST. A developer who also holds an engineering role can run a report with the same primary role selected. To explain the difference, inspect secondary roles and privileges inherited by all active roles.
Every normal session has one primary role and may have active secondary roles. For operations such as SELECT, permissions can come from the active roles and their descendants. For CREATE object statements, the primary role and the roles it inherits must supply the necessary permissions. The primary role normally owns the new object. Check for an applicable future OWNERSHIP grant.
USE SECONDARY ROLES NONE removes secondary roles from that session’s test, while inherited role and PUBLIC privileges still apply. Do permission tests from separate authorized test identities with only their intended role granted and no direct privilege grants. Under the documented user-based access model, direct user privileges are considered when secondary roles are set to ALL. Look at the actual roles reported by the client and session because connection settings can affect them.
Write a job matrix and test denials as well as successes
Least privilege means giving a person or process the smallest set of rights it needs for its job. Start with one job, one operation and one object boundary. Ana needs SELECT on approved reports. Ravi needs INSERT on the target he maintains. Dev needs CREATE TABLE in a sandbox. Mira reviews and approves grants through a delegated administrative role.
A positive test proves the intended action works. A negative test proves another action stays denied. Record both in the same scope and watch out for a shared ancestor, a second membership or PUBLIC access. Assign roles to workload identities separately from human memberships. Review them whenever someone moves teams or a workload is retired.
| Lab role | Must succeed | Must be denied |
|---|---|---|
| M17_ANALYST | Read approved orders | Insert into reporting orders |
| M17_ENG | Insert into reporting orders | Grant analyst access |
| M17_DEV | Create a table in the dev sandbox | Read REPORTING.ORDERS |
Prove why a read succeeds and a create still fails
Use a disposable account, SYSADMIN, SECURITYADMIN, one test operator and an approved M17_LAB_WH. Substitute the operator and warehouse. Build an account role with compute and another with data privileges. The test uses both roles to read and then changes only the primary role to prove the CREATE rule.
Prepare separate compute and data roles
SYSADMIN creates a one-row table. SECURITYADMIN places warehouse use on M17D_COMPUTE and places database, schema, SELECT and CREATE TABLE rights on M17D_DATA. The two roles are granted separately to the operator, with no inheritance between them.
USE ROLE SYSADMIN;
USE WAREHOUSE M17_LAB_WH;
CREATE DATABASE M17D_SESSION;
CREATE SCHEMA M17D_SESSION.REPORTING;
CREATE TABLE M17D_SESSION.REPORTING.ORDERS AS SELECT 101 AS order_id;
USE ROLE SECURITYADMIN;
CREATE ROLE M17D_COMPUTE;
CREATE ROLE M17D_DATA;
GRANT ROLE M17D_COMPUTE TO USER <TEST_OPERATOR>;
GRANT ROLE M17D_DATA TO USER <TEST_OPERATOR>;
GRANT USAGE ON WAREHOUSE M17_LAB_WH TO ROLE M17D_COMPUTE;
GRANT USAGE ON DATABASE M17D_SESSION TO ROLE M17D_DATA;
GRANT USAGE ON SCHEMA M17D_SESSION.REPORTING TO ROLE M17D_DATA;
GRANT SELECT ON TABLE M17D_SESSION.REPORTING.ORDERS TO ROLE M17D_DATA;
GRANT CREATE TABLE ON SCHEMA M17D_SESSION.REPORTING TO ROLE M17D_DATA;Read with both roles and create with the correct primary role
Start with compute as primary and no secondary roles. The read fails. Activate only M17D_DATA as secondary, so the read returns order 101. The marked CREATE still fails because compute is the primary role. Run the expected failures separately. Finally set data as primary and use a simple CREATE with no table read. SHOW GRANTS ON TABLE, run as SECURITYADMIN, should show M17D_DATA as owner.
USE ROLE M17D_COMPUTE;
USE SECONDARY ROLES NONE;
USE WAREHOUSE M17_LAB_WH;
-- Expected denial. Run alone.
SELECT order_id FROM M17D_SESSION.REPORTING.ORDERS;
USE SECONDARY ROLES M17D_DATA;
SELECT CURRENT_ROLE(), CURRENT_SECONDARY_ROLES();
SELECT order_id FROM M17D_SESSION.REPORTING.ORDERS;
-- Expected denial. Run alone.
CREATE TABLE M17D_SESSION.REPORTING.DENIED_TEST (order_id NUMBER);
USE ROLE M17D_DATA;
USE SECONDARY ROLES NONE;
CREATE TABLE M17D_SESSION.REPORTING.ALLOWED_TEST (order_id NUMBER);
USE ROLE SECURITYADMIN;
SHOW GRANTS ON TABLE M17D_SESSION.REPORTING.ALLOWED_TEST;Clear temporary roles and test resources
Drop only the isolated lab database. Remove the operator’s role memberships under SECURITYADMIN and drop the two roles. The warehouse was supplied and is kept.
USE ROLE SYSADMIN;
DROP DATABASE M17D_SESSION;
USE ROLE SECURITYADMIN;
REVOKE ROLE M17D_DATA FROM USER <TEST_OPERATOR>;
REVOKE ROLE M17D_COMPUTE FROM USER <TEST_OPERATOR>;
DROP ROLE M17D_DATA;
DROP ROLE M17D_COMPUTE;Choose a safe session test in production
A connection can choose a role explicitly or start from the user’s default. A default is a preference and does not create a grant. Snowflake reevaluates the secondary role set when statements execute with ALL. Direct user privileges are considered with ALL under the documented user-based access model. Check effective configuration in a fresh test session.
Use the intended identity for release acceptance. Capture CURRENT_ROLE and CURRENT_SECONDARY_ROLES, inspect inherited and PUBLIC privileges and first test a read the identity should be denied. A broad operator session can otherwise conceal a broken job grant.
Explain the design in an interview
Name the rule, then explain how you would prove it.
Can secondary roles help SELECT and CREATE in the same way?
A read can combine privileges through active roles. For CREATE, I pick a primary role whose inherited privileges provide the required rights. I verify the new object’s owner afterward.
Why can two users with the same primary role see different data?
I compare the active secondary roles, their child roles, and eligible direct user grants. I reproduce the query with the same connection context.
Words used in this lesson
- Primary role
- The current account role that provides creation authority and owns newly created objects.
- Secondary roles
- Additional active roles that can supply privileges for other statements.
- Least privilege
- Give each identity only the access needed for its work.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Snowflake Primary and Secondary Roles: Test Least Privilege
Read the active context
Inspect primary, secondary and inherited roles before interpreting a result.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Read the active context
Inspect primary, secondary and inherited roles before interpreting a result.
Primary role → Secondary roles → Inherited grants
- Prove the intended path
Use a dedicated identity and record an expected success and denial.
Clean test identity → Allowed action → Denied action
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Show the session context before a role test
USE ROLE M17_ANALYST;
USE SECONDARY ROLES NONE;
SELECT CURRENT_USER() AS user_name,
CURRENT_ROLE() AS primary_role,
CURRENT_SECONDARY_ROLES() AS secondary_roles,
CURRENT_WAREHOUSE() AS warehouse_name;What to expect and why
Run as the approved analyst test user after the role is granted. Expect M17_ANALYST as the primary role and no active secondary roles. CURRENT_SECONDARY_ROLES returns a JSON-formatted description. The current warehouse can be unset. Set the approved lab warehouse before a table query.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
An analyst can still read a table after SELECT is revoked from M17_ANALYST. What would you inspect?
Compare your reasoning
Check the role’s inherited grants, active secondary roles, PUBLIC, any direct user grants and alternate paths such as a granted view. Retest the exact table with the intended test identity.
Official sources and curriculum
Module 17 · See module scope
By Rohit Ray · Independent educational lesson · Explore this visual lab