Debian Codegraph @

ansible_collections/community/general/plugins/module_utils/_keycloak_clientsecret.py

raw
1 #!/usr/bin/env python
2 # Copyright (c) 2022, John Cant <[email protected]>
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
7 # Do not use this from other collections or standalone plugins/modules!
8
9 from __future__ import annotations
10
11 import typing as t
12
13 from ansible.module_utils.basic import AnsibleModule
14
15 from ansible_collections.community.general.plugins.module_utils._keycloak import (
16 KeycloakAPI,
17 keycloak_argument_spec,
18 )
19
20
21 def keycloak_clientsecret_module() -> AnsibleModule:
22 """
23 Returns an AnsibleModule definition for modules that interact with a client
24 secret.
25
26 :return: argument_spec dict
27 """
28 argument_spec = keycloak_argument_spec()
29
30 meta_args = dict(
31 realm=dict(default="master"),
32 id=dict(type="str"),
33 client_id=dict(type="str", aliases=["clientId"]),
34 )
35
36 argument_spec.update(meta_args)
37
38 module = AnsibleModule(
39 argument_spec=argument_spec,
40 supports_check_mode=True,
41 required_one_of=(
42 [
43 ["id", "client_id"],
44 ["token", "auth_realm", "auth_username", "auth_password", "auth_client_id", "auth_client_secret"],
45 ]
46 ),
47 required_together=([["auth_username", "auth_password"]]),
48 mutually_exclusive=[["token", "auth_realm"], ["token", "auth_username"], ["token", "auth_password"]],
49 )
50
51 return module
52
53
54 def keycloak_clientsecret_module_resolve_params(module: AnsibleModule, kc: KeycloakAPI) -> tuple[str, dict[str, t.Any]]:
55 """
56 Given an AnsibleModule definition for keycloak_clientsecret_*, and a
57 KeycloakAPI client, resolve the params needed to interact with the Keycloak
58 client secret, looking up the client by clientId if necessary via an API
59 call.
60
61 :return: tuple of id, realm
62 """
63
64 realm = module.params.get("realm")
65 id = module.params.get("id")
66 client_id = module.params.get("client_id")
67
68 # only lookup the client_id if id isn't provided.
69 # in the case that both are provided, prefer the ID, since it is one
70 # less lookup.
71 if id is None:
72 # Due to the required_one_of spec, client_id is guaranteed to not be None
73 client = kc.get_client_by_clientid(client_id, realm=realm)
74
75 if client is None:
76 module.fail_json(msg=f"Client does not exist {client_id}")
77
78 id = client["id"]
79
80 return id, realm