Debian Codegraph @

ansible_collections/fortinet/fortios/plugins/modules/fortios_user_ldap.py

raw symbols SCIP
1 #!/usr/bin/python
2 from __future__ import absolute_import, division, print_function
3
4 # Copyright: (c) 2022 Fortinet
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 __metaclass__ = type
13
14 ANSIBLE_METADATA = {
15 "status": ["preview"],
16 "supported_by": "community",
17 "metadata_version": "1.1",
18 }
19
20 DOCUMENTATION = """
21 ---
22 module: fortios_user_ldap
23 short_description: Configure LDAP server entries in Fortinet's FortiOS and FortiGate.
24 description:
25 - This module is able to configure a FortiGate or FortiOS (FOS) device by allowing the
26 user to set and modify user feature and ldap category.
27 Examples include all parameters and values need to be adjusted to datasources before usage.
28 Tested with FOS v6.0.0
29 version_added: "2.0.0"
30 author:
31 - Link Zheng (@chillancezen)
32 - Jie Xue (@JieX19)
33 - Hongbin Lu (@fgtdev-hblu)
34 - Frank Shen (@frankshen01)
35 - Miguel Angel Munoz (@mamunozgonzalez)
36 - Nicolas Thomas (@thomnico)
37 notes:
38 - Legacy fortiosapi has been deprecated, httpapi is the preferred way to run playbooks
39
40 - The module supports check_mode.
41
42 requirements:
43 - ansible>=2.16
44 options:
45 access_token:
46 description:
47 - Token-based authentication.
48 Generated from GUI of Fortigate.
49 type: str
50 required: false
51 enable_log:
52 description:
53 - Enable/Disable logging for task.
54 type: bool
55 required: false
56 default: false
57 vdom:
58 description:
59 - Virtual domain, among those defined previously. A vdom is a
60 virtual instance of the FortiGate that can be configured and
61 used as a different unit.
62 type: str
63 default: root
64 member_path:
65 type: str
66 description:
67 - Member attribute path to operate on.
68 - Delimited by a slash character if there are more than one attribute.
69 - Parameter marked with member_path is legitimate for doing member operation.
70 member_state:
71 type: str
72 description:
73 - Add or delete a member under specified attribute path.
74 - When member_state is specified, the state option is ignored.
75 choices:
76 - 'present'
77 - 'absent'
78
79 state:
80 description:
81 - Indicates whether to create or remove the object.
82 type: str
83 required: true
84 choices:
85 - 'present'
86 - 'absent'
87 user_ldap:
88 description:
89 - Configure LDAP server entries.
90 default: null
91 type: dict
92 suboptions:
93 account_key_cert_field:
94 description:
95 - Define subject identity field in certificate for user access right checking.
96 type: str
97 choices:
98 - 'othername'
99 - 'rfc822name'
100 - 'dnsname'
101 - 'cn'
102 account_key_filter:
103 description:
104 - Account key filter, using the UPN as the search filter.
105 type: str
106 account_key_processing:
107 description:
108 - Account key processing operation. The FortiGate will keep either the whole domain or strip the domain from the subject identity.
109 type: str
110 choices:
111 - 'same'
112 - 'strip'
113 account_key_upn_san:
114 description:
115 - Define SAN in certificate for user principle name matching.
116 type: str
117 choices:
118 - 'othername'
119 - 'rfc822name'
120 - 'dnsname'
121 antiphish:
122 description:
123 - Enable/disable AntiPhishing credential backend.
124 type: str
125 choices:
126 - 'enable'
127 - 'disable'
128 ca_cert:
129 description:
130 - CA certificate name. Source vpn.certificate.ca.name.
131 type: str
132 client_cert:
133 description:
134 - Client certificate name. Source vpn.certificate.local.name.
135 type: str
136 client_cert_auth:
137 description:
138 - Enable/disable using client certificate for TLS authentication.
139 type: str
140 choices:
141 - 'enable'
142 - 'disable'
143 cnid:
144 description:
145 - Common name identifier for the LDAP server. The common name identifier for most LDAP servers is "cn".
146 type: str
147 dn:
148 description:
149 - Distinguished name used to look up entries on the LDAP server.
150 type: str
151 group_filter:
152 description:
153 - Filter used for group matching.
154 type: str
155 group_member_check:
156 description:
157 - Group member checking methods.
158 type: str
159 choices:
160 - 'user-attr'
161 - 'group-object'
162 - 'posix-group-object'
163 group_object_filter:
164 description:
165 - Filter used for group searching.
166 type: str
167 group_search_base:
168 description:
169 - Search base used for group searching.
170 type: str
171 interface:
172 description:
173 - Specify outgoing interface to reach server. Source system.interface.name.
174 type: str
175 interface_select_method:
176 description:
177 - Specify how to select outgoing interface to reach server.
178 type: str
179 choices:
180 - 'auto'
181 - 'sdwan'
182 - 'specify'
183 member_attr:
184 description:
185 - Name of attribute from which to get group membership.
186 type: str
187 name:
188 description:
189 - LDAP server entry name.
190 required: true
191 type: str
192 obtain_user_info:
193 description:
194 - Enable/disable obtaining of user information.
195 type: str
196 choices:
197 - 'enable'
198 - 'disable'
199 password:
200 description:
201 - Password for initial binding.
202 type: str
203 password_attr:
204 description:
205 - Name of attribute to get password hash.
206 type: str
207 password_expiry_warning:
208 description:
209 - Enable/disable password expiry warnings.
210 type: str
211 choices:
212 - 'enable'
213 - 'disable'
214 password_renewal:
215 description:
216 - Enable/disable online password renewal.
217 type: str
218 choices:
219 - 'enable'
220 - 'disable'
221 port:
222 description:
223 - Port to be used for communication with the LDAP server .
224 type: int
225 search_type:
226 description:
227 - Search type.
228 type: list
229 elements: str
230 choices:
231 - 'recursive'
232 secondary_server:
233 description:
234 - Secondary LDAP server CN domain name or IP.
235 type: str
236 secure:
237 description:
238 - Port to be used for authentication.
239 type: str
240 choices:
241 - 'disable'
242 - 'starttls'
243 - 'ldaps'
244 server:
245 description:
246 - LDAP server CN domain name or IP.
247 type: str
248 server_identity_check:
249 description:
250 - Enable/disable LDAP server identity check (verify server domain name/IP address against the server certificate).
251 type: str
252 choices:
253 - 'enable'
254 - 'disable'
255 source_ip:
256 description:
257 - FortiGate IP address to be used for communication with the LDAP server.
258 type: str
259 source_ip_interface:
260 description:
261 - Source interface for communication with the LDAP server. Source system.interface.name.
262 type: str
263 source_port:
264 description:
265 - Source port to be used for communication with the LDAP server.
266 type: int
267 ssl_min_proto_version:
268 description:
269 - Minimum supported protocol version for SSL/TLS connections .
270 type: str
271 choices:
272 - 'default'
273 - 'SSLv3'
274 - 'TLSv1'
275 - 'TLSv1-1'
276 - 'TLSv1-2'
277 - 'TLSv1-3'
278 status_ttl:
279 description:
280 - Time for which server reachability is cached so that when a server is unreachable, it will not be retried for at least this period of
281 time (0 = cache disabled).
282 type: int
283 tertiary_server:
284 description:
285 - Tertiary LDAP server CN domain name or IP.
286 type: str
287 two_factor:
288 description:
289 - Enable/disable two-factor authentication.
290 type: str
291 choices:
292 - 'disable'
293 - 'fortitoken-cloud'
294 two_factor_authentication:
295 description:
296 - Authentication method by FortiToken Cloud.
297 type: str
298 choices:
299 - 'fortitoken'
300 - 'email'
301 - 'sms'
302 two_factor_filter:
303 description:
304 - Filter used to synchronize users to FortiToken Cloud.
305 type: str
306 two_factor_notification:
307 description:
308 - Notification method for user activation by FortiToken Cloud.
309 type: str
310 choices:
311 - 'email'
312 - 'sms'
313 type:
314 description:
315 - Authentication type for LDAP searches.
316 type: str
317 choices:
318 - 'simple'
319 - 'anonymous'
320 - 'regular'
321 user_info_exchange_server:
322 description:
323 - MS Exchange server from which to fetch user information. Source user.exchange.name.
324 type: str
325 username:
326 description:
327 - Username (full DN) for initial binding.
328 type: str
329 vrf_select:
330 description:
331 - VRF ID used for connection to server.
332 type: int
333 """
334
335 EXAMPLES = """
336 - name: Configure LDAP server entries.
337 fortinet.fortios.fortios_user_ldap:
338 vdom: "{{ vdom }}"
339 state: "present"
340 access_token: "<your_own_value>"
341 user_ldap:
342 account_key_cert_field: "othername"
343 account_key_filter: "<your_own_value>"
344 account_key_processing: "same"
345 account_key_upn_san: "othername"
346 antiphish: "enable"
347 ca_cert: "<your_own_value> (source vpn.certificate.ca.name)"
348 client_cert: "<your_own_value> (source vpn.certificate.local.name)"
349 client_cert_auth: "enable"
350 cnid: "<your_own_value>"
351 dn: "<your_own_value>"
352 group_filter: "<your_own_value>"
353 group_member_check: "user-attr"
354 group_object_filter: "<your_own_value>"
355 group_search_base: "<your_own_value>"
356 interface: "<your_own_value> (source system.interface.name)"
357 interface_select_method: "auto"
358 member_attr: "<your_own_value>"
359 name: "default_name_20"
360 obtain_user_info: "enable"
361 password: "<your_own_value>"
362 password_attr: "<your_own_value>"
363 password_expiry_warning: "enable"
364 password_renewal: "enable"
365 port: "389"
366 search_type: "recursive"
367 secondary_server: "<your_own_value>"
368 secure: "disable"
369 server: "192.168.100.40"
370 server_identity_check: "enable"
371 source_ip: "84.230.14.43"
372 source_ip_interface: "<your_own_value> (source system.interface.name)"
373 source_port: "0"
374 ssl_min_proto_version: "default"
375 status_ttl: "300"
376 tertiary_server: "<your_own_value>"
377 two_factor: "disable"
378 two_factor_authentication: "fortitoken"
379 two_factor_filter: "<your_own_value>"
380 two_factor_notification: "email"
381 type: "simple"
382 user_info_exchange_server: "<your_own_value> (source user.exchange.name)"
383 username: "<your_own_value>"
384 vrf_select: "0"
385 """
386
387 RETURN = """
388 build:
389 description: Build number of the fortigate image
390 returned: always
391 type: str
392 sample: '1547'
393 http_method:
394 description: Last method used to provision the content into FortiGate
395 returned: always
396 type: str
397 sample: 'PUT'
398 http_status:
399 description: Last result given by FortiGate on last operation applied
400 returned: always
401 type: str
402 sample: "200"
403 mkey:
404 description: Master key (id) used in the last call to FortiGate
405 returned: success
406 type: str
407 sample: "id"
408 name:
409 description: Name of the table used to fulfill the request
410 returned: always
411 type: str
412 sample: "urlfilter"
413 path:
414 description: Path of the table used to fulfill the request
415 returned: always
416 type: str
417 sample: "webfilter"
418 revision:
419 description: Internal revision number
420 returned: always
421 type: str
422 sample: "17.0.2.10658"
423 serial:
424 description: Serial number of the unit
425 returned: always
426 type: str
427 sample: "FGVMEVYYQT3AB5352"
428 status:
429 description: Indication of the operation's result
430 returned: always
431 type: str
432 sample: "success"
433 vdom:
434 description: Virtual domain used
435 returned: always
436 type: str
437 sample: "root"
438 version:
439 description: Version of the FortiGate
440 returned: always
441 type: str
442 sample: "v5.6.3"
443 """
444 from ansible.module_utils.basic import AnsibleModule
445 from ansible.module_utils.connection import Connection
446 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
447 FortiOSHandler,
448 )
449 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
450 check_legacy_fortiosapi,
451 )
452 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
453 schema_to_module_spec,
454 )
455 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
456 check_schema_versioning,
457 )
458 from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import (
459 FAIL_SOCKET_MSG,
460 )
461 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.data_post_processor import (
462 remove_invalid_fields,
463 )
464 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
465 is_same_comparison,
466 )
467 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
468 serialize,
469 )
470 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
471 find_current_values,
472 )
473 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
474 unify_data_format,
475 )
476
477
478 def filter_user_ldap_data(json):
479 option_list = [
480 "account_key_cert_field",
481 "account_key_filter",
482 "account_key_processing",
483 "account_key_upn_san",
484 "antiphish",
485 "ca_cert",
486 "client_cert",
487 "client_cert_auth",
488 "cnid",
489 "dn",
490 "group_filter",
491 "group_member_check",
492 "group_object_filter",
493 "group_search_base",
494 "interface",
495 "interface_select_method",
496 "member_attr",
497 "name",
498 "obtain_user_info",
499 "password",
500 "password_attr",
501 "password_expiry_warning",
502 "password_renewal",
503 "port",
504 "search_type",
505 "secondary_server",
506 "secure",
507 "server",
508 "server_identity_check",
509 "source_ip",
510 "source_ip_interface",
511 "source_port",
512 "ssl_min_proto_version",
513 "status_ttl",
514 "tertiary_server",
515 "two_factor",
516 "two_factor_authentication",
517 "two_factor_filter",
518 "two_factor_notification",
519 "type",
520 "user_info_exchange_server",
521 "username",
522 "vrf_select",
523 ]
524
525 json = remove_invalid_fields(json)
526 dictionary = {}
527
528 for attribute in option_list:
529 if attribute in json and json[attribute] is not None:
530 dictionary[attribute] = json[attribute]
531
532 return dictionary
533
534
535 def flatten_single_path(data, path, index):
536 if (
537 not data
538 or index == len(path)
539 or path[index] not in data
540 or (not data[path[index]] and not isinstance(data[path[index]], list))
541 ):
542 return
543
544 if index == len(path) - 1:
545 data[path[index]] = " ".join(str(elem) for elem in data[path[index]])
546 if len(data[path[index]]) == 0:
547 data[path[index]] = None
548 elif isinstance(data[path[index]], list):
549 for value in data[path[index]]:
550 flatten_single_path(value, path, index + 1)
551 else:
552 flatten_single_path(data[path[index]], path, index + 1)
553
554
555 def flatten_multilists_attributes(data):
556 multilist_attrs = [
557 ["search_type"],
558 ]
559
560 for attr in multilist_attrs:
561 flatten_single_path(data, attr, 0)
562
563 return data
564
565
566 def underscore_to_hyphen(data):
567 new_data = None
568 if isinstance(data, list):
569 new_data = []
570 for i, elem in enumerate(data):
571 new_data.append(underscore_to_hyphen(elem))
572 elif isinstance(data, dict):
573 new_data = {}
574 for k, v in data.items():
575 new_data[k.replace("_", "-")] = underscore_to_hyphen(v)
576 else:
577 return data
578 return new_data
579
580
581 def user_ldap(data, fos, check_mode=False):
582
583 state = None
584 vdom = data["vdom"]
585 parameters = None
586 state = data.get("state", None)
587 user_ldap_data = data["user_ldap"]
588
589 filtered_data = filter_user_ldap_data(user_ldap_data)
590 filtered_data = flatten_multilists_attributes(filtered_data)
591 converted_data = underscore_to_hyphen(filtered_data)
592
593 # check_mode starts from here
594 if check_mode:
595 diff = {
596 "before": "",
597 "after": filtered_data,
598 }
599 mkeyname = fos.get_mkeyname(None, None)
600 mkey = fos.get_mkey("user", "ldap", filtered_data, vdom=vdom)
601 current_data = fos.get(
602 "user", "ldap", vdom=vdom, mkey=mkey, parameters=parameters
603 )
604 is_existed = (
605 current_data
606 and current_data.get("http_status") == 200
607 and (
608 mkeyname
609 and isinstance(current_data.get("results"), list)
610 and len(current_data["results"]) > 0
611 or not mkeyname
612 and current_data["results"] # global object response
613 )
614 )
615
616 # 2. if it exists and the state is 'present' then compare current settings with desired
617 if state == "present" or state is True or state is None:
618 # for non global modules, mkeyname must exist and it's a new module when mkey is None
619 if mkeyname is not None and mkey is None:
620 return False, True, filtered_data, diff
621
622 # if mkey exists then compare each other
623 # record exits and they're matched or not
624 copied_filtered_data = filtered_data.copy()
625 copied_filtered_data.pop(mkeyname, None)
626 unified_filtered_data = unify_data_format(copied_filtered_data)
627
628 current_data_results = current_data.get("results", {})
629 current_config = (
630 current_data_results[0]
631 if mkeyname
632 and isinstance(current_data_results, list)
633 and len(current_data_results) > 0
634 else current_data_results
635 )
636 if is_existed:
637 unified_current_values = find_current_values(
638 unified_filtered_data,
639 unify_data_format(current_config),
640 )
641
642 is_same = is_same_comparison(
643 serialize(unified_current_values), serialize(unified_filtered_data)
644 )
645
646 return (
647 False,
648 not is_same,
649 filtered_data,
650 {"before": unified_current_values, "after": unified_filtered_data},
651 )
652
653 # record does not exist
654 return False, True, filtered_data, diff
655
656 if state == "absent":
657 if mkey is None:
658 return (
659 False,
660 False,
661 filtered_data,
662 {"before": current_data["results"][0], "after": ""},
663 )
664
665 if is_existed:
666 return (
667 False,
668 True,
669 filtered_data,
670 {"before": current_data["results"][0], "after": ""},
671 )
672 return False, False, filtered_data, {}
673
674 return True, False, {"reason: ": "Must provide state parameter"}, {}
675 # pass post processed data to member operations
676 # no need to do underscore_to_hyphen since do_member_operation handles it by itself
677 data_copy = data.copy()
678 data_copy["user_ldap"] = filtered_data
679 fos.do_member_operation(
680 "user",
681 "ldap",
682 data_copy,
683 )
684
685 if state == "present" or state is True:
686 return fos.set(
687 "user", "ldap", data=converted_data, vdom=vdom, parameters=parameters
688 )
689
690 elif state == "absent":
691 return fos.delete(
692 "user",
693 "ldap",
694 mkey=converted_data["name"],
695 vdom=vdom,
696 parameters=parameters,
697 )
698 else:
699 fos._module.fail_json(msg="state must be present or absent!")
700
701
702 def is_successful_status(resp):
703 return (
704 "status" in resp
705 and resp["status"] == "success"
706 or "http_status" in resp
707 and resp["http_status"] == 200
708 or "http_method" in resp
709 and resp["http_method"] == "DELETE"
710 and resp["http_status"] == 404
711 )
712
713
714 def fortios_user(data, fos, check_mode):
715
716 if data["user_ldap"]:
717 resp = user_ldap(data, fos, check_mode)
718 else:
719 fos._module.fail_json(msg="missing task body: %s" % ("user_ldap"))
720 if isinstance(resp, tuple) and len(resp) == 4:
721 return resp
722 return (
723 not is_successful_status(resp),
724 is_successful_status(resp)
725 and (resp["revision_changed"] if "revision_changed" in resp else True),
726 resp,
727 {},
728 )
729
730
731 versioned_schema = {
732 "type": "list",
733 "elements": "dict",
734 "children": {
735 "name": {"v_range": [["v6.0.0", ""]], "type": "string", "required": True},
736 "server": {"v_range": [["v6.0.0", ""]], "type": "string"},
737 "secondary_server": {"v_range": [["v6.0.0", ""]], "type": "string"},
738 "tertiary_server": {"v_range": [["v6.0.0", ""]], "type": "string"},
739 "status_ttl": {"v_range": [["v7.4.4", ""]], "type": "integer"},
740 "server_identity_check": {
741 "v_range": [["v6.0.0", ""]],
742 "type": "string",
743 "options": [{"value": "enable"}, {"value": "disable"}],
744 },
745 "source_ip": {"v_range": [["v6.0.0", ""]], "type": "string"},
746 "source_ip_interface": {"v_range": [["v7.6.0", ""]], "type": "string"},
747 "source_port": {"v_range": [["v7.0.0", ""]], "type": "integer"},
748 "cnid": {"v_range": [["v6.0.0", ""]], "type": "string"},
749 "dn": {"v_range": [["v6.0.0", ""]], "type": "string"},
750 "type": {
751 "v_range": [["v6.0.0", ""]],
752 "type": "string",
753 "options": [
754 {"value": "simple"},
755 {"value": "anonymous"},
756 {"value": "regular"},
757 ],
758 },
759 "two_factor": {
760 "v_range": [["v6.2.0", ""]],
761 "type": "string",
762 "options": [{"value": "disable"}, {"value": "fortitoken-cloud"}],
763 },
764 "two_factor_authentication": {
765 "v_range": [["v6.2.0", "v6.2.0"], ["v6.2.5", ""]],
766 "type": "string",
767 "options": [{"value": "fortitoken"}, {"value": "email"}, {"value": "sms"}],
768 },
769 "two_factor_notification": {
770 "v_range": [["v6.2.0", "v6.2.0"], ["v6.2.5", ""]],
771 "type": "string",
772 "options": [{"value": "email"}, {"value": "sms"}],
773 },
774 "two_factor_filter": {"v_range": [["v7.2.1", ""]], "type": "string"},
775 "username": {"v_range": [["v6.0.0", ""]], "type": "string"},
776 "password": {"v_range": [["v6.0.0", ""]], "type": "string"},
777 "group_member_check": {
778 "v_range": [["v6.0.0", ""]],
779 "type": "string",
780 "options": [
781 {"value": "user-attr"},
782 {"value": "group-object"},
783 {"value": "posix-group-object"},
784 ],
785 },
786 "group_search_base": {"v_range": [["v6.0.0", ""]], "type": "string"},
787 "group_object_filter": {"v_range": [["v6.0.0", ""]], "type": "string"},
788 "group_filter": {"v_range": [["v6.0.0", ""]], "type": "string"},
789 "secure": {
790 "v_range": [["v6.0.0", ""]],
791 "type": "string",
792 "options": [
793 {"value": "disable"},
794 {"value": "starttls"},
795 {"value": "ldaps"},
796 ],
797 },
798 "ssl_min_proto_version": {
799 "v_range": [["v6.0.0", ""]],
800 "type": "string",
801 "options": [
802 {"value": "default"},
803 {"value": "SSLv3"},
804 {"value": "TLSv1"},
805 {"value": "TLSv1-1"},
806 {"value": "TLSv1-2"},
807 {"value": "TLSv1-3", "v_range": [["v7.4.1", ""]]},
808 ],
809 },
810 "ca_cert": {"v_range": [["v6.0.0", ""]], "type": "string"},
811 "port": {"v_range": [["v6.0.0", ""]], "type": "integer"},
812 "password_expiry_warning": {
813 "v_range": [["v6.0.0", ""]],
814 "type": "string",
815 "options": [{"value": "enable"}, {"value": "disable"}],
816 },
817 "password_renewal": {
818 "v_range": [["v6.0.0", ""]],
819 "type": "string",
820 "options": [{"value": "enable"}, {"value": "disable"}],
821 },
822 "member_attr": {"v_range": [["v6.0.0", ""]], "type": "string"},
823 "account_key_processing": {
824 "v_range": [["v6.0.0", ""]],
825 "type": "string",
826 "options": [{"value": "same"}, {"value": "strip"}],
827 },
828 "account_key_cert_field": {
829 "v_range": [["v7.4.1", ""]],
830 "type": "string",
831 "options": [
832 {"value": "othername"},
833 {"value": "rfc822name"},
834 {"value": "dnsname"},
835 {"value": "cn", "v_range": [["v7.4.4", ""]]},
836 ],
837 },
838 "account_key_filter": {"v_range": [["v6.0.0", ""]], "type": "string"},
839 "search_type": {
840 "v_range": [["v6.2.0", ""]],
841 "type": "list",
842 "options": [{"value": "recursive"}],
843 "multiple_values": True,
844 "elements": "str",
845 },
846 "client_cert_auth": {
847 "v_range": [["v7.2.0", ""]],
848 "type": "string",
849 "options": [{"value": "enable"}, {"value": "disable"}],
850 },
851 "client_cert": {"v_range": [["v7.2.0", ""]], "type": "string"},
852 "obtain_user_info": {
853 "v_range": [["v6.2.0", ""]],
854 "type": "string",
855 "options": [{"value": "enable"}, {"value": "disable"}],
856 },
857 "user_info_exchange_server": {"v_range": [["v6.2.0", ""]], "type": "string"},
858 "interface_select_method": {
859 "v_range": [["v6.2.0", "v6.2.0"], ["v6.2.5", ""]],
860 "type": "string",
861 "options": [{"value": "auto"}, {"value": "sdwan"}, {"value": "specify"}],
862 },
863 "interface": {
864 "v_range": [["v6.2.0", "v6.2.0"], ["v6.2.5", ""]],
865 "type": "string",
866 },
867 "vrf_select": {"v_range": [["v7.6.1", ""]], "type": "integer"},
868 "antiphish": {
869 "v_range": [["v7.0.0", ""]],
870 "type": "string",
871 "options": [{"value": "enable"}, {"value": "disable"}],
872 },
873 "password_attr": {"v_range": [["v7.0.0", ""]], "type": "string"},
874 "account_key_upn_san": {
875 "v_range": [["v7.2.4", "v7.4.0"]],
876 "type": "string",
877 "options": [
878 {"value": "othername"},
879 {"value": "rfc822name"},
880 {"value": "dnsname"},
881 ],
882 },
883 },
884 "v_range": [["v6.0.0", ""]],
885 }
886
887
888 def main():
889 module_spec = schema_to_module_spec(versioned_schema)
890 mkeyname = "name"
891 fields = {
892 "access_token": {"required": False, "type": "str", "no_log": True},
893 "enable_log": {"required": False, "type": "bool", "default": False},
894 "vdom": {"required": False, "type": "str", "default": "root"},
895 "member_path": {"required": False, "type": "str"},
896 "member_state": {
897 "type": "str",
898 "required": False,
899 "choices": ["present", "absent"],
900 },
901 "state": {"required": True, "type": "str", "choices": ["present", "absent"]},
902 "user_ldap": {
903 "required": False,
904 "type": "dict",
905 "default": None,
906 "options": {},
907 },
908 }
909 for attribute_name in module_spec["options"]:
910 fields["user_ldap"]["options"][attribute_name] = module_spec["options"][
911 attribute_name
912 ]
913 if mkeyname and mkeyname == attribute_name:
914 fields["user_ldap"]["options"][attribute_name]["required"] = True
915
916 module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
917 check_legacy_fortiosapi(module)
918
919 is_error = False
920 has_changed = False
921 result = None
922 diff = None
923
924 versions_check_result = None
925 if module._socket_path:
926 connection = Connection(module._socket_path)
927 if "access_token" in module.params:
928 connection.set_custom_option("access_token", module.params["access_token"])
929
930 if "enable_log" in module.params:
931 connection.set_custom_option("enable_log", module.params["enable_log"])
932 else:
933 connection.set_custom_option("enable_log", False)
934 fos = FortiOSHandler(connection, module, mkeyname, admin_passwd_header=False)
935 versions_check_result = check_schema_versioning(
936 fos, versioned_schema, "user_ldap"
937 )
938
939 is_error, has_changed, result, diff = fortios_user(
940 module.params, fos, module.check_mode
941 )
942
943 else:
944 module.fail_json(**FAIL_SOCKET_MSG)
945
946 if versions_check_result and versions_check_result["matched"] is False:
947 module.warn(
948 "Ansible has detected version mismatch between FortOS system and your playbook, see more details by specifying option -vvv"
949 )
950
951 if not is_error:
952 if versions_check_result and versions_check_result["matched"] is False:
953 module.exit_json(
954 changed=has_changed,
955 version_check_warning=versions_check_result,
956 meta=result,
957 diff=diff,
958 )
959 else:
960 module.exit_json(changed=has_changed, meta=result, diff=diff)
961 else:
962 if versions_check_result and versions_check_result["matched"] is False:
963 module.fail_json(
964 msg="Error in repo",
965 version_check_warning=versions_check_result,
966 meta=result,
967 )
968 else:
969 module.fail_json(msg="Error in repo", meta=result)
970
971
972 if __name__ == "__main__":
973 main()