| 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_token |
| 11 |
short_description: Manipulate Consul tokens |
| 12 |
version_added: 8.3.0 |
| 13 |
description: |
| 14 |
- Allows the addition, modification and deletion of tokens in a Consul cluster using the agent. For more details on using |
| 15 |
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.token |
| 21 |
- community.general._consul.actiongroup_consul |
| 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 |
action_group: |
| 31 |
version_added: 8.3.0 |
| 32 |
options: |
| 33 |
state: |
| 34 |
description: |
| 35 |
- Whether the token should be present or absent. |
| 36 |
choices: ['present', 'absent'] |
| 37 |
default: present |
| 38 |
type: str |
| 39 |
accessor_id: |
| 40 |
description: |
| 41 |
- Specifies a UUID to use as the token's Accessor ID. If not specified a UUID is generated for this field. |
| 42 |
type: str |
| 43 |
secret_id: |
| 44 |
description: |
| 45 |
- Specifies a UUID to use as the token's Secret ID. If not specified a UUID is generated for this field. |
| 46 |
type: str |
| 47 |
description: |
| 48 |
description: |
| 49 |
- Free form human readable description of the token. |
| 50 |
type: str |
| 51 |
policies: |
| 52 |
type: list |
| 53 |
elements: dict |
| 54 |
description: |
| 55 |
- List of policies to attach to the token. Each policy is a dict. |
| 56 |
- If the parameter is left blank, any policies currently assigned are not changed. |
| 57 |
- Any empty array (V([])) clears any policies previously set. |
| 58 |
suboptions: |
| 59 |
name: |
| 60 |
description: |
| 61 |
- The name of the policy to attach to this token; see M(community.general.consul_policy) for more info. |
| 62 |
- Either this or O(policies[].id) must be specified. |
| 63 |
type: str |
| 64 |
id: |
| 65 |
description: |
| 66 |
- The ID of the policy to attach to this token; see M(community.general.consul_policy) for more info. |
| 67 |
- Either this or O(policies[].name) must be specified. |
| 68 |
type: str |
| 69 |
roles: |
| 70 |
type: list |
| 71 |
elements: dict |
| 72 |
description: |
| 73 |
- List of roles to attach to the token. Each role is a dict. |
| 74 |
- If the parameter is left blank, any roles currently assigned are not changed. |
| 75 |
- Any empty array (V([])) clears any roles previously set. |
| 76 |
suboptions: |
| 77 |
name: |
| 78 |
description: |
| 79 |
- The name of the role to attach to this token; see M(community.general.consul_role) for more info. |
| 80 |
- Either this or O(roles[].id) must be specified. |
| 81 |
type: str |
| 82 |
id: |
| 83 |
description: |
| 84 |
- The ID of the role to attach to this token; see M(community.general.consul_role) for more info. |
| 85 |
- Either this or O(roles[].name) must be specified. |
| 86 |
type: str |
| 87 |
templated_policies: |
| 88 |
description: |
| 89 |
- The list of templated policies that should be applied to the role. |
| 90 |
type: list |
| 91 |
elements: dict |
| 92 |
suboptions: |
| 93 |
template_name: |
| 94 |
description: |
| 95 |
- The templated policy name. |
| 96 |
type: str |
| 97 |
required: true |
| 98 |
template_variables: |
| 99 |
description: |
| 100 |
- The templated policy variables. |
| 101 |
- Not all templated policies require variables. |
| 102 |
type: dict |
| 103 |
service_identities: |
| 104 |
type: list |
| 105 |
elements: dict |
| 106 |
description: |
| 107 |
- List of service identities to attach to the token. |
| 108 |
- If not specified, any service identities currently assigned are not changed. |
| 109 |
- If the parameter is an empty array (V([])), any node identities assigned are unassigned. |
| 110 |
suboptions: |
| 111 |
service_name: |
| 112 |
description: |
| 113 |
- The name of the service. |
| 114 |
- Must not be longer than 256 characters, must start and end with a lowercase alphanumeric character. |
| 115 |
- May only contain lowercase alphanumeric characters as well as V(-) and V(_). |
| 116 |
type: str |
| 117 |
required: true |
| 118 |
datacenters: |
| 119 |
description: |
| 120 |
- The datacenters where the token is effective. |
| 121 |
- If an empty array (V([])) is specified, the token is valid in all datacenters. |
| 122 |
- Including those which do not yet exist but may in the future. |
| 123 |
type: list |
| 124 |
elements: str |
| 125 |
node_identities: |
| 126 |
type: list |
| 127 |
elements: dict |
| 128 |
description: |
| 129 |
- List of node identities to attach to the token. |
| 130 |
- If not specified, any node identities currently assigned are not changed. |
| 131 |
- If the parameter is an empty array (V([])), any node identities assigned are unassigned. |
| 132 |
suboptions: |
| 133 |
node_name: |
| 134 |
description: |
| 135 |
- The name of the node. |
| 136 |
- Must not be longer than 256 characters, must start and end with a lowercase alphanumeric character. |
| 137 |
- May only contain lowercase alphanumeric characters as well as V(-) and V(_). |
| 138 |
type: str |
| 139 |
required: true |
| 140 |
datacenter: |
| 141 |
description: |
| 142 |
- The nodes datacenter. |
| 143 |
- This results in effective token only being valid in this datacenter. |
| 144 |
type: str |
| 145 |
required: true |
| 146 |
local: |
| 147 |
description: |
| 148 |
- If true, indicates that the token should not be replicated globally and instead be local to the current datacenter. |
| 149 |
type: bool |
| 150 |
expiration_ttl: |
| 151 |
description: |
| 152 |
- This is a convenience field and if set it initializes the C(expiration_time). Can be specified in the form of V(60s) |
| 153 |
or V(5m) (that is, 60 seconds or 5 minutes, respectively). Ingored when the token is updated! |
| 154 |
type: str |
| 155 |
""" |
| 156 |
|
| 157 |
EXAMPLES = r""" |
| 158 |
- name: Create / Update a token by accessor_id |
| 159 |
community.general.consul_token: |
| 160 |
state: present |
| 161 |
accessor_id: 07a7de84-c9c7-448a-99cc-beaf682efd21 |
| 162 |
token: 8adddd91-0bd6-d41d-ae1a-3b49cfa9a0e8 |
| 163 |
roles: |
| 164 |
- name: role1 |
| 165 |
- name: role2 |
| 166 |
service_identities: |
| 167 |
- service_name: service1 |
| 168 |
datacenters: [dc1, dc2] |
| 169 |
node_identities: |
| 170 |
- node_name: node1 |
| 171 |
datacenter: dc1 |
| 172 |
expiration_ttl: 50m |
| 173 |
|
| 174 |
- name: Delete a token |
| 175 |
community.general.consul_token: |
| 176 |
state: absent |
| 177 |
accessor_id: 07a7de84-c9c7-448a-99cc-beaf682efd21 |
| 178 |
token: 8adddd91-0bd6-d41d-ae1a-3b49cfa9a0e8 |
| 179 |
""" |
| 180 |
|
| 181 |
RETURN = r""" |
| 182 |
token: |
| 183 |
description: The token as returned by the Consul HTTP API. |
| 184 |
returned: always |
| 185 |
type: dict |
| 186 |
sample: |
| 187 |
AccessorID: 07a7de84-c9c7-448a-99cc-beaf682efd21 |
| 188 |
CreateIndex: 632 |
| 189 |
CreateTime: "2024-01-14T21:53:01.402749174+01:00" |
| 190 |
Description: Testing |
| 191 |
Hash: rj5PeDHddHslkpW7Ij4OD6N4bbSXiecXFmiw2SYXg2A= |
| 192 |
Local: false |
| 193 |
ModifyIndex: 633 |
| 194 |
SecretID: bd380fba-da17-7cee-8576-8d6427c6c930 |
| 195 |
ServiceIdentities: ["ServiceName": "test"] |
| 196 |
operation: |
| 197 |
description: The operation performed. |
| 198 |
returned: changed |
| 199 |
type: str |
| 200 |
sample: update |
| 201 |
""" |
| 202 |
|
| 203 |
from ansible.module_utils.basic import AnsibleModule |
| 204 |
|
| 205 |
from ansible_collections.community.general.plugins.module_utils._consul import ( |
| 206 |
AUTH_ARGUMENTS_SPEC, |
| 207 |
_ConsulModule, |
| 208 |
) |
| 209 |
|
| 210 |
|
| 211 |
def normalize_link_obj(api_obj, module_obj, key): |
| 212 |
api_objs = api_obj.get(key) |
| 213 |
module_objs = module_obj.get(key) |
| 214 |
if api_objs is None or module_objs is None: |
| 215 |
return |
| 216 |
name_to_id = {i["Name"]: i["ID"] for i in api_objs} |
| 217 |
id_to_name = {i["ID"]: i["Name"] for i in api_objs} |
| 218 |
|
| 219 |
for obj in module_objs: |
| 220 |
identifier = obj.get("ID") |
| 221 |
name = obj.get("Name") |
| 222 |
if identifier and not name and identifier in id_to_name: |
| 223 |
obj["Name"] = id_to_name[identifier] |
| 224 |
if not identifier and name and name in name_to_id: |
| 225 |
obj["ID"] = name_to_id[name] |
| 226 |
|
| 227 |
|
| 228 |
class ConsulTokenModule(_ConsulModule): |
| 229 |
api_endpoint = "acl/token" |
| 230 |
result_key = "token" |
| 231 |
unique_identifiers = ["accessor_id"] |
| 232 |
|
| 233 |
create_only_fields = {"expiration_ttl"} |
| 234 |
|
| 235 |
def read_object(self): |
| 236 |
# if `accessor_id` is not supplied we can only create objects and are not idempotent |
| 237 |
if not self.id_from_obj(self.params): |
| 238 |
return None |
| 239 |
return super().read_object() |
| 240 |
|
| 241 |
def needs_update(self, api_obj, module_obj): |
| 242 |
# SecretID is usually not supplied |
| 243 |
if "SecretID" not in module_obj and "SecretID" in api_obj: |
| 244 |
del api_obj["SecretID"] |
| 245 |
normalize_link_obj(api_obj, module_obj, "Roles") |
| 246 |
normalize_link_obj(api_obj, module_obj, "Policies") |
| 247 |
# ExpirationTTL is only supported on create, not for update |
| 248 |
# it writes to ExpirationTime, so we need to remove that as well |
| 249 |
if "ExpirationTTL" in module_obj: |
| 250 |
del module_obj["ExpirationTTL"] |
| 251 |
return super().needs_update(api_obj, module_obj) |
| 252 |
|
| 253 |
|
| 254 |
NAME_ID_SPEC = dict( |
| 255 |
name=dict(type="str"), |
| 256 |
id=dict(type="str"), |
| 257 |
) |
| 258 |
|
| 259 |
NODE_ID_SPEC = dict( |
| 260 |
node_name=dict(type="str", required=True), |
| 261 |
datacenter=dict(type="str", required=True), |
| 262 |
) |
| 263 |
|
| 264 |
SERVICE_ID_SPEC = dict( |
| 265 |
service_name=dict(type="str", required=True), |
| 266 |
datacenters=dict(type="list", elements="str"), |
| 267 |
) |
| 268 |
|
| 269 |
TEMPLATE_POLICY_SPEC = dict( |
| 270 |
template_name=dict(type="str", required=True), |
| 271 |
template_variables=dict(type="dict"), |
| 272 |
) |
| 273 |
|
| 274 |
|
| 275 |
_ARGUMENT_SPEC = { |
| 276 |
"description": dict(), |
| 277 |
"accessor_id": dict(), |
| 278 |
"secret_id": dict(no_log=True), |
| 279 |
"roles": dict( |
| 280 |
type="list", |
| 281 |
elements="dict", |
| 282 |
options=NAME_ID_SPEC, |
| 283 |
mutually_exclusive=[("name", "id")], |
| 284 |
required_one_of=[("name", "id")], |
| 285 |
), |
| 286 |
"policies": dict( |
| 287 |
type="list", |
| 288 |
elements="dict", |
| 289 |
options=NAME_ID_SPEC, |
| 290 |
mutually_exclusive=[("name", "id")], |
| 291 |
required_one_of=[("name", "id")], |
| 292 |
), |
| 293 |
"templated_policies": dict( |
| 294 |
type="list", |
| 295 |
elements="dict", |
| 296 |
options=TEMPLATE_POLICY_SPEC, |
| 297 |
), |
| 298 |
"node_identities": dict( |
| 299 |
type="list", |
| 300 |
elements="dict", |
| 301 |
options=NODE_ID_SPEC, |
| 302 |
), |
| 303 |
"service_identities": dict( |
| 304 |
type="list", |
| 305 |
elements="dict", |
| 306 |
options=SERVICE_ID_SPEC, |
| 307 |
), |
| 308 |
"local": dict(type="bool"), |
| 309 |
"expiration_ttl": dict(type="str"), |
| 310 |
"state": dict(default="present", choices=["present", "absent"]), |
| 311 |
} |
| 312 |
_ARGUMENT_SPEC.update(AUTH_ARGUMENTS_SPEC) |
| 313 |
|
| 314 |
|
| 315 |
def main(): |
| 316 |
module = AnsibleModule( |
| 317 |
_ARGUMENT_SPEC, |
| 318 |
required_if=[("state", "absent", ["accessor_id"])], |
| 319 |
supports_check_mode=True, |
| 320 |
) |
| 321 |
consul_module = ConsulTokenModule(module) |
| 322 |
consul_module.execute() |
| 323 |
|
| 324 |
|
| 325 |
if __name__ == "__main__": |
| 326 |
main() |