| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2025, mariusbertram <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: keycloak_user_execute_actions_email |
| 11 |
|
| 12 |
short_description: Send a Keycloak execute-actions email to a user |
| 13 |
|
| 14 |
version_added: 12.0.0 |
| 15 |
|
| 16 |
description: |
| 17 |
- Triggers the Keycloak endpoint C(execute-actions-email) for a user. |
| 18 |
This sends an email with one or more required actions the user must complete (for example resetting the password). |
| 19 |
- If no O(actions) list is provided, the default action C(UPDATE_PASSWORD) is used. |
| 20 |
- You must supply either the user's O(id) or O(username). Supplying only C(username) causes an extra lookup call. |
| 21 |
- This module always reports RV(ignore:changed=true) because sending an email is a side effect and cannot be made idempotent. |
| 22 |
attributes: |
| 23 |
check_mode: |
| 24 |
support: full |
| 25 |
diff_mode: |
| 26 |
support: none |
| 27 |
options: |
| 28 |
auth_username: |
| 29 |
aliases: [] |
| 30 |
realm: |
| 31 |
description: |
| 32 |
- The Keycloak realm where the user resides. |
| 33 |
type: str |
| 34 |
default: master |
| 35 |
id: |
| 36 |
description: |
| 37 |
- The unique ID (UUID) of the user. |
| 38 |
- Mutually exclusive with O(username). |
| 39 |
type: str |
| 40 |
username: |
| 41 |
description: |
| 42 |
- Username of the user. |
| 43 |
- Mutually exclusive with O(id). |
| 44 |
type: str |
| 45 |
actions: |
| 46 |
description: |
| 47 |
- List of required actions to include in the email. |
| 48 |
type: list |
| 49 |
elements: str |
| 50 |
default: |
| 51 |
- UPDATE_PASSWORD |
| 52 |
client_id: |
| 53 |
description: |
| 54 |
- Optional client ID used for the redirect link. |
| 55 |
aliases: [clientId] |
| 56 |
type: str |
| 57 |
redirect_uri: |
| 58 |
description: |
| 59 |
- Optional redirect URI. Must be valid for the given client if O(client_id) is set. |
| 60 |
aliases: [redirectUri] |
| 61 |
type: str |
| 62 |
lifespan: |
| 63 |
description: |
| 64 |
- Optional lifespan (in seconds) for the action token (supported on newer Keycloak versions). Forwarded as query parameter if provided. |
| 65 |
type: int |
| 66 |
extends_documentation_fragment: |
| 67 |
- community.general._keycloak |
| 68 |
- community.general._keycloak.actiongroup_keycloak |
| 69 |
- community.general._attributes |
| 70 |
author: |
| 71 |
- Marius Bertram (@mariusbertram) |
| 72 |
""" |
| 73 |
|
| 74 |
EXAMPLES = r""" |
| 75 |
- name: Password reset email (default action) with 1h lifespan |
| 76 |
community.general.keycloak_user_execute_actions_email: |
| 77 |
username: johndoe |
| 78 |
realm: MyRealm |
| 79 |
auth_client_id: admin-cli |
| 80 |
auth_keycloak_url: https://auth.example.com/auth |
| 81 |
auth_realm: master |
| 82 |
auth_username: ADMIN |
| 83 |
auth_password: SECRET |
| 84 |
lifespan: 3600 |
| 85 |
delegate_to: localhost |
| 86 |
|
| 87 |
- name: Multiple required actions using token auth |
| 88 |
community.general.keycloak_user_execute_actions_email: |
| 89 |
username: johndoe |
| 90 |
actions: |
| 91 |
- UPDATE_PASSWORD |
| 92 |
- VERIFY_EMAIL |
| 93 |
realm: MyRealm |
| 94 |
auth_client_id: admin-cli |
| 95 |
auth_keycloak_url: https://auth.example.com/auth |
| 96 |
token: TOKEN |
| 97 |
delegate_to: localhost |
| 98 |
|
| 99 |
- name: Email by user id with redirect |
| 100 |
community.general.keycloak_user_execute_actions_email: |
| 101 |
id: 9d59aa76-2755-48c6-b1af-beb70a82c3cd |
| 102 |
client_id: my-frontend |
| 103 |
redirect_uri: https://app.example.com/post-actions |
| 104 |
actions: |
| 105 |
- UPDATE_PASSWORD |
| 106 |
realm: MyRealm |
| 107 |
auth_client_id: admin-cli |
| 108 |
auth_keycloak_url: https://auth.example.com/auth |
| 109 |
auth_realm: master |
| 110 |
auth_username: ADMIN |
| 111 |
auth_password: SECRET |
| 112 |
delegate_to: localhost |
| 113 |
""" |
| 114 |
|
| 115 |
RETURN = r""" |
| 116 |
user_id: |
| 117 |
description: The user ID the email was (or would be, in check mode) sent to. |
| 118 |
returned: success |
| 119 |
type: str |
| 120 |
actions: |
| 121 |
description: List of actions included in the email. |
| 122 |
returned: success |
| 123 |
type: list |
| 124 |
elements: str |
| 125 |
""" |
| 126 |
|
| 127 |
from ansible.module_utils.basic import AnsibleModule |
| 128 |
|
| 129 |
from ansible_collections.community.general.plugins.module_utils._keycloak import ( |
| 130 |
KeycloakAPI, |
| 131 |
KeycloakError, |
| 132 |
get_token, |
| 133 |
keycloak_argument_spec, |
| 134 |
) |
| 135 |
|
| 136 |
|
| 137 |
def main(): |
| 138 |
argument_spec = keycloak_argument_spec() |
| 139 |
# Avoid alias collision as in keycloak_user: clear auth_username aliases locally |
| 140 |
argument_spec["auth_username"]["aliases"] = [] |
| 141 |
|
| 142 |
meta_args = dict( |
| 143 |
realm=dict(type="str", default="master"), |
| 144 |
id=dict(type="str"), |
| 145 |
username=dict(type="str"), |
| 146 |
actions=dict(type="list", elements="str", default=["UPDATE_PASSWORD"]), |
| 147 |
client_id=dict(type="str", aliases=["clientId"]), |
| 148 |
redirect_uri=dict(type="str", aliases=["redirectUri"]), |
| 149 |
lifespan=dict(type="int"), |
| 150 |
) |
| 151 |
argument_spec.update(meta_args) |
| 152 |
|
| 153 |
module = AnsibleModule( |
| 154 |
argument_spec=argument_spec, |
| 155 |
supports_check_mode=True, |
| 156 |
required_one_of=[["id", "username"]], |
| 157 |
mutually_exclusive=[["id", "username"]], |
| 158 |
) |
| 159 |
|
| 160 |
try: |
| 161 |
connection_header = get_token(module.params) |
| 162 |
except KeycloakError as e: |
| 163 |
module.fail_json(msg=str(e)) |
| 164 |
|
| 165 |
kc = KeycloakAPI(module, connection_header) |
| 166 |
|
| 167 |
realm = module.params.get("realm") |
| 168 |
user_id = module.params.get("id") |
| 169 |
username = module.params.get("username") |
| 170 |
actions = module.params.get("actions") |
| 171 |
client_id = module.params.get("client_id") |
| 172 |
redirect_uri = module.params.get("redirect_uri") |
| 173 |
lifespan = module.params.get("lifespan") |
| 174 |
|
| 175 |
# Resolve user ID if only username is provided |
| 176 |
if user_id is None: |
| 177 |
user_obj = kc.get_user_by_username(username=username, realm=realm) |
| 178 |
if user_obj is None: |
| 179 |
module.fail_json(msg=f"User '{username}' not found in realm {realm}") |
| 180 |
user_id = user_obj["id"] |
| 181 |
|
| 182 |
if module.check_mode: |
| 183 |
module.exit_json( |
| 184 |
changed=True, msg=f"Would send execute-actions email to user {user_id}", user_id=user_id, actions=actions |
| 185 |
) |
| 186 |
|
| 187 |
try: |
| 188 |
kc.send_execute_actions_email( |
| 189 |
user_id=user_id, |
| 190 |
realm=realm, |
| 191 |
client_id=client_id, |
| 192 |
data=actions, |
| 193 |
redirect_uri=redirect_uri, |
| 194 |
lifespan=lifespan, |
| 195 |
) |
| 196 |
except Exception as e: |
| 197 |
module.fail_json(msg=str(e)) |
| 198 |
|
| 199 |
module.exit_json( |
| 200 |
changed=True, msg=f"Execute-actions email sent to user {user_id}", user_id=user_id, actions=actions |
| 201 |
) |
| 202 |
|
| 203 |
|
| 204 |
if __name__ == "__main__": |
| 205 |
main() |