Before you beginSet up the Maple Market governance lab
Why the same customer value can appear three ways
Maple Market Support needs a full address to contact a customer. The analyst only needs the domain to compare email providers. A restricted reader must never see either part. A masking policy is a rule Snowflake evaluates when a query uses a protected column. The stored value stays the same. Full masking hides the whole value. Partial masking exposes a small approved part.
Encryption protects data with keys at rest or in transit. Masking controls the value a query can return or operate on, even after a permitted connection. It is evaluated where the column is used, including filters and joins. A filter on a raw email may stop matching for a role whose email is masked. Masking is also different from granting read access. A consumer still needs permission on the table or view.
- Query the contact viewReference masked EMAIL to Evaluate the email rule
- Evaluate the email ruleSupport role is active to Support
- Evaluate the email ruleAnalyst with verified address to Analyst
- Evaluate the email ruleOtherwise to Restricted
- Query the contact view
- The role must have SELECT.
- Evaluate the email rule
- Read the role context and CONTACT_VERIFIED.
- Support
- Full address.
- Analyst
- Domain on a verified address.
- Restricted
- REDACTED.
Create and attach full and conditional masking policies
Use Enterprise Edition or higher and the lab owner after seeding CORE. The schema grants CREATE MASKING POLICY to the creator. The lab keeps the table and policies under one owner, with APPLY on the specific policies. In a decentralized team, the policy owner can grant that APPLY to an approved table owner. A central policy administrator can use account-level APPLY MASKING POLICY. Avoid handing that account-wide power to readers.
The first argument VAL is the protected value. The second is the Boolean verification flag. RETURNS VARCHAR must match the first argument’s type. USING passes the columns in that order. The first CASE branch preserves a NULL email. The next allows support and the lab owner. The final branches show the verified domain to analysts and hide everything else. The full-contact policy similarly protects names, phones and sales email.
IS_ROLE_IN_SESSION checks the active primary and secondary roles and their inherited roles. CURRENT_ROLE gives the active primary role name. Because the India role inherits the base analyst role, the first function finds it even when the second returns M18_IN. An unrelated reader granted Support as an active secondary role could see full data. This is why the test uses distinct users and turns secondary roles off.
USE ROLE M18_OWNER;
CREATE MASKING POLICY LS_M18_GOV_LAB.GOV.CONTACT_EMAIL
AS (VAL VARCHAR, VERIFIED BOOLEAN) RETURNS VARCHAR ->
CASE
WHEN VAL IS NULL THEN NULL
WHEN IS_ROLE_IN_SESSION('M18_OWNER')
OR IS_ROLE_IN_SESSION('M18_SUPPORT') THEN VAL
WHEN IS_ROLE_IN_SESSION('M18_ANALYST') AND COALESCE(VERIFIED,FALSE)
AND POSITION('@' IN VAL) > 1 THEN '***@' || SPLIT_PART(VAL,'@',2)
ELSE 'REDACTED'
END;
GRANT APPLY ON MASKING POLICY LS_M18_GOV_LAB.GOV.CONTACT_EMAIL TO ROLE M18_OWNER;
ALTER TABLE LS_M18_GOV_LAB.CORE.CUSTOMERS MODIFY COLUMN EMAIL
SET MASKING POLICY LS_M18_GOV_LAB.GOV.CONTACT_EMAIL
USING (EMAIL, CONTACT_VERIFIED);
CREATE MASKING POLICY LS_M18_GOV_LAB.GOV.FULL_CONTACT
AS (VAL VARCHAR) RETURNS VARCHAR ->
CASE
WHEN VAL IS NULL THEN NULL
WHEN IS_ROLE_IN_SESSION('M18_OWNER')
OR IS_ROLE_IN_SESSION('M18_SUPPORT') THEN VAL
ELSE 'REDACTED'
END;
GRANT APPLY ON MASKING POLICY LS_M18_GOV_LAB.GOV.FULL_CONTACT TO ROLE M18_OWNER;
ALTER TABLE LS_M18_GOV_LAB.CORE.CUSTOMERS MODIFY COLUMN FULL_NAME
SET MASKING POLICY LS_M18_GOV_LAB.GOV.FULL_CONTACT;
ALTER TABLE LS_M18_GOV_LAB.CORE.CUSTOMERS MODIFY COLUMN PHONE
SET MASKING POLICY LS_M18_GOV_LAB.GOV.FULL_CONTACT;
ALTER TABLE LS_M18_GOV_LAB.CORE.SALES MODIFY COLUMN BUYER_EMAIL
SET MASKING POLICY LS_M18_GOV_LAB.GOV.FULL_CONTACT;Read the same customer records under three roles
After the policies are attached, publish the contact view to the three approved roles. Its column list excludes credit limits and the verification flag. Consumers can query this secure view without SELECT on the base table. Snowflake evaluates the base masking policy as it provides the view’s results. Verify the expected output with the roles that use it.
USE ROLE M18_OWNER;
GRANT USAGE ON DATABASE LS_M18_GOV_LAB TO ROLE M18_SUPPORT;
GRANT USAGE ON DATABASE LS_M18_GOV_LAB TO ROLE M18_ANALYST;
GRANT USAGE ON DATABASE LS_M18_GOV_LAB TO ROLE M18_RESTRICTED;
GRANT USAGE ON SCHEMA LS_M18_GOV_LAB.CORE TO ROLE M18_SUPPORT;
GRANT USAGE ON SCHEMA LS_M18_GOV_LAB.CORE TO ROLE M18_ANALYST;
GRANT USAGE ON SCHEMA LS_M18_GOV_LAB.CORE TO ROLE M18_RESTRICTED;
CREATE SECURE VIEW LS_M18_GOV_LAB.CORE.CUSTOMER_CONTACT AS
SELECT CUSTOMER_ID, FULL_NAME, EMAIL, PHONE FROM LS_M18_GOV_LAB.CORE.CUSTOMERS;
GRANT SELECT ON VIEW LS_M18_GOV_LAB.CORE.CUSTOMER_CONTACT TO ROLE M18_SUPPORT;
GRANT SELECT ON VIEW LS_M18_GOV_LAB.CORE.CUSTOMER_CONTACT TO ROLE M18_ANALYST;
GRANT SELECT ON VIEW LS_M18_GOV_LAB.CORE.CUSTOMER_CONTACT TO ROLE M18_RESTRICTED;Run the same query in each approved test session
For each test user, select the approved lab warehouse and the assigned M18_SUPPORT, M18_ANALYST or M18_RESTRICTED role. For example, run USE ROLE M18_ANALYST in the analyst session before the block. The default reader can see the contact records but cannot see their raw addresses. Customer 102 is unverified, so the analyst gets the same placeholder as the restricted user for that row.
As a negative test, try SELECT CREDIT_LIMIT FROM LS_M18_GOV_LAB.CORE.CUSTOMERS in the analyst session. Expect access denied, because that role has access to the contact view only. Run the positive query again to prove the reader session still works.
| Active role | Customer 101 email | Customer 102 email |
|---|---|---|
| M18_SUPPORT | asha@example.invalid | luc@example.invalid |
| M18_ANALYST | ***@example.invalid | REDACTED |
| M18_RESTRICTED | REDACTED | REDACTED |
USE SECONDARY ROLES NONE;
SELECT CURRENT_ROLE(), IS_ROLE_IN_SESSION('M18_ANALYST') AS HAS_ANALYST;
SELECT CUSTOMER_ID, EMAIL
FROM LS_M18_GOV_LAB.CORE.CUSTOMER_CONTACT
WHERE CUSTOMER_ID IN (101,102)
ORDER BY CUSTOMER_ID;Protect tagged VARCHAR columns with one masking rule
If many tables have the same kind of contact field, tag-based masking attaches a policy to the tag and applies it to matching data types. A tag can carry one masking policy per data type. A directly attached policy takes precedence on a column. One tag on a whole schema can affect every matching column that inherits it, so the lab uses one explicit column on a separate scratch table.
An administrator must grant the lab tag owner global APPLY MASKING POLICY for setting and removing a policy from this tag. Run this optional block in the authorized Enterprise lab. Tests use one fictional phone. M18_ANALYST gets REDACTED because the column inherits the policy through the tag. M18_SUPPORT gets +1-202-555-0101. A temporary copy or new export should have its protections checked independently before sharing.
USE ROLE M18_OWNER;
CREATE TAG LS_M18_GOV_LAB.GOV.CONTACT_MASK ALLOWED_VALUES 'restricted';
GRANT APPLY ON TAG LS_M18_GOV_LAB.GOV.CONTACT_MASK TO ROLE M18_OWNER;
ALTER TAG LS_M18_GOV_LAB.GOV.CONTACT_MASK
SET MASKING POLICY LS_M18_GOV_LAB.GOV.FULL_CONTACT;
CREATE TABLE LS_M18_GOV_LAB.GOV.PHONE_POLICY_TEST (PHONE VARCHAR);
INSERT INTO LS_M18_GOV_LAB.GOV.PHONE_POLICY_TEST VALUES ('+1-202-555-0101');
ALTER TABLE LS_M18_GOV_LAB.GOV.PHONE_POLICY_TEST MODIFY COLUMN PHONE
SET TAG LS_M18_GOV_LAB.GOV.CONTACT_MASK = 'restricted';
GRANT USAGE ON SCHEMA LS_M18_GOV_LAB.GOV TO ROLE M18_SUPPORT;
GRANT USAGE ON SCHEMA LS_M18_GOV_LAB.GOV TO ROLE M18_ANALYST;
GRANT SELECT ON TABLE LS_M18_GOV_LAB.GOV.PHONE_POLICY_TEST TO ROLE M18_SUPPORT;
GRANT SELECT ON TABLE LS_M18_GOV_LAB.GOV.PHONE_POLICY_TEST TO ROLE M18_ANALYST;
-- Run this SELECT in each reader's independent session after setting its role.
SELECT PHONE FROM LS_M18_GOV_LAB.GOV.PHONE_POLICY_TEST;Inspect, change and remove a policy without an exposure gap
As M18_OWNER use DESCRIBE MASKING POLICY LS_M18_GOV_LAB.GOV.CONTACT_EMAIL to inspect the signature and body. Query Information Schema POLICY_REFERENCES for the protected table. A missing binding calls for checking the table, tag application and matching type. Unexpected full data calls for checking inherited and secondary roles first.
On the private scratch phone table only, first revoke both read grants and verify that each reader now gets a denial. Then remove the tag from the column, unset the rule from the tag and drop the scratch objects. To update an existing rule in place use ALTER MASKING POLICY ... SET BODY -> expression, preserving its signature. Review the change and retest all roles. The removal SQL for a directly attached policy is MODIFY COLUMN ... UNSET MASKING POLICY. Remove the consumer path before doing that. The core lab keeps its policies attached.
DESCRIBE MASKING POLICY LS_M18_GOV_LAB.GOV.CONTACT_EMAIL;
SELECT POLICY_NAME, REF_COLUMN_NAME, POLICY_STATUS
FROM TABLE(LS_M18_GOV_LAB.INFORMATION_SCHEMA.POLICY_REFERENCES(
REF_ENTITY_NAME => 'LS_M18_GOV_LAB.CORE.CUSTOMERS', REF_ENTITY_DOMAIN => 'TABLE'))
ORDER BY REF_COLUMN_NAME;Change and remove a direct policy on a private test column
Run as M18_OWNER. This separate table has no consumer grants. The reads show HIDDEN, then WITHHELD and finally the fictional source value after detaching. SET BODY updates the rule without an unprotected gap. UNSET removes the binding, so remove the consumer path first if you ever retire a real policy.
USE ROLE M18_OWNER;
USE SCHEMA LS_M18_GOV_LAB.GOV;
CREATE TABLE MASK_CHANGE_TEST (CONTACT VARCHAR);
INSERT INTO MASK_CHANGE_TEST VALUES ('demo@example.invalid');
CREATE MASKING POLICY TEST_MASK AS (VAL VARCHAR) RETURNS VARCHAR -> 'HIDDEN';
GRANT APPLY ON MASKING POLICY TEST_MASK TO ROLE M18_OWNER;
ALTER TABLE MASK_CHANGE_TEST MODIFY COLUMN CONTACT SET MASKING POLICY TEST_MASK;
SELECT CONTACT FROM MASK_CHANGE_TEST;
ALTER MASKING POLICY TEST_MASK SET BODY -> 'WITHHELD';
SELECT CONTACT FROM MASK_CHANGE_TEST;
ALTER TABLE MASK_CHANGE_TEST MODIFY COLUMN CONTACT UNSET MASKING POLICY;
SELECT CONTACT FROM MASK_CHANGE_TEST;
DROP TABLE MASK_CHANGE_TEST;
DROP MASKING POLICY TEST_MASK;Close the tag-based test
Run the two revokes first. Verify the read denial under each reader before running the removal statements. Keep the FULL_CONTACT policy because the main customer and sales tests still use it.
REVOKE SELECT ON TABLE LS_M18_GOV_LAB.GOV.PHONE_POLICY_TEST FROM ROLE M18_SUPPORT;
REVOKE SELECT ON TABLE LS_M18_GOV_LAB.GOV.PHONE_POLICY_TEST FROM ROLE M18_ANALYST;Remove the tag-based bindings and scratch objects
The lab owner keeps direct masking attached and removes only the tag demo. These steps also release the schema usage that was added for the demo.
ALTER TABLE LS_M18_GOV_LAB.GOV.PHONE_POLICY_TEST MODIFY COLUMN PHONE
UNSET TAG LS_M18_GOV_LAB.GOV.CONTACT_MASK;
ALTER TAG LS_M18_GOV_LAB.GOV.CONTACT_MASK
UNSET MASKING POLICY LS_M18_GOV_LAB.GOV.FULL_CONTACT;
DROP TABLE LS_M18_GOV_LAB.GOV.PHONE_POLICY_TEST;
DROP TAG LS_M18_GOV_LAB.GOV.CONTACT_MASK;
REVOKE USAGE ON SCHEMA LS_M18_GOV_LAB.GOV FROM ROLE M18_SUPPORT;
REVOKE USAGE ON SCHEMA LS_M18_GOV_LAB.GOV FROM ROLE M18_ANALYST;Interview practice
Explain the decision and the evidence you would use to test it.
How is masking different from encryption?
Encryption protects storage and transport. Masking chooses a permitted query value for a role. In the example, both readers connect securely but one sees a domain and the other sees REDACTED.
Why does M18_IN get the analyst version?
The India role inherits M18_ANALYST, and IS_ROLE_IN_SESSION checks that inheritance. I would turn secondary roles off in the test and compare CURRENT_ROLE with the Boolean membership result.
How would you change a masking rule already used by dashboards?
I would compare the current binding and type, test the replacement on synthetic data under each consumer, then update the body in place or use a reviewed atomic replacement. I would also test joins and filters that may rely on that value.
Words used in this lesson
- Dynamic Data Masking
- Return different values for a column during a query.
- Conditional masking
- Use extra columns, such as a verified flag, as inputs.
- Tag-based masking
- Connect a masking rule to a label and matching column types.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Snowflake Dynamic Data Masking and tag-based masking
Dynamic masking during a query
All readers start with the same underlying customer row. The policy chooses the value from the role and flag.
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
- Dynamic masking during a query
All readers start with the same underlying customer row. The policy chooses the value from the role and flag.
Query the contact view → Evaluate the email rule → Support → Analyst → Restricted
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.
Before running this lesson
All records are fictional. Outputs are expected or simulated. Live practice needs an authorized scratch account, test roles, an approved warehouse and the editions listed in the lesson.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Predict the effect of running the contact query as M18_IN with secondary roles disabled.
Compare your reasoning
It inherits M18_ANALYST, so verified customer 101 shows the domain and customer 102 shows REDACTED.
Connect this lesson to the learning path
Official sources and curriculum
Module 18 · See module scope
By Rohit Ray · Independent educational lesson · Explore this visual lab