| 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_permission_info |
| 12 |
|
| 13 |
version_added: 7.2.0 |
| 14 |
|
| 15 |
short_description: Query Keycloak client authorization permissions information |
| 16 |
|
| 17 |
description: |
| 18 |
- This module allows querying information about Keycloak client authorization permissions from the resources endpoint using |
| 19 |
the Keycloak REST API. Authorization permissions are only available if a client has Authorization enabled. |
| 20 |
- This module requires access to the REST API using OpenID Connect; the user connecting and the realm being used must have |
| 21 |
the requisite access rights. In a default Keycloak installation, admin-cli and an admin user would work, as would a separate |
| 22 |
realm definition with the scope tailored to your needs and a user having the expected roles. |
| 23 |
- The names of module options are snake_cased versions of the camelCase options used by Keycloak. The Authorization Services |
| 24 |
paths and payloads have not officially been documented by the Keycloak project. |
| 25 |
U(https://www.puppeteers.net/blog/keycloak-authorization-services-rest-api-paths-and-payload/). |
| 26 |
attributes: |
| 27 |
action_group: |
| 28 |
version_added: 10.2.0 |
| 29 |
|
| 30 |
options: |
| 31 |
name: |
| 32 |
description: |
| 33 |
- Name of the authorization permission to create. |
| 34 |
type: str |
| 35 |
required: true |
| 36 |
client_id: |
| 37 |
description: |
| 38 |
- The clientId of the keycloak client that should have the authorization scope. |
| 39 |
- This is usually a human-readable name of the Keycloak client. |
| 40 |
type: str |
| 41 |
required: true |
| 42 |
realm: |
| 43 |
description: |
| 44 |
- The name of the Keycloak realm the Keycloak client is in. |
| 45 |
type: str |
| 46 |
required: true |
| 47 |
|
| 48 |
extends_documentation_fragment: |
| 49 |
- community.general._keycloak |
| 50 |
- community.general._keycloak.actiongroup_keycloak |
| 51 |
- community.general._attributes |
| 52 |
- community.general._attributes.info_module |
| 53 |
|
| 54 |
author: |
| 55 |
- Samuli Seppänen (@mattock) |
| 56 |
""" |
| 57 |
|
| 58 |
EXAMPLES = r""" |
| 59 |
- name: Query Keycloak authorization permission |
| 60 |
community.general.keycloak_authz_permission_info: |
| 61 |
name: ScopePermission |
| 62 |
client_id: myclient |
| 63 |
realm: myrealm |
| 64 |
auth_keycloak_url: http://localhost:8080/auth |
| 65 |
auth_username: keycloak |
| 66 |
auth_password: keycloak |
| 67 |
auth_realm: master |
| 68 |
""" |
| 69 |
|
| 70 |
RETURN = r""" |
| 71 |
msg: |
| 72 |
description: Message as to what action was taken. |
| 73 |
returned: always |
| 74 |
type: str |
| 75 |
|
| 76 |
queried_state: |
| 77 |
description: State of the resource (a policy) as seen by Keycloak. |
| 78 |
returned: on success |
| 79 |
type: dict |
| 80 |
contains: |
| 81 |
id: |
| 82 |
description: ID of the authorization permission. |
| 83 |
type: str |
| 84 |
sample: 9da05cd2-b273-4354-bbd8-0c133918a454 |
| 85 |
name: |
| 86 |
description: Name of the authorization permission. |
| 87 |
type: str |
| 88 |
sample: ResourcePermission |
| 89 |
description: |
| 90 |
description: Description of the authorization permission. |
| 91 |
type: str |
| 92 |
sample: Resource Permission |
| 93 |
type: |
| 94 |
description: Type of the authorization permission. |
| 95 |
type: str |
| 96 |
sample: resource |
| 97 |
decisionStrategy: |
| 98 |
description: The decision strategy. |
| 99 |
type: str |
| 100 |
sample: UNANIMOUS |
| 101 |
logic: |
| 102 |
description: The logic used for the permission (part of the payload, but has a fixed value). |
| 103 |
type: str |
| 104 |
sample: POSITIVE |
| 105 |
config: |
| 106 |
description: Configuration of the permission (empty in all observed cases). |
| 107 |
type: dict |
| 108 |
sample: {} |
| 109 |
""" |
| 110 |
|
| 111 |
from ansible.module_utils.basic import AnsibleModule |
| 112 |
|
| 113 |
from ansible_collections.community.general.plugins.module_utils._keycloak import ( |
| 114 |
KeycloakAPI, |
| 115 |
KeycloakError, |
| 116 |
get_token, |
| 117 |
keycloak_argument_spec, |
| 118 |
) |
| 119 |
|
| 120 |
|
| 121 |
def main(): |
| 122 |
""" |
| 123 |
Module execution |
| 124 |
|
| 125 |
:return: |
| 126 |
""" |
| 127 |
argument_spec = keycloak_argument_spec() |
| 128 |
|
| 129 |
meta_args = dict( |
| 130 |
name=dict(type="str", required=True), |
| 131 |
client_id=dict(type="str", required=True), |
| 132 |
realm=dict(type="str", required=True), |
| 133 |
) |
| 134 |
|
| 135 |
argument_spec.update(meta_args) |
| 136 |
|
| 137 |
module = AnsibleModule( |
| 138 |
argument_spec=argument_spec, |
| 139 |
supports_check_mode=True, |
| 140 |
required_one_of=( |
| 141 |
[["token", "auth_realm", "auth_username", "auth_password", "auth_client_id", "auth_client_secret"]] |
| 142 |
), |
| 143 |
required_together=([["auth_username", "auth_password"]]), |
| 144 |
required_by={"refresh_token": "auth_realm"}, |
| 145 |
) |
| 146 |
|
| 147 |
# Convenience variables |
| 148 |
name = module.params.get("name") |
| 149 |
client_id = module.params.get("client_id") |
| 150 |
realm = module.params.get("realm") |
| 151 |
|
| 152 |
result = dict(changed=False, msg="", queried_state={}) |
| 153 |
|
| 154 |
# Obtain access token, initialize API |
| 155 |
try: |
| 156 |
connection_header = get_token(module.params) |
| 157 |
except KeycloakError as e: |
| 158 |
module.fail_json(msg=str(e)) |
| 159 |
|
| 160 |
kc = KeycloakAPI(module, connection_header) |
| 161 |
|
| 162 |
# Get id of the client based on client_id |
| 163 |
cid = kc.get_client_id(client_id, realm=realm) |
| 164 |
if not cid: |
| 165 |
module.fail_json(msg=f"Invalid client {client_id} for realm {realm}") |
| 166 |
|
| 167 |
# Get current state of the permission using its name as the search |
| 168 |
# filter. This returns False if it is not found. |
| 169 |
permission = kc.get_authz_permission_by_name(name=name, client_id=cid, realm=realm) |
| 170 |
|
| 171 |
result["queried_state"] = permission |
| 172 |
|
| 173 |
module.exit_json(**result) |
| 174 |
|
| 175 |
|
| 176 |
if __name__ == "__main__": |
| 177 |
main() |