Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2022, Fynn Chen <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING 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_clientsecret_regenerate
11
12 short_description: Regenerate Keycloak client secret using Keycloak API
13
14 version_added: 6.1.0
15
16 description:
17 - This module allows you to regenerate a Keycloak client secret using the Keycloak REST API. It requires access to the REST
18 API using OpenID Connect; the user connecting and the client being used must have the requisite access rights. In a default
19 Keycloak installation, admin-cli and an admin user would work, as would a separate client definition with the scope tailored
20 to your needs and a user having the expected roles.
21 - When regenerating a client secret, where possible provide the client's ID (not client_id) to the module. This removes
22 a lookup to the API to translate the client_id into the client ID.
23 - 'Note that this module returns the client secret. To avoid this showing up in the logs, please add C(no_log: true) to
24 the task.'
25 attributes:
26 check_mode:
27 support: full
28 diff_mode:
29 support: none
30 action_group:
31 version_added: 10.2.0
32
33 options:
34 realm:
35 type: str
36 description:
37 - They Keycloak realm under which this client resides.
38 default: 'master'
39
40 id:
41 description:
42 - The unique identifier for this client.
43 - This parameter is not required for getting or generating a client secret but providing it reduces the number of API
44 calls required.
45 type: str
46
47 client_id:
48 description:
49 - The client_id of the client. Passing this instead of ID results in an extra API call.
50 aliases:
51 - clientId
52 type: str
53
54
55 extends_documentation_fragment:
56 - community.general._keycloak
57 - community.general._keycloak.actiongroup_keycloak
58 - community.general._attributes
59
60 author:
61 - Fynn Chen (@fynncfchen)
62 - John Cant (@johncant)
63 """
64
65 EXAMPLES = r"""
66 - name: Regenerate a Keycloak client secret, authentication with credentials
67 community.general.keycloak_clientsecret_regenerate:
68 id: '9d59aa76-2755-48c6-b1af-beb70a82c3cd'
69 realm: MyCustomRealm
70 auth_client_id: admin-cli
71 auth_keycloak_url: https://auth.example.com/auth
72 auth_realm: master
73 auth_username: USERNAME
74 auth_password: PASSWORD
75 delegate_to: localhost
76 no_log: true
77
78 - name: Regenerate a Keycloak client secret, authentication with token
79 community.general.keycloak_clientsecret_regenerate:
80 id: '9d59aa76-2755-48c6-b1af-beb70a82c3cd'
81 realm: MyCustomRealm
82 auth_client_id: admin-cli
83 auth_keycloak_url: https://auth.example.com/auth
84 token: TOKEN
85 delegate_to: localhost
86 no_log: true
87
88 - name: Regenerate a Keycloak client secret, passing client_id instead of id
89 community.general.keycloak_clientsecret_info:
90 client_id: 'myClientId'
91 realm: MyCustomRealm
92 auth_client_id: admin-cli
93 auth_keycloak_url: https://auth.example.com/auth
94 token: TOKEN
95 delegate_to: localhost
96 no_log: true
97
98 - name: Regenerate a new Keycloak client secret, authentication with auth_client_id and auth_client_secret
99 community.general.keycloak_clientsecret_regenerate:
100 id: '9d59aa76-2755-48c6-b1af-beb70a82c3cd'
101 realm: MyCustomRealm
102 auth_client_id: admin-cli
103 auth_client_secret: SECRET
104 auth_keycloak_url: https://auth.example.com/auth
105 delegate_to: localhost
106 no_log: true
107 """
108
109 RETURN = r"""
110 msg:
111 description: Message as to what action was taken.
112 returned: always
113 type: str
114
115 end_state:
116 description: Representation of the client credential after module execution.
117 returned: on success
118 type: dict
119 contains:
120 type:
121 description: Credential type.
122 type: str
123 returned: always
124 sample: secret
125 value:
126 description: Client secret.
127 type: str
128 returned: always
129 sample: cUGnX1EIeTtPPAkcyGMv0ncyqDPu68P1
130 """
131
132 from ansible_collections.community.general.plugins.module_utils._keycloak import (
133 KeycloakAPI,
134 KeycloakError,
135 get_token,
136 )
137 from ansible_collections.community.general.plugins.module_utils._keycloak_clientsecret import (
138 keycloak_clientsecret_module,
139 keycloak_clientsecret_module_resolve_params,
140 )
141
142
143 def main():
144 """
145 Module keycloak_clientsecret_regenerate
146
147 :return:
148 """
149
150 module = keycloak_clientsecret_module()
151
152 # Obtain access token, initialize API
153 try:
154 connection_header = get_token(module.params)
155 except KeycloakError as e:
156 module.fail_json(msg=str(e))
157
158 kc = KeycloakAPI(module, connection_header)
159
160 id, realm = keycloak_clientsecret_module_resolve_params(module, kc)
161
162 if module.check_mode:
163 dummy_result = {
164 "msg": "No action taken while in check mode",
165 "end_state": {"type": "secret", "value": "X" * 32},
166 }
167 module.exit_json(**dummy_result)
168
169 # Create new secret
170 clientsecret = kc.create_clientsecret(id=id, realm=realm)
171
172 result = {"msg": f"New client secret has been generated for ID {id}", "end_state": clientsecret}
173 module.exit_json(**result)
174
175
176 if __name__ == "__main__":
177 main()