| 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_realm_info |
| 11 |
|
| 12 |
short_description: Allows obtaining Keycloak realm public information using Keycloak API |
| 13 |
|
| 14 |
version_added: 4.3.0 |
| 15 |
|
| 16 |
description: |
| 17 |
- This module allows you to get Keycloak realm public information using the Keycloak REST API. |
| 18 |
- The names of module options are snake_cased versions of the camelCase ones found in the Keycloak API and its documentation |
| 19 |
at U(https://www.keycloak.org/docs-api/8.0/rest-api/index.html). |
| 20 |
- Attributes are multi-valued in the Keycloak API. All attributes are lists of individual values and are returned that way |
| 21 |
by this module. You may pass single values for attributes when calling the module, and this is translated into a list |
| 22 |
suitable for the API. |
| 23 |
extends_documentation_fragment: |
| 24 |
- community.general._attributes |
| 25 |
- community.general._attributes.info_module |
| 26 |
|
| 27 |
options: |
| 28 |
auth_keycloak_url: |
| 29 |
description: |
| 30 |
- URL to the Keycloak instance. |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
aliases: |
| 34 |
- url |
| 35 |
validate_certs: |
| 36 |
description: |
| 37 |
- Verify TLS certificates (do not disable this in production). |
| 38 |
type: bool |
| 39 |
default: true |
| 40 |
|
| 41 |
realm: |
| 42 |
type: str |
| 43 |
description: |
| 44 |
- They Keycloak realm ID. |
| 45 |
default: 'master' |
| 46 |
|
| 47 |
author: |
| 48 |
- Fynn Chen (@fynncfchen) |
| 49 |
""" |
| 50 |
|
| 51 |
EXAMPLES = r""" |
| 52 |
- name: Get a Keycloak public key |
| 53 |
community.general.keycloak_realm_info: |
| 54 |
realm: MyCustomRealm |
| 55 |
auth_keycloak_url: https://auth.example.com/auth |
| 56 |
delegate_to: localhost |
| 57 |
""" |
| 58 |
|
| 59 |
RETURN = r""" |
| 60 |
msg: |
| 61 |
description: Message as to what action was taken. |
| 62 |
returned: always |
| 63 |
type: str |
| 64 |
|
| 65 |
realm_info: |
| 66 |
description: |
| 67 |
- Representation of the realm public information. |
| 68 |
returned: always |
| 69 |
type: dict |
| 70 |
contains: |
| 71 |
realm: |
| 72 |
description: Realm ID. |
| 73 |
type: str |
| 74 |
returned: always |
| 75 |
sample: MyRealm |
| 76 |
public_key: |
| 77 |
description: Public key of the realm. |
| 78 |
type: str |
| 79 |
returned: always |
| 80 |
sample: MIIBIjANBgkqhkiG9w0BAQEFAAO... |
| 81 |
token-service: |
| 82 |
description: Token endpoint URL. |
| 83 |
type: str |
| 84 |
returned: always |
| 85 |
sample: https://auth.example.com/auth/realms/MyRealm/protocol/openid-connect |
| 86 |
account-service: |
| 87 |
description: Account console URL. |
| 88 |
type: str |
| 89 |
returned: always |
| 90 |
sample: https://auth.example.com/auth/realms/MyRealm/account |
| 91 |
tokens-not-before: |
| 92 |
description: The token not before. |
| 93 |
type: int |
| 94 |
returned: always |
| 95 |
sample: 0 |
| 96 |
""" |
| 97 |
|
| 98 |
from ansible.module_utils.basic import AnsibleModule |
| 99 |
|
| 100 |
from ansible_collections.community.general.plugins.module_utils._keycloak import KeycloakAPI |
| 101 |
|
| 102 |
|
| 103 |
def main(): |
| 104 |
""" |
| 105 |
Module execution |
| 106 |
|
| 107 |
:return: |
| 108 |
""" |
| 109 |
argument_spec = dict( |
| 110 |
auth_keycloak_url=dict(type="str", aliases=["url"], required=True, no_log=False), |
| 111 |
validate_certs=dict(type="bool", default=True), |
| 112 |
realm=dict(default="master"), |
| 113 |
) |
| 114 |
|
| 115 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 116 |
|
| 117 |
result = dict(changed=False, msg="", realm_info="") |
| 118 |
|
| 119 |
kc = KeycloakAPI(module, {}) |
| 120 |
|
| 121 |
realm = module.params.get("realm") |
| 122 |
|
| 123 |
realm_info = kc.get_realm_info_by_id(realm=realm) |
| 124 |
|
| 125 |
result["realm_info"] = realm_info |
| 126 |
result["msg"] = f"Get realm public info successful for ID {realm}" |
| 127 |
module.exit_json(**result) |
| 128 |
|
| 129 |
|
| 130 |
if __name__ == "__main__": |
| 131 |
main() |