Skip to content

Commit

Permalink
clickhouse_info: add grants field for roles and users return vals (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersson007 authored Aug 28, 2024
1 parent 07cd26a commit ab108f0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/0-info_add_grants.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- clickhouse_info - add the ``grants`` return value for users and roles.
15 changes: 14 additions & 1 deletion plugins/modules/clickhouse_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,12 @@ def get_roles(module, client):

roles_info = {}
for row in result:
roles_info[row[0]] = {
role_name = row[0]
roles_info[role_name] = {
"id": str(row[1]),
"storage": row[2],
}
roles_info[role_name]["grants"] = get_grants(module, client, role_name)

return roles_info

Expand Down Expand Up @@ -451,10 +453,21 @@ def get_users(module, client):
}

user_info[user_name]["roles"] = get_user_roles(module, client, user_name)
user_info[user_name]["grants"] = get_grants(module, client, user_name)

return user_info


def get_grants(module, client, name):
"""Get grants.
Return a list of grants.
"""
query = ("SHOW GRANTS FOR '%s'" % name)
result = execute_query(module, client, query)
return [row[0] for row in result]


def get_user_roles(module, client, user_name):
"""Get user roles.
Expand Down
11 changes: 7 additions & 4 deletions tests/integration/targets/clickhouse_info/tasks/initial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

- name: Grant role
community.clickhouse.clickhouse_client:
execute: "GRANT {{ item }} TO bob"
loop:
- accountant
- sales
execute: "GRANT accountant, sales TO bob"

- name: Grant role
community.clickhouse.clickhouse_client:
execute: "GRANT sales TO accountant"

- name: Get info
register: result
Expand All @@ -34,7 +35,9 @@
- result is not changed
- result["users"]["default"] != {}
- result["users"]["bob"]["roles"] == ["accountant", "sales"] or result["users"]["bob"]["roles"] == ["sales", "accountant"]
- result["users"]["bob"]["grants"] == ["GRANT accountant, sales TO bob"]
- result["roles"]["accountant"] != {}
- result["roles"]["accountant"]["grants"] == ["GRANT sales TO accountant"]
- result["databases"]["default"]["engine"] == "Atomic"
- result["version"] != {}
- result["driver"]["version"] != {}
Expand Down

0 comments on commit ab108f0

Please sign in to comment.