| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: 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: mq_user |
| 10 |
version_added: 6.0.0 |
| 11 |
short_description: Manage users in existing Amazon MQ broker |
| 12 |
description: |
| 13 |
- Manage Amazon MQ users. |
| 14 |
- Pending changes are taking into account for idempotency. |
| 15 |
author: |
| 16 |
- FCO (@fotto) |
| 17 |
options: |
| 18 |
broker_id: |
| 19 |
description: |
| 20 |
- The ID of the MQ broker to work on. |
| 21 |
type: str |
| 22 |
required: true |
| 23 |
username: |
| 24 |
description: |
| 25 |
- The name of the user to create/update/delete. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
state: |
| 29 |
description: |
| 30 |
- Create/Update vs Delete of user. |
| 31 |
default: present |
| 32 |
choices: [ 'present', 'absent' ] |
| 33 |
type: str |
| 34 |
console_access: |
| 35 |
description: |
| 36 |
- Whether the user can access the MQ Console. |
| 37 |
- Defaults to C(false) on creation. |
| 38 |
type: bool |
| 39 |
groups: |
| 40 |
description: |
| 41 |
- Set group memberships for user. |
| 42 |
- Defaults to C([]) on creation. |
| 43 |
type: list |
| 44 |
elements: str |
| 45 |
password: |
| 46 |
description: |
| 47 |
- Set password for user. |
| 48 |
- Defaults to a random password on creation. |
| 49 |
- Ignored unless I(allow_pw_update=true). |
| 50 |
type: str |
| 51 |
allow_pw_update: |
| 52 |
description: |
| 53 |
- When I(allow_pw_update=true) and I(password) is set, the password |
| 54 |
will always be updated for the user. |
| 55 |
default: false |
| 56 |
type: bool |
| 57 |
extends_documentation_fragment: |
| 58 |
- amazon.aws.boto3 |
| 59 |
- amazon.aws.common.modules |
| 60 |
- amazon.aws.region.modules |
| 61 |
""" |
| 62 |
|
| 63 |
EXAMPLES = r""" |
| 64 |
- name: create/update user - set provided password if user doesn't exist, yet |
| 65 |
community.aws.mq_user: |
| 66 |
state: present |
| 67 |
broker_id: "aws-mq-broker-id" |
| 68 |
username: "sample_user1" |
| 69 |
console_access: false |
| 70 |
groups: ["g1", "g2"] |
| 71 |
password: "plain-text-password" |
| 72 |
|
| 73 |
- name: allow console access and update group list - relying on default state |
| 74 |
community.aws.mq_user: |
| 75 |
broker_id: "aws-mq-broker-id" |
| 76 |
username: "sample_user1" |
| 77 |
region: "{{ aws_region }}" |
| 78 |
console_access: true |
| 79 |
groups: ["g1", "g2", "g3"] |
| 80 |
|
| 81 |
- name: remove user - setting all credentials explicitly |
| 82 |
community.aws.mq_user: |
| 83 |
state: absent |
| 84 |
broker_id: "aws-mq-broker-id" |
| 85 |
username: "other_user" |
| 86 |
""" |
| 87 |
|
| 88 |
RETURN = r""" |
| 89 |
user: |
| 90 |
description: |
| 91 |
- just echos the username |
| 92 |
- "only present when state=present" |
| 93 |
type: str |
| 94 |
returned: success |
| 95 |
""" |
| 96 |
|
| 97 |
import secrets |
| 98 |
|
| 99 |
try: |
| 100 |
import botocore |
| 101 |
except ImportError: |
| 102 |
# handled by AnsibleAWSModule |
| 103 |
pass |
| 104 |
|
| 105 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 106 |
|
| 107 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 108 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 109 |
|
| 110 |
CREATE_DEFAULTS = { |
| 111 |
"console_access": False, |
| 112 |
"groups": [], |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
def _group_change_required(user_response, requested_groups): |
| 117 |
current_groups = [] |
| 118 |
if "Groups" in user_response: |
| 119 |
current_groups = user_response["Groups"] |
| 120 |
elif "Pending" in user_response: |
| 121 |
# to support automatic testing without broker reboot |
| 122 |
current_groups = user_response["Pending"]["Groups"] |
| 123 |
if len(current_groups) != len(requested_groups): |
| 124 |
return True |
| 125 |
if len(current_groups) != len(set(current_groups) & set(requested_groups)): |
| 126 |
return True |
| 127 |
# |
| 128 |
return False |
| 129 |
|
| 130 |
|
| 131 |
def _console_access_change_required(user_response, requested_boolean): |
| 132 |
current_boolean = CREATE_DEFAULTS["console_access"] |
| 133 |
if "ConsoleAccess" in user_response: |
| 134 |
current_boolean = user_response["ConsoleAccess"] |
| 135 |
elif "Pending" in user_response: |
| 136 |
# to support automatic testing without broker reboot |
| 137 |
current_boolean = user_response["Pending"]["ConsoleAccess"] |
| 138 |
# |
| 139 |
return current_boolean != requested_boolean |
| 140 |
|
| 141 |
|
| 142 |
def generate_password(): |
| 143 |
return secrets.token_hex(20) |
| 144 |
|
| 145 |
|
| 146 |
# returns API response object |
| 147 |
def _create_user(conn, module): |
| 148 |
kwargs = {"BrokerId": module.params["broker_id"], "Username": module.params["username"]} |
| 149 |
if "groups" in module.params and module.params["groups"] is not None: |
| 150 |
kwargs["Groups"] = module.params["groups"] |
| 151 |
else: |
| 152 |
kwargs["Groups"] = CREATE_DEFAULTS["groups"] |
| 153 |
if "password" in module.params and module.params["password"]: |
| 154 |
kwargs["Password"] = module.params["password"] |
| 155 |
else: |
| 156 |
kwargs["Password"] = generate_password() |
| 157 |
if "console_access" in module.params and module.params["console_access"] is not None: |
| 158 |
kwargs["ConsoleAccess"] = module.params["console_access"] |
| 159 |
else: |
| 160 |
kwargs["ConsoleAccess"] = CREATE_DEFAULTS["console_access"] |
| 161 |
try: |
| 162 |
response = conn.create_user(**kwargs) |
| 163 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 164 |
module.fail_json_aws(e, msg="Couldn't create user") |
| 165 |
return response |
| 166 |
|
| 167 |
|
| 168 |
# returns API response object |
| 169 |
def _update_user(conn, module, kwargs): |
| 170 |
try: |
| 171 |
response = conn.update_user(**kwargs) |
| 172 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 173 |
module.fail_json_aws(e, msg="Couldn't update user") |
| 174 |
return response |
| 175 |
|
| 176 |
|
| 177 |
def get_matching_user(conn, module, broker_id, username): |
| 178 |
try: |
| 179 |
response = conn.describe_user(BrokerId=broker_id, Username=username) |
| 180 |
except is_boto3_error_code("NotFoundException"): |
| 181 |
return None |
| 182 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 183 |
module.fail_json_aws(e, msg="Couldn't get user details") |
| 184 |
return response |
| 185 |
|
| 186 |
|
| 187 |
def ensure_user_present(conn, module): |
| 188 |
user = get_matching_user(conn, module, module.params["broker_id"], module.params["username"]) |
| 189 |
changed = False |
| 190 |
|
| 191 |
if user is None: |
| 192 |
if not module.check_mode: |
| 193 |
_response = _create_user(conn, module) |
| 194 |
changed = True |
| 195 |
else: |
| 196 |
kwargs = {} |
| 197 |
if "groups" in module.params and module.params["groups"] is not None: |
| 198 |
if _group_change_required(user, module.params["groups"]): |
| 199 |
kwargs["Groups"] = module.params["groups"] |
| 200 |
if "console_access" in module.params and module.params["console_access"] is not None: |
| 201 |
if _console_access_change_required(user, module.params["console_access"]): |
| 202 |
kwargs["ConsoleAccess"] = module.params["console_access"] |
| 203 |
if "password" in module.params and module.params["password"]: |
| 204 |
if "allow_pw_update" in module.params and module.params["allow_pw_update"]: |
| 205 |
kwargs["Password"] = module.params["password"] |
| 206 |
if len(kwargs) == 0: |
| 207 |
changed = False |
| 208 |
else: |
| 209 |
if not module.check_mode: |
| 210 |
kwargs["BrokerId"] = module.params["broker_id"] |
| 211 |
kwargs["Username"] = module.params["username"] |
| 212 |
_update_user(conn, module, kwargs) |
| 213 |
# |
| 214 |
changed = True |
| 215 |
# |
| 216 |
user = get_matching_user(conn, module, module.params["broker_id"], module.params["username"]) |
| 217 |
|
| 218 |
return {"changed": changed, "user": camel_dict_to_snake_dict(user, ignore_list=["Tags"])} |
| 219 |
|
| 220 |
|
| 221 |
def ensure_user_absent(conn, module): |
| 222 |
user = get_matching_user(conn, module, module.params["broker_id"], module.params["username"]) |
| 223 |
result = {"changed": False} |
| 224 |
if user is None: |
| 225 |
return result |
| 226 |
# better support for testing |
| 227 |
if "Pending" in user and "PendingChange" in user["Pending"] and user["Pending"]["PendingChange"] == "DELETE": |
| 228 |
return result |
| 229 |
|
| 230 |
result = {"changed": True} |
| 231 |
if module.check_mode: |
| 232 |
return result |
| 233 |
|
| 234 |
try: |
| 235 |
conn.delete_user(BrokerId=user["BrokerId"], Username=user["Username"]) |
| 236 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 237 |
module.fail_json_aws(e, msg="Couldn't delete user") |
| 238 |
|
| 239 |
return result |
| 240 |
|
| 241 |
|
| 242 |
def main(): |
| 243 |
argument_spec = dict( |
| 244 |
broker_id=dict(required=True, type="str"), |
| 245 |
username=dict(required=True, type="str"), |
| 246 |
console_access=dict(required=False, type="bool"), |
| 247 |
groups=dict(required=False, type="list", elements="str"), |
| 248 |
password=dict(required=False, type="str", no_log=True), |
| 249 |
allow_pw_update=dict(default=False, required=False, type="bool"), |
| 250 |
state=dict(default="present", choices=["present", "absent"]), |
| 251 |
) |
| 252 |
|
| 253 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 254 |
|
| 255 |
connection = module.client("mq") |
| 256 |
|
| 257 |
state = module.params.get("state") |
| 258 |
|
| 259 |
try: |
| 260 |
if state == "present": |
| 261 |
result = ensure_user_present(connection, module) |
| 262 |
elif state == "absent": |
| 263 |
result = ensure_user_absent(connection, module) |
| 264 |
except botocore.exceptions.ClientError as e: |
| 265 |
module.fail_json_aws(e) |
| 266 |
|
| 267 |
module.exit_json(**result) |
| 268 |
|
| 269 |
|
| 270 |
if __name__ == "__main__": |
| 271 |
main() |