| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2021, Florian Dambrine <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: pritunl_user |
| 10 |
author: "Florian Dambrine (@Lowess)" |
| 11 |
version_added: 2.3.0 |
| 12 |
short_description: Manage Pritunl Users using the Pritunl API |
| 13 |
description: |
| 14 |
- A module to manage Pritunl users using the Pritunl API. |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._pritunl |
| 17 |
- community.general._attributes |
| 18 |
attributes: |
| 19 |
check_mode: |
| 20 |
support: none |
| 21 |
diff_mode: |
| 22 |
support: none |
| 23 |
options: |
| 24 |
organization: |
| 25 |
type: str |
| 26 |
required: true |
| 27 |
aliases: |
| 28 |
- org |
| 29 |
description: |
| 30 |
- The name of the organization the user is part of. |
| 31 |
state: |
| 32 |
type: str |
| 33 |
default: 'present' |
| 34 |
choices: |
| 35 |
- present |
| 36 |
- absent |
| 37 |
description: |
| 38 |
- If V(present), the module adds user O(user_name) to the Pritunl O(organization). If V(absent), removes the user O(user_name) |
| 39 |
from the Pritunl O(organization). |
| 40 |
user_name: |
| 41 |
type: str |
| 42 |
required: true |
| 43 |
default: |
| 44 |
description: |
| 45 |
- Name of the user to create or delete from Pritunl. |
| 46 |
user_email: |
| 47 |
type: str |
| 48 |
default: |
| 49 |
description: |
| 50 |
- Email address associated with the user O(user_name). |
| 51 |
user_type: |
| 52 |
type: str |
| 53 |
default: client |
| 54 |
choices: |
| 55 |
- client |
| 56 |
- server |
| 57 |
description: |
| 58 |
- Type of the user O(user_name). |
| 59 |
user_groups: |
| 60 |
type: list |
| 61 |
elements: str |
| 62 |
default: |
| 63 |
description: |
| 64 |
- List of groups associated with the user O(user_name). |
| 65 |
user_disabled: |
| 66 |
type: bool |
| 67 |
default: |
| 68 |
description: |
| 69 |
- Enable/Disable the user O(user_name). |
| 70 |
user_gravatar: |
| 71 |
type: bool |
| 72 |
default: |
| 73 |
description: |
| 74 |
- Enable/Disable Gravatar usage for the user O(user_name). |
| 75 |
user_mac_addresses: |
| 76 |
type: list |
| 77 |
elements: str |
| 78 |
description: |
| 79 |
- Allowed MAC addresses for the user O(user_name). |
| 80 |
version_added: 5.0.0 |
| 81 |
""" |
| 82 |
|
| 83 |
EXAMPLES = r""" |
| 84 |
- name: Create the user Foo with email address [email protected] in MyOrg |
| 85 |
community.general.pritunl_user: |
| 86 |
state: present |
| 87 |
organization: MyOrg |
| 88 |
user_name: Foo |
| 89 |
user_email: [email protected] |
| 90 |
user_mac_addresses: |
| 91 |
- "00:00:00:00:00:99" |
| 92 |
|
| 93 |
- name: Disable the user Foo but keep it in Pritunl |
| 94 |
community.general.pritunl_user: |
| 95 |
state: present |
| 96 |
organization: MyOrg |
| 97 |
user_name: Foo |
| 98 |
user_email: [email protected] |
| 99 |
user_disabled: true |
| 100 |
|
| 101 |
- name: Make sure the user Foo is not part of MyOrg anymore |
| 102 |
community.general.pritunl_user: |
| 103 |
state: absent |
| 104 |
organization: MyOrg |
| 105 |
user_name: Foo |
| 106 |
""" |
| 107 |
|
| 108 |
RETURN = r""" |
| 109 |
response: |
| 110 |
description: JSON representation of Pritunl Users. |
| 111 |
returned: success |
| 112 |
type: dict |
| 113 |
sample: |
| 114 |
{ |
| 115 |
"audit": false, |
| 116 |
"auth_type": "google", |
| 117 |
"bypass_secondary": false, |
| 118 |
"client_to_client": false, |
| 119 |
"disabled": false, |
| 120 |
"dns_mapping": null, |
| 121 |
"dns_servers": null, |
| 122 |
"dns_suffix": null, |
| 123 |
"email": "[email protected]", |
| 124 |
"gravatar": true, |
| 125 |
"groups": [ |
| 126 |
"foo", |
| 127 |
"bar" |
| 128 |
], |
| 129 |
"id": "5d070dafe63q3b2e6s472c3b", |
| 130 |
"name": "[email protected]", |
| 131 |
"network_links": [], |
| 132 |
"organization": "58070daee6sf342e6e4s2c36", |
| 133 |
"organization_name": "Acme", |
| 134 |
"otp_auth": true, |
| 135 |
"otp_secret": "35H5EJA3XB2$4CWG", |
| 136 |
"pin": false, |
| 137 |
"port_forwarding": [], |
| 138 |
"servers": [] |
| 139 |
} |
| 140 |
""" |
| 141 |
|
| 142 |
|
| 143 |
from ansible.module_utils.basic import AnsibleModule |
| 144 |
from ansible.module_utils.common.dict_transformations import dict_merge |
| 145 |
|
| 146 |
from ansible_collections.community.general.plugins.module_utils._pritunl_api import ( |
| 147 |
PritunlException, |
| 148 |
delete_pritunl_user, |
| 149 |
get_pritunl_settings, |
| 150 |
list_pritunl_organizations, |
| 151 |
list_pritunl_users, |
| 152 |
post_pritunl_user, |
| 153 |
pritunl_argument_spec, |
| 154 |
) |
| 155 |
|
| 156 |
|
| 157 |
def add_or_update_pritunl_user(module): |
| 158 |
result = {} |
| 159 |
|
| 160 |
org_name = module.params.get("organization") |
| 161 |
user_name = module.params.get("user_name") |
| 162 |
|
| 163 |
user_params = { |
| 164 |
"name": user_name, |
| 165 |
"email": module.params.get("user_email"), |
| 166 |
"groups": module.params.get("user_groups"), |
| 167 |
"disabled": module.params.get("user_disabled"), |
| 168 |
"gravatar": module.params.get("user_gravatar"), |
| 169 |
"mac_addresses": module.params.get("user_mac_addresses"), |
| 170 |
"type": module.params.get("user_type"), |
| 171 |
} |
| 172 |
|
| 173 |
org_obj_list = list_pritunl_organizations( |
| 174 |
**dict_merge( |
| 175 |
get_pritunl_settings(module), |
| 176 |
{"filters": {"name": org_name}}, |
| 177 |
) |
| 178 |
) |
| 179 |
|
| 180 |
if len(org_obj_list) == 0: |
| 181 |
module.fail_json(msg=f"Can not add user to organization '{org_name}' which does not exist") |
| 182 |
|
| 183 |
org_id = org_obj_list[0]["id"] |
| 184 |
|
| 185 |
# Grab existing users from this org |
| 186 |
users = list_pritunl_users( |
| 187 |
**dict_merge( |
| 188 |
get_pritunl_settings(module), |
| 189 |
{ |
| 190 |
"organization_id": org_id, |
| 191 |
"filters": {"name": user_name}, |
| 192 |
}, |
| 193 |
) |
| 194 |
) |
| 195 |
|
| 196 |
# Check if the pritunl user already exists |
| 197 |
if len(users) > 0: |
| 198 |
# Compare remote user params with local user_params and trigger update if needed |
| 199 |
user_params_changed = False |
| 200 |
for key in user_params.keys(): |
| 201 |
# When a param is not specified grab existing ones to prevent from changing it with the PUT request |
| 202 |
if user_params[key] is None: |
| 203 |
user_params[key] = users[0].get(key) |
| 204 |
|
| 205 |
# 'groups' and 'mac_addresses' are list comparison |
| 206 |
if key == "groups" or key == "mac_addresses": |
| 207 |
remote_list = users[0].get(key) or [] |
| 208 |
local_list = user_params[key] or [] |
| 209 |
if set(remote_list) != set(local_list): |
| 210 |
user_params_changed = True |
| 211 |
|
| 212 |
# otherwise it is either a boolean or a string |
| 213 |
else: |
| 214 |
if users[0].get(key) != user_params[key]: |
| 215 |
user_params_changed = True |
| 216 |
|
| 217 |
# Trigger a PUT on the API to update the current user if settings have changed |
| 218 |
if user_params_changed: |
| 219 |
response = post_pritunl_user( |
| 220 |
**dict_merge( |
| 221 |
get_pritunl_settings(module), |
| 222 |
{ |
| 223 |
"organization_id": org_id, |
| 224 |
"user_id": users[0]["id"], |
| 225 |
"user_data": user_params, |
| 226 |
}, |
| 227 |
) |
| 228 |
) |
| 229 |
|
| 230 |
result["changed"] = True |
| 231 |
result["response"] = response |
| 232 |
else: |
| 233 |
result["changed"] = False |
| 234 |
result["response"] = users |
| 235 |
else: |
| 236 |
response = post_pritunl_user( |
| 237 |
**dict_merge( |
| 238 |
get_pritunl_settings(module), |
| 239 |
{ |
| 240 |
"organization_id": org_id, |
| 241 |
"user_data": user_params, |
| 242 |
}, |
| 243 |
) |
| 244 |
) |
| 245 |
result["changed"] = True |
| 246 |
result["response"] = response |
| 247 |
|
| 248 |
module.exit_json(**result) |
| 249 |
|
| 250 |
|
| 251 |
def remove_pritunl_user(module): |
| 252 |
result = {} |
| 253 |
|
| 254 |
org_name = module.params.get("organization") |
| 255 |
user_name = module.params.get("user_name") |
| 256 |
|
| 257 |
org_obj_list = [] |
| 258 |
|
| 259 |
org_obj_list = list_pritunl_organizations( |
| 260 |
**dict_merge( |
| 261 |
get_pritunl_settings(module), |
| 262 |
{ |
| 263 |
"filters": {"name": org_name}, |
| 264 |
}, |
| 265 |
) |
| 266 |
) |
| 267 |
|
| 268 |
if len(org_obj_list) == 0: |
| 269 |
module.fail_json(msg=f"Can not remove user '{user_name}' from a non existing organization '{org_name}'") |
| 270 |
|
| 271 |
org_id = org_obj_list[0]["id"] |
| 272 |
|
| 273 |
# Grab existing users from this org |
| 274 |
users = list_pritunl_users( |
| 275 |
**dict_merge( |
| 276 |
get_pritunl_settings(module), |
| 277 |
{ |
| 278 |
"organization_id": org_id, |
| 279 |
"filters": {"name": user_name}, |
| 280 |
}, |
| 281 |
) |
| 282 |
) |
| 283 |
|
| 284 |
# Check if the pritunl user exists, if not, do nothing |
| 285 |
if len(users) == 0: |
| 286 |
result["changed"] = False |
| 287 |
result["response"] = {} |
| 288 |
|
| 289 |
# Otherwise remove the org from Pritunl |
| 290 |
else: |
| 291 |
response = delete_pritunl_user( |
| 292 |
**dict_merge( |
| 293 |
get_pritunl_settings(module), |
| 294 |
{ |
| 295 |
"organization_id": org_id, |
| 296 |
"user_id": users[0]["id"], |
| 297 |
}, |
| 298 |
) |
| 299 |
) |
| 300 |
result["changed"] = True |
| 301 |
result["response"] = response |
| 302 |
|
| 303 |
module.exit_json(**result) |
| 304 |
|
| 305 |
|
| 306 |
def main(): |
| 307 |
argument_spec = pritunl_argument_spec() |
| 308 |
|
| 309 |
argument_spec.update( |
| 310 |
dict( |
| 311 |
organization=dict(required=True, type="str", aliases=["org"]), |
| 312 |
state=dict(choices=["present", "absent"], default="present"), |
| 313 |
user_name=dict(required=True, type="str"), |
| 314 |
user_type=dict(choices=["client", "server"], default="client"), |
| 315 |
user_email=dict(type="str"), |
| 316 |
user_groups=dict(type="list", elements="str"), |
| 317 |
user_disabled=dict(type="bool"), |
| 318 |
user_gravatar=dict(type="bool"), |
| 319 |
user_mac_addresses=dict(type="list", elements="str"), |
| 320 |
) |
| 321 |
) |
| 322 |
|
| 323 |
module = AnsibleModule(argument_spec=argument_spec) |
| 324 |
|
| 325 |
state = module.params.get("state") |
| 326 |
|
| 327 |
try: |
| 328 |
if state == "present": |
| 329 |
add_or_update_pritunl_user(module) |
| 330 |
elif state == "absent": |
| 331 |
remove_pritunl_user(module) |
| 332 |
except PritunlException as e: |
| 333 |
module.fail_json(msg=f"{e}") |
| 334 |
|
| 335 |
|
| 336 |
if __name__ == "__main__": |
| 337 |
main() |