Module 17 · Lesson 6 of 12Detailed study · about 24 min

Snowflake Primary and Secondary Roles: Test Least Privilege

Inspect active roles, distinguish read checks from object creation and test a clean session to prove the intended permission path.

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.

A permission test matrix for Maple Market
Lab roleMust succeedMust be denied
M17_ANALYSTRead approved ordersInsert into reporting orders
M17_ENGInsert into reporting ordersGrant analyst access
M17_DEVCreate a table in the dev sandboxRead 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.

SQL · configuration template
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.

SQL · configuration template
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.

SQL · configuration template
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
Interactive explainer1 / 2

Snowflake Primary and Secondary Roles: Test Least Privilege

Primary role
Secondary roles
Inherited grants

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
  1. Read the active context

    Inspect primary, secondary and inherited roles before interpreting a result.

    Primary role → Secondary roles → Inherited grants

  2. 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.

  1. No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
  2. 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.
  3. 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.
  4. 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

SQL · configuration template
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.

Which active role determines ownership of an object created in a normal session?

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