ShopKart's access rules
ShopKart's customers table has personal data (names, emails). The rules the founders want:
| Who | Role | Can see |
|---|---|---|
| Data engineers | TEACHTRAILS_ROLE (owns the data) | Everything |
| Analysts | SHOPKART_ANALYST | All tables and rows, but emails masked |
| The Kolkata store manager | SHOPKART_KOLKATA | Only Kolkata customers, emails masked |
๐1. Authentication
Who are you? SSO, password + MFA, or key pair
๐ญ2. Roles and grants (RBAC)
May your roles use this database, schema and table?
๐ฆ3. Row access policy
Which rows may you see?
๐ญ4. Masking policy
What do the values in each column look like for you?
Roles and grants
An admin creates the roles. We ran this in Snowsight as ACCOUNTADMIN on our trial; in a company, USERADMIN creates roles and SECURITYADMIN manages grants.
-- Run by an admin (USERADMIN creates roles; SECURITYADMIN / ACCOUNTADMIN can grant anything)
USE ROLE ACCOUNTADMIN;
CREATE ROLE IF NOT EXISTS shopkart_analyst COMMENT = 'Analysts: read-only, masked PII';
CREATE ROLE IF NOT EXISTS shopkart_kolkata COMMENT = 'Kolkata manager: own city only';
GRANT ROLE shopkart_analyst TO ROLE SYSADMIN; -- keep the hierarchy connected
GRANT ROLE shopkart_kolkata TO ROLE SYSADMIN;
GRANT USAGE ON WAREHOUSE shopkart_wh TO ROLE shopkart_analyst;
GRANT USAGE ON WAREHOUSE shopkart_wh TO ROLE shopkart_kolkata;
-- ...then GRANT ROLE shopkart_analyst TO USER <each analyst>;Then the owner of the data grants read access. To read a table, a role needs every level: USAGE on the database, USAGE on the schema, SELECT on the table, and USAGE on a warehouse to run the query.
-- Run by the data owner (TEACHTRAILS_ROLE owns the SHOPKART database)
USE SCHEMA shopkart.raw;
GRANT USAGE ON DATABASE shopkart TO ROLE shopkart_analyst;
GRANT USAGE ON SCHEMA shopkart.raw TO ROLE shopkart_analyst;
GRANT SELECT ON ALL TABLES IN SCHEMA shopkart.raw TO ROLE shopkart_analyst;
GRANT USAGE ON DATABASE shopkart TO ROLE shopkart_kolkata;
GRANT USAGE ON SCHEMA shopkart.raw TO ROLE shopkart_kolkata;
GRANT SELECT ON TABLE customers TO ROLE shopkart_kolkata;
SHOW GRANTS TO ROLE shopkart_analyst;| privilege | granted_on | name |
|---|---|---|
| USAGE | DATABASE | SHOPKART |
| USAGE | SCHEMA | SHOPKART.RAW |
| SELECT | TABLE | SHOPKART.RAW.CUSTOMERS |
| SELECT | TABLE | SHOPKART.RAW.EVENTS |
| SELECT | TABLE | SHOPKART.RAW.ORDERS |
| SELECT | TABLE | SHOPKART.RAW.PRODUCTS |
| USAGE | WAREHOUSE | SHOPKART_WH |
ON ALL TABLES covers tables that exist now. For tables created later you'd add a future grant, but look what happened when the schema owner tried:
-- Also give analysts every table created in this schema from now on
GRANT SELECT ON FUTURE TABLES IN SCHEMA shopkart.raw TO ROLE shopkart_analyst;003001 (42501): SQL access control error:
Insufficient privileges to operate on SCHEMA 'RAW'. Your primary role TEACHTRAILS_ROLE or one of
your secondary roles must have MANAGE GRANTS granted on ACCOUNT.Owning a schema isn't enough for future grants: they need MANAGE GRANTS, which SECURITYADMIN has. That's by design, so security admins keep control. Another option is a managed access schema (CREATE SCHEMA ... WITH MANAGED ACCESS), where only the schema owner or MANAGE GRANTS can grant anything, not each table's owner.
| Good practice | Why |
|---|---|
| Access roles + functional roles | Small roles hold privileges (e.g. SHOPKART_READ); job roles (ANALYST) are granted those. Easy to change. |
| Grant every custom role to SYSADMIN | Otherwise objects it creates are invisible to admins |
| Least privilege | Analysts get SELECT, not INSERT/DELETE; pipelines get only what they write |
| Separate service users | Pipelines use their own users with key-pair auth, never a person's login |
Dynamic data masking
A masking policy is a small function: it takes the real value and returns what the current user is allowed to see. You attach it to columns; the table itself doesn't change.
-- Dynamic data masking: who sees real email addresses?
CREATE OR REPLACE MASKING POLICY email_mask AS (val VARCHAR) RETURNS VARCHAR ->
CASE
WHEN IS_ROLE_IN_SESSION('TEACHTRAILS_ROLE') THEN val -- data engineers: real value
ELSE REGEXP_REPLACE(val, '^(.).*(@.*)$', '\\1*****\\2') -- everyone else: m*****@example.com
END;
ALTER TABLE customers MODIFY COLUMN email SET MASKING POLICY email_mask;USE ROLE teachtrails_role;
SELECT CURRENT_ROLE() AS role, customer_id, name, email, city
FROM shopkart.raw.customers ORDER BY customer_id LIMIT 3;
USE ROLE shopkart_analyst;
SELECT CURRENT_ROLE() AS role, CURRENT_SECONDARY_ROLES() AS secondary, customer_id, name, email
FROM shopkart.raw.customers ORDER BY customer_id LIMIT 3;| ROLE | CUSTOMER_ID | NAME | CITY | |
|---|---|---|---|---|
| TEACHTRAILS_ROLE | 1 | Meera Sharma | [email protected] | Delhi |
| TEACHTRAILS_ROLE | 2 | Sneha Iyer | [email protected] | Kolkata |
| TEACHTRAILS_ROLE | 3 | Rahul Mehta | [email protected] | Bengaluru |
| ROLE | SECONDARY | CUSTOMER_ID | NAME | |
|---|---|---|---|---|
| SHOPKART_ANALYST | {"roles":"SHOPKART_KOLKATA,TEACHTRAILS_ROLE","value":"ALL"} | 1 | Meera Sharma | [email protected] |
| SHOPKART_ANALYST | {"roles":"SHOPKART_KOLKATA,TEACHTRAILS_ROLE","value":"ALL"} | 2 | Sneha Iyer | [email protected] |
| SHOPKART_ANALYST | {"roles":"SHOPKART_KOLKATA,TEACHTRAILS_ROLE","value":"ALL"} | 3 | Rahul Mehta | [email protected] |
USE SECONDARY ROLES NONE; -- only the primary role counts now
SELECT CURRENT_ROLE() AS role, customer_id, name, email, city
FROM shopkart.raw.customers ORDER BY customer_id LIMIT 3;| ROLE | CUSTOMER_ID | NAME | CITY | |
|---|---|---|---|---|
| SHOPKART_ANALYST | 1 | Meera Sharma | m*****@example.com | Delhi |
| SHOPKART_ANALYST | 2 | Sneha Iyer | s*****@example.com | Kolkata |
| SHOPKART_ANALYST | 3 | Rahul Mehta | r*****@example.com | Bengaluru |
Now the analyst sees m*****@example.com. Same table, same query, different result, decided at query time. Other common masks: show the last 4 digits of a phone number, return SHA2(val) so analysts can still join and count distinct customers without seeing them, or NULL.
| Function | Checks |
|---|---|
| CURRENT_ROLE() | Only the primary role |
| IS_ROLE_IN_SESSION('R') | Primary or secondary roles, including roles they inherit (recommended) |
| INVOKER_ROLE() | The role running the statement (useful inside views) |
Row access policies
A row access policy returns TRUE or FALSE for each row. Rows where it returns FALSE simply don't exist for that user. A small mapping table says which role may see which city:
USE ROLE teachtrails_role;
USE SCHEMA shopkart.raw;
-- Row access policy: managers see only their own city's customers
CREATE OR REPLACE TABLE city_access (role_name VARCHAR, city VARCHAR); -- a mapping table
INSERT INTO city_access VALUES ('SHOPKART_KOLKATA', 'Kolkata');
CREATE OR REPLACE ROW ACCESS POLICY city_rows AS (customer_city VARCHAR) RETURNS BOOLEAN ->
IS_ROLE_IN_SESSION('TEACHTRAILS_ROLE') -- data engineers: every row
OR IS_ROLE_IN_SESSION('SHOPKART_ANALYST') -- analysts: every row (emails still masked)
OR EXISTS (SELECT 1 FROM city_access a
WHERE a.role_name = CURRENT_ROLE() AND a.city = customer_city);
ALTER TABLE customers ADD ROW ACCESS POLICY city_rows ON (city);USE ROLE shopkart_kolkata;
USE SECONDARY ROLES NONE;
SELECT city, COUNT(*) AS customers FROM shopkart.raw.customers GROUP BY city;
SELECT customer_id, name, email, city FROM shopkart.raw.customers ORDER BY customer_id LIMIT 3;
USE ROLE shopkart_analyst;
SELECT city, COUNT(*) AS customers FROM shopkart.raw.customers GROUP BY city ORDER BY city;| CITY | CUSTOMERS |
|---|---|
| Kolkata | 37 |
| CUSTOMER_ID | NAME | CITY | |
|---|---|---|---|
| 2 | Sneha Iyer | s*****@example.com | Kolkata |
| 5 | Siddharth Mehta | s*****@example.com | Kolkata |
| 10 | Rahul Gupta | r*****@example.com | Kolkata |
| CITY | CUSTOMERS |
|---|---|
| Bengaluru | 44 |
| Delhi | 45 |
| Kolkata | 37 |
| Mumbai | 40 |
| Pune | 34 |
The Kolkata manager sees 37 customers, all from Kolkata, with masked emails: both policies work together. The analyst sees all 200. To give the Mumbai manager access, you add one row to city_access; no policy changes.
-- Which policies protect this table?
SELECT policy_name, policy_kind, ref_column_name
FROM TABLE(information_schema.policy_references(
REF_ENTITY_NAME => 'shopkart.raw.customers', REF_ENTITY_DOMAIN => 'table'));| POLICY_NAME | POLICY_KIND | REF_COLUMN_NAME |
|---|---|---|
| EMAIL_MASK | MASKING_POLICY | |
| CITY_ROWS | ROW_ACCESS_POLICY | NULL |
More governance tools
| Feature | What it does |
|---|---|
| Secure views | CREATE SECURE VIEW hides the view's definition and blocks optimizer tricks that could leak hidden rows; used for sharing |
| Object tags | Label columns (e.g. pii = 'email'); tag-based masking applies a policy to every column with that tag |
| Data classification | Snowflake scans columns and suggests tags like EMAIL or PHONE_NUMBER automatically |
| Access history | SNOWFLAKE.ACCOUNT_USAGE.ACCESS_HISTORY: who read or wrote which columns (Enterprise) |
| Network policies | Allow logins only from certain IP addresses or private links |
| Trust Center | Snowsight page that scans the account for risky settings (e.g. users without MFA) |
Logging in safely
Snowflake is moving all human users to multi-factor login and blocking password-only sign-ins. Pipelines and tools should use service users with key-pair authentication or OAuth. For this course we created exactly that: TEACHTRAILS_BOT, a TYPE = SERVICE user that signs in with an RSA key kept on our machine, no password at all. Every query in this course ran through it.
๐ฏ Interview questions on this topic
Try to answer out loud first, then tap to check.
Q1Explain Snowflake's access control model.Fresher+
Q2What privileges does a user need to query a table?Fresher+
Q3How do you mask PII so only some roles see real values?1โ3 yrs+
Q4How would you restrict regional managers to their region's rows?1โ3 yrs+
Q5What are future grants and what are their limits?1โ3 yrs+
Q6An analyst can see unmasked PII despite a masking policy. How do you debug it?3+ yrs+
Key takeaways
- โRoles decide what you can touch; policies decide what you see.
- โGrant read access at every level, and keep custom roles under SYSADMIN.
- โMasking and row access policies work together at query time without copying data.
- โTest policies the way real users connect: secondary roles matter.
Frequently asked questions
What is RBAC in Snowflake?
Role-based access control: privileges (like SELECT on a table) are granted to roles, and roles are granted to users or to other roles. A user acts through one primary role at a time, plus any secondary roles, and can do whatever those roles allow.
What's the difference between a masking policy and a row access policy?
A masking policy changes what a column's values look like for different users (for example, shows emails as m*****@example.com). A row access policy decides which rows a user can see at all (for example, only Kolkata customers). Both are written once and applied to tables; both are Enterprise features.
Why did my masking policy show real data to an analyst?
Probably secondary roles. New users often have DEFAULT_SECONDARY_ROLES = ('ALL'), so every role granted to them is active, and IS_ROLE_IN_SESSION finds a privileged role. Test with USE SECONDARY ROLES NONE, and think about which roles each user really has.
Should data engineers use ACCOUNTADMIN?
No. Use a functional role that owns your databases (like our TEACHTRAILS_ROLE), granted to SYSADMIN. Keep ACCOUNTADMIN for a few admins, protected with MFA, for account-level tasks.
How do Snowflake users log in securely?
People: SSO through your company's identity provider (Okta, Microsoft Entra ID) or passwords with MFA; Snowflake is phasing out single-factor password logins. Programs and pipelines: key-pair authentication, OAuth, or programmatic access tokens, with service users (TYPE = SERVICE) that can't log in with a password. That's how this course's test user connects.
๐ง Quick check: Snowflake security
1. In Snowflake, privileges are granted to...
2. To read a table, a role needs...
3. A Kolkata manager should only see Kolkata customers. Use a...
4. Our analyst saw real emails until we ran USE SECONDARY ROLES NONE. Why?
5. GRANT SELECT ON FUTURE TABLES failed for the schema owner. What was missing?
Checking your accessโฆ