| 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_binding_rule |
| 11 |
short_description: Manipulate Consul binding rules |
| 12 |
version_added: 8.3.0 |
| 13 |
description: |
| 14 |
- Allows the addition, modification and deletion of binding rules in a Consul cluster using the agent. For more details |
| 15 |
on using and configuring binding rules, see U(https://developer.hashicorp.com/consul/api-docs/acl/binding-rules). |
| 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 binding rule 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 binding rule. |
| 40 |
- 'Note: This is used to identify the binding rule. But since the API does not support a name, it is prefixed to the |
| 41 |
description.' |
| 42 |
type: str |
| 43 |
required: true |
| 44 |
description: |
| 45 |
description: |
| 46 |
- Free form human readable description of the binding rule. |
| 47 |
type: str |
| 48 |
auth_method: |
| 49 |
description: |
| 50 |
- The name of the auth method that this rule applies to. |
| 51 |
type: str |
| 52 |
required: true |
| 53 |
selector: |
| 54 |
description: |
| 55 |
- Specifies the expression used to match this rule against valid identities returned from an auth method validation. |
| 56 |
- If empty this binding rule matches all valid identities returned from the auth method. |
| 57 |
type: str |
| 58 |
bind_type: |
| 59 |
description: |
| 60 |
- Specifies the way the binding rule affects a token created at login. |
| 61 |
type: str |
| 62 |
choices: [service, node, role, templated-policy] |
| 63 |
bind_name: |
| 64 |
description: |
| 65 |
- The name to bind to a token at login-time. |
| 66 |
- What it binds to can be adjusted with different values of the O(bind_type) parameter. |
| 67 |
type: str |
| 68 |
bind_vars: |
| 69 |
description: |
| 70 |
- Specifies the templated policy variables when O(bind_type) is set to V(templated-policy). |
| 71 |
type: dict |
| 72 |
""" |
| 73 |
|
| 74 |
EXAMPLES = r""" |
| 75 |
- name: Create a binding rule |
| 76 |
community.general.consul_binding_rule: |
| 77 |
name: my_name |
| 78 |
description: example rule |
| 79 |
auth_method: minikube |
| 80 |
bind_type: service |
| 81 |
bind_name: "{{ serviceaccount.name }}" |
| 82 |
token: "{{ consul_management_token }}" |
| 83 |
|
| 84 |
- name: Remove a binding rule |
| 85 |
community.general.consul_binding_rule: |
| 86 |
name: my_name |
| 87 |
auth_method: minikube |
| 88 |
state: absent |
| 89 |
""" |
| 90 |
|
| 91 |
RETURN = r""" |
| 92 |
binding_rule: |
| 93 |
description: The binding rule as returned by the Consul HTTP API. |
| 94 |
returned: always |
| 95 |
type: dict |
| 96 |
sample: |
| 97 |
Description: "my_name: example rule" |
| 98 |
AuthMethod: minikube |
| 99 |
Selector: serviceaccount.namespace==default |
| 100 |
BindType: service |
| 101 |
BindName: "{{ serviceaccount.name }}" |
| 102 |
CreateIndex: 30 |
| 103 |
ID: 59c8a237-e481-4239-9202-45f117950c5f |
| 104 |
ModifyIndex: 33 |
| 105 |
operation: |
| 106 |
description: The operation performed. |
| 107 |
returned: changed |
| 108 |
type: str |
| 109 |
sample: update |
| 110 |
""" |
| 111 |
|
| 112 |
|
| 113 |
from ansible.module_utils.basic import AnsibleModule |
| 114 |
|
| 115 |
from ansible_collections.community.general.plugins.module_utils._consul import ( |
| 116 |
AUTH_ARGUMENTS_SPEC, |
| 117 |
RequestError, |
| 118 |
_ConsulModule, |
| 119 |
) |
| 120 |
|
| 121 |
|
| 122 |
class ConsulBindingRuleModule(_ConsulModule): |
| 123 |
api_endpoint = "acl/binding-rule" |
| 124 |
result_key = "binding_rule" |
| 125 |
unique_identifiers = ["id"] |
| 126 |
|
| 127 |
def read_object(self): |
| 128 |
url = f"acl/binding-rules?authmethod={self.params['auth_method']}" |
| 129 |
try: |
| 130 |
results = self.get(url) |
| 131 |
for result in results: |
| 132 |
if result.get("Description").startswith(f"{self.params['name']}: "): |
| 133 |
return result |
| 134 |
except RequestError as e: |
| 135 |
if e.status == 404: |
| 136 |
return |
| 137 |
elif e.status == 403 and b"ACL not found" in e.response_data: |
| 138 |
return |
| 139 |
raise |
| 140 |
|
| 141 |
def module_to_obj(self, is_update): |
| 142 |
obj = super().module_to_obj(is_update) |
| 143 |
del obj["Name"] |
| 144 |
return obj |
| 145 |
|
| 146 |
def prepare_object(self, existing, obj): |
| 147 |
final = super().prepare_object(existing, obj) |
| 148 |
name = self.params["name"] |
| 149 |
description = final.pop("Description", "").split(": ", 1)[-1] |
| 150 |
final["Description"] = f"{name}: {description}" |
| 151 |
return final |
| 152 |
|
| 153 |
|
| 154 |
_ARGUMENT_SPEC = { |
| 155 |
"name": dict(type="str", required=True), |
| 156 |
"description": dict(type="str"), |
| 157 |
"auth_method": dict(type="str", required=True), |
| 158 |
"selector": dict(type="str"), |
| 159 |
"bind_type": dict(type="str", choices=["service", "node", "role", "templated-policy"]), |
| 160 |
"bind_name": dict(type="str"), |
| 161 |
"bind_vars": dict(type="dict"), |
| 162 |
"state": dict(default="present", choices=["present", "absent"]), |
| 163 |
} |
| 164 |
_ARGUMENT_SPEC.update(AUTH_ARGUMENTS_SPEC) |
| 165 |
|
| 166 |
|
| 167 |
def main(): |
| 168 |
module = AnsibleModule( |
| 169 |
_ARGUMENT_SPEC, |
| 170 |
supports_check_mode=True, |
| 171 |
) |
| 172 |
consul_module = ConsulBindingRuleModule(module) |
| 173 |
consul_module.execute() |
| 174 |
|
| 175 |
|
| 176 |
if __name__ == "__main__": |
| 177 |
main() |