| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2017, Eike Frost <[email protected]> |
| 4 |
# Copyright (c) 2021, Christophe Gilles <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or |
| 6 |
# https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: keycloak_authz_custom_policy |
| 12 |
|
| 13 |
short_description: Allows administration of Keycloak client custom Javascript policies using Keycloak API |
| 14 |
|
| 15 |
version_added: 7.5.0 |
| 16 |
|
| 17 |
description: |
| 18 |
- This module allows the administration of Keycloak client custom Javascript using the Keycloak REST API. Custom Javascript |
| 19 |
policies are only available if a client has Authorization enabled and if they have been deployed to the Keycloak server |
| 20 |
as JAR files. |
| 21 |
- This module requires access to the REST API using OpenID Connect; the user connecting and the realm being used must have |
| 22 |
the requisite access rights. In a default Keycloak installation, admin-cli and an admin user would work, as would a separate |
| 23 |
realm definition with the scope tailored to your needs and a user having the expected roles. |
| 24 |
- The names of module options are snake_cased versions of the camelCase options used by Keycloak. The Authorization Services |
| 25 |
paths and payloads have not officially been documented by the Keycloak project. |
| 26 |
U(https://www.puppeteers.net/blog/keycloak-authorization-services-rest-api-paths-and-payload/). |
| 27 |
attributes: |
| 28 |
check_mode: |
| 29 |
support: full |
| 30 |
diff_mode: |
| 31 |
support: none |
| 32 |
action_group: |
| 33 |
version_added: 10.2.0 |
| 34 |
|
| 35 |
options: |
| 36 |
state: |
| 37 |
description: |
| 38 |
- State of the custom policy. |
| 39 |
- On V(present), the custom policy is created (or updated if it exists already). |
| 40 |
- On V(absent), the custom policy is removed if it exists. |
| 41 |
choices: ['present', 'absent'] |
| 42 |
default: 'present' |
| 43 |
type: str |
| 44 |
name: |
| 45 |
description: |
| 46 |
- Name of the custom policy to create. |
| 47 |
type: str |
| 48 |
required: true |
| 49 |
policy_type: |
| 50 |
description: |
| 51 |
- The type of the policy. This must match the name of the custom policy deployed to the server. |
| 52 |
- Multiple policies pointing to the same policy type can be created, but their names have to differ. |
| 53 |
type: str |
| 54 |
required: true |
| 55 |
client_id: |
| 56 |
description: |
| 57 |
- The V(clientId) of the Keycloak client that should have the custom policy attached to it. |
| 58 |
- This is usually a human-readable name of the Keycloak client. |
| 59 |
type: str |
| 60 |
required: true |
| 61 |
realm: |
| 62 |
description: |
| 63 |
- The name of the Keycloak realm the Keycloak client is in. |
| 64 |
type: str |
| 65 |
required: true |
| 66 |
|
| 67 |
extends_documentation_fragment: |
| 68 |
- community.general._keycloak |
| 69 |
- community.general._keycloak.actiongroup_keycloak |
| 70 |
- community.general._attributes |
| 71 |
|
| 72 |
author: |
| 73 |
- Samuli Seppänen (@mattock) |
| 74 |
""" |
| 75 |
|
| 76 |
EXAMPLES = r""" |
| 77 |
- name: Manage Keycloak custom authorization policy |
| 78 |
community.general.keycloak_authz_custom_policy: |
| 79 |
name: OnlyOwner |
| 80 |
state: present |
| 81 |
policy_type: script-policy.js |
| 82 |
client_id: myclient |
| 83 |
realm: myrealm |
| 84 |
auth_keycloak_url: http://localhost:8080/auth |
| 85 |
auth_username: keycloak |
| 86 |
auth_password: keycloak |
| 87 |
auth_realm: master |
| 88 |
""" |
| 89 |
|
| 90 |
RETURN = r""" |
| 91 |
msg: |
| 92 |
description: Message as to what action was taken. |
| 93 |
returned: always |
| 94 |
type: str |
| 95 |
|
| 96 |
end_state: |
| 97 |
description: Representation of the custom policy after module execution. |
| 98 |
returned: on success |
| 99 |
type: dict |
| 100 |
contains: |
| 101 |
name: |
| 102 |
description: Name of the custom policy. |
| 103 |
type: str |
| 104 |
returned: when I(state=present) |
| 105 |
sample: file:delete |
| 106 |
policy_type: |
| 107 |
description: Type of custom policy. |
| 108 |
type: str |
| 109 |
returned: when I(state=present) |
| 110 |
sample: File delete |
| 111 |
""" |
| 112 |
|
| 113 |
from ansible.module_utils.basic import AnsibleModule |
| 114 |
|
| 115 |
from ansible_collections.community.general.plugins.module_utils._keycloak import ( |
| 116 |
KeycloakAPI, |
| 117 |
KeycloakError, |
| 118 |
get_token, |
| 119 |
keycloak_argument_spec, |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
def main(): |
| 124 |
""" |
| 125 |
Module execution |
| 126 |
|
| 127 |
:return: |
| 128 |
""" |
| 129 |
argument_spec = keycloak_argument_spec() |
| 130 |
|
| 131 |
meta_args = dict( |
| 132 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 133 |
name=dict(type="str", required=True), |
| 134 |
policy_type=dict(type="str", required=True), |
| 135 |
client_id=dict(type="str", required=True), |
| 136 |
realm=dict(type="str", required=True), |
| 137 |
) |
| 138 |
|
| 139 |
argument_spec.update(meta_args) |
| 140 |
|
| 141 |
module = AnsibleModule( |
| 142 |
argument_spec=argument_spec, |
| 143 |
supports_check_mode=True, |
| 144 |
required_one_of=( |
| 145 |
[["token", "auth_realm", "auth_username", "auth_password", "auth_client_id", "auth_client_secret"]] |
| 146 |
), |
| 147 |
required_together=([["auth_username", "auth_password"]]), |
| 148 |
required_by={"refresh_token": "auth_realm"}, |
| 149 |
) |
| 150 |
|
| 151 |
result = dict(changed=False, msg="", end_state={}) |
| 152 |
|
| 153 |
# Obtain access token, initialize API |
| 154 |
try: |
| 155 |
connection_header = get_token(module.params) |
| 156 |
except KeycloakError as e: |
| 157 |
module.fail_json(msg=str(e)) |
| 158 |
|
| 159 |
kc = KeycloakAPI(module, connection_header) |
| 160 |
|
| 161 |
# Convenience variables |
| 162 |
state = module.params.get("state") |
| 163 |
name = module.params.get("name") |
| 164 |
policy_type = module.params.get("policy_type") |
| 165 |
client_id = module.params.get("client_id") |
| 166 |
realm = module.params.get("realm") |
| 167 |
|
| 168 |
cid = kc.get_client_id(client_id, realm=realm) |
| 169 |
if not cid: |
| 170 |
module.fail_json(msg=f"Invalid client {client_id} for realm {realm}") |
| 171 |
|
| 172 |
before_authz_custom_policy = kc.get_authz_policy_by_name(name=name, client_id=cid, realm=realm) |
| 173 |
|
| 174 |
desired_authz_custom_policy = {} |
| 175 |
desired_authz_custom_policy["name"] = name |
| 176 |
desired_authz_custom_policy["type"] = policy_type |
| 177 |
|
| 178 |
# Modifying existing custom policies is not possible |
| 179 |
if before_authz_custom_policy and state == "present": |
| 180 |
result["msg"] = f"Custom policy {name} already exists" |
| 181 |
result["changed"] = False |
| 182 |
result["end_state"] = desired_authz_custom_policy |
| 183 |
elif not before_authz_custom_policy and state == "present": |
| 184 |
if module.check_mode: |
| 185 |
result["msg"] = f"Would create custom policy {name}" |
| 186 |
else: |
| 187 |
kc.create_authz_custom_policy( |
| 188 |
payload=desired_authz_custom_policy, policy_type=policy_type, client_id=cid, realm=realm |
| 189 |
) |
| 190 |
result["msg"] = f"Custom policy {name} created" |
| 191 |
|
| 192 |
result["changed"] = True |
| 193 |
result["end_state"] = desired_authz_custom_policy |
| 194 |
elif before_authz_custom_policy and state == "absent": |
| 195 |
if module.check_mode: |
| 196 |
result["msg"] = f"Would remove custom policy {name}" |
| 197 |
else: |
| 198 |
kc.remove_authz_custom_policy(policy_id=before_authz_custom_policy["id"], client_id=cid, realm=realm) |
| 199 |
result["msg"] = f"Custom policy {name} removed" |
| 200 |
|
| 201 |
result["changed"] = True |
| 202 |
result["end_state"] = {} |
| 203 |
elif not before_authz_custom_policy and state == "absent": |
| 204 |
result["msg"] = f"Custom policy {name} does not exist" |
| 205 |
result["changed"] = False |
| 206 |
result["end_state"] = {} |
| 207 |
|
| 208 |
module.exit_json(**result) |
| 209 |
|
| 210 |
|
| 211 |
if __name__ == "__main__": |
| 212 |
main() |