| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) Ansible project |
| 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_component_info |
| 11 |
|
| 12 |
short_description: Retrieve component info in Keycloak |
| 13 |
|
| 14 |
version_added: 8.2.0 |
| 15 |
|
| 16 |
description: |
| 17 |
- This module retrieve information on component from Keycloak. |
| 18 |
attributes: |
| 19 |
action_group: |
| 20 |
version_added: 10.2.0 |
| 21 |
|
| 22 |
options: |
| 23 |
realm: |
| 24 |
description: |
| 25 |
- The name of the realm. |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Name of the Component. |
| 31 |
type: str |
| 32 |
provider_type: |
| 33 |
description: |
| 34 |
- Provider type of components. |
| 35 |
- 'Examples: V(org.keycloak.storage.UserStorageProvider), V(org.keycloak.services.clientregistration.policy.ClientRegistrationPolicy), |
| 36 |
V(org.keycloak.keys.KeyProvider), V(org.keycloak.userprofile.UserProfileProvider), V(org.keycloak.storage.ldap.mappers.LDAPStorageMapper).' |
| 37 |
type: str |
| 38 |
parent_id: |
| 39 |
description: |
| 40 |
- Container ID of the components. |
| 41 |
type: str |
| 42 |
|
| 43 |
|
| 44 |
extends_documentation_fragment: |
| 45 |
- community.general._keycloak |
| 46 |
- community.general._keycloak.actiongroup_keycloak |
| 47 |
- community.general._attributes |
| 48 |
- community.general._attributes.info_module |
| 49 |
|
| 50 |
author: |
| 51 |
- Andre Desrosiers (@desand01) |
| 52 |
""" |
| 53 |
|
| 54 |
EXAMPLES = r""" |
| 55 |
- name: Retrive info of a UserStorageProvider named myldap |
| 56 |
community.general.keycloak_component_info: |
| 57 |
auth_keycloak_url: http://localhost:8080/auth |
| 58 |
auth_username: admin |
| 59 |
auth_password: password |
| 60 |
auth_realm: master |
| 61 |
realm: myrealm |
| 62 |
name: myldap |
| 63 |
provider_type: org.keycloak.storage.UserStorageProvider |
| 64 |
|
| 65 |
- name: Retrive key info component |
| 66 |
community.general.keycloak_component_info: |
| 67 |
auth_keycloak_url: http://localhost:8080/auth |
| 68 |
auth_username: admin |
| 69 |
auth_password: password |
| 70 |
auth_realm: master |
| 71 |
realm: myrealm |
| 72 |
name: rsa-enc-generated |
| 73 |
provider_type: org.keycloak.keys.KeyProvider |
| 74 |
|
| 75 |
- name: Retrive all component from realm master |
| 76 |
community.general.keycloak_component_info: |
| 77 |
auth_keycloak_url: http://localhost:8080/auth |
| 78 |
auth_username: admin |
| 79 |
auth_password: password |
| 80 |
auth_realm: master |
| 81 |
realm: myrealm |
| 82 |
|
| 83 |
- name: Retrive all sub components of parent component filter by type |
| 84 |
community.general.keycloak_component_info: |
| 85 |
auth_keycloak_url: http://localhost:8080/auth |
| 86 |
auth_username: admin |
| 87 |
auth_password: password |
| 88 |
auth_realm: master |
| 89 |
realm: myrealm |
| 90 |
parent_id: "075ef2fa-19fc-4a6d-bf4c-249f57365fd2" |
| 91 |
provider_type: "org.keycloak.storage.ldap.mappers.LDAPStorageMapper" |
| 92 |
""" |
| 93 |
|
| 94 |
RETURN = r""" |
| 95 |
components: |
| 96 |
description: JSON representation of components. |
| 97 |
returned: always |
| 98 |
type: list |
| 99 |
elements: dict |
| 100 |
""" |
| 101 |
|
| 102 |
from urllib.parse import quote |
| 103 |
|
| 104 |
from ansible.module_utils.basic import AnsibleModule |
| 105 |
|
| 106 |
from ansible_collections.community.general.plugins.module_utils._keycloak import ( |
| 107 |
KeycloakAPI, |
| 108 |
KeycloakError, |
| 109 |
get_token, |
| 110 |
keycloak_argument_spec, |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
def main(): |
| 115 |
""" |
| 116 |
Module execution |
| 117 |
|
| 118 |
:return: |
| 119 |
""" |
| 120 |
argument_spec = keycloak_argument_spec() |
| 121 |
|
| 122 |
meta_args = dict( |
| 123 |
name=dict(type="str"), |
| 124 |
realm=dict(type="str", required=True), |
| 125 |
parent_id=dict(type="str"), |
| 126 |
provider_type=dict(type="str"), |
| 127 |
) |
| 128 |
|
| 129 |
argument_spec.update(meta_args) |
| 130 |
|
| 131 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 132 |
|
| 133 |
result = dict(changed=False, components=[]) |
| 134 |
|
| 135 |
# Obtain access token, initialize API |
| 136 |
try: |
| 137 |
connection_header = get_token(module.params) |
| 138 |
except KeycloakError as e: |
| 139 |
module.fail_json(msg=str(e)) |
| 140 |
|
| 141 |
kc = KeycloakAPI(module, connection_header) |
| 142 |
|
| 143 |
realm = module.params.get("realm") |
| 144 |
parentId = module.params.get("parent_id") |
| 145 |
name = module.params.get("name") |
| 146 |
providerType = module.params.get("provider_type") |
| 147 |
|
| 148 |
objRealm = kc.get_realm_by_id(realm) |
| 149 |
if not objRealm: |
| 150 |
module.fail_json(msg=f"Failed to retrive realm '{realm}'") |
| 151 |
|
| 152 |
filters = [] |
| 153 |
|
| 154 |
if parentId: |
| 155 |
filters.append(f"parent={quote(parentId, safe='')}") |
| 156 |
else: |
| 157 |
filters.append(f"parent={quote(objRealm['id'], safe='')}") |
| 158 |
|
| 159 |
if name: |
| 160 |
filters.append(f"name={quote(name, safe='')}") |
| 161 |
if providerType: |
| 162 |
filters.append(f"type={quote(providerType, safe='')}") |
| 163 |
|
| 164 |
result["components"] = kc.get_components(filter="&".join(filters), realm=realm) |
| 165 |
|
| 166 |
module.exit_json(**result) |
| 167 |
|
| 168 |
|
| 169 |
if __name__ == "__main__": |
| 170 |
main() |