|
2 | 2 | {% if flags.WHICH in ['run', 'run-operation'] %} |
3 | 3 | {% set revoke_statements = [] %} |
4 | 4 | {% set grant_statements = [] %} |
5 | | - {% set existing_grants = [] %} |
6 | 5 | {% set execute_statements = [] %} |
7 | 6 |
|
8 | 7 | {% for object in objects %} |
9 | | - {% do log("====> Processing " ~ object_type ~ " for " ~ object ~ " with grants " ~ grant_types | join(", ") ~ " and roles " ~ grant_roles | join(", "), info=True) %} |
| 8 | + {% set existing_grants = [] %} {# reset per object #} |
| 9 | + {% do log("====> Processing " ~ object_type ~ " for " ~ object ~ " with grants " ~ grant_types | join(", ") ~ " for roles " ~ grant_roles | join(", "), info=True) %} |
10 | 10 | {% set query %} |
11 | 11 | show grants on {{ object_type }} {{ target.database }}.{{ object }}; |
12 | 12 | {% endset %} |
|
16 | 16 | {% if row.privilege not in ["OWNERSHIP", "SELECT", "REFERENCES", "REBUILD"] %} |
17 | 17 | {% if row.privilege in grant_types %} |
18 | 18 | {% if row.grantee_name not in grant_roles %} |
19 | | - {{ revoke_statements.append({ "privilege" : row.privilege, "role" : row.grantee_name, "object" : object }) }} |
| 19 | + {{ revoke_statements.append({ "privilege": row.privilege, "role": row.grantee_name, "object": object }) }} |
20 | 20 | {% else %} |
21 | | - {{ existing_grants.append({ "privilege" : row.privilege, "role" : row.grantee_name, "object" : object }) }} |
22 | | - {%endif%} |
| 21 | + {# add only once per (role, privilege, object) #} |
| 22 | + {% set exists = false %} |
| 23 | + {% for eg in existing_grants %} |
| 24 | + {% if eg.role == row.grantee_name and eg.privilege == row.privilege and eg.object == object %} |
| 25 | + {% set exists = true %} |
| 26 | + {% endif %} |
| 27 | + {% endfor %} |
| 28 | + {% if not exists %} |
| 29 | + {{ existing_grants.append({ "privilege": row.privilege, "role": row.grantee_name, "object": object }) }} |
| 30 | + {% endif %} |
| 31 | + {% endif %} |
23 | 32 | {% else %} |
24 | | - {{ revoke_statements.append({ "privilege" : row.privilege, "role" : row.grantee_name, "object" : object }) }} |
25 | | - {%endif%} |
| 33 | + {{ revoke_statements.append({ "privilege": row.privilege, "role": row.grantee_name, "object": object }) }} |
| 34 | + {% endif %} |
26 | 35 | {% endif %} |
27 | 36 | {% endfor %} |
| 37 | + |
28 | 38 | {% for role in grant_roles %} |
29 | 39 | {% do log("====> Checking " ~ object_type ~ " for " ~ object ~ " with role " ~ role, info=True) %} |
30 | 40 | {% set existing_role_grants = [] %} |
31 | 41 | {% for existing_grant in existing_grants %} |
32 | | - {% if existing_grant.role == role %} |
33 | | - {{ existing_role_grants.append(existing_grant.privilege) }} |
| 42 | + {% if existing_grant.role == role and existing_grant.object == object %} |
| 43 | + {% if existing_grant.privilege not in existing_role_grants %} |
| 44 | + {{ existing_role_grants.append(existing_grant.privilege) }} |
| 45 | + {% endif %} |
34 | 46 | {% endif %} |
35 | 47 | {% endfor %} |
36 | | - {% do log("====> Checking " ~ object_type ~ " for " ~ object ~ " with role " ~ role ~ " : existing_role_grants - " ~ existing_role_grants | join(", "), info=True) %} |
| 48 | + {% do log("====> Existing grants for role " ~ role ~ " on " ~ object ~ " : " ~ (existing_role_grants | join(", ")), info=True) %} |
37 | 49 | {% for privilege in grant_types %} |
38 | | - {% do log("====> Checking " ~ object_type ~ " for " ~ object ~ " with privilege " ~ privilege, info=True) %} |
39 | 50 | {% if privilege not in existing_role_grants %} |
40 | | - {{ grant_statements.append({ "privilege" : privilege, "role" : role, "object" : object }) }} |
| 51 | + {{ grant_statements.append({ "privilege": privilege, "role": role, "object": object }) }} |
41 | 52 | {% endif %} |
42 | 53 | {% endfor %} |
43 | 54 | {% endfor %} |
44 | | - {%endif%} |
45 | | - {%endfor%} |
| 55 | + {% endif %} |
| 56 | + {% endfor %} |
46 | 57 | {% for stm in revoke_statements %} |
47 | 58 | {{ execute_statements.append("revoke " ~ stm.privilege ~ " on " ~ object_type ~ " " ~ target.database ~ "." ~ stm.object ~ " from role " ~ stm.role ~ ";") }} |
48 | 59 | {% endfor %} |
49 | 60 | {% for stm in grant_statements %} |
50 | 61 | {{ execute_statements.append("grant " ~ stm.privilege ~ " on " ~ object_type ~ " " ~ target.database ~ "." ~ stm.object ~ " to role " ~ stm.role ~ ";") }} |
51 | 62 | {% endfor %} |
52 | 63 | {% if execute_statements | length > 0 %} |
53 | | - {% do log("Executing privilege grants and revokes for " ~ object_type ~"s...", info=True) %} |
| 64 | + {% do log("Executing privilege grants and revokes for " ~ object_type ~ "s...", info=True) %} |
54 | 65 | {% for statement in execute_statements %} |
55 | 66 | {% do log(statement, info=True) %} |
56 | 67 | {% set grant = run_query(statement) %} |
|
0 commit comments