Skip to content

Commit 882a3db

Browse files
Enhance grant_usage_to_application macro to handle schema grants and add new macros for database and schema usage privileges
1 parent a0b88c5 commit 882a3db

File tree

2 files changed

+108
-4
lines changed

2 files changed

+108
-4
lines changed

macros/grants/grant_usage_to_application.sql

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{% if flags.WHICH in ['run', 'run-operation'] %}
33
{% set revoke_statements = [] %}
44
{% set grant_statements = [] %}
5+
{% set grant_schemas = [] %}
56
{% set execute_statements = [] %}
67
{% set grant_types = ["USAGE"] %}
78
{% set matching_objects %}
@@ -18,6 +19,9 @@
1819

1920
{% for object in objects %}
2021
{% set existing_grants = [] %}
22+
{% if object[1] not in grant_schemas %}
23+
{{ grant_schemas.append(object[1]) }}
24+
{% endif %}
2125
{% set query %}
2226
show grants on {{ object_type }} {{ target.database }}.{{ object[2] }};
2327
{% endset %}
@@ -27,12 +31,12 @@
2731
{% if row.privilege not in ["OWNERSHIP", "SELECT", "REFERENCES", "REBUILD"] %}
2832
{% if row.privilege in grant_types %}
2933
{% if row.grantee_name not in grant_applications %}
30-
{{ revoke_statements.append({ "privilege" : row.privilege, "role" : row.grantee_name, "object" : object[2] }) }}
34+
{{ revoke_statements.append({ "privilege" : row.privilege, "role" : row.grantee_name, "schema" : object[1], "object" : object[2] }) }}
3135
{% else %}
32-
{{ existing_grants.append({ "privilege" : row.privilege, "role" : row.grantee_name, "object" : object[2] }) }}
36+
{{ existing_grants.append({ "privilege" : row.privilege, "role" : row.grantee_name, "schema" : object[1], "object" : object[2] }) }}
3337
{%endif%}
3438
{% else %}
35-
{{ revoke_statements.append({ "privilege" : row.privilege, "role" : row.grantee_name, "object" : object[2] }) }}
39+
{{ revoke_statements.append({ "privilege" : row.privilege, "role" : row.grantee_name, "schema" : object[1], "object" : object[2] }) }}
3640
{%endif%}
3741
{% endif %}
3842
{% endfor %}
@@ -46,7 +50,7 @@
4650
{% endfor %}
4751
{% for privilege in grant_types %}
4852
{% if privilege not in existing_role_grants %}
49-
{{ grant_statements.append({ "privilege" : privilege, "role" : application, "object" : object[2] }) }}
53+
{{ grant_statements.append({ "privilege" : privilege, "role" : application, "schema" : object[1], "object" : object[2] }) }}
5054
{% endif %}
5155
{% endfor %}
5256
{% endfor %}
@@ -58,6 +62,9 @@
5862
{% for stm in grant_statements %}
5963
{{ execute_statements.append("grant " ~ stm.privilege ~ " on " ~ object_type ~ " " ~ target.database ~ "." ~ stm.object ~ " to application " ~ stm.role ~ ";") }}
6064
{% endfor %}
65+
66+
{% do dbt_dataengineers_utils.grant_database_usage_to_application(grant_applications, target.database) %}
67+
{% do dbt_dataengineers_utils.grant_schema_usage_to_application(grant_applications, target.database, grant_schemas) %}
6168
{% if execute_statements | length > 0 %}
6269
{% do log("Executing privilege grants and revokes for " ~ object_type ~"s...", info=True) %}
6370
{% for statement in execute_statements %}
@@ -69,4 +76,74 @@
6976
{% do log("No privilege grants or revokes to execute for " ~ object_type ~ "s.", info=True) %}
7077
{% endif %}
7178
{% endif %}
79+
{% endmacro %}
80+
81+
{% macro grant_database_usage_to_application(grant_applications, target_database) %}
82+
{% set existing_database_usage = [] %}
83+
{% set execute_statements = [] %}
84+
{% set matching_objects %}
85+
show grants on database {{ target_database }}
86+
->>
87+
select
88+
"name" as database_name,
89+
"grantee_name" as application_name
90+
from $1
91+
where "privilege" = 'USAGE'
92+
and "granted_to" = 'APPLICATION';
93+
{% endset %}
94+
{% set objects = run_query(matching_objects) %}
95+
{% for object in objects %}
96+
{{ existing_database_usage.append(object[1]) }}
97+
{% endfor %}
98+
{% for application_name in grant_applications %}
99+
{% if application_name not in existing_database_usage %}
100+
{{ execute_statements.append("grant usage on database " ~ target_database ~ " to application " ~ application_name ~ ";") }}
101+
{% endif %}
102+
{% endfor %}
103+
{% if execute_statements | length > 0 %}
104+
{% do log("Executing usage grants for applications on database ...", info=True) %}
105+
{% for statement in execute_statements %}
106+
{% do log(statement, info=True) %}
107+
{% set grant = run_query(statement) %}
108+
{% endfor %}
109+
{% do log("Usage grants executed successfully for applications.", info=True) %}
110+
{% else %}
111+
{% do log("No usage grants to execute for applications.", info=True) %}
112+
{% endif %}
113+
{% endmacro %}
114+
115+
{% macro grant_schema_usage_to_application(grant_applications, target_database, schemas) %}
116+
{% set existing_schema_usage = [] %}
117+
{% set execute_statements = [] %}
118+
{% for schema in schemas %}
119+
{% set matching_objects %}
120+
show grants on schema {{ target_database }}.{{ schema }}
121+
->>
122+
select
123+
"name" as schema_name,
124+
"grantee_name" as application_name
125+
from $1
126+
where "privilege" = 'USAGE'
127+
and "granted_to" = 'APPLICATION';
128+
{% endset %}
129+
{% set objects = run_query(matching_objects) %}
130+
{% for object in objects %}
131+
{{ existing_schema_usage.append(object[1]) }}
132+
{% endfor %}
133+
{% for application_name in grant_applications %}
134+
{% if application_name not in existing_schema_usage %}
135+
{{ execute_statements.append("grant usage on schema " ~ target_database ~ "." ~ schema ~ " to application " ~ application_name ~ ";") }}
136+
{% endif %}
137+
{% endfor %}
138+
{% endfor %}
139+
{% if execute_statements | length > 0 %}
140+
{% do log("Executing usage grants for applications on schemas ...", info=True) %}
141+
{% for statement in execute_statements %}
142+
{% do log(statement, info=True) %}
143+
{% set grant = run_query(statement) %}
144+
{% endfor %}
145+
{% do log("Usage grants executed successfully for applications.", info=True) %}
146+
{% else %}
147+
{% do log("No usage grants to execute for applications.", info=True) %}
148+
{% endif %}
72149
{% endmacro %}

macros/grants/grants.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,33 @@ macros:
7979
type: List[string]
8080
description: List of application roles to grant usage privileges to
8181

82+
- name: grant_database_usage_to_application
83+
description: This macro grants usage privileges on a database to a specific application role.
84+
docs:
85+
show: false
86+
arguments:
87+
- name: grant_applications
88+
type: List[string]
89+
description: List of application roles to grant usage privileges to
90+
- name: target_database
91+
type: string
92+
description: target database
93+
94+
- name: grant_schema_usage_to_application
95+
description: This macro grants usage privileges on a schema to a specific application role.
96+
docs:
97+
show: false
98+
arguments:
99+
- name: grant_applications
100+
type: List[string]
101+
description: List of application roles to grant usage privileges to
102+
- name: target_database
103+
type: string
104+
description: target database
105+
- name: schemas
106+
type: List[string]
107+
description: List of schemas to grant usage privileges on
108+
82109
- name: grant_privileges
83110
description: This macro is an example of how to grant privileges across environments based on targets
84111
docs:

0 commit comments

Comments
 (0)