Created
November 1, 2023 10:20
-
-
Save hakanilter/00d120b02b6cccc650a58b29bb1ae862 to your computer and use it in GitHub Desktop.
DBT Databricks automatic data masking macro, must be configured as a post-hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% macro get_query_results_as_dict(query) %} | |
{{ return(adapter.dispatch('get_query_results_as_dict', 'dbt_utils')(query)) }} | |
{% endmacro %} | |
{% macro automatic_dynamic_masking() %} | |
{% set sensitive_columns = ['email', 'firstname', 'lastname', 'middlename', 'name', 'phone', 'telephone'] %} | |
{% set query %} | |
SELECT * FROM {{ this }} LIMIT 0 | |
{% endset %} | |
{% set results = get_query_results_as_dict(query) %} | |
{% if execute %} | |
{% for column_name in results.keys() %} | |
{% set normalized = column_name|lower|replace(' ', '')|replace('_', '') %} | |
{% if normalized in sensitive_columns %} | |
{{ print("Found sensitive column, applying auto masking: " ~ column_name) }} | |
{% set query %} | |
ALTER TABLE {{ this }} ALTER COLUMN {{ column_name }} | |
SET MASK mycatalog.myschema.myemail_filter | |
{% endset %} | |
{% do run_query(query) %} | |
{% endif %} | |
{% endfor %} | |
{% endif %} | |
{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment