Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024, Florian Apolloner (@apollo13)
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: consul_acl_bootstrap
11 short_description: Bootstrap ACLs in Consul
12 version_added: 8.3.0
13 description:
14 - Allows bootstrapping of ACLs in a Consul cluster, see U(https://developer.hashicorp.com/consul/api-docs/acl#bootstrap-acls)
15 for details.
16 author:
17 - Florian Apolloner (@apollo13)
18 extends_documentation_fragment:
19 - community.general._consul
20 - community.general._attributes
21 attributes:
22 check_mode:
23 support: none
24 diff_mode:
25 support: none
26 options:
27 state:
28 description:
29 - Whether the token should be present or absent.
30 choices: ['present', 'bootstrapped']
31 default: present
32 type: str
33 bootstrap_secret:
34 description:
35 - The secret to be used as secret ID for the initial token.
36 - Needs to be an UUID.
37 type: str
38 """
39
40 EXAMPLES = r"""
41 - name: Bootstrap the ACL system
42 community.general.consul_acl_bootstrap:
43 bootstrap_secret: 22eaeed1-bdbd-4651-724e-42ae6c43e387
44 """
45
46 RETURN = r"""
47 result:
48 description:
49 - The bootstrap result as returned by the Consul HTTP API.
50 - B(Note:) If O(bootstrap_secret) has been specified the C(SecretID) and C(ID) do not contain the secret but C(VALUE_SPECIFIED_IN_NO_LOG_PARAMETER).
51 If you pass O(bootstrap_secret), make sure your playbook/role does not depend on this return value!
52 returned: changed
53 type: dict
54 sample:
55 AccessorID: 834a5881-10a9-a45b-f63c-490e28743557
56 CreateIndex: 25
57 CreateTime: '2024-01-21T20:26:27.114612038+01:00'
58 Description: Bootstrap Token (Global Management)
59 Hash: X2AgaFhnQGRhSSF/h0m6qpX1wj/HJWbyXcxkEM/5GrY=
60 ID: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
61 Local: false
62 ModifyIndex: 25
63 Policies:
64 - ID: 00000000-0000-0000-0000-000000000001
65 Name: global-management
66 SecretID: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
67 """
68
69 from ansible.module_utils.basic import AnsibleModule
70
71 from ansible_collections.community.general.plugins.module_utils._consul import (
72 AUTH_ARGUMENTS_SPEC,
73 RequestError,
74 _ConsulModule,
75 )
76
77 _ARGUMENT_SPEC = {
78 "state": dict(type="str", choices=["present", "bootstrapped"], default="present"),
79 "bootstrap_secret": dict(type="str", no_log=True),
80 }
81 _ARGUMENT_SPEC.update(AUTH_ARGUMENTS_SPEC)
82 _ARGUMENT_SPEC.pop("token")
83
84
85 def main():
86 module = AnsibleModule(_ARGUMENT_SPEC)
87 consul_module = _ConsulModule(module)
88
89 data = {}
90 if "bootstrap_secret" in module.params:
91 data["BootstrapSecret"] = module.params["bootstrap_secret"]
92
93 try:
94 response = consul_module.put("acl/bootstrap", data=data)
95 except RequestError as e:
96 if e.status == 403 and b"ACL bootstrap no longer allowed" in e.response_data:
97 return module.exit_json(changed=False)
98 raise
99 else:
100 return module.exit_json(changed=True, result=response)
101
102
103 if __name__ == "__main__":
104 main()