Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # Copyright (c) 2017, Ansible Project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 module: ipa_hbacrule
10 author: Thomas Krahn (@Nosmoht)
11 short_description: Manage FreeIPA HBAC rule
12 description:
13 - Add, modify or delete an IPA HBAC rule using IPA API.
14 attributes:
15 check_mode:
16 support: full
17 diff_mode:
18 support: none
19 options:
20 cn:
21 description:
22 - Canonical name.
23 - Can not be changed as it is the unique identifier.
24 required: true
25 aliases: ["name"]
26 type: str
27 description:
28 description: Description.
29 type: str
30 host:
31 description:
32 - List of host names to assign.
33 - If an empty list is passed all hosts are removed from the rule.
34 - If option is omitted hosts are not checked or changed.
35 type: list
36 elements: str
37 hostcategory:
38 description: Host category.
39 choices: ['all']
40 type: str
41 hostgroup:
42 description:
43 - List of hostgroup names to assign.
44 - If an empty list is passed all hostgroups are removed from the rule.
45 - If option is omitted hostgroups are not checked or changed.
46 type: list
47 elements: str
48 service:
49 description:
50 - List of service names to assign.
51 - If an empty list is passed all services are removed from the rule.
52 - If option is omitted services are not checked or changed.
53 type: list
54 elements: str
55 servicecategory:
56 description: Service category.
57 choices: ['all']
58 type: str
59 servicegroup:
60 description:
61 - List of service group names to assign.
62 - If an empty list is passed all assigned service groups are removed from the rule.
63 - If option is omitted service groups are not checked or changed.
64 type: list
65 elements: str
66 sourcehost:
67 description:
68 - List of source host names to assign.
69 - If an empty list if passed all assigned source hosts are removed from the rule.
70 - If option is omitted source hosts are not checked or changed.
71 type: list
72 elements: str
73 sourcehostcategory:
74 description: Source host category.
75 choices: ['all']
76 type: str
77 sourcehostgroup:
78 description:
79 - List of source host group names to assign.
80 - If an empty list if passed all assigned source host groups are removed from the rule.
81 - If option is omitted source host groups are not checked or changed.
82 type: list
83 elements: str
84 state:
85 description: State to ensure.
86 default: "present"
87 choices: ["absent", "disabled", "enabled", "present"]
88 type: str
89 user:
90 description:
91 - List of user names to assign.
92 - If an empty list if passed all assigned users are removed from the rule.
93 - If option is omitted users are not checked or changed.
94 type: list
95 elements: str
96 usercategory:
97 description: User category.
98 choices: ['all']
99 type: str
100 usergroup:
101 description:
102 - List of user group names to assign.
103 - If an empty list if passed all assigned user groups are removed from the rule.
104 - If option is omitted user groups are not checked or changed.
105 type: list
106 elements: str
107 extends_documentation_fragment:
108 - community.general._ipa.documentation
109 - community.general._ipa.connection_notes
110 - community.general._attributes
111 """
112
113 EXAMPLES = r"""
114 - name: Ensure rule to allow all users to access any host from any host
115 community.general.ipa_hbacrule:
116 name: allow_all
117 description: Allow all users to access any host from any host
118 hostcategory: all
119 servicecategory: all
120 usercategory: all
121 state: present
122 ipa_host: ipa.example.com
123 ipa_user: admin
124 ipa_pass: topsecret
125
126 - name: Ensure rule with certain limitations
127 community.general.ipa_hbacrule:
128 name: allow_all_developers_access_to_db
129 description: Allow all developers to access any database from any host
130 hostgroup:
131 - db-server
132 usergroup:
133 - developers
134 state: present
135 ipa_host: ipa.example.com
136 ipa_user: admin
137 ipa_pass: topsecret
138
139 - name: Ensure rule is absent
140 community.general.ipa_hbacrule:
141 name: rule_to_be_deleted
142 state: absent
143 ipa_host: ipa.example.com
144 ipa_user: admin
145 ipa_pass: topsecret
146 """
147
148 RETURN = r"""
149 hbacrule:
150 description: HBAC rule as returned by IPA API.
151 returned: always
152 type: dict
153 """
154
155 import traceback
156
157 from ansible.module_utils.basic import AnsibleModule
158
159 from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec
160 from ansible_collections.community.general.plugins.module_utils._version import LooseVersion
161
162
163 class HBACRuleIPAClient(IPAClient):
164 def __init__(self, module, host, port, protocol):
165 super().__init__(module, host, port, protocol)
166
167 def hbacrule_find(self, name):
168 return self._post_json(method="hbacrule_find", name=None, item={"all": True, "cn": name})
169
170 def hbacrule_add(self, name, item):
171 return self._post_json(method="hbacrule_add", name=name, item=item)
172
173 def hbacrule_mod(self, name, item):
174 return self._post_json(method="hbacrule_mod", name=name, item=item)
175
176 def hbacrule_del(self, name):
177 return self._post_json(method="hbacrule_del", name=name)
178
179 def hbacrule_add_host(self, name, item):
180 return self._post_json(method="hbacrule_add_host", name=name, item=item)
181
182 def hbacrule_remove_host(self, name, item):
183 return self._post_json(method="hbacrule_remove_host", name=name, item=item)
184
185 def hbacrule_add_service(self, name, item):
186 return self._post_json(method="hbacrule_add_service", name=name, item=item)
187
188 def hbacrule_remove_service(self, name, item):
189 return self._post_json(method="hbacrule_remove_service", name=name, item=item)
190
191 def hbacrule_add_user(self, name, item):
192 return self._post_json(method="hbacrule_add_user", name=name, item=item)
193
194 def hbacrule_remove_user(self, name, item):
195 return self._post_json(method="hbacrule_remove_user", name=name, item=item)
196
197 def hbacrule_add_sourcehost(self, name, item):
198 return self._post_json(method="hbacrule_add_sourcehost", name=name, item=item)
199
200 def hbacrule_remove_sourcehost(self, name, item):
201 return self._post_json(method="hbacrule_remove_sourcehost", name=name, item=item)
202
203
204 def get_hbacrule_dict(
205 description=None,
206 hostcategory=None,
207 ipaenabledflag=None,
208 servicecategory=None,
209 sourcehostcategory=None,
210 usercategory=None,
211 ):
212 data = {}
213 if description is not None:
214 data["description"] = description
215 if hostcategory is not None:
216 data["hostcategory"] = hostcategory
217 if ipaenabledflag is not None:
218 data["ipaenabledflag"] = ipaenabledflag
219 if servicecategory is not None:
220 data["servicecategory"] = servicecategory
221 if sourcehostcategory is not None:
222 data["sourcehostcategory"] = sourcehostcategory
223 if usercategory is not None:
224 data["usercategory"] = usercategory
225 return data
226
227
228 def get_hbcarule_diff(client, ipa_hbcarule, module_hbcarule):
229 return client.get_diff(ipa_data=ipa_hbcarule, module_data=module_hbcarule)
230
231
232 def ensure(module, client):
233 name = module.params["cn"]
234 state = module.params["state"]
235
236 ipa_version = client.get_ipa_version()
237 if state in ["present", "enabled"]:
238 if LooseVersion(ipa_version) < LooseVersion("4.9.10"):
239 ipaenabledflag = "TRUE"
240 else:
241 ipaenabledflag = True
242 else:
243 if LooseVersion(ipa_version) < LooseVersion("4.9.10"):
244 ipaenabledflag = "FALSE"
245 else:
246 ipaenabledflag = False
247
248 host = module.params["host"]
249 hostcategory = module.params["hostcategory"]
250 hostgroup = module.params["hostgroup"]
251 service = module.params["service"]
252 servicecategory = module.params["servicecategory"]
253 servicegroup = module.params["servicegroup"]
254 sourcehost = module.params["sourcehost"]
255 sourcehostcategory = module.params["sourcehostcategory"]
256 sourcehostgroup = module.params["sourcehostgroup"]
257 user = module.params["user"]
258 usercategory = module.params["usercategory"]
259 usergroup = module.params["usergroup"]
260
261 module_hbacrule = get_hbacrule_dict(
262 description=module.params["description"],
263 hostcategory=hostcategory,
264 ipaenabledflag=ipaenabledflag,
265 servicecategory=servicecategory,
266 sourcehostcategory=sourcehostcategory,
267 usercategory=usercategory,
268 )
269 ipa_hbacrule = client.hbacrule_find(name=name)
270
271 changed = False
272 if state in ["present", "enabled", "disabled"]:
273 if not ipa_hbacrule:
274 changed = True
275 if not module.check_mode:
276 ipa_hbacrule = client.hbacrule_add(name=name, item=module_hbacrule)
277 else:
278 diff = get_hbcarule_diff(client, ipa_hbacrule, module_hbacrule)
279 if len(diff) > 0:
280 changed = True
281 if not module.check_mode:
282 data = {}
283 for key in diff:
284 data[key] = module_hbacrule.get(key)
285 client.hbacrule_mod(name=name, item=data)
286
287 if host is not None:
288 changed = (
289 client.modify_if_diff(
290 name,
291 ipa_hbacrule.get("memberhost_host", []),
292 host,
293 client.hbacrule_add_host,
294 client.hbacrule_remove_host,
295 "host",
296 )
297 or changed
298 )
299
300 if hostgroup is not None:
301 changed = (
302 client.modify_if_diff(
303 name,
304 ipa_hbacrule.get("memberhost_hostgroup", []),
305 hostgroup,
306 client.hbacrule_add_host,
307 client.hbacrule_remove_host,
308 "hostgroup",
309 )
310 or changed
311 )
312
313 if service is not None:
314 changed = (
315 client.modify_if_diff(
316 name,
317 ipa_hbacrule.get("memberservice_hbacsvc", []),
318 service,
319 client.hbacrule_add_service,
320 client.hbacrule_remove_service,
321 "hbacsvc",
322 )
323 or changed
324 )
325
326 if servicegroup is not None:
327 changed = (
328 client.modify_if_diff(
329 name,
330 ipa_hbacrule.get("memberservice_hbacsvcgroup", []),
331 servicegroup,
332 client.hbacrule_add_service,
333 client.hbacrule_remove_service,
334 "hbacsvcgroup",
335 )
336 or changed
337 )
338
339 if sourcehost is not None:
340 changed = (
341 client.modify_if_diff(
342 name,
343 ipa_hbacrule.get("sourcehost_host", []),
344 sourcehost,
345 client.hbacrule_add_sourcehost,
346 client.hbacrule_remove_sourcehost,
347 "host",
348 )
349 or changed
350 )
351
352 if sourcehostgroup is not None:
353 changed = (
354 client.modify_if_diff(
355 name,
356 ipa_hbacrule.get("sourcehost_group", []),
357 sourcehostgroup,
358 client.hbacrule_add_sourcehost,
359 client.hbacrule_remove_sourcehost,
360 "hostgroup",
361 )
362 or changed
363 )
364
365 if user is not None:
366 changed = (
367 client.modify_if_diff(
368 name,
369 ipa_hbacrule.get("memberuser_user", []),
370 user,
371 client.hbacrule_add_user,
372 client.hbacrule_remove_user,
373 "user",
374 )
375 or changed
376 )
377
378 if usergroup is not None:
379 changed = (
380 client.modify_if_diff(
381 name,
382 ipa_hbacrule.get("memberuser_group", []),
383 usergroup,
384 client.hbacrule_add_user,
385 client.hbacrule_remove_user,
386 "group",
387 )
388 or changed
389 )
390 else:
391 if ipa_hbacrule:
392 changed = True
393 if not module.check_mode:
394 client.hbacrule_del(name=name)
395
396 return changed, client.hbacrule_find(name=name)
397
398
399 def main():
400 argument_spec = ipa_argument_spec()
401 argument_spec.update(
402 cn=dict(type="str", required=True, aliases=["name"]),
403 description=dict(type="str"),
404 host=dict(type="list", elements="str"),
405 hostcategory=dict(type="str", choices=["all"]),
406 hostgroup=dict(type="list", elements="str"),
407 service=dict(type="list", elements="str"),
408 servicecategory=dict(type="str", choices=["all"]),
409 servicegroup=dict(type="list", elements="str"),
410 sourcehost=dict(type="list", elements="str"),
411 sourcehostcategory=dict(type="str", choices=["all"]),
412 sourcehostgroup=dict(type="list", elements="str"),
413 state=dict(type="str", default="present", choices=["present", "absent", "enabled", "disabled"]),
414 user=dict(type="list", elements="str"),
415 usercategory=dict(type="str", choices=["all"]),
416 usergroup=dict(type="list", elements="str"),
417 )
418
419 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
420
421 client = HBACRuleIPAClient(
422 module=module,
423 host=module.params["ipa_host"],
424 port=module.params["ipa_port"],
425 protocol=module.params["ipa_prot"],
426 )
427
428 try:
429 client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"])
430 changed, hbacrule = ensure(module, client)
431 module.exit_json(changed=changed, hbacrule=hbacrule)
432 except Exception as e:
433 module.fail_json(msg=f"{e}", exception=traceback.format_exc())
434
435
436 if __name__ == "__main__":
437 main()