| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2021 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_access_key |
| 10 |
version_added: 2.1.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
short_description: Manage AWS IAM User access keys |
| 13 |
description: |
| 14 |
- Manage AWS IAM user access keys. |
| 15 |
author: |
| 16 |
- Mark Chappell (@tremble) |
| 17 |
options: |
| 18 |
user_name: |
| 19 |
description: |
| 20 |
- The name of the IAM User to which the key belongs. |
| 21 |
required: true |
| 22 |
type: str |
| 23 |
aliases: ['username'] |
| 24 |
id: |
| 25 |
description: |
| 26 |
- The ID of the access key. |
| 27 |
- Required when O(state=absent). |
| 28 |
- Mutually exclusive with O(rotate_keys). |
| 29 |
required: false |
| 30 |
type: str |
| 31 |
state: |
| 32 |
description: |
| 33 |
- Create or remove the access key. |
| 34 |
- When O(state=present) and O(id) is not defined a new key will be created. |
| 35 |
required: false |
| 36 |
type: str |
| 37 |
default: 'present' |
| 38 |
choices: [ 'present', 'absent' ] |
| 39 |
active: |
| 40 |
description: |
| 41 |
- Whether the key should be enabled or disabled. |
| 42 |
- Defaults to V(true) when creating a new key. |
| 43 |
required: false |
| 44 |
type: bool |
| 45 |
aliases: ['enabled'] |
| 46 |
rotate_keys: |
| 47 |
description: |
| 48 |
- When there are already 2 access keys attached to the IAM user the oldest |
| 49 |
key will be removed and a new key created. |
| 50 |
- Ignored if O(state=absent) |
| 51 |
- Mutually exclusive with O(id). |
| 52 |
required: false |
| 53 |
type: bool |
| 54 |
default: false |
| 55 |
notes: |
| 56 |
- For security reasons, this module should be used with B(no_log=true) and (register) functionalities |
| 57 |
when creating new access key. |
| 58 |
extends_documentation_fragment: |
| 59 |
- amazon.aws.common.modules |
| 60 |
- amazon.aws.region.modules |
| 61 |
- amazon.aws.boto3 |
| 62 |
""" |
| 63 |
|
| 64 |
EXAMPLES = r""" |
| 65 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 66 |
|
| 67 |
- name: Create a new access key |
| 68 |
amazon.aws.iam_access_key: |
| 69 |
user_name: example_user |
| 70 |
state: present |
| 71 |
no_log: true |
| 72 |
|
| 73 |
- name: Delete the access_key |
| 74 |
amazon.aws.iam_access_key: |
| 75 |
user_name: example_user |
| 76 |
id: AKIA1EXAMPLE1EXAMPLE |
| 77 |
state: absent |
| 78 |
""" |
| 79 |
|
| 80 |
RETURN = r""" |
| 81 |
access_key: |
| 82 |
description: A dictionary containing all the access key information. |
| 83 |
returned: When the key exists. |
| 84 |
type: complex |
| 85 |
contains: |
| 86 |
access_key_id: |
| 87 |
description: The ID for the access key. |
| 88 |
returned: success |
| 89 |
type: str |
| 90 |
sample: AKIA1EXAMPLE1EXAMPLE |
| 91 |
create_date: |
| 92 |
description: The date and time, in ISO 8601 date-time format, when the access key was created. |
| 93 |
returned: success |
| 94 |
type: str |
| 95 |
sample: "2021-10-09T13:25:42+00:00" |
| 96 |
user_name: |
| 97 |
description: The name of the IAM user to which the key is attached. |
| 98 |
returned: success |
| 99 |
type: str |
| 100 |
sample: example_user |
| 101 |
status: |
| 102 |
description: |
| 103 |
- The status of the key. |
| 104 |
- C(Active) means it can be used. |
| 105 |
- C(Inactive) means it can not be used. |
| 106 |
returned: success |
| 107 |
type: str |
| 108 |
sample: Inactive |
| 109 |
secret_access_key: |
| 110 |
description: |
| 111 |
- The secret access key. |
| 112 |
- A secret access key is the equivalent of a password which can not be changed and as such should be considered sensitive data. |
| 113 |
- Secret access keys can only be accessed at creation time. |
| 114 |
returned: When a new key is created. |
| 115 |
type: str |
| 116 |
sample: example/Example+EXAMPLE+example/Example |
| 117 |
deleted_access_key_id: |
| 118 |
description: |
| 119 |
- The access key deleted during rotation. |
| 120 |
returned: When a key was deleted during the rotation of access keys |
| 121 |
type: str |
| 122 |
sample: AKIA1EXAMPLE1EXAMPLE |
| 123 |
""" |
| 124 |
|
| 125 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import AnsibleIAMError |
| 126 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import IAMErrorHandler |
| 127 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import get_iam_access_keys |
| 128 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import normalize_iam_access_key |
| 129 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 130 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 131 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import scrub_none_parameters |
| 132 |
|
| 133 |
|
| 134 |
@IAMErrorHandler.deletion_error_handler("Failed to delete access key for user") |
| 135 |
def delete_access_key(access_keys, user, access_key_id): |
| 136 |
if not access_key_id: |
| 137 |
return False |
| 138 |
if access_key_id not in [k["access_key_id"] for k in access_keys]: |
| 139 |
return False |
| 140 |
|
| 141 |
if module.check_mode: |
| 142 |
return True |
| 143 |
|
| 144 |
client.delete_access_key(aws_retry=True, UserName=user, AccessKeyId=access_key_id) |
| 145 |
return True |
| 146 |
|
| 147 |
|
| 148 |
@IAMErrorHandler.common_error_handler("Failed to update access key for user") |
| 149 |
def update_access_key_state(access_keys, user, access_key_id, enabled): |
| 150 |
keys = {k["access_key_id"]: k for k in access_keys} |
| 151 |
|
| 152 |
if access_key_id not in keys: |
| 153 |
raise AnsibleIAMError(message=f'Access key "{access_key_id}" not found attached to User "{user}"') |
| 154 |
|
| 155 |
if enabled is None: |
| 156 |
return False |
| 157 |
|
| 158 |
access_key = keys.get(access_key_id) |
| 159 |
|
| 160 |
desired_status = "Active" if enabled else "Inactive" |
| 161 |
if access_key.get("status") == desired_status: |
| 162 |
return False |
| 163 |
|
| 164 |
if module.check_mode: |
| 165 |
return True |
| 166 |
|
| 167 |
client.update_access_key(aws_retry=True, UserName=user, AccessKeyId=access_key_id, Status=desired_status) |
| 168 |
return True |
| 169 |
|
| 170 |
|
| 171 |
@IAMErrorHandler.common_error_handler("Failed to create access key for user") |
| 172 |
def create_access_key(access_keys, user, rotate_keys, enabled): |
| 173 |
changed = False |
| 174 |
oldest_key = False |
| 175 |
|
| 176 |
if len(access_keys) > 1 and rotate_keys: |
| 177 |
oldest_key = access_keys[0].get("access_key_id") |
| 178 |
changed |= delete_access_key(access_keys, user, oldest_key) |
| 179 |
|
| 180 |
if module.check_mode: |
| 181 |
if oldest_key: |
| 182 |
return dict(deleted_access_key=oldest_key) |
| 183 |
return dict() |
| 184 |
|
| 185 |
results = client.create_access_key(aws_retry=True, UserName=user) |
| 186 |
access_key = normalize_iam_access_key(results.get("AccessKey")) |
| 187 |
|
| 188 |
# Update settings which can't be managed on creation |
| 189 |
if enabled is False: |
| 190 |
access_key_id = access_key["access_key_id"] |
| 191 |
update_access_key_state([access_key], user, access_key_id, enabled) |
| 192 |
access_key["status"] = "Inactive" |
| 193 |
|
| 194 |
if oldest_key: |
| 195 |
access_key["deleted_access_key"] = oldest_key |
| 196 |
|
| 197 |
return access_key |
| 198 |
|
| 199 |
|
| 200 |
def update_access_key(access_keys, user, access_key_id, enabled): |
| 201 |
changed = update_access_key_state(access_keys, user, access_key_id, enabled) |
| 202 |
access_keys = get_iam_access_keys(client, user) |
| 203 |
keys = {k["access_key_id"]: k for k in access_keys} |
| 204 |
return changed, {"access_key": keys.get(access_key_id, None)} |
| 205 |
|
| 206 |
|
| 207 |
def main(): |
| 208 |
global module |
| 209 |
global client |
| 210 |
|
| 211 |
argument_spec = dict( |
| 212 |
user_name=dict(required=True, type="str", aliases=["username"]), |
| 213 |
id=dict(required=False, type="str"), |
| 214 |
state=dict(required=False, choices=["present", "absent"], default="present"), |
| 215 |
active=dict(required=False, type="bool", aliases=["enabled"]), |
| 216 |
rotate_keys=dict(required=False, type="bool", default=False), |
| 217 |
) |
| 218 |
|
| 219 |
required_if = [ |
| 220 |
["state", "absent", ("id",)], |
| 221 |
] |
| 222 |
mutually_exclusive = [ |
| 223 |
["rotate_keys", "id"], |
| 224 |
] |
| 225 |
|
| 226 |
module = AnsibleAWSModule( |
| 227 |
argument_spec=argument_spec, |
| 228 |
supports_check_mode=True, |
| 229 |
required_if=required_if, |
| 230 |
mutually_exclusive=mutually_exclusive, |
| 231 |
) |
| 232 |
|
| 233 |
client = module.client("iam", retry_decorator=AWSRetry.jittered_backoff()) |
| 234 |
|
| 235 |
state = module.params.get("state") |
| 236 |
user = module.params.get("user_name") |
| 237 |
access_key_id = module.params.get("id") |
| 238 |
rotate_keys = module.params.get("rotate_keys") |
| 239 |
enabled = module.params.get("active") |
| 240 |
|
| 241 |
access_keys = get_iam_access_keys(client, user) |
| 242 |
results = dict() |
| 243 |
|
| 244 |
try: |
| 245 |
if state == "absent": |
| 246 |
changed = delete_access_key(access_keys, user, access_key_id) |
| 247 |
module.exit_json(changed=changed) |
| 248 |
|
| 249 |
if access_key_id: |
| 250 |
changed, results = update_access_key(access_keys, user, access_key_id, enabled) |
| 251 |
else: |
| 252 |
secret_key = create_access_key(access_keys, user, rotate_keys, enabled) |
| 253 |
changed = True |
| 254 |
results = { |
| 255 |
"access_key_id": secret_key.get("access_key_id", None), |
| 256 |
"secret_access_key": secret_key.pop("secret_access_key", None), |
| 257 |
"deleted_access_key_id": secret_key.pop("deleted_access_key", None), |
| 258 |
"access_key": secret_key or None, |
| 259 |
} |
| 260 |
results = scrub_none_parameters(results) |
| 261 |
|
| 262 |
module.exit_json(changed=changed, **results) |
| 263 |
except AnsibleIAMError as e: |
| 264 |
module.fail_json_aws_error(e) |
| 265 |
|
| 266 |
|
| 267 |
if __name__ == "__main__": |
| 268 |
main() |