Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # Copyright (c) 2018, Fran Fitzpatrick <[email protected]>
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_config
10 author: Fran Fitzpatrick (@fxfitz)
11 short_description: Manage Global FreeIPA Configuration Settings
12 description:
13 - Modify global configuration settings of a FreeIPA Server.
14 attributes:
15 check_mode:
16 support: full
17 diff_mode:
18 support: none
19 options:
20 ipaconfigstring:
21 description: Extra hashes to generate in password plug-in.
22 aliases: ["configstring"]
23 type: list
24 elements: str
25 choices: ["AllowNThash", "KDC:Disable Last Success", "KDC:Disable Lockout", "KDC:Disable Default Preauth for SPNs"]
26 version_added: '2.5.0'
27 ipadefaultloginshell:
28 description: Default shell for new users.
29 aliases: ["loginshell"]
30 type: str
31 ipadefaultemaildomain:
32 description: Default e-mail domain for new users.
33 aliases: ["emaildomain"]
34 type: str
35 ipadefaultprimarygroup:
36 description: Default group for new users.
37 aliases: ["primarygroup"]
38 type: str
39 version_added: '2.5.0'
40 ipagroupobjectclasses:
41 description: A list of group objectclasses.
42 aliases: ["groupobjectclasses"]
43 type: list
44 elements: str
45 version_added: '7.3.0'
46 ipagroupsearchfields:
47 description: A list of fields to search in when searching for groups.
48 aliases: ["groupsearchfields"]
49 type: list
50 elements: str
51 version_added: '2.5.0'
52 ipahomesrootdir:
53 description: Default location of home directories.
54 aliases: ["homesrootdir"]
55 type: str
56 version_added: '2.5.0'
57 ipakrbauthzdata:
58 description: Default types of PAC supported for services.
59 aliases: ["krbauthzdata"]
60 type: list
61 elements: str
62 choices: ["MS-PAC", "PAD", "nfs:NONE"]
63 version_added: '2.5.0'
64 ipamaxusernamelength:
65 description: Maximum length of usernames.
66 aliases: ["maxusernamelength"]
67 type: int
68 version_added: '2.5.0'
69 ipapwdexpadvnotify:
70 description: Notice of impending password expiration, in days.
71 aliases: ["pwdexpadvnotify"]
72 type: int
73 version_added: '2.5.0'
74 ipasearchrecordslimit:
75 description: Maximum number of records to search (-1 or 0 is unlimited).
76 aliases: ["searchrecordslimit"]
77 type: int
78 version_added: '2.5.0'
79 ipasearchtimelimit:
80 description: Maximum amount of time (seconds) for a search (-1 or 0 is unlimited).
81 aliases: ["searchtimelimit"]
82 type: int
83 version_added: '2.5.0'
84 ipaselinuxusermaporder:
85 description: The SELinux user map order (order in increasing priority of SELinux users).
86 aliases: ["selinuxusermaporder"]
87 type: list
88 elements: str
89 version_added: '3.7.0'
90 ipauserauthtype:
91 description:
92 - The authentication type to use by default.
93 - The choice V(idp) has been added in community.general 7.3.0.
94 - The choice V(passkey) has been added in community.general 8.1.0.
95 aliases: ["userauthtype"]
96 choices: ["password", "radius", "otp", "pkinit", "hardened", "idp", "passkey", "disabled"]
97 type: list
98 elements: str
99 version_added: '2.5.0'
100 ipauserobjectclasses:
101 description: A list of user objectclasses.
102 aliases: ["userobjectclasses"]
103 type: list
104 elements: str
105 version_added: '7.3.0'
106 ipausersearchfields:
107 description: A list of fields to search in when searching for users.
108 aliases: ["usersearchfields"]
109 type: list
110 elements: str
111 version_added: '2.5.0'
112 extends_documentation_fragment:
113 - community.general._ipa.documentation
114 - community.general._ipa.connection_notes
115 - community.general._attributes
116 """
117
118 EXAMPLES = r"""
119 - name: Ensure password plugin features DC:Disable Last Success and KDC:Disable Lockout are enabled
120 community.general.ipa_config:
121 ipaconfigstring: ["KDC:Disable Last Success", "KDC:Disable Lockout"]
122 ipa_host: localhost
123 ipa_user: admin
124 ipa_pass: supersecret
125
126 - name: Ensure the default login shell is bash
127 community.general.ipa_config:
128 ipadefaultloginshell: /bin/bash
129 ipa_host: localhost
130 ipa_user: admin
131 ipa_pass: supersecret
132
133 - name: Ensure the default e-mail domain is ansible.com
134 community.general.ipa_config:
135 ipadefaultemaildomain: ansible.com
136 ipa_host: localhost
137 ipa_user: admin
138 ipa_pass: supersecret
139
140 - name: Ensure the default primary group is set to ipausers
141 community.general.ipa_config:
142 ipadefaultprimarygroup: ipausers
143 ipa_host: localhost
144 ipa_user: admin
145 ipa_pass: supersecret
146
147 - name: Ensure the group search fields are set to 'cn,description'
148 community.general.ipa_config:
149 ipagroupsearchfields: ['cn', 'description']
150 ipa_host: localhost
151 ipa_user: admin
152 ipa_pass: supersecret
153
154 - name: Ensure the home directory location is set to /home
155 community.general.ipa_config:
156 ipahomesrootdir: /home
157 ipa_host: localhost
158 ipa_user: admin
159 ipa_pass: supersecret
160
161 - name: Ensure the default types of PAC supported for services is set to MS-PAC and PAD
162 community.general.ipa_config:
163 ipakrbauthzdata: ["MS-PAC", "PAD"]
164 ipa_host: localhost
165 ipa_user: admin
166 ipa_pass: supersecret
167
168 - name: Ensure the maximum user name length is set to 32
169 community.general.ipa_config:
170 ipamaxusernamelength: 32
171 ipa_host: localhost
172 ipa_user: admin
173 ipa_pass: supersecret
174
175 - name: Ensure the password expiration notice is set to 4 days
176 community.general.ipa_config:
177 ipapwdexpadvnotify: 4
178 ipa_host: localhost
179 ipa_user: admin
180 ipa_pass: supersecret
181
182 - name: Ensure the search record limit is set to 100
183 community.general.ipa_config:
184 ipasearchrecordslimit: 100
185 ipa_host: localhost
186 ipa_user: admin
187 ipa_pass: supersecret
188
189 - name: Ensure the search time limit is set to 2 seconds
190 community.general.ipa_config:
191 ipasearchtimelimit: 2
192 ipa_host: localhost
193 ipa_user: admin
194 ipa_pass: supersecret
195
196 - name: Ensure the default user auth type is password
197 community.general.ipa_config:
198 ipauserauthtype: ['password']
199 ipa_host: localhost
200 ipa_user: admin
201 ipa_pass: supersecret
202
203 - name: Ensure the user search fields is set to 'uid,givenname,sn,ou,title'
204 community.general.ipa_config:
205 ipausersearchfields: ['uid', 'givenname', 'sn', 'ou', 'title']
206 ipa_host: localhost
207 ipa_user: admin
208 ipa_pass: supersecret
209
210 - name: Ensure the SELinux user map order is set
211 community.general.ipa_config:
212 ipaselinuxusermaporder:
213 - "guest_u:s0"
214 - "xguest_u:s0"
215 - "user_u:s0"
216 - "staff_u:s0-s0:c0.c1023"
217 - "unconfined_u:s0-s0:c0.c1023"
218 ipa_host: localhost
219 ipa_user: admin
220 ipa_pass: supersecret
221 """
222
223 RETURN = r"""
224 config:
225 description: Configuration as returned by IPA API.
226 returned: always
227 type: dict
228 """
229
230 import traceback
231
232 from ansible.module_utils.basic import AnsibleModule
233
234 from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec
235
236
237 class ConfigIPAClient(IPAClient):
238 def __init__(self, module, host, port, protocol):
239 super().__init__(module, host, port, protocol)
240
241 def config_show(self):
242 return self._post_json(method="config_show", name=None)
243
244 def config_mod(self, name, item):
245 return self._post_json(method="config_mod", name=name, item=item)
246
247
248 def get_config_dict(
249 ipaconfigstring=None,
250 ipadefaultloginshell=None,
251 ipadefaultemaildomain=None,
252 ipadefaultprimarygroup=None,
253 ipagroupsearchfields=None,
254 ipagroupobjectclasses=None,
255 ipahomesrootdir=None,
256 ipakrbauthzdata=None,
257 ipamaxusernamelength=None,
258 ipapwdexpadvnotify=None,
259 ipasearchrecordslimit=None,
260 ipasearchtimelimit=None,
261 ipaselinuxusermaporder=None,
262 ipauserauthtype=None,
263 ipausersearchfields=None,
264 ipauserobjectclasses=None,
265 ):
266 config = {}
267 if ipaconfigstring is not None:
268 config["ipaconfigstring"] = ipaconfigstring
269 if ipadefaultloginshell is not None:
270 config["ipadefaultloginshell"] = ipadefaultloginshell
271 if ipadefaultemaildomain is not None:
272 config["ipadefaultemaildomain"] = ipadefaultemaildomain
273 if ipadefaultprimarygroup is not None:
274 config["ipadefaultprimarygroup"] = ipadefaultprimarygroup
275 if ipagroupobjectclasses is not None:
276 config["ipagroupobjectclasses"] = ipagroupobjectclasses
277 if ipagroupsearchfields is not None:
278 config["ipagroupsearchfields"] = ",".join(ipagroupsearchfields)
279 if ipahomesrootdir is not None:
280 config["ipahomesrootdir"] = ipahomesrootdir
281 if ipakrbauthzdata is not None:
282 config["ipakrbauthzdata"] = ipakrbauthzdata
283 if ipamaxusernamelength is not None:
284 config["ipamaxusernamelength"] = str(ipamaxusernamelength)
285 if ipapwdexpadvnotify is not None:
286 config["ipapwdexpadvnotify"] = str(ipapwdexpadvnotify)
287 if ipasearchrecordslimit is not None:
288 config["ipasearchrecordslimit"] = str(ipasearchrecordslimit)
289 if ipasearchtimelimit is not None:
290 config["ipasearchtimelimit"] = str(ipasearchtimelimit)
291 if ipaselinuxusermaporder is not None:
292 config["ipaselinuxusermaporder"] = "$".join(ipaselinuxusermaporder)
293 if ipauserauthtype is not None:
294 config["ipauserauthtype"] = ipauserauthtype
295 if ipauserobjectclasses is not None:
296 config["ipauserobjectclasses"] = ipauserobjectclasses
297 if ipausersearchfields is not None:
298 config["ipausersearchfields"] = ",".join(ipausersearchfields)
299
300 return config
301
302
303 def get_config_diff(client, ipa_config, module_config):
304 return client.get_diff(ipa_data=ipa_config, module_data=module_config)
305
306
307 def ensure(module, client):
308 module_config = get_config_dict(
309 ipaconfigstring=module.params.get("ipaconfigstring"),
310 ipadefaultloginshell=module.params.get("ipadefaultloginshell"),
311 ipadefaultemaildomain=module.params.get("ipadefaultemaildomain"),
312 ipadefaultprimarygroup=module.params.get("ipadefaultprimarygroup"),
313 ipagroupobjectclasses=module.params.get("ipagroupobjectclasses"),
314 ipagroupsearchfields=module.params.get("ipagroupsearchfields"),
315 ipahomesrootdir=module.params.get("ipahomesrootdir"),
316 ipakrbauthzdata=module.params.get("ipakrbauthzdata"),
317 ipamaxusernamelength=module.params.get("ipamaxusernamelength"),
318 ipapwdexpadvnotify=module.params.get("ipapwdexpadvnotify"),
319 ipasearchrecordslimit=module.params.get("ipasearchrecordslimit"),
320 ipasearchtimelimit=module.params.get("ipasearchtimelimit"),
321 ipaselinuxusermaporder=module.params.get("ipaselinuxusermaporder"),
322 ipauserauthtype=module.params.get("ipauserauthtype"),
323 ipausersearchfields=module.params.get("ipausersearchfields"),
324 ipauserobjectclasses=module.params.get("ipauserobjectclasses"),
325 )
326 ipa_config = client.config_show()
327 diff = get_config_diff(client, ipa_config, module_config)
328
329 changed = False
330 new_config = {}
331 for module_key in diff:
332 if module_config.get(module_key) != ipa_config.get(module_key, None):
333 changed = True
334 new_config.update({module_key: module_config.get(module_key)})
335
336 if changed and not module.check_mode:
337 client.config_mod(name=None, item=new_config)
338
339 return changed, client.config_show()
340
341
342 def main():
343 argument_spec = ipa_argument_spec()
344 argument_spec.update(
345 ipaconfigstring=dict(
346 type="list",
347 elements="str",
348 choices=[
349 "AllowNThash",
350 "KDC:Disable Last Success",
351 "KDC:Disable Lockout",
352 "KDC:Disable Default Preauth for SPNs",
353 ],
354 aliases=["configstring"],
355 ),
356 ipadefaultloginshell=dict(type="str", aliases=["loginshell"]),
357 ipadefaultemaildomain=dict(type="str", aliases=["emaildomain"]),
358 ipadefaultprimarygroup=dict(type="str", aliases=["primarygroup"]),
359 ipagroupobjectclasses=dict(type="list", elements="str", aliases=["groupobjectclasses"]),
360 ipagroupsearchfields=dict(type="list", elements="str", aliases=["groupsearchfields"]),
361 ipahomesrootdir=dict(type="str", aliases=["homesrootdir"]),
362 ipakrbauthzdata=dict(
363 type="list", elements="str", choices=["MS-PAC", "PAD", "nfs:NONE"], aliases=["krbauthzdata"]
364 ),
365 ipamaxusernamelength=dict(type="int", aliases=["maxusernamelength"]),
366 ipapwdexpadvnotify=dict(type="int", aliases=["pwdexpadvnotify"]),
367 ipasearchrecordslimit=dict(type="int", aliases=["searchrecordslimit"]),
368 ipasearchtimelimit=dict(type="int", aliases=["searchtimelimit"]),
369 ipaselinuxusermaporder=dict(type="list", elements="str", aliases=["selinuxusermaporder"]),
370 ipauserauthtype=dict(
371 type="list",
372 elements="str",
373 aliases=["userauthtype"],
374 choices=["password", "radius", "otp", "pkinit", "hardened", "idp", "passkey", "disabled"],
375 ),
376 ipausersearchfields=dict(type="list", elements="str", aliases=["usersearchfields"]),
377 ipauserobjectclasses=dict(type="list", elements="str", aliases=["userobjectclasses"]),
378 )
379
380 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
381
382 client = ConfigIPAClient(
383 module=module,
384 host=module.params["ipa_host"],
385 port=module.params["ipa_port"],
386 protocol=module.params["ipa_prot"],
387 )
388
389 try:
390 client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"])
391 changed, user = ensure(module, client)
392 module.exit_json(changed=changed, user=user)
393 except Exception as e:
394 module.fail_json(msg=f"{e}", exception=traceback.format_exc())
395
396
397 if __name__ == "__main__":
398 main()