Debian Codegraph @

ansible_collections/community/general/plugins/modules/keycloak_realm_keys_metadata_info.py

raw
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_keys_metadata_info
11
12 short_description: Allows obtaining Keycloak realm keys metadata using Keycloak API
13
14 version_added: 9.3.0
15
16 description:
17 - This module allows you to get Keycloak realm keys metadata 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/latest/rest-api/index.html).
20 attributes:
21 action_group:
22 version_added: 10.2.0
23
24 options:
25 realm:
26 type: str
27 description:
28 - They Keycloak realm to fetch keys metadata.
29 default: 'master'
30
31 extends_documentation_fragment:
32 - community.general._keycloak
33 - community.general._keycloak.actiongroup_keycloak
34 - community.general._attributes
35 - community.general._attributes.info_module
36
37 author:
38 - Thomas Bach (@thomasbach-dev)
39 """
40
41 EXAMPLES = r"""
42 - name: Fetch Keys metadata
43 community.general.keycloak_realm_keys_metadata_info:
44 auth_keycloak_url: https://auth.example.com/auth
45 auth_realm: master
46 auth_username: USERNAME
47 auth_password: PASSWORD
48 realm: MyCustomRealm
49 delegate_to: localhost
50 register: keycloak_keys_metadata
51
52 - name: Write the Keycloak keys certificate into a file
53 ansible.builtin.copy:
54 dest: /tmp/keycloak.cert
55 content: |
56 {{ keys_metadata['keycloak_keys_metadata']['keys']
57 | selectattr('algorithm', 'equalto', 'RS256')
58 | map(attribute='certificate')
59 | first
60 }}
61 delegate_to: localhost
62 """
63
64 RETURN = r"""
65 msg:
66 description: Message as to what action was taken.
67 returned: always
68 type: str
69
70 keys_metadata:
71 description:
72
73 - Representation of the realm keys metadata (see U(https://www.keycloak.org/docs-api/latest/rest-api/index.html#KeysMetadataRepresentation)).
74 returned: always
75 type: dict
76 contains:
77 active:
78 description: A mapping (that is, a dict) from key algorithms to UUIDs.
79 type: dict
80 returned: always
81 keys:
82 description: A list of dicts providing detailed information on the keys.
83 type: list
84 elements: dict
85 returned: always
86 """
87
88 from ansible.module_utils.basic import AnsibleModule
89
90 from ansible_collections.community.general.plugins.module_utils._keycloak import (
91 KeycloakAPI,
92 KeycloakError,
93 get_token,
94 keycloak_argument_spec,
95 )
96
97
98 def main():
99 argument_spec = keycloak_argument_spec()
100
101 meta_args = dict(
102 realm=dict(default="master"),
103 )
104 argument_spec.update(meta_args)
105
106 module = AnsibleModule(
107 argument_spec=argument_spec,
108 supports_check_mode=True,
109 required_one_of=(
110 [["token", "auth_realm", "auth_username", "auth_password", "auth_client_id", "auth_client_secret"]]
111 ),
112 required_together=([["auth_username", "auth_password"]]),
113 required_by={"refresh_token": "auth_realm"},
114 )
115
116 result = dict(changed=False, msg="", keys_metadata="")
117
118 # Obtain access token, initialize API
119 try:
120 connection_header = get_token(module.params)
121 except KeycloakError as e:
122 module.fail_json(msg=str(e))
123
124 kc = KeycloakAPI(module, connection_header)
125
126 realm = module.params.get("realm")
127
128 keys_metadata = kc.get_realm_keys_metadata_by_id(realm=realm)
129
130 result["keys_metadata"] = keys_metadata
131 result["msg"] = f"Get realm keys metadata successful for ID {realm}"
132 module.exit_json(**result)
133
134
135 if __name__ == "__main__":
136 main()