CertSafari
    Snowflake SnowPro Advanced: Data Engineer (DEA-C02)· Lessons

    Domain 4 · Lesson 15/22

    Column-level security with masking policies and external tokenization

    Establish and maintain data protection.

    16 min read
    7% of exam
    5 sources
    Published 20 Oct 2026
    Docs as of 4 Oct 2026

    What you will be able to do

    • Write a CREATE MASKING POLICY statement whose conditions use CURRENT_ROLE, IS_ROLE_IN_SESSION or an entitlement table, and apply it to a table or view column
    • Choose between Dynamic Data Masking and External Tokenization for a given sensitive column
    • Grant masking privileges so policy administrators are kept separate from object owners
    • Manage the policy lifecycle with ALTER, SET, UNSET and DROP, and avoid the errors each one raises
    • Assign a masking policy to a tag so that every tagged column is protected automatically

    Key concept

    Masking policy (query-time column protection) — A masking policy is a schema-level object attached to a column. It does not change the stored data. Every time a query reads the column, the policy's conditions decide whether that caller sees the real value, a partly masked value, a fully masked value or a tokenized value.

    1.How masking policies protect a column at query time

    Column-level security in Snowflake means attaching a masking policy to a column in a table or view. It comes in two features. Dynamic Data Masking masks plain-text data when a query runs. External Tokenization stores the data as tokens and turns them back into real values (detokenizes them) when a query runs. Both use the same policy object.

    A policy has one data type, one or more conditions, and one or more masking functions. Because it is a separate object from the tables it protects, you write it once and attach it to any column with a matching data type. One email policy can cover thousands of email columns across databases and schemas. Changing the policy body changes protection everywhere it is attached, with no need to reattach it.

    Snowflake rewrites the query so the policy expression replaces the column wherever it appears: in the SELECT list, in join predicates, in WHERE, in ORDER BY and in GROUP BY. This has a practical side effect. Suppose a policy returns 0 for an unauthorized role and that role joins on the masked column. The join compares the masked values, not the stored ones, so rows that match in the real data may not match in the query.

    The other option is a secure view per audience, which does not scale. Every audience needs its own view, and every view grows its own BI dashboards. Masking policies avoid that sprawl because one table serves every role, and each role sees what its policy allows.

    Checkpoint 1 of 9· Check yourself

    A masking policy on the NUMBER column CUSTOMERS.CUSTOMER_ID returns the real value for the FINANCE role and 0 for every other role. ORDERS.CUSTOMER_ID has no policy. A user with the REPORTING role runs a query that joins ORDERS to CUSTOMERS on CUSTOMER_ID. What happens?

    Sources123

    2.Dynamic Data Masking versus External Tokenization

    The two features use the same policy syntax. They differ in where the protected value comes from. With Dynamic Data Masking, plain text is stored in Snowflake and the policy hides it from unauthorized roles. With External Tokenization, a third-party provider tokenizes the data before it is loaded, so Snowflake never stores the plain text. For authorized roles, the policy body calls an external function, which makes a REST call to the provider to detokenize the value. Everyone else sees the token.

    Dynamic Data Masking: PAYROLL sees the stored value, everyone else a fixed masksql
    CREATE MASKING POLICY employee_ssn_mask AS (val string) RETURNS string ->
      CASE
        WHEN CURRENT_ROLE() IN ('PAYROLL') THEN val
        ELSE '******'
      END;
    External Tokenization: PAYROLL gets the value detokenized by the external function ssn_unprotect, everyone else the tokensql
    CREATE MASKING POLICY employee_ssn_detokenize AS (val string) RETURNS string ->
      CASE
        WHEN CURRENT_ROLE() IN ('PAYROLL') THEN ssn_unprotect(VAL)
        ELSE val -- sees tokenized data
      END;

    Take a card number that arrives already tokenized by a vault, where the support team sometimes needs the real number. That is a masking policy whose authorized branch calls the detokenizing external function. Table-level SELECT grants are not enough, because they cannot show the support role a value the table does not contain. A UDF that decrypts with a key stored next to the data is not tokenization either. Role mapping must also exist on both sides: the provider applies its own tokenization policy, which is defined outside Snowflake, to decide what to return.

    Checkpoint 2 of 9· Fill the gap

    A third-party provider tokenizes SSNs before they are loaded into Snowflake. Members of the PAYROLL role must see the real SSN, and everyone else should see the token. What goes in the authorized branch of this policy?

    CREATE MASKING POLICY employee_ssn_detokenize AS (val string) RETURNS string ->
      CASE
        WHEN CURRENT_ROLE() IN ('PAYROLL') THEN  ? 
        ELSE val -- sees tokenized data
      END;

    Checkpoint 3 of 9· Exam question

    A payment_card_number column arrives already tokenized by a third-party vault before it is loaded into Snowflake, and the support team occasionally needs the real card number revealed for verified customer calls. Which design correctly combines column-level security mechanisms for this requirement?

    Sources3

    3.Writing role-based conditions

    Most policies branch on the caller's role. CURRENT_ROLE() checks only the role active in the session. That is exact, but it ignores role hierarchy: a role that inherits ANALYST does not pass CURRENT_ROLE() IN ('ANALYST'). When inherited or secondary roles should count, use IS_ROLE_IN_SESSION. For views and shares, the context functions INVOKER_ROLE and INVOKER_SHARE cover the same need.

    A policy body can return a range of outputs:

    • NULL, or a fixed string such as '********', for a full mask
    • REGEXP_REPLACE to keep part of the value, for example an email domain
    • SHA2 for a hash. Hashes can collide, so use this with care
    • a UDF, or OBJECT_INSERT to mask a single key inside VARIANT data
    • CURRENT_ACCOUNT() comparisons, so that a production account sees real values and development accounts do not

    For entitlements that change often, look up a custom entitlement table instead of hard-coding role names. A subquery in the policy body must be wrapped in EXISTS. A memoizable function that caches the lookup works too: the policy calls is_role_authorized(CURRENT_ROLE()) and returns NULL when it is false.

    Conditional masking passes more than one column to the policy. In email_visibility (email varchar, visibility string), the first argument is always the column being masked. The others are conditional columns from the same table or view. In that example, a row whose visibility is 'PUBLIC' shows its email to every role.

    Checkpoint 4 of 9· Check yourself

    Role SR_ANALYST has been granted the ANALYST role, so it inherits ANALYST. A masking policy on an email column unmasks only WHEN CURRENT_ROLE() IN ('ANALYST') THEN val. A user whose active role is SR_ANALYST queries the column. What do they see, and what change lets inherited roles count?

    Sources423

    4.Privileges and segregation of duties

    Masking policies separate the people who write policies from the people who own data. A security or privacy officer, usually a custom MASKING_ADMIN role, decides which columns are protected. The table owner does not. Secure views offer no such separation, which is a large part of why they fall short for governance. A well-written policy can also stop ACCOUNTADMIN and SECURITYADMIN from seeing data they have no need to see.

    Privileges for managing masking policies
    PrivilegeGranted onWhat it allows
    CREATE MASKING POLICYSchemaCreating masking policies in that schema
    APPLY MASKING POLICYAccountSetting or unsetting any masking policy on any column. ACCOUNTADMIN has it by default. It grants no extra table privileges
    APPLYA single masking policyOptional. Lets the policy owner allow object owners to set or unset that one policy (decentralized management)
    OWNERSHIPA single masking policyFull control, including altering most of the policy's properties
    SECURITYADMIN gives the masking administrator its two core privilegessql
    use role securityadmin; GRANT CREATE MASKING POLICY on SCHEMA <db_name.schema_name> to ROLE masking_admin; GRANT APPLY MASKING POLICY on ACCOUNT to ROLE masking_admin;

    Centralized management means MASKING_ADMIN applies every policy. In a large organization, where hundreds of teams own sensitive columns, the policy owner can grant APPLY ON MASKING POLICY to those teams' owner roles instead. Each team then attaches the shared policy to its own columns, while only the security team can change what the policy does. This is the hybrid or decentralized approach.

    Checkpoint 5 of 9· Match them up

    A security team is setting up segregation of duties for masking policies. Match each privilege to what it lets the grantee do.

    Tap a term, then the definition that fits it.

    Sources21

    5.Policy lifecycle: set, alter, replace, unset, drop

    You can attach a policy to a column when you create the table or view, or later with ALTER. A column holds one masking policy at a time. Setting a second policy on a protected column fails. To swap policies in a single statement, add the FORCE keyword to the SET MASKING POLICY clause. Without FORCE, you first unset the old policy and then set the new one.

    Attaching a policy to an existing table columnsql
    -- apply masking policy to a table column
    
    ALTER TABLE IF EXISTS user_info MODIFY COLUMN email SET MASKING POLICY email_mask;

    To change what a policy does, run ALTER MASKING POLICY. The policy stays attached during the change, so the column is never unprotected. Use GET_DDL or DESCRIBE MASKING POLICY first to read the current body. Don't add IF EXISTS to the ALTER: if the name is wrong, the statement still reports success and nothing changes.

    You cannot drop a policy while it is attached to anything. Detach it from each column with ALTER TABLE … MODIFY COLUMN email UNSET MASKING POLICY (or the ALTER VIEW equivalent), then run DROP MASKING POLICY. To audit where a policy is attached, query the Account Usage view POLICY_REFERENCES or the Information Schema table function of the same name. MASKING_POLICIES lists every masking policy in the account. To see which policies a particular query used, look in its Query Profile; QUERY_HISTORY does not record policy names.

    Checkpoint 6 of 9· Put it in order

    A security team wants to retire the masking policy email_mask. It is still attached to email columns in several tables and views. Put the steps in the order that removes it without an error.

    1. 1.Unset email_mask from each of those columns with ALTER TABLE or ALTER VIEW … MODIFY COLUMN … UNSET MASKING POLICY
    2. 2.Run DROP MASKING POLICY email_mask
    3. 3.Query POLICY_REFERENCES to list every table and view column where email_mask is set

    Checkpoint 7 of 9· Exam question

    A data engineer runs ALTER TABLE customers MODIFY COLUMN email SET MASKING POLICY email_mask; on a column that already has a different masking policy, legacy_email_mask, applied. The statement fails with an error that a policy is already assigned. What lets the engineer replace the existing policy in one statement?

    Sources13

    6.Tag-based masking: protect every tagged column at once

    Tags are key–value labels attached to objects and columns, as explained in Object tagging and tag propagation. A tag-based masking policy sets a masking policy on the tag itself with ALTER TAG. Any column that carries the tag, and whose data type matches the policy signature, is then masked automatically. Nobody has to set the policy on each column.

    A tag can hold one masking policy per data type. A common starting point is one generic policy each for STRING, NUMBER and TIMESTAMP_LTZ. Authorized roles see the raw value and every other role sees a fixed mask. Attach all of them to a single PII tag. Tag a newly loaded column and it is protected immediately. You can also set the tag-based policy at database, schema or table level. The policy body can branch on which policy came from the tag, or on the tag's string value on the column.

    Checkpoint 8 of 9· Check yourself

    Your governance team wants any column tagged PII to be masked automatically as soon as it is tagged. The PII columns include VARCHAR emails, NUMBER account IDs and TIMESTAMP_LTZ birth dates. No one should need to run a per-column ALTER TABLE. Which setup does this?

    Sources5

    7.Best practices for masking policies

    Most masking-policy failures come from a few limits:

    • Input and output types must match. A TIMESTAMP policy cannot return '*MASKED*'. Return a made-up timestamp such as date_from_parts(0001, 01, 01)::timestamp_ntz instead.
    • Keep conditional columns few. Snowflake evaluates every argument column at query time, and every column must come from the same table or view.
    • Use EXISTS for subqueries and memoizable functions for mapping lookups.
    • Be careful with hashes. SHA2 output can collide and can be joined on.
    • Watch materialized views. A policy cannot go on a column that a materialized view uses, and a materialized view cannot include a masked column. Virtual columns cannot take a policy either; mask the source column.
    • Test with two roles. Query once with the authorized role and once with PUBLIC, then confirm the masked output.

    Checkpoint 9 of 9· Check yourself

    Before release, a reviewer reads four draft masking policies. Which draft fails when someone runs CREATE MASKING POLICY?

    Sources23

    Sources

    (5)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “the masking policy is applied to the column at every location where the column appears”
      ↩︎ How masking policies protect a column at query time
      “can prohibit privileged users with the ACCOUNTADMIN or SECURITYADMIN role from unnecessarily viewing data”
      ↩︎ Privileges and segregation of duties
      “A column cannot be attached to multiple masking policies.”
      ↩︎ Policy lifecycle: set, alter, replace, unset, drop
      “Masking policy names are not included in the QUERY_HISTORY view.”
      ↩︎ Policy lifecycle: set, alter, replace, unset, drop
      “Policy <policy_name> cannot be dropped/replaced as it is associated with one or more entities.”
      ↩︎ Exam trap 3
      “At query runtime, the masking policy is applied to the column at every location where the column appears.”
      ↩︎ Checkpoint
      “statement to UNSET the policy first, then try the DROP statement again.”
      ↩︎ Checkpoint
    2. 2.
      “e.g. projections, join predicate, where clause predicate, order by, and group by”
      ↩︎ How masking policies protect a column at query time
      “Always use EXISTS when including a subquery in the masking policy body.”
      ↩︎ Writing role-based conditions
      “This privilege controls who can [un]set masking policies on columns and is granted to the ACCOUNTADMIN role by default.”
      ↩︎ Privileges and segregation of duties
      “Using a hashing function in a masking policy may result in collisions; therefore, exercise caution with this approach.”
      ↩︎ Best practices for masking policies
      “Currently, Snowflake does not support different input and output data types in a masking policy”
      ↩︎ Exam trap 2
      “used by a policy owner to decentralize the [un]set operations of a given masking policy on columns to the object owners”
      ↩︎ Checkpoint
      “the input and output data types must match.”
      ↩︎ Checkpoint
    3. 3.
      “Masking policies solve this management challenge by avoiding an explosion of views and dashboards to manage.”
      ↩︎ How masking policies protect a column at query time
      “External Tokenization requires a third-party tokenization provider to tokenize data before loading data into Snowflake.”
      ↩︎ Dynamic Data Masking versus External Tokenization
      “role mapping must exist in Snowflake and the tokenization provider to ensure that the correct data can be returned from a given query”
      ↩︎ Dynamic Data Masking versus External Tokenization
      “The first column always specifies the column to mask.”
      ↩︎ Writing role-based conditions
      “This command does not require unsetting a masking policy from a column, if the masking policy is set on a column.”
      ↩︎ Policy lifecycle: set, alter, replace, unset, drop
      “Minimize the number of column arguments in the policy definition.”
      ↩︎ Best practices for masking policies
      “sensitive data in Snowflake is not modified in an existing table (i.e. no static masking)”
      ↩︎ Key concept
      “Object owners (i.e. the role that has the OWNERSHIP privilege on the object) do not have the privilege to unset masking policies.”
      ↩︎ Exam trap 1
      “Object owners cannot view column data in which a masking policy applies.”
      ↩︎ Prediction
      “You can apply the masking policy to one or more table/view columns with the matching data type.”
      ↩︎ Checkpoint
    4. 4.
      “If role hierarchy needs to be considered, this policy could similarly use IS_ROLE_IN_SESSION to be more inclusive of other roles”
      ↩︎ Writing role-based conditions
    5. 5.
      “allow a masking policy to be set on a tag using an ALTER TAG command”
      ↩︎ Tag-based masking: protect every tagged column at once
      “The tag can support one masking policy for each data type that Snowflake supports.”
      ↩︎ Tag-based masking: protect every tagged column at once

    Continue to part 2 of 2

    Row access, projection and aggregation policies, clean rooms and Horizon enforcement

    Spotted a mistake, or was something unclear? Tell us.