Debian Codegraph @

ansible_collections/community/proxmox/plugins/modules/proxmox_access_acl.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2025, Markus Kötter <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-FileCopyrightText: (c) 2025, Markus Kötter <[email protected]>
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8
9 DOCUMENTATION = r"""
10 ---
11 module: proxmox_access_acl
12 short_description: Manages ACLs on the Proxmox PVE cluster
13 version_added: "1.1.0"
14 author:
15 - Markus Kötter (@commonism)
16 description:
17 - Setting ACLs via C(/access/acls) to grant permission to interact with objects.
18 attributes:
19 check_mode:
20 support: none
21 diff_mode:
22 support: none
23 options:
24 state:
25 description:
26 - Indicate desired state of the ACL.
27 type: str
28 choices: ["present", "absent"]
29 default: present
30 path:
31 description:
32 - Access Control Path.
33 type: str
34 roleid:
35 description:
36 - The name of the role.
37 type: str
38 type:
39 description:
40 - Type of access control.
41 choices: ["user", "group", "token"]
42 type: str
43 ugid:
44 description:
45 - The ID of user or group.
46 type: str
47 propagate:
48 description:
49 - Allow to propagate (inherit) permissions.
50 type: bool
51 default: true
52 extends_documentation_fragment:
53 - community.proxmox.proxmox.actiongroup_proxmox
54 - community.proxmox.proxmox.documentation
55 - community.proxmox.attributes
56 """
57
58 EXAMPLES = r"""
59 - name: Create ACE
60 community.proxmox.proxmox_access_acl:
61 state: "present"
62 path: /vms/100
63 type: user
64 ugid: "a01mako@pam"
65 roleid: PVEVMUser
66 propagate: 1
67
68 - name: Delete all ACEs for a given path
69 community.proxmox.proxmox_access_acl:
70 state: "absent"
71 path: /vms/100
72 """
73
74 RETURN = r"""
75 # These are examples of possible return values, and in general should use other names for return values.
76 old_acls:
77 description: The original name param that was passed in.
78 type: list
79 returned: always
80 new_acls:
81 description: The output message that the test module generates.
82 type: list
83 returned: when changed
84 """
85
86 from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
87 ProxmoxAnsible,
88 ansible_to_proxmox_bool,
89 create_proxmox_module,
90 proxmox_to_ansible_bool,
91 )
92
93
94 def _ace_matches(ace, desired):
95 if ace["path"] != desired["path"]:
96 return False
97 roleid = desired.get("roleid")
98 if roleid and ace["roleid"] != roleid:
99 return False
100 ace_type = desired.get("type")
101 if ace_type and ace["type"] != ace_type:
102 return False
103 ugid = desired.get("ugid")
104 if ugid and ace["ugid"] != ugid:
105 return False
106 propagate = desired.get("propagate")
107 if propagate:
108 ace_propagate = proxmox_to_ansible_bool(ace.get("propagate", 1))
109 if ace_propagate != propagate:
110 return False
111 return True
112
113
114 def _build_put_payload(ace_data, delete=False):
115 payload = {
116 "path": ace_data["path"],
117 "roles": ace_data["roleid"],
118 "propagate": ace_data["propagate"],
119 f"{ace_data['type']}s": ace_data["ugid"],
120 }
121 if delete:
122 payload["delete"] = 1
123 return payload
124
125
126 def module_args():
127 return dict(
128 state=dict(choices=["present", "absent"], default="present"),
129 path=dict(type="str", required=False),
130 roleid=dict(type="str", required=False),
131 type=dict(type="str", choices=["user", "group", "token"]),
132 ugid=dict(type="str"),
133 propagate=dict(type="bool", default=True),
134 )
135
136
137 def module_options():
138 return dict(
139 supports_check_mode=False,
140 required_if=[
141 ["state", "present", ["path", "roleid", "type", "ugid"]],
142 ["state", "absent", ["path"]],
143 ],
144 )
145
146
147 class ProxmoxAccessACLAnsible(ProxmoxAnsible):
148 def _get_acls(self):
149 return self.proxmox_api.access.acl.get()
150
151 def _put_acl(self, **data):
152 return self.proxmox_api.access.acl.put(**data)
153
154 def _filter_matching_aces(self, existing_acls, desired):
155 return [ace for ace in existing_acls if _ace_matches(ace, desired)]
156
157 def create(self, existing_acls, desired):
158 if self._filter_matching_aces(existing_acls, desired):
159 return False
160
161 payload = _build_put_payload(
162 {
163 "path": desired["path"],
164 "roleid": desired["roleid"],
165 "type": desired["type"],
166 "ugid": desired["ugid"],
167 "propagate": ansible_to_proxmox_bool(desired["propagate"]),
168 },
169 delete=False,
170 )
171 self._put_acl(**payload)
172 return True
173
174 def delete(self, existing_acls, desired):
175 to_remove = self._filter_matching_aces(existing_acls, desired)
176
177 if not to_remove:
178 return False
179
180 for ace in to_remove:
181 payload = _build_put_payload(
182 {
183 "path": ace["path"],
184 "roleid": ace["roleid"],
185 "type": ace["type"],
186 "ugid": ace["ugid"],
187 "propagate": ace.get("propagate", 1),
188 },
189 delete=True,
190 )
191 self._put_acl(**payload)
192 return True
193
194
195 def run_module():
196 module = create_proxmox_module(module_args(), **module_options())
197 proxmox = ProxmoxAccessACLAnsible(module)
198
199 state = module.params.get("state")
200 path = module.params["path"]
201 roleid = module.params["roleid"]
202 ace_type = module.params["type"]
203 ugid = module.params["ugid"]
204 propagate = module.params["propagate"]
205
206 result = dict(
207 changed=False,
208 old_acls=[],
209 )
210
211 try:
212 result["old_acls"] = existing_acls = proxmox._get_acls()
213 desired_ace = dict(path=path, roleid=roleid, type=ace_type, ugid=ugid, propagate=propagate)
214
215 if state == "present":
216 r = proxmox.create(existing_acls, desired_ace)
217 elif state == "absent":
218 r = proxmox.delete(existing_acls, desired_ace)
219
220 result["changed"] = r
221 if r:
222 result["new_acls"] = proxmox._get_acls()
223 except Exception as e:
224 module.fail_json(msg=str(e), **result)
225
226 module.exit_json(**result)
227
228
229 def main():
230 run_module()
231
232
233 if __name__ == "__main__":
234 main()