Debian Codegraph @

ansible_collections/community/general/plugins/modules/consul_auth_method.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_auth_method
11 short_description: Manipulate Consul auth methods
12 version_added: 8.3.0
13 description:
14 - Allows the addition, modification and deletion of auth methods in a Consul cluster using the agent. For more details on
15 using and configuring ACLs, see U(https://www.consul.io/docs/guides/acl.html).
16 author:
17 - Florian Apolloner (@apollo13)
18 extends_documentation_fragment:
19 - community.general._consul
20 - community.general._consul.actiongroup_consul
21 - community.general._consul.token
22 - community.general._attributes
23 attributes:
24 check_mode:
25 support: full
26 diff_mode:
27 support: partial
28 details:
29 - In check mode the diff misses operational attributes.
30 options:
31 state:
32 description:
33 - Whether the token should be present or absent.
34 choices: ['present', 'absent']
35 default: present
36 type: str
37 name:
38 description:
39 - Specifies a name for the ACL auth method.
40 - The name can contain alphanumeric characters, dashes C(-), and underscores C(_).
41 type: str
42 required: true
43 type:
44 description:
45 - The type of auth method being configured.
46 - This field is immutable.
47 - Required when the auth method is created.
48 type: str
49 choices: ['kubernetes', 'jwt', 'oidc', 'aws-iam']
50 description:
51 description:
52 - Free form human readable description of the auth method.
53 type: str
54 display_name:
55 description:
56 - An optional name to use instead of O(name) when displaying information about this auth method.
57 type: str
58 max_token_ttl:
59 description:
60 - This specifies the maximum life of any token created by this auth method.
61 - Can be specified in the form of V(60s) or V(5m) (that is, 60 seconds or 5 minutes, respectively).
62 type: str
63 token_locality:
64 description:
65 - Defines the kind of token that this auth method should produce.
66 type: str
67 choices: ['local', 'global']
68 config:
69 description:
70 - The raw configuration to use for the chosen auth method.
71 - Contents vary depending upon the O(type) chosen.
72 - Required when the auth method is created.
73 type: dict
74 """
75
76 EXAMPLES = r"""
77 - name: Create an auth method
78 community.general.consul_auth_method:
79 name: test
80 type: jwt
81 config:
82 jwt_validation_pubkeys:
83 - |
84 -----BEGIN PUBLIC KEY-----
85 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu1SU1LfVLPHCozMxH2Mo
86 4lgOEePzNm0tRgeLezV6ffAt0gunVTLw7onLRnrq0/IzW7yWR7QkrmBL7jTKEn5u
87 +qKhbwKfBstIs+bMY2Zkp18gnTxKLxoS2tFczGkPLPgizskuemMghRniWaoLcyeh
88 kd3qqGElvW/VDL5AaWTg0nLVkjRo9z+40RQzuVaE8AkAFmxZzow3x+VJYKdjykkJ
89 0iT9wCS0DRTXu269V264Vf/3jvredZiKRkgwlL9xNAwxXFg0x/XFw005UWVRIkdg
90 cKWTjpBP2dPwVZ4WWC+9aGVd+Gyn1o0CLelf4rEjGoXbAAEgAqeGUxrcIlbjXfbc
91 mwIDAQAB
92 -----END PUBLIC KEY-----
93 token: "{{ consul_management_token }}"
94
95 - name: Delete auth method
96 community.general.consul_auth_method:
97 name: test
98 state: absent
99 token: "{{ consul_management_token }}"
100 """
101
102 RETURN = r"""
103 auth_method:
104 description: The auth method as returned by the Consul HTTP API.
105 returned: always
106 type: dict
107 sample:
108 Config:
109 JWTValidationPubkeys:
110 - |-
111 -----BEGIN PUBLIC KEY-----
112 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu1SU1LfVLPHCozMxH2Mo
113 4lgOEePzNm0tRgeLezV6ffAt0gunVTLw7onLRnrq0/IzW7yWR7QkrmBL7jTKEn5u
114 +qKhbwKfBstIs+bMY2Zkp18gnTxKLxoS2tFczGkPLPgizskuemMghRniWaoLcyeh
115 kd3qqGElvW/VDL5AaWTg0nLVkjRo9z+40RQzuVaE8AkAFmxZzow3x+VJYKdjykkJ
116 0iT9wCS0DRTXu269V264Vf/3jvredZiKRkgwlL9xNAwxXFg0x/XFw005UWVRIkdg
117 cKWTjpBP2dPwVZ4WWC+9aGVd+Gyn1o0CLelf4rEjGoXbAAEgAqeGUxrcIlbjXfbc
118 mwIDAQAB
119 -----END PUBLIC KEY-----
120 CreateIndex: 416
121 ModifyIndex: 487
122 Name: test
123 Type: jwt
124 operation:
125 description: The operation performed.
126 returned: changed
127 type: str
128 sample: update
129 """
130
131 import re
132
133 from ansible.module_utils.basic import AnsibleModule
134
135 from ansible_collections.community.general.plugins.module_utils._consul import (
136 AUTH_ARGUMENTS_SPEC,
137 _ConsulModule,
138 camel_case_key,
139 )
140
141
142 def normalize_ttl(ttl):
143 matches = re.findall(r"(\d+)(:h|m|s)", ttl)
144 ttl = 0
145 for value, unit in matches:
146 value = int(value)
147 if unit == "m":
148 value *= 60
149 elif unit == "h":
150 value *= 60 * 60
151 ttl += value
152
153 new_ttl = ""
154 hours, remainder = divmod(ttl, 3600)
155 if hours:
156 new_ttl += f"{hours}h"
157 minutes, seconds = divmod(remainder, 60)
158 if minutes:
159 new_ttl += f"{minutes}m"
160 if seconds:
161 new_ttl += f"{seconds}s"
162 return new_ttl
163
164
165 class ConsulAuthMethodModule(_ConsulModule):
166 api_endpoint = "acl/auth-method"
167 result_key = "auth_method"
168 unique_identifiers = ["name"]
169
170 def map_param(self, k, v, is_update):
171 if k == "config" and v:
172 v = {camel_case_key(k2): v2 for k2, v2 in v.items()}
173 return super().map_param(k, v, is_update)
174
175 def needs_update(self, api_obj, module_obj):
176 if "MaxTokenTTL" in module_obj:
177 module_obj["MaxTokenTTL"] = normalize_ttl(module_obj["MaxTokenTTL"])
178 return super().needs_update(api_obj, module_obj)
179
180
181 _ARGUMENT_SPEC = {
182 "name": dict(type="str", required=True),
183 "type": dict(type="str", choices=["kubernetes", "jwt", "oidc", "aws-iam"]),
184 "description": dict(type="str"),
185 "display_name": dict(type="str"),
186 "max_token_ttl": dict(type="str", no_log=False),
187 "token_locality": dict(type="str", choices=["local", "global"]),
188 "config": dict(type="dict"),
189 "state": dict(default="present", choices=["present", "absent"]),
190 }
191 _ARGUMENT_SPEC.update(AUTH_ARGUMENTS_SPEC)
192
193
194 def main():
195 module = AnsibleModule(
196 _ARGUMENT_SPEC,
197 supports_check_mode=True,
198 )
199 consul_module = ConsulAuthMethodModule(module)
200 consul_module.execute()
201
202
203 if __name__ == "__main__":
204 main()