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 %}
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 %}
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 %}
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 %}
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 %}
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 %}
0 commit comments