Skip to content

Commit c66ea86

Browse files
* added macro grant_internal_share_read to apply select permissions on all tables and views
* added macro `create_internal_share` which will create a share which allows unsecured objects and grant reference usages on downstream databases * updated macro `has_matching_nodes` to cater for line breaks in the arguments being passed in * updated version of `dbt_utils` to 1.3.3
1 parent 64a62da commit c66ea86

File tree

9 files changed

+188
-28
lines changed

9 files changed

+188
-28
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
# Data Engineers Snowflake DataOps Utils Project Changelog
22
This file contains the changelog for the Data Engineers Snowflake DataOps Utils project, detailing updates, fixes, and enhancements made to the project over time.
33

4-
## v0.3.9 2024-06-10 - Grant Object For Procedures
4+
## v0.3.10 - 2026-02-10 - Grant Shares
5+
6+
* added macro `grant_internal_share_read` to apply `select` permissions on all tables and views
7+
* added macro `create_internal_share` which will create a share which allows unsecured objects and grant reference usages on downstream databases
8+
* updated macro `has_matching_nodes` to cater for line breaks in the arguments being passed in
9+
* updated version of `dbt_utils` to 1.3.3
10+
11+
## v0.3.9.1 2025-07-10 - Tag Doc Fix
12+
13+
* fixed issue hwhen
14+
15+
## v0.3.9 2025-06-10 - Grant Object For Procedures
516

617
* added macro `grant_procedure_usage` to enable the ability to grant usage of a stored procedure to a role
718

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Below is a catalogue of publicly supported macros grouped by domain. Internal he
5454
- `grant_integration_ownership`
5555
- `grant_database_usage`
5656
- `grant_integration_usage`
57+
- `grant_internal_share_read`
5758
- `grant_object`
5859
- `grant_privileges`
5960
- `grant_schema_monitor`
@@ -111,6 +112,11 @@ Below is a catalogue of publicly supported macros grouped by domain. Internal he
111112
- `ref` (enhanced include_database)
112113
- `source` (enhanced include_database)
113114

115+
**shares**
116+
117+
- `create_internal_share`
118+
- `create_share`
119+
114120
**tags**
115121

116122
- `apply_meta_as_tags`

macros/dynamic_tables/dynamic_tables.yml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@ version: 2
22

33
macros:
44
- name: target_lag_environment
5-
description: Set the dynamic table target lag duration based on the active target (prod/test/other) to tune freshness vs cost.
5+
description: |
6+
Returns the lag duration for a dynamic table based on the current target environment.
7+
Accepts durations for prod, test, and other environments, and returns the appropriate value.
68
docs:
79
show: true
810
arguments:
911
- name: duration_prod
10-
type: number
11-
description: specifies the lag duration for the production environment
12+
type: any
13+
description: Lag duration for the production environment
1214
- name: duration_test
13-
type: number
14-
description: specifies the lag duration for the test environment
15+
type: any
16+
description: Lag duration for the test environment
1517
- name: duration_other
16-
type: number
17-
description: specifies the lag duration for other environments
18+
type: any
19+
description: Lag duration for other environments
20+
1821
- name: target_warehouse_environment
19-
description: Select the warehouse to be used for dynamic tables based on target environment (e.g. smaller warehouse for dev/test).
22+
description: |
23+
Returns the warehouse name to be used for dynamic tables based on the current target environment.
24+
Uses DEV_WH for local-dev, otherwise DATAOPS_WH.
2025
docs:
21-
show: true
26+
show: true
27+
arguments: []
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{% macro grant_internal_share_read(share_name, exclude_schemas=[]) %}
2+
{% set database = target.database %}
3+
{% set schemas = dbt_dataengineers_utils._grants_collect_schemas(exclude_schemas) %}
4+
{% do log("Granting SELECT on all tables and views in all schemas for share: " ~ share_name, info=True) %}
5+
{% for schema in schemas %}
6+
{# Get all tables in the schema #}
7+
{% set tables_query %}
8+
show tables in schema {{ database }}.{{ schema }};
9+
{% endset %}
10+
{% set tables_result = run_query(tables_query) %}
11+
{% if execute and tables_result is not none %}
12+
{% for row in tables_result %}
13+
{% set grant_table_sql %}
14+
grant select on table {{ database }}.{{ schema }}.{{ row.name }} to share {{ share_name }};
15+
{% endset %}
16+
{% do run_query(grant_table_sql) %}
17+
{% endfor %}
18+
{% endif %}
19+
{# Get all views in the schema #}
20+
{% set views_query %}
21+
show views in schema {{ database }}.{{ schema }};
22+
{% endset %}
23+
{% set views_result = run_query(views_query) %}
24+
{% if execute and views_result is not none %}
25+
{% for row in views_result %}
26+
{% set grant_view_sql %}
27+
grant select on view {{ database }}.{{ schema }}.{{ row.name }} to share {{ share_name }};
28+
{% endset %}
29+
{% do run_query(grant_view_sql) %}
30+
{% endfor %}
31+
{% endif %}
32+
{% endfor %}
33+
{% endmacro %}

macros/grants/grants.yml

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ version: 2
99
# - Set var('grants_dry_run', true) in a dbt invocation to log all statements without executing them for newly refactored macros
1010

1111
macros:
12+
- name: grant_internal_share_read
13+
description: Grants SELECT on all tables and views in all schemas of the target database to the specified share. Executes grant statements for each table and view found.
14+
docs:
15+
show: true
16+
arguments:
17+
- name: share_name
18+
type: string
19+
description: Name of the share to grant SELECT privileges to
20+
- name: exclude_schemas
21+
type: List[string]
22+
description: List of schemas to exclude from granting
23+
1224
- name: grant_database_ownership
1325
description: This macro grants ownership privileges to the specified rolename on the database.
1426
docs:
@@ -61,16 +73,24 @@ macros:
6173

6274

6375
- name: grant_object
64-
description: This macro grants specific object permissions to specific roles.
76+
description: |
77+
Grants specified privileges on objects (TABLE, VIEW, SCHEMA, etc.) to the provided roles.
78+
Handles both granting and revoking privileges based on current state, logging a summary of actions taken.
6579
docs:
6680
show: true
6781
arguments:
6882
- name: object_type
6983
type: string
70-
description: Type of the object eg- TABLE, VIEW, SCHEMA
84+
description: Type of the object (e.g., TABLE, VIEW, SCHEMA)
7185
- name: objects
7286
type: List[string]
7387
description: List of objects to apply the permission to (format = schema.object)
88+
- name: grant_types
89+
type: List[string]
90+
description: List of privilege types to grant (e.g., SELECT, REFERENCES)
91+
- name: grant_roles
92+
type: List[string]
93+
description: List of roles to grant privileges to
7494

7595
- name: grant_usage_to_application
7696
description: This macro grants usage privileges on specific objects to a specific application role.
@@ -115,7 +135,9 @@ macros:
115135
description: List of schemas to grant usage privileges on
116136

117137
- name: grant_privileges
118-
description: This macro is an example of how to grant privileges across environments based on targets
138+
description: |
139+
Grants a bundle of privileges across environments based on target context.
140+
Calls multiple grant macros for database, schema, and role management, orchestrating environment-specific grants.
119141
docs:
120142
show: true
121143
arguments:
@@ -124,16 +146,18 @@ macros:
124146
description: List of schemas which are domain specific that should be exposed
125147

126148
- name: grant_schema_monitor
127-
description: This macro grants monitor privilege inside all schemas to the specified rolename.
149+
description: |
150+
Grants MONITOR privilege on all objects in all schemas to the specified roles, excluding any schemas listed.
151+
Uses grant_schema_monitor_specific for per-schema operations and supports dry-run mode.
128152
docs:
129153
show: true
130154
arguments:
131155
- name: exclude_schemas
132156
type: List[string]
133157
description: List of schemas to exclude
134158
- name: grant_roles
135-
type: "List[string]"
136-
description: Name of the roles to apply eg- ['READERS_PROD', 'ANALYST', 'OPS_SUPPORT']
159+
type: List[string]
160+
description: List of roles to apply (e.g., ['READERS_PROD', 'ANALYST', 'OPS_SUPPORT'])
137161

138162
- name: grant_schema_monitor_specific
139163
description: This macro grants monitor privilege inside specific schemas to the specified rolename.
@@ -226,7 +250,10 @@ macros:
226250
description: Specifies if to revoke current grants or not
227251

228252
- name: grant_share_read
229-
description: This macro grants monitor privilege inside specific schemas to the specified rolename.
253+
description: |
254+
Grants SELECT on specified views to the provided shares, and optionally revokes unmanaged grants.
255+
Handles both granting and revoking privileges for views in schemas, based on the view_names and grant_shares arguments.
256+
Uses grant_share_read_specific_schema for per-schema operations.
230257
docs:
231258
show: true
232259
arguments:
@@ -237,8 +264,8 @@ macros:
237264
type: List[string]
238265
description: List of shares to apply
239266
- name: revoke_current_grants
240-
type: "boolean"
241-
description: Revoke current grants on the schemas
267+
type: boolean
268+
description: Whether to revoke unmanaged grants before applying new ones
242269

243270
- name: grant_share_read_specific_schema
244271
description: This macro grants select permissions to the specified view for the shares provided.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% macro create_internal_share(share_name, reference_databases, environments) %}
2+
{% if flags.WHICH in ['run', 'run-operation'] %}
3+
{% if execute %}
4+
{% if target.name in environments %}
5+
{% do log("Creating or Updating Share" ~ share_name, info=True) %}
6+
{% set sql %}
7+
create share if not exists {{ share_name }} secure_objects_only=false;
8+
grant usage on database {{ target.database }} to share {{ share_name }};
9+
{% for reference_database in reference_databases %}
10+
grant reference_usage on database {{ reference_database }} to share {{ share_name }};
11+
{% endfor %}
12+
{% endset %}
13+
{% set results = run_query(sql) %}
14+
{% endif %}
15+
{% endif %}
16+
{% endif %}
17+
{% endmacro %}

macros/shares/shares.yml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,35 @@ version: 2
22

33
macros:
44
- name: create_share
5-
description: This macro grants creates a share and grants usage on the associated accounts
5+
description: |
6+
Creates or updates a Snowflake share and grants usage to specified accounts.
7+
This macro is used to provision secure data sharing between Snowflake accounts.
8+
It creates the share if it does not exist, and grants usage privileges to the target database and reference usage to additional databases for the listed accounts.
9+
Only runs in specified environments.
610
docs:
711
show: true
812
arguments:
913
- name: share_name
1014
type: string
11-
description: Name of the share to be created or updated
15+
description: The name of the share to be created or updated.
1216
- name: accounts
13-
description: List of accounts to grant usage on the share
1417
type: List[string]
18+
description: List of Snowflake accounts to grant usage on the share.
1519
- name: environments
20+
type: List[string]
21+
description: List of environments in which the macro should run.
22+
23+
- name: create_internal_share
24+
description: Creates or updates a Snowflake share, grants usage on the target database, and reference usage on additional databases. Only runs in specified environments.
25+
docs:
26+
show: true
27+
arguments:
28+
- name: share_name
1629
type: string
17-
description: The environments to include the share
30+
description: Name of the share to be created or updated
31+
- name: reference_databases
32+
type: List[string]
33+
description: List of databases to grant reference usage to the share
34+
- name: environments
35+
type: List[string]
36+
description: List of environments in which the macro should run

macros/tags/tags.yml

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,55 @@ version: 2
22

33
macros:
44
- name: apply_meta_as_tags
5-
description: Post-hook macro to apply selected model/column meta entries as Snowflake tags.
5+
description: |
6+
Post-hook macro to apply selected model/column meta entries as Snowflake tags.
7+
Iterates over columns in a model and applies tags based on provided tag_names, using set_column_tag_value for each match.
68
docs:
79
show: true
10+
arguments:
11+
- name: tag_names
12+
type: List[string]
13+
description: List of tag names to apply to columns if present in meta
14+
815
- name: model_contains_tag_meta
9-
description: check if the model contains a compatible meta entry for a tag.
16+
description: |
17+
Checks if the model contains any compatible meta entry for the provided tag names.
18+
Returns True if any column meta matches a tag name, otherwise False.
1019
docs:
1120
show: false
21+
arguments:
22+
- name: tag_names
23+
type: List[string]
24+
description: List of tag names to check for in column meta
25+
- name: model_node
26+
type: object
27+
description: The model node to inspect for meta tags
28+
1229
- name: set_column_tag_value
13-
description: apply a tag to a column if a matching meta key exists.
30+
description: |
31+
Applies or unsets a tag on a column if a matching meta key exists and the value is not public/none.
32+
Handles tag name mapping and executes the appropriate ALTER statement for the column.
1433
docs:
15-
show: false
34+
show: false
35+
arguments:
36+
- name: materlization
37+
type: string
38+
description: The materialization type (table, view, etc.)
39+
- name: model_schema
40+
type: string
41+
description: The schema of the model
42+
- name: model_name
43+
type: string
44+
description: The name of the model
45+
- name: column_name
46+
type: string
47+
description: The column to apply the tag to
48+
- name: tag_name
49+
type: string
50+
description: The tag name to apply
51+
- name: desired_tag_value
52+
type: string
53+
description: The value to set for the tag
54+
- name: existing_tags_for_table
55+
type: object
56+
description: Existing tags for the table, used to check current tag state

packages.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
packages:
22
- package: dbt-labs/dbt_utils
3-
version: "1.3.1"
3+
version: "1.3.3"

0 commit comments

Comments
 (0)