Debian Codegraph @

ansible_collections/community/general/plugins/modules/keycloak_clientsecret_info.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_info
11
12 short_description: Retrieve client secret using Keycloak API
13
14 version_added: 6.1.0
15
16 description:
17 - This module allows you to get a Keycloak client secret using the Keycloak REST API. It requires access to the REST API
18 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 retrieving a new client secret, where possible provide the client's O(id) (not O(client_id)) to the module. This
22 removes a lookup to the API to translate the O(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 action_group:
27 version_added: 10.2.0
28
29 options:
30 realm:
31 type: str
32 description:
33 - They Keycloak realm under which this client resides.
34 default: 'master'
35
36 id:
37 description:
38 - The unique identifier for this client.
39 - This parameter is not required for getting or generating a client secret but providing it reduces the number of API
40 calls required.
41 type: str
42
43 client_id:
44 description:
45 - The O(client_id) of the client. Passing this instead of O(id) results in an extra API call.
46 aliases:
47 - clientId
48 type: str
49
50
51 extends_documentation_fragment:
52 - community.general._keycloak
53 - community.general._keycloak.actiongroup_keycloak
54 - community.general._attributes
55 - community.general._attributes.info_module
56
57 author:
58 - Fynn Chen (@fynncfchen)
59 - John Cant (@johncant)
60 """
61
62 EXAMPLES = r"""
63 - name: Get a Keycloak client secret, authentication with credentials
64 community.general.keycloak_clientsecret_info:
65 id: '9d59aa76-2755-48c6-b1af-beb70a82c3cd'
66 realm: MyCustomRealm
67 auth_client_id: admin-cli
68 auth_keycloak_url: https://auth.example.com/auth
69 auth_realm: master
70 auth_username: USERNAME
71 auth_password: PASSWORD
72 delegate_to: localhost
73 no_log: true
74
75 - name: Get a new Keycloak client secret, authentication with token
76 community.general.keycloak_clientsecret_info:
77 id: '9d59aa76-2755-48c6-b1af-beb70a82c3cd'
78 realm: MyCustomRealm
79 auth_client_id: admin-cli
80 auth_keycloak_url: https://auth.example.com/auth
81 token: TOKEN
82 delegate_to: localhost
83 no_log: true
84
85 - name: Get a new Keycloak client secret, passing client_id instead of id
86 community.general.keycloak_clientsecret_info:
87 client_id: 'myClientId'
88 realm: MyCustomRealm
89 auth_client_id: admin-cli
90 auth_keycloak_url: https://auth.example.com/auth
91 token: TOKEN
92 delegate_to: localhost
93 no_log: true
94
95 - name: Get a new Keycloak client secret, authentication with auth_client_id and auth_client_secret
96 community.general.keycloak_clientsecret_info:
97 id: '9d59aa76-2755-48c6-b1af-beb70a82c3cd'
98 realm: MyCustomRealm
99 auth_client_id: admin-cli
100 auth_client_secret: SECRET
101 auth_keycloak_url: https://auth.example.com/auth
102 delegate_to: localhost
103 no_log: true
104 """
105
106 RETURN = r"""
107 msg:
108 description: Textual description of whether we succeeded or failed.
109 returned: always
110 type: str
111
112 clientsecret_info:
113 description: Representation of the client secret.
114 returned: on success
115 type: complex
116 contains:
117 type:
118 description: Credential type.
119 type: str
120 returned: always
121 sample: secret
122 value:
123 description: Client secret.
124 type: str
125 returned: always
126 sample: cUGnX1EIeTtPPAkcyGMv0ncyqDPu68P1
127 """
128
129 from ansible_collections.community.general.plugins.module_utils._keycloak import (
130 KeycloakAPI,
131 KeycloakError,
132 get_token,
133 )
134 from ansible_collections.community.general.plugins.module_utils._keycloak_clientsecret import (
135 keycloak_clientsecret_module,
136 keycloak_clientsecret_module_resolve_params,
137 )
138
139
140 def main():
141 """
142 Module keycloak_clientsecret_info
143
144 :return:
145 """
146
147 module = keycloak_clientsecret_module()
148
149 # Obtain access token, initialize API
150 try:
151 connection_header = get_token(module.params)
152 except KeycloakError as e:
153 module.fail_json(msg=str(e))
154
155 kc = KeycloakAPI(module, connection_header)
156
157 id, realm = keycloak_clientsecret_module_resolve_params(module, kc)
158
159 clientsecret = kc.get_clientsecret(id=id, realm=realm)
160
161 result = {"clientsecret_info": clientsecret, "msg": f"Get client secret successful for ID {id}"}
162
163 module.exit_json(**result)
164
165
166 if __name__ == "__main__":
167 main()