| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2017 Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: iam_user |
| 10 |
version_added: 5.0.0 |
| 11 |
short_description: Manage AWS IAM users |
| 12 |
description: |
| 13 |
- A module to manage AWS IAM users. |
| 14 |
- The module does not manage groups that users belong to, groups memberships can be managed using M(amazon.aws.iam_group). |
| 15 |
- This module was originally added to C(community.aws) in release 1.0.0. |
| 16 |
author: |
| 17 |
- Josh Souza (@joshsouza) |
| 18 |
options: |
| 19 |
name: |
| 20 |
description: |
| 21 |
- The name of the user. |
| 22 |
- >- |
| 23 |
Note: user names are unique within an account. Paths (O(path)) do B(not) affect |
| 24 |
the uniqueness requirements of O(name). For example it is not permitted to have both |
| 25 |
C(/Path1/MyUser) and C(/Path2/MyUser) in the same account. |
| 26 |
- O(user_name) was added as an alias in release 7.2.0. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
aliases: ['user_name'] |
| 30 |
path: |
| 31 |
description: |
| 32 |
- The path for the user. |
| 33 |
- For more information about IAM paths, see the AWS IAM identifiers documentation |
| 34 |
U(https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_identifiers.html). |
| 35 |
aliases: ['prefix', 'path_prefix'] |
| 36 |
required: false |
| 37 |
type: str |
| 38 |
version_added: 7.2.0 |
| 39 |
boundary: |
| 40 |
description: |
| 41 |
- The ARN of an IAM managed policy to apply as a boundary policy for this user. |
| 42 |
- Boundary policies can be used to restrict the permissions a user can exercise, but does not |
| 43 |
grant any policies in and of itself. |
| 44 |
- For more information on boundaries, see |
| 45 |
U(https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_boundaries.html). |
| 46 |
- Set to the empty string V("") to remove the boundary policy. |
| 47 |
aliases: ["boundary_policy_arn", "permissions_boundary"] |
| 48 |
required: false |
| 49 |
type: str |
| 50 |
version_added: 7.2.0 |
| 51 |
password: |
| 52 |
description: |
| 53 |
- The password to apply to the user. |
| 54 |
required: false |
| 55 |
type: str |
| 56 |
version_added: 2.2.0 |
| 57 |
version_added_collection: community.aws |
| 58 |
password_reset_required: |
| 59 |
description: |
| 60 |
- Defines if the user is required to set a new password when they log in. |
| 61 |
- Ignored unless a new password is set. |
| 62 |
required: false |
| 63 |
type: bool |
| 64 |
default: false |
| 65 |
version_added: 3.1.0 |
| 66 |
version_added_collection: community.aws |
| 67 |
update_password: |
| 68 |
default: always |
| 69 |
choices: ['always', 'on_create'] |
| 70 |
description: |
| 71 |
- When to update user passwords. |
| 72 |
- O(update_password=always) will ensure the password is set to O(password). |
| 73 |
- O(update_password=on_create) will only set the password for newly created users. |
| 74 |
type: str |
| 75 |
version_added: 2.2.0 |
| 76 |
version_added_collection: community.aws |
| 77 |
remove_password: |
| 78 |
description: |
| 79 |
- Option to delete user login passwords. |
| 80 |
- This field is mutually exclusive to O(password). |
| 81 |
type: 'bool' |
| 82 |
version_added: 2.2.0 |
| 83 |
version_added_collection: community.aws |
| 84 |
managed_policies: |
| 85 |
description: |
| 86 |
- A list of managed policy ARNs or friendly names to attach to the user. |
| 87 |
- To embed an inline policy, use M(community.aws.iam_policy). |
| 88 |
required: false |
| 89 |
type: list |
| 90 |
default: [] |
| 91 |
elements: str |
| 92 |
aliases: ['managed_policy'] |
| 93 |
state: |
| 94 |
description: |
| 95 |
- Create or remove the IAM user. |
| 96 |
required: true |
| 97 |
choices: [ 'present', 'absent' ] |
| 98 |
type: str |
| 99 |
purge_policies: |
| 100 |
description: |
| 101 |
- When O(purge_policies=true) any managed policies not listed in O(managed_policies) will be detached. |
| 102 |
required: false |
| 103 |
default: false |
| 104 |
type: bool |
| 105 |
aliases: ['purge_policy', 'purge_managed_policies'] |
| 106 |
wait: |
| 107 |
description: |
| 108 |
- When O(wait=True) the module will wait for up to O(wait_timeout) seconds |
| 109 |
for IAM user creation before returning. |
| 110 |
default: True |
| 111 |
type: bool |
| 112 |
version_added: 2.2.0 |
| 113 |
version_added_collection: community.aws |
| 114 |
wait_timeout: |
| 115 |
description: |
| 116 |
- How long (in seconds) to wait for creation / updates to complete. |
| 117 |
default: 120 |
| 118 |
type: int |
| 119 |
version_added: 2.2.0 |
| 120 |
version_added_collection: community.aws |
| 121 |
notes: |
| 122 |
- Support for O(tags) and O(purge_tags) was added in release 2.1.0. |
| 123 |
extends_documentation_fragment: |
| 124 |
- amazon.aws.common.modules |
| 125 |
- amazon.aws.region.modules |
| 126 |
- amazon.aws.tags |
| 127 |
- amazon.aws.boto3 |
| 128 |
""" |
| 129 |
|
| 130 |
EXAMPLES = r""" |
| 131 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 132 |
# Note: This module does not allow management of groups that users belong to. |
| 133 |
# Groups should manage their membership directly using amazon.aws.iam_group, |
| 134 |
# as users belong to them. |
| 135 |
|
| 136 |
- name: Create a user |
| 137 |
amazon.aws.iam_user: |
| 138 |
name: testuser1 |
| 139 |
state: present |
| 140 |
|
| 141 |
- name: Create a user with a password |
| 142 |
amazon.aws.iam_user: |
| 143 |
name: testuser1 |
| 144 |
password: SomeSecurePassword |
| 145 |
state: present |
| 146 |
|
| 147 |
- name: Create a user and attach a managed policy using its ARN |
| 148 |
amazon.aws.iam_user: |
| 149 |
name: testuser1 |
| 150 |
managed_policies: |
| 151 |
- arn:aws:iam::aws:policy/AmazonSNSFullAccess |
| 152 |
state: present |
| 153 |
|
| 154 |
- name: Remove all managed policies from an existing user with an empty list |
| 155 |
amazon.aws.iam_user: |
| 156 |
name: testuser1 |
| 157 |
state: present |
| 158 |
purge_policies: true |
| 159 |
|
| 160 |
- name: Create user with tags |
| 161 |
amazon.aws.iam_user: |
| 162 |
name: testuser1 |
| 163 |
state: present |
| 164 |
tags: |
| 165 |
Env: Prod |
| 166 |
|
| 167 |
- name: Delete the user |
| 168 |
amazon.aws.iam_user: |
| 169 |
name: testuser1 |
| 170 |
state: absent |
| 171 |
""" |
| 172 |
|
| 173 |
RETURN = r""" |
| 174 |
user: |
| 175 |
description: Dictionary containing all the user information. |
| 176 |
returned: success |
| 177 |
type: complex |
| 178 |
contains: |
| 179 |
arn: |
| 180 |
description: The Amazon Resource Name (ARN) specifying the user. |
| 181 |
type: str |
| 182 |
returned: always |
| 183 |
sample: "arn:aws:iam::123456789012:user/testuser1" |
| 184 |
create_date: |
| 185 |
description: The date and time, in ISO 8601 date-time format, when the user was created. |
| 186 |
type: str |
| 187 |
returned: always |
| 188 |
sample: "2017-02-08T04:36:28+00:00" |
| 189 |
user_id: |
| 190 |
description: The stable and unique string identifying the user. |
| 191 |
type: str |
| 192 |
returned: always |
| 193 |
sample: "AGPA12345EXAMPLE54321" |
| 194 |
user_name: |
| 195 |
description: The friendly name that identifies the user. |
| 196 |
type: str |
| 197 |
returned: always |
| 198 |
sample: "testuser1" |
| 199 |
path: |
| 200 |
description: The path to the user. |
| 201 |
type: str |
| 202 |
returned: always |
| 203 |
sample: "/" |
| 204 |
tags: |
| 205 |
description: User tags. |
| 206 |
type: dict |
| 207 |
returned: always |
| 208 |
sample: {"Env": "Prod"} |
| 209 |
attached_policies: |
| 210 |
version_added: 7.2.0 |
| 211 |
description: |
| 212 |
- List containing basic information about managed policies attached to the group. |
| 213 |
returned: always |
| 214 |
type: list |
| 215 |
elements: dict |
| 216 |
sample: [ |
| 217 |
{ |
| 218 |
"policy_arn": "arn:aws:iam::123456789012:policy/test_policy", |
| 219 |
"policy_name": "test_policy" |
| 220 |
} |
| 221 |
] |
| 222 |
contains: |
| 223 |
policy_arn: |
| 224 |
description: The Amazon Resource Name (ARN) specifying the managed policy. |
| 225 |
type: str |
| 226 |
sample: "arn:aws:iam::123456789012:policy/test_policy" |
| 227 |
policy_name: |
| 228 |
description: The friendly name that identifies the policy. |
| 229 |
type: str |
| 230 |
sample: test_policy |
| 231 |
""" |
| 232 |
|
| 233 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 234 |
|
| 235 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import AnsibleIAMError |
| 236 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import IAMErrorHandler |
| 237 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import convert_managed_policy_names_to_arns |
| 238 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import get_iam_user |
| 239 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import normalize_iam_user |
| 240 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import validate_iam_identifiers |
| 241 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 242 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 243 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list |
| 244 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags |
| 245 |
|
| 246 |
|
| 247 |
@IAMErrorHandler.common_error_handler("wait for IAM user creation") |
| 248 |
def _wait_user_exists(connection, **params): |
| 249 |
waiter = connection.get_waiter("user_exists") |
| 250 |
waiter.wait(**params) |
| 251 |
|
| 252 |
|
| 253 |
def wait_iam_exists(connection, module): |
| 254 |
if not module.params.get("wait"): |
| 255 |
return |
| 256 |
|
| 257 |
user_name = module.params.get("name") |
| 258 |
wait_timeout = module.params.get("wait_timeout") |
| 259 |
|
| 260 |
delay = min(wait_timeout, 5) |
| 261 |
max_attempts = wait_timeout // delay |
| 262 |
waiter_config = {"Delay": delay, "MaxAttempts": max_attempts} |
| 263 |
|
| 264 |
_wait_user_exists(connection, WaiterConfig=waiter_config, UserName=user_name) |
| 265 |
|
| 266 |
|
| 267 |
@IAMErrorHandler.common_error_handler("create user") |
| 268 |
def create_user(connection, module, user_name, path, boundary, tags): |
| 269 |
params = {"UserName": user_name} |
| 270 |
if path: |
| 271 |
params["Path"] = path |
| 272 |
if boundary: |
| 273 |
params["PermissionsBoundary"] = boundary |
| 274 |
if tags: |
| 275 |
params["Tags"] = ansible_dict_to_boto3_tag_list(tags) |
| 276 |
|
| 277 |
if module.check_mode: |
| 278 |
module.exit_json(changed=True, create_params=params) |
| 279 |
|
| 280 |
user = connection.create_user(aws_retry=True, **params)["User"] |
| 281 |
|
| 282 |
return normalize_iam_user(user) |
| 283 |
|
| 284 |
|
| 285 |
@IAMErrorHandler.common_error_handler("create user login profile") |
| 286 |
def _create_login_profile(connection, **params): |
| 287 |
return connection.create_login_profile(aws_retry=True, **params) |
| 288 |
|
| 289 |
|
| 290 |
# Uses the list error handler because we "update" as a quick test for existence |
| 291 |
# when our next step would be update or create. |
| 292 |
@IAMErrorHandler.list_error_handler("update user login profile") |
| 293 |
def _update_login_profile(connection, **params): |
| 294 |
return connection.update_login_profile(aws_retry=True, **params) |
| 295 |
|
| 296 |
|
| 297 |
def _create_or_update_login_profile(connection, name, password, reset): |
| 298 |
# Apply new password / update password for the user |
| 299 |
user_params = { |
| 300 |
"UserName": name, |
| 301 |
"Password": password, |
| 302 |
"PasswordResetRequired": reset, |
| 303 |
} |
| 304 |
|
| 305 |
retval = _update_login_profile(connection, **user_params) |
| 306 |
if retval: |
| 307 |
return retval |
| 308 |
return _create_login_profile(connection, **user_params) |
| 309 |
|
| 310 |
|
| 311 |
def ensure_login_profile(connection, check_mode, user_name, password, update, reset, new_user): |
| 312 |
if password is None: |
| 313 |
return False, None |
| 314 |
if update == "on_create" and not new_user: |
| 315 |
return False, None |
| 316 |
|
| 317 |
if check_mode: |
| 318 |
return True, None |
| 319 |
|
| 320 |
return True, _create_or_update_login_profile(connection, user_name, password, reset) |
| 321 |
|
| 322 |
|
| 323 |
@IAMErrorHandler.list_error_handler("get login profile") |
| 324 |
def _get_login_profile(connection, name): |
| 325 |
return connection.get_login_profile(aws_retry=True, UserName=name).get("LoginProfile") |
| 326 |
|
| 327 |
|
| 328 |
@IAMErrorHandler.deletion_error_handler("delete login profile") |
| 329 |
def _delete_login_profile(connection, name): |
| 330 |
connection.delete_login_profile(aws_retry=True, UserName=name) |
| 331 |
|
| 332 |
|
| 333 |
def remove_login_profile(connection, check_mode, user_name, remove_password, new_user): |
| 334 |
if new_user: |
| 335 |
return False |
| 336 |
if not remove_password: |
| 337 |
return False |
| 338 |
|
| 339 |
# In theory we could skip this check outside check_mode |
| 340 |
login_profile = _get_login_profile(connection, user_name) |
| 341 |
if not login_profile: |
| 342 |
return False |
| 343 |
|
| 344 |
if check_mode: |
| 345 |
return True |
| 346 |
|
| 347 |
_delete_login_profile(connection, user_name) |
| 348 |
return True |
| 349 |
|
| 350 |
|
| 351 |
@IAMErrorHandler.list_error_handler("get policies for user") |
| 352 |
def _list_attached_policies(connection, user_name): |
| 353 |
return connection.list_attached_user_policies(aws_retry=True, UserName=user_name)["AttachedPolicies"] |
| 354 |
|
| 355 |
|
| 356 |
@IAMErrorHandler.common_error_handler("attach policy to user") |
| 357 |
def attach_policies(connection, check_mode, user_name, policies): |
| 358 |
if not policies: |
| 359 |
return False |
| 360 |
if check_mode: |
| 361 |
return True |
| 362 |
for policy_arn in policies: |
| 363 |
connection.attach_user_policy(UserName=user_name, PolicyArn=policy_arn) |
| 364 |
|
| 365 |
|
| 366 |
@IAMErrorHandler.common_error_handler("detach policy from user") |
| 367 |
def detach_policies(connection, check_mode, user_name, policies): |
| 368 |
if not policies: |
| 369 |
return False |
| 370 |
if check_mode: |
| 371 |
return True |
| 372 |
for policy_arn in policies: |
| 373 |
connection.detach_user_policy(UserName=user_name, PolicyArn=policy_arn) |
| 374 |
|
| 375 |
|
| 376 |
def ensure_managed_policies(connection, check_mode, user_name, managed_policies, purge_policies): |
| 377 |
if managed_policies is None: |
| 378 |
return False |
| 379 |
|
| 380 |
managed_policies = convert_managed_policy_names_to_arns(connection, managed_policies) |
| 381 |
|
| 382 |
# Manage managed policies |
| 383 |
attached_policies_desc = _list_attached_policies(connection, user_name) |
| 384 |
current_attached_policies = [policy["PolicyArn"] for policy in attached_policies_desc] |
| 385 |
|
| 386 |
policies_to_add = list(set(managed_policies) - set(current_attached_policies)) |
| 387 |
policies_to_remove = [] |
| 388 |
if purge_policies: |
| 389 |
policies_to_remove = list(set(current_attached_policies) - set(managed_policies)) |
| 390 |
|
| 391 |
if not policies_to_add and not policies_to_remove: |
| 392 |
return False |
| 393 |
|
| 394 |
if check_mode: |
| 395 |
return True |
| 396 |
|
| 397 |
detach_policies(connection, check_mode, user_name, policies_to_remove) |
| 398 |
attach_policies(connection, check_mode, user_name, policies_to_add) |
| 399 |
|
| 400 |
return True |
| 401 |
|
| 402 |
|
| 403 |
@IAMErrorHandler.common_error_handler("set tags for user") |
| 404 |
def ensure_user_tags(connection, check_mode, user, user_name, new_tags, purge_tags): |
| 405 |
if new_tags is None: |
| 406 |
return False |
| 407 |
|
| 408 |
existing_tags = user["tags"] |
| 409 |
|
| 410 |
tags_to_add, tags_to_remove = compare_aws_tags(existing_tags, new_tags, purge_tags=purge_tags) |
| 411 |
|
| 412 |
if not tags_to_remove and not tags_to_add: |
| 413 |
return False |
| 414 |
|
| 415 |
if check_mode: |
| 416 |
return True |
| 417 |
|
| 418 |
if tags_to_remove: |
| 419 |
connection.untag_user(UserName=user_name, TagKeys=tags_to_remove) |
| 420 |
if tags_to_add: |
| 421 |
connection.tag_user(UserName=user_name, Tags=ansible_dict_to_boto3_tag_list(tags_to_add)) |
| 422 |
|
| 423 |
return True |
| 424 |
|
| 425 |
|
| 426 |
@IAMErrorHandler.deletion_error_handler("remove permissions boundary for user") |
| 427 |
def _delete_user_permissions_boundary(connection, check_mode, user_name): |
| 428 |
if check_mode: |
| 429 |
return True |
| 430 |
connection.delete_user_permissions_boundary(aws_retry=True, UserName=user_name) |
| 431 |
|
| 432 |
|
| 433 |
@IAMErrorHandler.common_error_handler("set permissions boundary for user") |
| 434 |
def _put_user_permissions_boundary(connection, check_mode, user_name, boundary): |
| 435 |
if check_mode: |
| 436 |
return True |
| 437 |
connection.put_user_permissions_boundary(aws_retry=True, UserName=user_name, PermissionsBoundary=boundary) |
| 438 |
|
| 439 |
|
| 440 |
def ensure_permissions_boundary(connection, check_mode, user, user_name, boundary): |
| 441 |
if boundary is None: |
| 442 |
return False |
| 443 |
|
| 444 |
current_boundary = user.get("permissions_boundary", "") if user else None |
| 445 |
|
| 446 |
if current_boundary: |
| 447 |
current_boundary = current_boundary.get("permissions_boundary_arn") |
| 448 |
|
| 449 |
if boundary == current_boundary: |
| 450 |
return False |
| 451 |
|
| 452 |
if check_mode: |
| 453 |
return True |
| 454 |
|
| 455 |
if boundary == "": |
| 456 |
_delete_user_permissions_boundary(connection, check_mode, user_name) |
| 457 |
else: |
| 458 |
_put_user_permissions_boundary(connection, check_mode, user_name, boundary) |
| 459 |
|
| 460 |
return True |
| 461 |
|
| 462 |
|
| 463 |
@IAMErrorHandler.common_error_handler("set path for user") |
| 464 |
def ensure_path(connection, check_mode, user, user_name, path): |
| 465 |
if path is None: |
| 466 |
return False |
| 467 |
|
| 468 |
current_path = user.get("path", "") if user else None |
| 469 |
|
| 470 |
if path == current_path: |
| 471 |
return False |
| 472 |
|
| 473 |
if check_mode: |
| 474 |
return True |
| 475 |
|
| 476 |
connection.update_user(aws_retry=True, UserName=user_name, NewPath=path) |
| 477 |
|
| 478 |
return True |
| 479 |
|
| 480 |
|
| 481 |
def create_or_update_user(connection, module): |
| 482 |
user_name = module.params.get("name") |
| 483 |
|
| 484 |
changed = False |
| 485 |
new_user = False |
| 486 |
user = get_iam_user(connection, user_name) |
| 487 |
|
| 488 |
boundary = module.params.get("boundary") |
| 489 |
if boundary: |
| 490 |
boundary = convert_managed_policy_names_to_arns(connection, [module.params.get("boundary")])[0] |
| 491 |
|
| 492 |
if user is None: |
| 493 |
user = create_user( |
| 494 |
connection, |
| 495 |
module, |
| 496 |
user_name, |
| 497 |
module.params.get("path"), |
| 498 |
boundary, |
| 499 |
module.params.get("tags"), |
| 500 |
) |
| 501 |
changed = True |
| 502 |
# Wait for user to be fully available before continuing |
| 503 |
wait_iam_exists(connection, module) |
| 504 |
new_user = True |
| 505 |
|
| 506 |
profile_changed, login_profile = ensure_login_profile( |
| 507 |
connection, |
| 508 |
module.check_mode, |
| 509 |
user_name, |
| 510 |
module.params.get("password"), |
| 511 |
module.params.get("update_password"), |
| 512 |
module.params.get("password_reset_required"), |
| 513 |
new_user, |
| 514 |
) |
| 515 |
changed |= profile_changed |
| 516 |
|
| 517 |
changed |= remove_login_profile( |
| 518 |
connection, |
| 519 |
module.check_mode, |
| 520 |
user_name, |
| 521 |
module.params.get("remove_password"), |
| 522 |
new_user, |
| 523 |
) |
| 524 |
|
| 525 |
changed |= ensure_permissions_boundary( |
| 526 |
connection, |
| 527 |
module.check_mode, |
| 528 |
user, |
| 529 |
user_name, |
| 530 |
boundary, |
| 531 |
) |
| 532 |
|
| 533 |
changed |= ensure_path( |
| 534 |
connection, |
| 535 |
module.check_mode, |
| 536 |
user, |
| 537 |
user_name, |
| 538 |
module.params.get("path"), |
| 539 |
) |
| 540 |
|
| 541 |
changed |= ensure_managed_policies( |
| 542 |
connection, |
| 543 |
module.check_mode, |
| 544 |
user_name, |
| 545 |
module.params.get("managed_policies"), |
| 546 |
module.params.get("purge_policies"), |
| 547 |
) |
| 548 |
|
| 549 |
changed |= ensure_user_tags( |
| 550 |
connection, |
| 551 |
module.check_mode, |
| 552 |
user, |
| 553 |
user_name, |
| 554 |
module.params.get("tags"), |
| 555 |
module.params.get("purge_tags"), |
| 556 |
) |
| 557 |
|
| 558 |
if module.check_mode: |
| 559 |
module.exit_json(changed=changed) |
| 560 |
|
| 561 |
# Get the user again |
| 562 |
user = get_iam_user(connection, user_name) |
| 563 |
|
| 564 |
if changed and login_profile: |
| 565 |
# `LoginProfile` is only returned on `create_login_profile` method |
| 566 |
user["password_reset_required"] = login_profile.get("LoginProfile", {}).get("PasswordResetRequired", False) |
| 567 |
|
| 568 |
try: |
| 569 |
# (camel_dict_to_snake_dict doesn't handle lists, so do this as a merge of two dictionaries) |
| 570 |
policies = {"attached_policies": _list_attached_policies(connection, user_name)} |
| 571 |
user.update(camel_dict_to_snake_dict(policies)) |
| 572 |
except AnsibleIAMError as e: |
| 573 |
module.warn( |
| 574 |
f"Failed to list attached policies - {str(e.exception)}", |
| 575 |
) |
| 576 |
pass |
| 577 |
|
| 578 |
module.exit_json(changed=changed, user=user) |
| 579 |
|
| 580 |
|
| 581 |
@IAMErrorHandler.deletion_error_handler("delete access key") |
| 582 |
def delete_access_key(connection, check_mode, user_name, key_id): |
| 583 |
if check_mode: |
| 584 |
return True |
| 585 |
connection.delete_access_key(aws_retry=True, UserName=user_name, AccessKeyId=key_id) |
| 586 |
return True |
| 587 |
|
| 588 |
|
| 589 |
@IAMErrorHandler.list_error_handler("list access keys") |
| 590 |
def delete_access_keys(connection, check_mode, user_name): |
| 591 |
access_keys = connection.list_access_keys(aws_retry=True, UserName=user_name)["AccessKeyMetadata"] |
| 592 |
if not access_keys: |
| 593 |
return False |
| 594 |
for access_key in access_keys: |
| 595 |
delete_access_key(connection, check_mode, user_name, access_key["AccessKeyId"]) |
| 596 |
return True |
| 597 |
|
| 598 |
|
| 599 |
@IAMErrorHandler.deletion_error_handler("delete SSH key") |
| 600 |
def delete_ssh_key(connection, check_mode, user_name, key_id): |
| 601 |
if check_mode: |
| 602 |
return True |
| 603 |
connection.delete_ssh_public_key(aws_retry=True, UserName=user_name, SSHPublicKeyId=key_id) |
| 604 |
return True |
| 605 |
|
| 606 |
|
| 607 |
@IAMErrorHandler.list_error_handler("list SSH keys") |
| 608 |
def delete_ssh_public_keys(connection, check_mode, user_name): |
| 609 |
public_keys = connection.list_ssh_public_keys(aws_retry=True, UserName=user_name)["SSHPublicKeys"] |
| 610 |
if not public_keys: |
| 611 |
return False |
| 612 |
for public_key in public_keys: |
| 613 |
delete_ssh_key(connection, check_mode, user_name, public_key["SSHPublicKeyId"]) |
| 614 |
return True |
| 615 |
|
| 616 |
|
| 617 |
@IAMErrorHandler.deletion_error_handler("delete service credential") |
| 618 |
def delete_service_credential(connection, check_mode, user_name, cred_id): |
| 619 |
if check_mode: |
| 620 |
return True |
| 621 |
connection.delete_ssh_public_key(aws_retry=True, UserName=user_name, SSHPublicKeyId=cred_id) |
| 622 |
return True |
| 623 |
|
| 624 |
|
| 625 |
@IAMErrorHandler.list_error_handler("list service credentials") |
| 626 |
def delete_service_credentials(connection, check_mode, user_name): |
| 627 |
credentials = connection.list_service_specific_credentials(aws_retry=True, UserName=user_name)[ |
| 628 |
"ServiceSpecificCredentials" |
| 629 |
] |
| 630 |
if not credentials: |
| 631 |
return False |
| 632 |
for credential in credentials: |
| 633 |
delete_service_credential(connection, check_mode, user_name, credential["ServiceSpecificCredentialId"]) |
| 634 |
return True |
| 635 |
|
| 636 |
|
| 637 |
@IAMErrorHandler.deletion_error_handler("delete signing certificate") |
| 638 |
def delete_signing_certificate(connection, check_mode, user_name, cert_id): |
| 639 |
if check_mode: |
| 640 |
return True |
| 641 |
connection.delete_signing_certificate(aws_retry=True, UserName=user_name, CertificateId=cert_id) |
| 642 |
return True |
| 643 |
|
| 644 |
|
| 645 |
@IAMErrorHandler.list_error_handler("list signing certificates") |
| 646 |
def delete_signing_certificates(connection, check_mode, user_name): |
| 647 |
certificates = connection.list_signing_certificates(aws_retry=True, UserName=user_name)["Certificates"] |
| 648 |
if not certificates: |
| 649 |
return False |
| 650 |
for certificate in certificates: |
| 651 |
delete_signing_certificate(connection, check_mode, user_name, certificate["CertificateId"]) |
| 652 |
return True |
| 653 |
|
| 654 |
|
| 655 |
@IAMErrorHandler.deletion_error_handler("delete MFA device") |
| 656 |
def delete_mfa_device(connection, check_mode, user_name, device_id): |
| 657 |
if check_mode: |
| 658 |
return True |
| 659 |
connection.deactivate_mfa_device(aws_retry=True, UserName=user_name, SerialNumber=device_id) |
| 660 |
return True |
| 661 |
|
| 662 |
|
| 663 |
@IAMErrorHandler.list_error_handler("list MFA devices") |
| 664 |
def delete_mfa_devices(connection, check_mode, user_name): |
| 665 |
devices = connection.list_mfa_devices(aws_retry=True, UserName=user_name)["MFADevices"] |
| 666 |
if not devices: |
| 667 |
return False |
| 668 |
for device in devices: |
| 669 |
delete_mfa_device(connection, check_mode, user_name, device["SerialNumber"]) |
| 670 |
return True |
| 671 |
|
| 672 |
|
| 673 |
def detach_all_policies(connection, check_mode, user_name): |
| 674 |
# Remove any attached policies |
| 675 |
attached_policies_desc = _list_attached_policies(connection, user_name) |
| 676 |
current_attached_policies = [policy["PolicyArn"] for policy in attached_policies_desc] |
| 677 |
detach_policies(connection, check_mode, user_name, current_attached_policies) |
| 678 |
|
| 679 |
|
| 680 |
@IAMErrorHandler.deletion_error_handler("delete inline policy") |
| 681 |
def delete_inline_policy(connection, check_mode, user_name, policy): |
| 682 |
if check_mode: |
| 683 |
return True |
| 684 |
connection.delete_user_policy(aws_retry=True, UserName=user_name, PolicyName=policy) |
| 685 |
return True |
| 686 |
|
| 687 |
|
| 688 |
@IAMErrorHandler.list_error_handler("list inline policies") |
| 689 |
def delete_inline_policies(connection, check_mode, user_name): |
| 690 |
inline_policies = connection.list_user_policies(aws_retry=True, UserName=user_name)["PolicyNames"] |
| 691 |
if not inline_policies: |
| 692 |
return False |
| 693 |
for policy_name in inline_policies: |
| 694 |
delete_inline_policy(connection, check_mode, user_name, policy_name) |
| 695 |
return True |
| 696 |
|
| 697 |
|
| 698 |
@IAMErrorHandler.deletion_error_handler("remove user from group") |
| 699 |
def remove_from_group(connection, check_mode, user_name, group_name): |
| 700 |
if check_mode: |
| 701 |
return True |
| 702 |
connection.remove_user_from_group(aws_retry=True, UserName=user_name, GroupName=group_name) |
| 703 |
return True |
| 704 |
|
| 705 |
|
| 706 |
@IAMErrorHandler.list_error_handler("list groups containing user") |
| 707 |
def remove_from_all_groups(connection, check_mode, user_name): |
| 708 |
user_groups = connection.list_groups_for_user(aws_retry=True, UserName=user_name)["Groups"] |
| 709 |
if not user_groups: |
| 710 |
return False |
| 711 |
for group in user_groups: |
| 712 |
remove_from_group(connection, check_mode, user_name, group["GroupName"]) |
| 713 |
return True |
| 714 |
|
| 715 |
|
| 716 |
@IAMErrorHandler.deletion_error_handler("delete user") |
| 717 |
def delete_user(connection, check_mode, user_name): |
| 718 |
if check_mode: |
| 719 |
return True |
| 720 |
connection.delete_user(aws_retry=True, UserName=user_name) |
| 721 |
return True |
| 722 |
|
| 723 |
|
| 724 |
def destroy_user(connection, module): |
| 725 |
user_name = module.params.get("name") |
| 726 |
|
| 727 |
user = get_iam_user(connection, user_name) |
| 728 |
# User is not present |
| 729 |
if not user: |
| 730 |
module.exit_json(changed=False) |
| 731 |
|
| 732 |
# Check mode means we would remove this user |
| 733 |
if module.check_mode: |
| 734 |
module.exit_json(changed=True) |
| 735 |
|
| 736 |
# Prior to removing the user we need to remove all of the related resources, or deletion will |
| 737 |
# fail. |
| 738 |
# Because policies (direct and indrect) can contain Deny rules, order is important here in case |
| 739 |
# we fail during deletion: lock out the user first *then* start removing policies... |
| 740 |
# - Prevent the user from creating new sessions |
| 741 |
# - Login profile |
| 742 |
# - Access keys |
| 743 |
# - SSH keys |
| 744 |
# - Service Credentials |
| 745 |
# - Certificates |
| 746 |
# - MFA Token (last so we don't end up in a state where it's possible still use password/keys) |
| 747 |
# - Remove policies and group membership |
| 748 |
# - Managed policies |
| 749 |
# - Inline policies |
| 750 |
# - Group membership |
| 751 |
|
| 752 |
remove_login_profile(connection, module.check_mode, user_name, True, False) |
| 753 |
delete_access_keys(connection, module.check_mode, user_name) |
| 754 |
delete_ssh_public_keys(connection, module.check_mode, user_name) |
| 755 |
delete_service_credentials(connection, module.check_mode, user_name) |
| 756 |
delete_signing_certificates(connection, module.check_mode, user_name) |
| 757 |
delete_mfa_devices(connection, module.check_mode, user_name) |
| 758 |
detach_all_policies(connection, module.check_mode, user_name) |
| 759 |
delete_inline_policies(connection, module.check_mode, user_name) |
| 760 |
remove_from_all_groups(connection, module.check_mode, user_name) |
| 761 |
changed = delete_user(connection, module.check_mode, user_name) |
| 762 |
module.exit_json(changed=changed) |
| 763 |
|
| 764 |
|
| 765 |
def main(): |
| 766 |
argument_spec = dict( |
| 767 |
name=dict(required=True, type="str", aliases=["user_name"]), |
| 768 |
path=dict(type="str", aliases=["prefix", "path_prefix"]), |
| 769 |
boundary=dict(type="str", aliases=["boundary_policy_arn", "permissions_boundary"]), |
| 770 |
password=dict(type="str", no_log=True), |
| 771 |
password_reset_required=dict(type="bool", default=False, no_log=False), |
| 772 |
update_password=dict(default="always", choices=["always", "on_create"], no_log=False), |
| 773 |
remove_password=dict(type="bool", no_log=False), |
| 774 |
managed_policies=dict(default=[], type="list", aliases=["managed_policy"], elements="str"), |
| 775 |
state=dict(choices=["present", "absent"], required=True), |
| 776 |
purge_policies=dict(default=False, type="bool", aliases=["purge_policy", "purge_managed_policies"]), |
| 777 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 778 |
purge_tags=dict(type="bool", default=True), |
| 779 |
wait=dict(type="bool", default=True), |
| 780 |
wait_timeout=dict(default=120, type="int"), |
| 781 |
) |
| 782 |
|
| 783 |
module = AnsibleAWSModule( |
| 784 |
argument_spec=argument_spec, |
| 785 |
supports_check_mode=True, |
| 786 |
mutually_exclusive=[["password", "remove_password"]], |
| 787 |
) |
| 788 |
|
| 789 |
identifier_problem = validate_iam_identifiers( |
| 790 |
"user", name=module.params.get("name"), path=module.params.get("path") |
| 791 |
) |
| 792 |
if identifier_problem: |
| 793 |
module.fail_json(msg=identifier_problem) |
| 794 |
|
| 795 |
retry_decorator = AWSRetry.jittered_backoff(catch_extra_error_codes=["EntityTemporarilyUnmodifiable"]) |
| 796 |
connection = module.client("iam", retry_decorator=retry_decorator) |
| 797 |
|
| 798 |
state = module.params.get("state") |
| 799 |
|
| 800 |
try: |
| 801 |
if state == "present": |
| 802 |
create_or_update_user(connection, module) |
| 803 |
else: |
| 804 |
destroy_user(connection, module) |
| 805 |
except AnsibleIAMError as e: |
| 806 |
module.fail_json_aws_error(e) |
| 807 |
|
| 808 |
|
| 809 |
if __name__ == "__main__": |
| 810 |
main() |