| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2017, Vitaliy Zhhuta <zhhuta () gmail.com> |
| 4 |
# insipred by Kamil Szczygiel <kamil.szczygiel () intel.com> influxdb_database module |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: influxdb_user |
| 12 |
short_description: Manage InfluxDB users |
| 13 |
description: |
| 14 |
- Manage InfluxDB users. |
| 15 |
author: "Vitaliy Zhhuta (@zhhuta)" |
| 16 |
requirements: |
| 17 |
- "influxdb >= 0.9" |
| 18 |
attributes: |
| 19 |
check_mode: |
| 20 |
support: full |
| 21 |
diff_mode: |
| 22 |
support: none |
| 23 |
options: |
| 24 |
user_name: |
| 25 |
description: |
| 26 |
- Name of the user. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
user_password: |
| 30 |
description: |
| 31 |
- Password to be set for the user. |
| 32 |
type: str |
| 33 |
admin: |
| 34 |
description: |
| 35 |
- Whether the user should be in the admin role or not. |
| 36 |
- Since version 2.8, the role is also updated. |
| 37 |
default: false |
| 38 |
type: bool |
| 39 |
state: |
| 40 |
description: |
| 41 |
- State of the user. |
| 42 |
choices: [absent, present] |
| 43 |
default: present |
| 44 |
type: str |
| 45 |
grants: |
| 46 |
description: |
| 47 |
- Privileges to grant to this user. |
| 48 |
- Takes a list of dicts containing the "database" and "privilege" keys. |
| 49 |
- If this argument is not provided, the current grants are left alone. |
| 50 |
- If an empty list is provided, all grants for the user are removed. |
| 51 |
type: list |
| 52 |
elements: dict |
| 53 |
extends_documentation_fragment: |
| 54 |
- community.general._influxdb |
| 55 |
- community.general._attributes |
| 56 |
""" |
| 57 |
|
| 58 |
EXAMPLES = r""" |
| 59 |
- name: Create a user on localhost using default login credentials |
| 60 |
community.general.influxdb_user: |
| 61 |
user_name: john |
| 62 |
user_password: s3cr3t |
| 63 |
|
| 64 |
- name: Create a user on localhost using custom login credentials |
| 65 |
community.general.influxdb_user: |
| 66 |
user_name: john |
| 67 |
user_password: s3cr3t |
| 68 |
login_username: "{{ influxdb_username }}" |
| 69 |
login_password: "{{ influxdb_password }}" |
| 70 |
|
| 71 |
- name: Create an admin user on a remote host using custom login credentials |
| 72 |
community.general.influxdb_user: |
| 73 |
user_name: john |
| 74 |
user_password: s3cr3t |
| 75 |
admin: true |
| 76 |
hostname: "{{ influxdb_hostname }}" |
| 77 |
login_username: "{{ influxdb_username }}" |
| 78 |
login_password: "{{ influxdb_password }}" |
| 79 |
|
| 80 |
- name: Create a user on localhost with privileges |
| 81 |
community.general.influxdb_user: |
| 82 |
user_name: john |
| 83 |
user_password: s3cr3t |
| 84 |
login_username: "{{ influxdb_username }}" |
| 85 |
login_password: "{{ influxdb_password }}" |
| 86 |
grants: |
| 87 |
- database: 'collectd' |
| 88 |
privilege: 'WRITE' |
| 89 |
- database: 'graphite' |
| 90 |
privilege: 'READ' |
| 91 |
|
| 92 |
- name: Destroy a user using custom login credentials |
| 93 |
community.general.influxdb_user: |
| 94 |
user_name: john |
| 95 |
login_username: "{{ influxdb_username }}" |
| 96 |
login_password: "{{ influxdb_password }}" |
| 97 |
state: absent |
| 98 |
""" |
| 99 |
|
| 100 |
RETURN = r"""#""" |
| 101 |
|
| 102 |
import json |
| 103 |
|
| 104 |
from ansible.module_utils.basic import AnsibleModule |
| 105 |
from ansible.module_utils.urls import ConnectionError |
| 106 |
|
| 107 |
import ansible_collections.community.general.plugins.module_utils._influxdb as influx |
| 108 |
|
| 109 |
|
| 110 |
def find_user(module, client, user_name): |
| 111 |
user_result = None |
| 112 |
|
| 113 |
try: |
| 114 |
users = client.get_list_users() |
| 115 |
for user in users: |
| 116 |
if user["user"] == user_name: |
| 117 |
user_result = user |
| 118 |
break |
| 119 |
except ConnectionError as e: |
| 120 |
module.fail_json(msg=f"{e}") |
| 121 |
return user_result |
| 122 |
|
| 123 |
|
| 124 |
def check_user_password(module, client, user_name, user_password): |
| 125 |
try: |
| 126 |
client.switch_user(user_name, user_password) |
| 127 |
client.get_list_users() |
| 128 |
except influx.exceptions.InfluxDBClientError as e: |
| 129 |
if e.code == 401: |
| 130 |
return False |
| 131 |
except ConnectionError as e: |
| 132 |
module.fail_json(msg=f"{e}") |
| 133 |
finally: |
| 134 |
# restore previous user |
| 135 |
client.switch_user(module.params["username"], module.params["password"]) |
| 136 |
return True |
| 137 |
|
| 138 |
|
| 139 |
def set_user_password(module, client, user_name, user_password): |
| 140 |
if not module.check_mode: |
| 141 |
try: |
| 142 |
client.set_user_password(user_name, user_password) |
| 143 |
except ConnectionError as e: |
| 144 |
module.fail_json(msg=f"{e}") |
| 145 |
|
| 146 |
|
| 147 |
def create_user(module, client, user_name, user_password, admin): |
| 148 |
if not module.check_mode: |
| 149 |
try: |
| 150 |
client.create_user(user_name, user_password, admin) |
| 151 |
except ConnectionError as e: |
| 152 |
module.fail_json(msg=f"{e}") |
| 153 |
|
| 154 |
|
| 155 |
def drop_user(module, client, user_name): |
| 156 |
if not module.check_mode: |
| 157 |
try: |
| 158 |
client.drop_user(user_name) |
| 159 |
except influx.exceptions.InfluxDBClientError as e: |
| 160 |
module.fail_json(msg=e.content) |
| 161 |
|
| 162 |
module.exit_json(changed=True) |
| 163 |
|
| 164 |
|
| 165 |
def set_user_grants(module, client, user_name, grants): |
| 166 |
changed = False |
| 167 |
|
| 168 |
current_grants = [] |
| 169 |
try: |
| 170 |
current_grants = client.get_list_privileges(user_name) |
| 171 |
except influx.exceptions.InfluxDBClientError as e: |
| 172 |
if not module.check_mode or "user not found" not in e.content: |
| 173 |
module.fail_json(msg=e.content) |
| 174 |
|
| 175 |
try: |
| 176 |
parsed_grants = [] |
| 177 |
# Fix privileges wording |
| 178 |
for v in current_grants: |
| 179 |
if v["privilege"] != "NO PRIVILEGES": |
| 180 |
if v["privilege"] == "ALL PRIVILEGES": |
| 181 |
v["privilege"] = "ALL" |
| 182 |
parsed_grants.append(v) |
| 183 |
|
| 184 |
# check if the current grants are included in the desired ones |
| 185 |
for current_grant in parsed_grants: |
| 186 |
if current_grant not in grants: |
| 187 |
if not module.check_mode: |
| 188 |
client.revoke_privilege(current_grant["privilege"], current_grant["database"], user_name) |
| 189 |
changed = True |
| 190 |
|
| 191 |
# check if the desired grants are included in the current ones |
| 192 |
for grant in grants: |
| 193 |
if grant not in parsed_grants: |
| 194 |
if not module.check_mode: |
| 195 |
client.grant_privilege(grant["privilege"], grant["database"], user_name) |
| 196 |
changed = True |
| 197 |
|
| 198 |
except influx.exceptions.InfluxDBClientError as e: |
| 199 |
module.fail_json(msg=e.content) |
| 200 |
|
| 201 |
return changed |
| 202 |
|
| 203 |
|
| 204 |
INFLUX_AUTH_FIRST_USER_REQUIRED = "error authorizing query: create admin user first or disable authentication" |
| 205 |
|
| 206 |
|
| 207 |
def main(): |
| 208 |
argument_spec = influx.InfluxDb.influxdb_argument_spec() |
| 209 |
argument_spec.update( |
| 210 |
state=dict(default="present", type="str", choices=["present", "absent"]), |
| 211 |
user_name=dict(required=True, type="str"), |
| 212 |
user_password=dict(type="str", no_log=True), |
| 213 |
admin=dict(default="False", type="bool"), |
| 214 |
grants=dict(type="list", elements="dict"), |
| 215 |
) |
| 216 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 217 |
|
| 218 |
state = module.params["state"] |
| 219 |
user_name = module.params["user_name"] |
| 220 |
user_password = module.params["user_password"] |
| 221 |
admin = module.params["admin"] |
| 222 |
grants = module.params["grants"] |
| 223 |
influxdb = influx.InfluxDb(module) |
| 224 |
client = influxdb.connect_to_influxdb() |
| 225 |
|
| 226 |
user = None |
| 227 |
try: |
| 228 |
user = find_user(module, client, user_name) |
| 229 |
except influx.exceptions.InfluxDBClientError as e: |
| 230 |
if e.code == 403: |
| 231 |
reason = None |
| 232 |
try: |
| 233 |
msg = json.loads(e.content) |
| 234 |
reason = msg["error"] |
| 235 |
except (KeyError, ValueError): |
| 236 |
module.fail_json(msg=f"{e}") |
| 237 |
|
| 238 |
if reason != INFLUX_AUTH_FIRST_USER_REQUIRED: |
| 239 |
module.fail_json(msg=f"{e}") |
| 240 |
else: |
| 241 |
module.fail_json(msg=f"{e}") |
| 242 |
|
| 243 |
changed = False |
| 244 |
|
| 245 |
if state == "present": |
| 246 |
if user: |
| 247 |
if not check_user_password(module, client, user_name, user_password) and user_password is not None: |
| 248 |
set_user_password(module, client, user_name, user_password) |
| 249 |
changed = True |
| 250 |
|
| 251 |
try: |
| 252 |
if admin and not user["admin"]: |
| 253 |
if not module.check_mode: |
| 254 |
client.grant_admin_privileges(user_name) |
| 255 |
changed = True |
| 256 |
elif not admin and user["admin"]: |
| 257 |
if not module.check_mode: |
| 258 |
client.revoke_admin_privileges(user_name) |
| 259 |
changed = True |
| 260 |
except influx.exceptions.InfluxDBClientError as e: |
| 261 |
module.fail_json(msg=f"{e}") |
| 262 |
|
| 263 |
else: |
| 264 |
user_password = user_password or "" |
| 265 |
create_user(module, client, user_name, user_password, admin) |
| 266 |
changed = True |
| 267 |
|
| 268 |
if grants is not None: |
| 269 |
if set_user_grants(module, client, user_name, grants): |
| 270 |
changed = True |
| 271 |
|
| 272 |
module.exit_json(changed=changed) |
| 273 |
|
| 274 |
if state == "absent": |
| 275 |
if user: |
| 276 |
drop_user(module, client, user_name) |
| 277 |
else: |
| 278 |
module.exit_json(changed=False) |
| 279 |
|
| 280 |
|
| 281 |
if __name__ == "__main__": |
| 282 |
main() |