| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2021, Lennert Mertens ([email protected]) |
| 4 |
# Copyright (c) 2019, Guillaume Martinez ([email protected]) |
| 5 |
# Copyright (c) 2015, Werner Dijkerman ([email protected]) |
| 6 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
module: gitlab_user |
| 13 |
short_description: Creates/updates/deletes/blocks/unblocks GitLab Users |
| 14 |
description: |
| 15 |
- When the user does not exist in GitLab, it is created. |
| 16 |
- When the user exists and state=absent, the user is deleted. |
| 17 |
- When the user exists and state=blocked, the user is blocked. |
| 18 |
- When changes are made to user, the user is updated. |
| 19 |
notes: |
| 20 |
- From community.general 0.2.0 and onwards, name, email and password are optional while deleting the user. |
| 21 |
author: |
| 22 |
- Werner Dijkerman (@dj-wasabi) |
| 23 |
- Guillaume Martinez (@Lunik) |
| 24 |
- Lennert Mertens (@LennertMertens) |
| 25 |
- Stef Graces (@stgrace) |
| 26 |
requirements: |
| 27 |
- python-gitlab python module |
| 28 |
- administrator rights on the GitLab server |
| 29 |
extends_documentation_fragment: |
| 30 |
- community.general._auth_basic |
| 31 |
- community.general._gitlab |
| 32 |
- community.general._attributes |
| 33 |
|
| 34 |
attributes: |
| 35 |
check_mode: |
| 36 |
support: full |
| 37 |
diff_mode: |
| 38 |
support: none |
| 39 |
|
| 40 |
options: |
| 41 |
name: |
| 42 |
description: |
| 43 |
- Name of the user you want to create. |
| 44 |
- Required only if O(state=present). |
| 45 |
type: str |
| 46 |
username: |
| 47 |
description: |
| 48 |
- The username of the user. |
| 49 |
required: true |
| 50 |
type: str |
| 51 |
password: |
| 52 |
description: |
| 53 |
- The password of the user. |
| 54 |
- GitLab server enforces minimum password length to 8, set this value with 8 or more characters. |
| 55 |
type: str |
| 56 |
reset_password: |
| 57 |
description: |
| 58 |
- Whether the user can change its password or not. |
| 59 |
default: false |
| 60 |
type: bool |
| 61 |
version_added: 3.3.0 |
| 62 |
email: |
| 63 |
description: |
| 64 |
- The email that belongs to the user. |
| 65 |
- Required only if O(state=present). |
| 66 |
type: str |
| 67 |
sshkey_name: |
| 68 |
description: |
| 69 |
- The name of the SSH public key. |
| 70 |
type: str |
| 71 |
sshkey_file: |
| 72 |
description: |
| 73 |
- The SSH public key itself. |
| 74 |
type: str |
| 75 |
sshkey_expires_at: |
| 76 |
description: |
| 77 |
- The expiration date of the SSH public key in ISO 8601 format C(YYYY-MM-DDTHH:MM:SSZ). |
| 78 |
- This is only used when creating or replacing SSH public keys. |
| 79 |
type: str |
| 80 |
version_added: 3.1.0 |
| 81 |
sshkey_update_mode: |
| 82 |
description: |
| 83 |
- Controls how existing SSH keys with the same O(sshkey_name) are handled. |
| 84 |
- Use V(create) to preserve the historical behavior and leave existing keys with the same name unchanged. |
| 85 |
- Use V(update) to replace an existing key only when exactly one key with the same name exists and its key material differs. |
| 86 |
- Use V(deduplicate) to delete all keys with the same name before creating the requested key. |
| 87 |
- SSH key comments are ignored when comparing key material. |
| 88 |
type: str |
| 89 |
choices: ["create", "update", "deduplicate"] |
| 90 |
default: create |
| 91 |
version_added: 13.0.0 |
| 92 |
group: |
| 93 |
description: |
| 94 |
- ID or Full path of parent group in the form of group/name. |
| 95 |
- Add user as a member to this group. |
| 96 |
type: str |
| 97 |
access_level: |
| 98 |
description: |
| 99 |
- The access level to the group. |
| 100 |
- The value V(master) is an alias for V(maintainer). |
| 101 |
default: guest |
| 102 |
type: str |
| 103 |
choices: ["guest", "reporter", "developer", "master", "maintainer", "owner"] |
| 104 |
state: |
| 105 |
description: |
| 106 |
- Create, delete or block a user. |
| 107 |
default: present |
| 108 |
type: str |
| 109 |
choices: ["present", "absent", "blocked", "unblocked"] |
| 110 |
confirm: |
| 111 |
description: |
| 112 |
- Require confirmation. |
| 113 |
type: bool |
| 114 |
default: true |
| 115 |
isadmin: |
| 116 |
description: |
| 117 |
- Grant admin privileges to the user. |
| 118 |
type: bool |
| 119 |
default: false |
| 120 |
external: |
| 121 |
description: |
| 122 |
- Define external parameter for this user. |
| 123 |
type: bool |
| 124 |
default: false |
| 125 |
identities: |
| 126 |
description: |
| 127 |
- List of identities to be added/updated for this user. |
| 128 |
- To remove all other identities from this user, set O(overwrite_identities=true). |
| 129 |
type: list |
| 130 |
elements: dict |
| 131 |
suboptions: |
| 132 |
provider: |
| 133 |
description: |
| 134 |
- The name of the external identity provider. |
| 135 |
type: str |
| 136 |
extern_uid: |
| 137 |
description: |
| 138 |
- User ID for external identity. |
| 139 |
type: str |
| 140 |
version_added: 3.3.0 |
| 141 |
overwrite_identities: |
| 142 |
description: |
| 143 |
- Overwrite identities with identities added in this module. |
| 144 |
- This means that all identities that the user has and that are not listed in O(identities) are removed from the user. |
| 145 |
- This is only done if a list is provided for O(identities). To remove all identities, provide an empty list. |
| 146 |
type: bool |
| 147 |
default: false |
| 148 |
version_added: 3.3.0 |
| 149 |
""" |
| 150 |
|
| 151 |
EXAMPLES = r""" |
| 152 |
- name: "Delete GitLab User" |
| 153 |
community.general.gitlab_user: |
| 154 |
api_url: https://gitlab.example.com/ |
| 155 |
api_token: "{{ access_token }}" |
| 156 |
username: myusername |
| 157 |
state: absent |
| 158 |
|
| 159 |
- name: "Create GitLab User" |
| 160 |
community.general.gitlab_user: |
| 161 |
api_url: https://gitlab.example.com/ |
| 162 |
validate_certs: true |
| 163 |
api_username: dj-wasabi |
| 164 |
api_password: "MySecretPassword" |
| 165 |
name: My Name |
| 166 |
username: myusername |
| 167 |
password: mysecretpassword |
| 168 |
email: [email protected] |
| 169 |
sshkey_name: MySSH |
| 170 |
sshkey_file: ssh-rsa AAAAB3NzaC1yc... |
| 171 |
state: present |
| 172 |
group: super_group/mon_group |
| 173 |
access_level: owner |
| 174 |
|
| 175 |
- name: "Replace a GitLab SSH key when one matching name exists" |
| 176 |
community.general.gitlab_user: |
| 177 |
api_url: https://gitlab.example.com/ |
| 178 |
api_token: "{{ access_token }}" |
| 179 |
name: My Name |
| 180 |
username: myusername |
| 181 |
email: [email protected] |
| 182 |
sshkey_name: MySSH |
| 183 |
sshkey_file: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... |
| 184 |
sshkey_update_mode: update |
| 185 |
state: present |
| 186 |
|
| 187 |
- name: "Create GitLab User using external identity provider" |
| 188 |
community.general.gitlab_user: |
| 189 |
api_url: https://gitlab.example.com/ |
| 190 |
validate_certs: true |
| 191 |
api_token: "{{ access_token }}" |
| 192 |
name: My Name |
| 193 |
username: myusername |
| 194 |
password: mysecretpassword |
| 195 |
email: [email protected] |
| 196 |
identities: |
| 197 |
- provider: Keycloak |
| 198 |
extern_uid: f278f95c-12c7-4d51-996f-758cc2eb11bc |
| 199 |
state: present |
| 200 |
group: super_group/mon_group |
| 201 |
access_level: owner |
| 202 |
|
| 203 |
- name: "Block GitLab User" |
| 204 |
community.general.gitlab_user: |
| 205 |
api_url: https://gitlab.example.com/ |
| 206 |
api_token: "{{ access_token }}" |
| 207 |
username: myusername |
| 208 |
state: blocked |
| 209 |
|
| 210 |
- name: "Unblock GitLab User" |
| 211 |
community.general.gitlab_user: |
| 212 |
api_url: https://gitlab.example.com/ |
| 213 |
api_token: "{{ access_token }}" |
| 214 |
username: myusername |
| 215 |
state: unblocked |
| 216 |
""" |
| 217 |
|
| 218 |
RETURN = r""" |
| 219 |
msg: |
| 220 |
description: Success or failure message. |
| 221 |
returned: always |
| 222 |
type: str |
| 223 |
sample: "Success" |
| 224 |
|
| 225 |
result: |
| 226 |
description: JSON-parsed response from the server. |
| 227 |
returned: always |
| 228 |
type: dict |
| 229 |
|
| 230 |
error: |
| 231 |
description: The error message returned by the GitLab API. |
| 232 |
returned: failed |
| 233 |
type: str |
| 234 |
sample: "400: path is already in use" |
| 235 |
|
| 236 |
user: |
| 237 |
description: API object. |
| 238 |
returned: always |
| 239 |
type: dict |
| 240 |
""" |
| 241 |
|
| 242 |
|
| 243 |
from ansible.module_utils.api import basic_auth_argument_spec |
| 244 |
from ansible.module_utils.basic import AnsibleModule |
| 245 |
|
| 246 |
from ansible_collections.community.general.plugins.module_utils._gitlab import ( |
| 247 |
auth_argument_spec, |
| 248 |
find_group, |
| 249 |
gitlab, |
| 250 |
gitlab_authentication, |
| 251 |
list_all_kwargs, |
| 252 |
) |
| 253 |
|
| 254 |
|
| 255 |
class GitLabUser: |
| 256 |
def __init__(self, module, gitlab_instance): |
| 257 |
self._module = module |
| 258 |
self._gitlab = gitlab_instance |
| 259 |
self.user_object = None |
| 260 |
self.ACCESS_LEVEL = { |
| 261 |
"guest": gitlab.const.GUEST_ACCESS, |
| 262 |
"reporter": gitlab.const.REPORTER_ACCESS, |
| 263 |
"developer": gitlab.const.DEVELOPER_ACCESS, |
| 264 |
"master": gitlab.const.MAINTAINER_ACCESS, |
| 265 |
"maintainer": gitlab.const.MAINTAINER_ACCESS, |
| 266 |
"owner": gitlab.const.OWNER_ACCESS, |
| 267 |
} |
| 268 |
|
| 269 |
""" |
| 270 |
@param username Username of the user |
| 271 |
@param options User options |
| 272 |
""" |
| 273 |
|
| 274 |
def create_or_update_user(self, username, options): |
| 275 |
changed = False |
| 276 |
potentionally_changed = False |
| 277 |
|
| 278 |
# Because we have already call userExists in main() |
| 279 |
if self.user_object is None: |
| 280 |
user = self.create_user( |
| 281 |
{ |
| 282 |
"name": options["name"], |
| 283 |
"username": username, |
| 284 |
"password": options["password"], |
| 285 |
"reset_password": options["reset_password"], |
| 286 |
"email": options["email"], |
| 287 |
"skip_confirmation": not options["confirm"], |
| 288 |
"admin": options["isadmin"], |
| 289 |
"external": options["external"], |
| 290 |
"identities": options["identities"], |
| 291 |
} |
| 292 |
) |
| 293 |
changed = True |
| 294 |
else: |
| 295 |
changed, user = self.update_user( |
| 296 |
self.user_object, |
| 297 |
{ |
| 298 |
# add "normal" parameters here, put uncheckable |
| 299 |
# params in the dict below |
| 300 |
"name": {"value": options["name"]}, |
| 301 |
"email": {"value": options["email"]}, |
| 302 |
# note: for some attributes like this one the key |
| 303 |
# from reading back from server is unfortunately |
| 304 |
# different to the one needed for pushing/writing, |
| 305 |
# in that case use the optional setter key |
| 306 |
"is_admin": {"value": options["isadmin"], "setter": "admin"}, |
| 307 |
"external": {"value": options["external"]}, |
| 308 |
"identities": {"value": options["identities"]}, |
| 309 |
}, |
| 310 |
{ |
| 311 |
# put "uncheckable" params here, this means params |
| 312 |
# which the gitlab does accept for setting but does |
| 313 |
# not return any information about it |
| 314 |
"skip_reconfirmation": {"value": not options["confirm"]}, |
| 315 |
"password": {"value": options["password"]}, |
| 316 |
"reset_password": {"value": options["reset_password"]}, |
| 317 |
"overwrite_identities": {"value": options["overwrite_identities"]}, |
| 318 |
}, |
| 319 |
) |
| 320 |
|
| 321 |
# note: as we unfortunately have some uncheckable parameters |
| 322 |
# where it is not possible to determine if the update |
| 323 |
# changed something or not, we must assume here that a |
| 324 |
# changed happened and that an user object update is needed |
| 325 |
potentionally_changed = True |
| 326 |
|
| 327 |
# Assign ssh keys |
| 328 |
if options["sshkey_name"] and options["sshkey_file"]: |
| 329 |
key_changed = self.add_ssh_key_to_user( |
| 330 |
user, |
| 331 |
{ |
| 332 |
"name": options["sshkey_name"], |
| 333 |
"file": options["sshkey_file"], |
| 334 |
"expires_at": options["sshkey_expires_at"], |
| 335 |
"update_mode": options["sshkey_update_mode"], |
| 336 |
}, |
| 337 |
) |
| 338 |
changed = changed or key_changed |
| 339 |
|
| 340 |
# Assign group |
| 341 |
if options["group_path"]: |
| 342 |
group_changed = self.assign_user_to_group(user, options["group_path"], options["access_level"]) |
| 343 |
changed = changed or group_changed |
| 344 |
|
| 345 |
self.user_object = user |
| 346 |
if (changed or potentionally_changed) and not self._module.check_mode: |
| 347 |
try: |
| 348 |
user.save() |
| 349 |
except Exception as e: |
| 350 |
self._module.fail_json(msg=f"Failed to update user: {e} ") |
| 351 |
|
| 352 |
if changed: |
| 353 |
if self._module.check_mode: |
| 354 |
self._module.exit_json(changed=True, msg=f"Successfully created or updated the user {username}") |
| 355 |
return True |
| 356 |
else: |
| 357 |
return False |
| 358 |
|
| 359 |
""" |
| 360 |
@param group User object |
| 361 |
""" |
| 362 |
|
| 363 |
def get_user_id(self, user): |
| 364 |
if user is not None: |
| 365 |
return user.id |
| 366 |
return None |
| 367 |
|
| 368 |
""" |
| 369 |
@param user User object |
| 370 |
@param sshkey_name Name of the ssh key |
| 371 |
""" |
| 372 |
|
| 373 |
@staticmethod |
| 374 |
def normalize_ssh_key(ssh_key): |
| 375 |
return " ".join(ssh_key.split(None, 2)[:2]) |
| 376 |
|
| 377 |
def find_ssh_keys(self, user, sshkey_name): |
| 378 |
return [key for key in user.keys.list(**list_all_kwargs) if key.title == sshkey_name] |
| 379 |
|
| 380 |
def ssh_key_exists(self, user, sshkey_name): |
| 381 |
return bool(self.find_ssh_keys(user, sshkey_name)) |
| 382 |
|
| 383 |
""" |
| 384 |
@param user User object |
| 385 |
@param sshkey Dict containing sshkey infos {"name": "", "file": "", "expires_at": ""} |
| 386 |
""" |
| 387 |
|
| 388 |
def add_ssh_key_to_user(self, user, sshkey): |
| 389 |
user_keys = self.find_ssh_keys(user, sshkey["name"]) |
| 390 |
desired_key = self.normalize_ssh_key(sshkey["file"]) |
| 391 |
update_mode = sshkey["update_mode"] |
| 392 |
matching_keys = [key for key in user_keys if self.normalize_ssh_key(key.key) == desired_key] |
| 393 |
|
| 394 |
if update_mode == "create": |
| 395 |
if user_keys: |
| 396 |
return False |
| 397 |
elif update_mode == "update": |
| 398 |
if len(user_keys) > 1: |
| 399 |
self._module.warn( |
| 400 |
f"Found multiple SSH keys named '{sshkey['name']}'. Skipping update because sshkey_update_mode=update requires a single matching key." |
| 401 |
) |
| 402 |
return False |
| 403 |
if matching_keys: |
| 404 |
return False |
| 405 |
elif len(user_keys) == 1 and matching_keys: |
| 406 |
return False |
| 407 |
|
| 408 |
if self._module.check_mode: |
| 409 |
return True |
| 410 |
|
| 411 |
try: |
| 412 |
for key in user_keys: |
| 413 |
key.delete() |
| 414 |
|
| 415 |
parameter = { |
| 416 |
"title": sshkey["name"], |
| 417 |
"key": sshkey["file"], |
| 418 |
} |
| 419 |
if sshkey["expires_at"] is not None: |
| 420 |
parameter["expires_at"] = sshkey["expires_at"] |
| 421 |
user.keys.create(parameter) |
| 422 |
except gitlab.exceptions.GitlabDeleteError as e: |
| 423 |
self._module.fail_json(msg=f"Failed to update sshkey for user: {e}") |
| 424 |
except gitlab.exceptions.GitlabCreateError as e: |
| 425 |
self._module.fail_json(msg=f"Failed to assign sshkey to user: {e}") |
| 426 |
return True |
| 427 |
|
| 428 |
""" |
| 429 |
@param group Group object |
| 430 |
@param user_id Id of the user to find |
| 431 |
""" |
| 432 |
|
| 433 |
def find_member(self, group, user_id): |
| 434 |
try: |
| 435 |
member = group.members.get(user_id) |
| 436 |
except gitlab.exceptions.GitlabGetError: |
| 437 |
return None |
| 438 |
return member |
| 439 |
|
| 440 |
""" |
| 441 |
@param group Group object |
| 442 |
@param user_id Id of the user to check |
| 443 |
""" |
| 444 |
|
| 445 |
def member_exists(self, group, user_id): |
| 446 |
member = self.find_member(group, user_id) |
| 447 |
|
| 448 |
return member is not None |
| 449 |
|
| 450 |
""" |
| 451 |
@param group Group object |
| 452 |
@param user_id Id of the user to check |
| 453 |
@param access_level GitLab access_level to check |
| 454 |
""" |
| 455 |
|
| 456 |
def member_as_good_access_level(self, group, user_id, access_level): |
| 457 |
member = self.find_member(group, user_id) |
| 458 |
|
| 459 |
return member.access_level == access_level |
| 460 |
|
| 461 |
""" |
| 462 |
@param user User object |
| 463 |
@param group_path Complete path of the Group including parent group path. <parent_path>/<group_path> |
| 464 |
@param access_level GitLab access_level to assign |
| 465 |
""" |
| 466 |
|
| 467 |
def assign_user_to_group(self, user, group_identifier, access_level): |
| 468 |
group = find_group(self._gitlab, group_identifier) |
| 469 |
|
| 470 |
if self._module.check_mode: |
| 471 |
return True |
| 472 |
|
| 473 |
if group is None: |
| 474 |
return False |
| 475 |
|
| 476 |
if self.member_exists(group, self.get_user_id(user)): |
| 477 |
member = self.find_member(group, self.get_user_id(user)) |
| 478 |
if not self.member_as_good_access_level(group, member.id, self.ACCESS_LEVEL[access_level]): |
| 479 |
member.access_level = self.ACCESS_LEVEL[access_level] |
| 480 |
member.save() |
| 481 |
return True |
| 482 |
else: |
| 483 |
try: |
| 484 |
group.members.create( |
| 485 |
{"user_id": self.get_user_id(user), "access_level": self.ACCESS_LEVEL[access_level]} |
| 486 |
) |
| 487 |
except gitlab.exceptions.GitlabCreateError as e: |
| 488 |
self._module.fail_json(msg=f"Failed to assign user to group: {e}") |
| 489 |
return True |
| 490 |
return False |
| 491 |
|
| 492 |
""" |
| 493 |
@param user User object |
| 494 |
@param arguments User attributes |
| 495 |
""" |
| 496 |
|
| 497 |
def update_user(self, user, arguments, uncheckable_args): |
| 498 |
changed = False |
| 499 |
|
| 500 |
for arg_key, arg_value in arguments.items(): |
| 501 |
av = arg_value["value"] |
| 502 |
|
| 503 |
if av is not None: |
| 504 |
if arg_key == "identities": |
| 505 |
changed = self.add_identities(user, av, uncheckable_args["overwrite_identities"]["value"]) |
| 506 |
|
| 507 |
elif getattr(user, arg_key) != av: |
| 508 |
setattr(user, arg_value.get("setter", arg_key), av) |
| 509 |
changed = True |
| 510 |
|
| 511 |
for arg_key, arg_value in uncheckable_args.items(): |
| 512 |
av = arg_value["value"] |
| 513 |
|
| 514 |
if av is not None: |
| 515 |
setattr(user, arg_value.get("setter", arg_key), av) |
| 516 |
|
| 517 |
return (changed, user) |
| 518 |
|
| 519 |
""" |
| 520 |
@param arguments User attributes |
| 521 |
""" |
| 522 |
|
| 523 |
def create_user(self, arguments): |
| 524 |
if self._module.check_mode: |
| 525 |
return True |
| 526 |
|
| 527 |
identities = None |
| 528 |
if "identities" in arguments: |
| 529 |
identities = arguments["identities"] |
| 530 |
del arguments["identities"] |
| 531 |
|
| 532 |
try: |
| 533 |
user = self._gitlab.users.create(arguments) |
| 534 |
if identities: |
| 535 |
self.add_identities(user, identities) |
| 536 |
|
| 537 |
except gitlab.exceptions.GitlabCreateError as e: |
| 538 |
self._module.fail_json(msg=f"Failed to create user: {e}") |
| 539 |
|
| 540 |
return user |
| 541 |
|
| 542 |
""" |
| 543 |
@param user User object |
| 544 |
@param identities List of identities to be added/updated |
| 545 |
@param overwrite_identities Overwrite user identities with identities passed to this module |
| 546 |
""" |
| 547 |
|
| 548 |
def add_identities(self, user, identities, overwrite_identities=False): |
| 549 |
changed = False |
| 550 |
if overwrite_identities: |
| 551 |
changed = self.delete_identities(user, identities) |
| 552 |
|
| 553 |
for identity in identities: |
| 554 |
if identity not in user.identities: |
| 555 |
user.provider = identity["provider"] |
| 556 |
user.extern_uid = identity["extern_uid"] |
| 557 |
if not self._module.check_mode: |
| 558 |
user.save() |
| 559 |
changed = True |
| 560 |
return changed |
| 561 |
|
| 562 |
""" |
| 563 |
@param user User object |
| 564 |
@param identities List of identities to be added/updated |
| 565 |
""" |
| 566 |
|
| 567 |
def delete_identities(self, user, identities): |
| 568 |
changed = False |
| 569 |
for identity in user.identities: |
| 570 |
if identity not in identities: |
| 571 |
if not self._module.check_mode: |
| 572 |
user.identityproviders.delete(identity["provider"]) |
| 573 |
changed = True |
| 574 |
return changed |
| 575 |
|
| 576 |
""" |
| 577 |
@param username Username of the user |
| 578 |
""" |
| 579 |
|
| 580 |
def find_user(self, username): |
| 581 |
return next( |
| 582 |
(user for user in self._gitlab.users.list(search=username, **list_all_kwargs) if user.username == username), |
| 583 |
None, |
| 584 |
) |
| 585 |
|
| 586 |
""" |
| 587 |
@param username Username of the user |
| 588 |
""" |
| 589 |
|
| 590 |
def exists_user(self, username): |
| 591 |
# When user exists, object will be stored in self.user_object. |
| 592 |
user = self.find_user(username) |
| 593 |
if user: |
| 594 |
self.user_object = user |
| 595 |
return True |
| 596 |
return False |
| 597 |
|
| 598 |
""" |
| 599 |
@param username Username of the user |
| 600 |
""" |
| 601 |
|
| 602 |
def is_active(self, username): |
| 603 |
user = self.find_user(username) |
| 604 |
return user.attributes["state"] == "active" |
| 605 |
|
| 606 |
def delete_user(self): |
| 607 |
if self._module.check_mode: |
| 608 |
return True |
| 609 |
|
| 610 |
user = self.user_object |
| 611 |
|
| 612 |
return user.delete() |
| 613 |
|
| 614 |
def block_user(self): |
| 615 |
if self._module.check_mode: |
| 616 |
return True |
| 617 |
|
| 618 |
user = self.user_object |
| 619 |
|
| 620 |
return user.block() |
| 621 |
|
| 622 |
def unblock_user(self): |
| 623 |
if self._module.check_mode: |
| 624 |
return True |
| 625 |
|
| 626 |
user = self.user_object |
| 627 |
|
| 628 |
return user.unblock() |
| 629 |
|
| 630 |
|
| 631 |
def sanitize_arguments(arguments): |
| 632 |
for key, value in list(arguments.items()): |
| 633 |
if value is None: |
| 634 |
del arguments[key] |
| 635 |
return arguments |
| 636 |
|
| 637 |
|
| 638 |
def main(): |
| 639 |
argument_spec = basic_auth_argument_spec() |
| 640 |
argument_spec.update(auth_argument_spec()) |
| 641 |
argument_spec.update( |
| 642 |
dict( |
| 643 |
name=dict(type="str"), |
| 644 |
state=dict(type="str", default="present", choices=["absent", "present", "blocked", "unblocked"]), |
| 645 |
username=dict(type="str", required=True), |
| 646 |
password=dict(type="str", no_log=True), |
| 647 |
reset_password=dict(type="bool", default=False, no_log=False), |
| 648 |
email=dict(type="str"), |
| 649 |
sshkey_name=dict(type="str"), |
| 650 |
sshkey_file=dict(type="str", no_log=False), |
| 651 |
sshkey_expires_at=dict(type="str", no_log=False), |
| 652 |
sshkey_update_mode=dict(type="str", default="create", choices=["create", "update", "deduplicate"]), |
| 653 |
group=dict(type="str"), |
| 654 |
access_level=dict( |
| 655 |
type="str", default="guest", choices=["developer", "guest", "maintainer", "master", "owner", "reporter"] |
| 656 |
), |
| 657 |
confirm=dict(type="bool", default=True), |
| 658 |
isadmin=dict(type="bool", default=False), |
| 659 |
external=dict(type="bool", default=False), |
| 660 |
identities=dict(type="list", elements="dict"), |
| 661 |
overwrite_identities=dict(type="bool", default=False), |
| 662 |
) |
| 663 |
) |
| 664 |
|
| 665 |
module = AnsibleModule( |
| 666 |
argument_spec=argument_spec, |
| 667 |
mutually_exclusive=[ |
| 668 |
["api_username", "api_token"], |
| 669 |
["api_username", "api_oauth_token"], |
| 670 |
["api_username", "api_job_token"], |
| 671 |
["api_token", "api_oauth_token"], |
| 672 |
["api_token", "api_job_token"], |
| 673 |
], |
| 674 |
required_together=[ |
| 675 |
["api_username", "api_password"], |
| 676 |
], |
| 677 |
required_one_of=[["api_username", "api_token", "api_oauth_token", "api_job_token"]], |
| 678 |
supports_check_mode=True, |
| 679 |
required_if=(("state", "present", ["name", "email"]),), |
| 680 |
) |
| 681 |
|
| 682 |
# check prerequisites and connect to gitlab server |
| 683 |
gitlab_instance = gitlab_authentication(module) |
| 684 |
|
| 685 |
user_name = module.params["name"] |
| 686 |
state = module.params["state"] |
| 687 |
user_username = module.params["username"].lower() |
| 688 |
user_password = module.params["password"] |
| 689 |
user_reset_password = module.params["reset_password"] |
| 690 |
user_email = module.params["email"] |
| 691 |
user_sshkey_name = module.params["sshkey_name"] |
| 692 |
user_sshkey_file = module.params["sshkey_file"] |
| 693 |
user_sshkey_expires_at = module.params["sshkey_expires_at"] |
| 694 |
user_sshkey_update_mode = module.params["sshkey_update_mode"] |
| 695 |
group_path = module.params["group"] |
| 696 |
access_level = module.params["access_level"] |
| 697 |
confirm = module.params["confirm"] |
| 698 |
user_isadmin = module.params["isadmin"] |
| 699 |
user_external = module.params["external"] |
| 700 |
user_identities = module.params["identities"] |
| 701 |
overwrite_identities = module.params["overwrite_identities"] |
| 702 |
|
| 703 |
gitlab_user = GitLabUser(module, gitlab_instance) |
| 704 |
user_exists = gitlab_user.exists_user(user_username) |
| 705 |
if user_exists: |
| 706 |
user_is_active = gitlab_user.is_active(user_username) |
| 707 |
else: |
| 708 |
user_is_active = False |
| 709 |
|
| 710 |
if state == "absent": |
| 711 |
if user_exists: |
| 712 |
gitlab_user.delete_user() |
| 713 |
module.exit_json(changed=True, msg=f"Successfully deleted user {user_username}") |
| 714 |
else: |
| 715 |
module.exit_json(changed=False, msg="User deleted or does not exists") |
| 716 |
|
| 717 |
if state == "blocked": |
| 718 |
if user_exists and user_is_active: |
| 719 |
gitlab_user.block_user() |
| 720 |
module.exit_json(changed=True, msg=f"Successfully blocked user {user_username}") |
| 721 |
else: |
| 722 |
module.exit_json(changed=False, msg="User already blocked or does not exists") |
| 723 |
|
| 724 |
if state == "unblocked": |
| 725 |
if user_exists and not user_is_active: |
| 726 |
gitlab_user.unblock_user() |
| 727 |
module.exit_json(changed=True, msg=f"Successfully unblocked user {user_username}") |
| 728 |
else: |
| 729 |
module.exit_json(changed=False, msg="User is not blocked or does not exists") |
| 730 |
|
| 731 |
if state == "present": |
| 732 |
if gitlab_user.create_or_update_user( |
| 733 |
user_username, |
| 734 |
{ |
| 735 |
"name": user_name, |
| 736 |
"password": user_password, |
| 737 |
"reset_password": user_reset_password, |
| 738 |
"email": user_email, |
| 739 |
"sshkey_name": user_sshkey_name, |
| 740 |
"sshkey_file": user_sshkey_file, |
| 741 |
"sshkey_expires_at": user_sshkey_expires_at, |
| 742 |
"sshkey_update_mode": user_sshkey_update_mode, |
| 743 |
"group_path": group_path, |
| 744 |
"access_level": access_level, |
| 745 |
"confirm": confirm, |
| 746 |
"isadmin": user_isadmin, |
| 747 |
"external": user_external, |
| 748 |
"identities": user_identities, |
| 749 |
"overwrite_identities": overwrite_identities, |
| 750 |
}, |
| 751 |
): |
| 752 |
module.exit_json( |
| 753 |
changed=True, |
| 754 |
msg=f"Successfully created or updated the user {user_username}", |
| 755 |
user=gitlab_user.user_object._attrs, |
| 756 |
) |
| 757 |
else: |
| 758 |
module.exit_json( |
| 759 |
changed=False, msg=f"No need to update the user {user_username}", user=gitlab_user.user_object._attrs |
| 760 |
) |
| 761 |
|
| 762 |
|
| 763 |
if __name__ == "__main__": |
| 764 |
main() |