Skip to content

Commit 5c75d14

Browse files
Refactor grant_object_application macro to support application grants and update variable naming for clarity
1 parent 76e16ed commit 5c75d14

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

macros/grants/grant_object_application.sql

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
{% set grant_statements = [] %}
2121

2222
{% for object in objects %}
23-
{% set existing_role_priv_map = {} %} {# key role -> list of privs #}
23+
{% set existing_application_priv_map = {} %} {# key applications -> list of privs #}
2424
{% do log('====> Processing ' ~ object_type ~ ' ' ~ object ~ ' with desired privileges ' ~ (grant_types | join(', ')) ~ ' for applications ' ~ (grant_applications | join(', ')), info=True) %}
2525
{% set query %}
2626
show grants on {{ object_type }} {{ target.database }}.{{ object }};
@@ -30,24 +30,24 @@
3030
{% for row in results %}
3131
{% if row.granted_to == 'APPLICATION' and row.privilege not in excluded_privs %}
3232
{# classify existing privilege #}
33-
{% set _role = row.grantee_name %}
33+
{% set _application = row.grantee_name %}
3434
{% set _priv = row.privilege %}
3535
{% if _priv in grant_types %}
36-
{% if _role not in grant_applications %}
37-
{{ revoke_statements.append('revoke ' ~ _priv | lower ~ ' on ' ~ object_type ~ ' ' ~ target.database ~ '.' ~ object ~ ' from application ' ~ _role | lower ~ ';') }}
36+
{% if _application not in grant_applications %}
37+
{% do revoke_statements.append('revoke ' ~ _priv | lower ~ ' on ' ~ object_type ~ ' ' ~ target.database ~ '.' ~ object ~ ' from application ' ~ _application | lower ~ ';') %}
3838
{% else %}
3939
{# track existing desired priv #}
40-
{% if existing_role_priv_map.get(_role) is none %}
41-
{% set _ = existing_role_priv_map.update({_role: []}) %}
40+
{% if existing_application_priv_map.get(_application) is none %}
41+
{% set _ = existing_application_priv_map.update({_application: []}) %}
4242
{% endif %}
43-
{% if _priv not in existing_role_priv_map.get(_role) %}
44-
{% set __ = existing_role_priv_map.get(_role).append(_priv) %}
43+
{% if _priv not in existing_application_priv_map.get(_application) %}
44+
{% set __ = existing_application_priv_map.get(_application).append(_priv) %}
4545
{% endif %}
4646
{% endif %}
4747
{% else %}
48-
{# privilege not desired -> revoke if granted to managed roles #}
49-
{% if _role in grant_applications or _priv in revokable_read_privs %}
50-
{{ revoke_statements.append('revoke ' ~ _priv | lower ~ ' on ' ~ object_type ~ ' ' ~ target.database ~ '.' ~ object ~ ' from application ' ~ _role | lower ~ ';') }}
48+
{# privilege not desired -> revoke if granted to managed applications #}
49+
{% if _application in grant_applications or _priv in revokable_read_privs %}
50+
{% do revoke_statements.append('revoke ' ~ _priv | lower ~ ' on ' ~ object_type ~ ' ' ~ target.database ~ '.' ~ object ~ ' from application ' ~ _application | lower ~ ';') %}
5151
{% endif %}
5252
{% endif %}
5353
{% endif %}
@@ -56,11 +56,11 @@
5656

5757
{# Determine grants needed #}
5858
{% for application in grant_applications %}
59-
{% set existing_for_application = existing_role_priv_map.get(application) if existing_role_priv_map.get(application) is not none else [] %}
59+
{% set existing_for_application = existing_application_priv_map.get(application) if existing_application_priv_map.get(application) is not none else [] %}
6060
{% do log('====> Existing grants for application ' ~ application ~ ' on ' ~ object ~ ' : ' ~ (existing_for_application | join(', ')), info=True) %}
6161
{% for privilege in grant_types %}
6262
{% if privilege not in existing_for_application %}
63-
{{ grant_statements.append('grant ' ~ privilege | lower ~ ' on ' ~ object_type ~ ' ' ~ target.database ~ '.' ~ object ~ ' to application ' ~ application | lower ~ ';') }}
63+
{% do grant_statements.append('grant ' ~ privilege | lower ~ ' on ' ~ object_type ~ ' ' ~ target.database ~ '.' ~ object ~ ' to application ' ~ application | lower ~ ';') %}
6464
{% endif %}
6565
{% endfor %}
6666
{% endfor %}

0 commit comments

Comments
 (0)