Debian Codegraph @

ansible_collections/fortinet/fortios/plugins/modules/fortios_user_fsso.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_fsso
23 short_description: Configure Fortinet Single Sign On (FSSO) agents 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 fsso 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_fsso:
88 description:
89 - Configure Fortinet Single Sign On (FSSO) agents.
90 default: null
91 type: dict
92 suboptions:
93 group_poll_interval:
94 description:
95 - Interval in minutes within to fetch groups from FSSO server, or unset to disable.
96 type: int
97 interface:
98 description:
99 - Specify outgoing interface to reach server. Source system.interface.name.
100 type: str
101 interface_select_method:
102 description:
103 - Specify how to select outgoing interface to reach server.
104 type: str
105 choices:
106 - 'auto'
107 - 'sdwan'
108 - 'specify'
109 ldap_poll:
110 description:
111 - Enable/disable automatic fetching of groups from LDAP server.
112 type: str
113 choices:
114 - 'enable'
115 - 'disable'
116 ldap_poll_filter:
117 description:
118 - Filter used to fetch groups.
119 type: str
120 ldap_poll_interval:
121 description:
122 - Interval in minutes within to fetch groups from LDAP server.
123 type: int
124 ldap_server:
125 description:
126 - LDAP server to get group information. Source user.ldap.name.
127 type: str
128 logon_timeout:
129 description:
130 - Interval in minutes to keep logons after FSSO server down.
131 type: int
132 name:
133 description:
134 - Name.
135 required: true
136 type: str
137 password:
138 description:
139 - Password of the first FSSO collector agent.
140 type: str
141 password2:
142 description:
143 - Password of the second FSSO collector agent.
144 type: str
145 password3:
146 description:
147 - Password of the third FSSO collector agent.
148 type: str
149 password4:
150 description:
151 - Password of the fourth FSSO collector agent.
152 type: str
153 password5:
154 description:
155 - Password of the fifth FSSO collector agent.
156 type: str
157 port:
158 description:
159 - Port of the first FSSO collector agent.
160 type: int
161 port2:
162 description:
163 - Port of the second FSSO collector agent.
164 type: int
165 port3:
166 description:
167 - Port of the third FSSO collector agent.
168 type: int
169 port4:
170 description:
171 - Port of the fourth FSSO collector agent.
172 type: int
173 port5:
174 description:
175 - Port of the fifth FSSO collector agent.
176 type: int
177 server:
178 description:
179 - Domain name or IP address of the first FSSO collector agent.
180 type: str
181 server2:
182 description:
183 - Domain name or IP address of the second FSSO collector agent.
184 type: str
185 server3:
186 description:
187 - Domain name or IP address of the third FSSO collector agent.
188 type: str
189 server4:
190 description:
191 - Domain name or IP address of the fourth FSSO collector agent.
192 type: str
193 server5:
194 description:
195 - Domain name or IP address of the fifth FSSO collector agent.
196 type: str
197 sni:
198 description:
199 - Server Name Indication.
200 type: str
201 source_ip:
202 description:
203 - Source IP for communications to FSSO agent.
204 type: str
205 source_ip6:
206 description:
207 - IPv6 source for communications to FSSO agent.
208 type: str
209 ssl:
210 description:
211 - Enable/disable use of SSL.
212 type: str
213 choices:
214 - 'enable'
215 - 'disable'
216 ssl_server_host_ip_check:
217 description:
218 - Enable/disable server host/IP verification.
219 type: str
220 choices:
221 - 'enable'
222 - 'disable'
223 ssl_trusted_cert:
224 description:
225 - Trusted server certificate or CA certificate. Source vpn.certificate.remote.name vpn.certificate.ca.name.
226 type: str
227 type:
228 description:
229 - Server type.
230 type: str
231 choices:
232 - 'default'
233 - 'fortinac'
234 - 'fortiems'
235 - 'fortiems-cloud'
236 user_info_server:
237 description:
238 - LDAP server to get user information. Source user.ldap.name.
239 type: str
240 vrf_select:
241 description:
242 - VRF ID used for connection to server.
243 type: int
244 """
245
246 EXAMPLES = """
247 - name: Configure Fortinet Single Sign On (FSSO) agents.
248 fortinet.fortios.fortios_user_fsso:
249 vdom: "{{ vdom }}"
250 state: "present"
251 access_token: "<your_own_value>"
252 user_fsso:
253 group_poll_interval: "0"
254 interface: "<your_own_value> (source system.interface.name)"
255 interface_select_method: "auto"
256 ldap_poll: "enable"
257 ldap_poll_filter: "<your_own_value>"
258 ldap_poll_interval: "180"
259 ldap_server: "<your_own_value> (source user.ldap.name)"
260 logon_timeout: "5"
261 name: "default_name_11"
262 password: "<your_own_value>"
263 password2: "<your_own_value>"
264 password3: "<your_own_value>"
265 password4: "<your_own_value>"
266 password5: "<your_own_value>"
267 port: "8000"
268 port2: "8000"
269 port3: "8000"
270 port4: "8000"
271 port5: "8000"
272 server: "192.168.100.40"
273 server2: "<your_own_value>"
274 server3: "<your_own_value>"
275 server4: "<your_own_value>"
276 server5: "<your_own_value>"
277 sni: "<your_own_value>"
278 source_ip: "84.230.14.43"
279 source_ip6: "<your_own_value>"
280 ssl: "enable"
281 ssl_server_host_ip_check: "enable"
282 ssl_trusted_cert: "<your_own_value> (source vpn.certificate.remote.name vpn.certificate.ca.name)"
283 type: "default"
284 user_info_server: "<your_own_value> (source user.ldap.name)"
285 vrf_select: "0"
286 """
287
288 RETURN = """
289 build:
290 description: Build number of the fortigate image
291 returned: always
292 type: str
293 sample: '1547'
294 http_method:
295 description: Last method used to provision the content into FortiGate
296 returned: always
297 type: str
298 sample: 'PUT'
299 http_status:
300 description: Last result given by FortiGate on last operation applied
301 returned: always
302 type: str
303 sample: "200"
304 mkey:
305 description: Master key (id) used in the last call to FortiGate
306 returned: success
307 type: str
308 sample: "id"
309 name:
310 description: Name of the table used to fulfill the request
311 returned: always
312 type: str
313 sample: "urlfilter"
314 path:
315 description: Path of the table used to fulfill the request
316 returned: always
317 type: str
318 sample: "webfilter"
319 revision:
320 description: Internal revision number
321 returned: always
322 type: str
323 sample: "17.0.2.10658"
324 serial:
325 description: Serial number of the unit
326 returned: always
327 type: str
328 sample: "FGVMEVYYQT3AB5352"
329 status:
330 description: Indication of the operation's result
331 returned: always
332 type: str
333 sample: "success"
334 vdom:
335 description: Virtual domain used
336 returned: always
337 type: str
338 sample: "root"
339 version:
340 description: Version of the FortiGate
341 returned: always
342 type: str
343 sample: "v5.6.3"
344 """
345 from ansible.module_utils.basic import AnsibleModule
346 from ansible.module_utils.connection import Connection
347 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
348 FortiOSHandler,
349 )
350 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
351 check_legacy_fortiosapi,
352 )
353 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
354 schema_to_module_spec,
355 )
356 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
357 check_schema_versioning,
358 )
359 from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import (
360 FAIL_SOCKET_MSG,
361 )
362 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.data_post_processor import (
363 remove_invalid_fields,
364 )
365 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
366 is_same_comparison,
367 )
368 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
369 serialize,
370 )
371 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
372 find_current_values,
373 )
374 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
375 unify_data_format,
376 )
377
378
379 def filter_user_fsso_data(json):
380 option_list = [
381 "group_poll_interval",
382 "interface",
383 "interface_select_method",
384 "ldap_poll",
385 "ldap_poll_filter",
386 "ldap_poll_interval",
387 "ldap_server",
388 "logon_timeout",
389 "name",
390 "password",
391 "password2",
392 "password3",
393 "password4",
394 "password5",
395 "port",
396 "port2",
397 "port3",
398 "port4",
399 "port5",
400 "server",
401 "server2",
402 "server3",
403 "server4",
404 "server5",
405 "sni",
406 "source_ip",
407 "source_ip6",
408 "ssl",
409 "ssl_server_host_ip_check",
410 "ssl_trusted_cert",
411 "type",
412 "user_info_server",
413 "vrf_select",
414 ]
415
416 json = remove_invalid_fields(json)
417 dictionary = {}
418
419 for attribute in option_list:
420 if attribute in json and json[attribute] is not None:
421 dictionary[attribute] = json[attribute]
422
423 return dictionary
424
425
426 def underscore_to_hyphen(data):
427 new_data = None
428 if isinstance(data, list):
429 new_data = []
430 for i, elem in enumerate(data):
431 new_data.append(underscore_to_hyphen(elem))
432 elif isinstance(data, dict):
433 new_data = {}
434 for k, v in data.items():
435 new_data[k.replace("_", "-")] = underscore_to_hyphen(v)
436 else:
437 return data
438 return new_data
439
440
441 def user_fsso(data, fos, check_mode=False):
442
443 state = None
444 vdom = data["vdom"]
445 parameters = None
446 state = data.get("state", None)
447 user_fsso_data = data["user_fsso"]
448
449 filtered_data = filter_user_fsso_data(user_fsso_data)
450 converted_data = underscore_to_hyphen(filtered_data)
451
452 # check_mode starts from here
453 if check_mode:
454 diff = {
455 "before": "",
456 "after": filtered_data,
457 }
458 mkeyname = fos.get_mkeyname(None, None)
459 mkey = fos.get_mkey("user", "fsso", filtered_data, vdom=vdom)
460 current_data = fos.get(
461 "user", "fsso", vdom=vdom, mkey=mkey, parameters=parameters
462 )
463 is_existed = (
464 current_data
465 and current_data.get("http_status") == 200
466 and (
467 mkeyname
468 and isinstance(current_data.get("results"), list)
469 and len(current_data["results"]) > 0
470 or not mkeyname
471 and current_data["results"] # global object response
472 )
473 )
474
475 # 2. if it exists and the state is 'present' then compare current settings with desired
476 if state == "present" or state is True or state is None:
477 # for non global modules, mkeyname must exist and it's a new module when mkey is None
478 if mkeyname is not None and mkey is None:
479 return False, True, filtered_data, diff
480
481 # if mkey exists then compare each other
482 # record exits and they're matched or not
483 copied_filtered_data = filtered_data.copy()
484 copied_filtered_data.pop(mkeyname, None)
485 unified_filtered_data = unify_data_format(copied_filtered_data)
486
487 current_data_results = current_data.get("results", {})
488 current_config = (
489 current_data_results[0]
490 if mkeyname
491 and isinstance(current_data_results, list)
492 and len(current_data_results) > 0
493 else current_data_results
494 )
495 if is_existed:
496 unified_current_values = find_current_values(
497 unified_filtered_data,
498 unify_data_format(current_config),
499 )
500
501 is_same = is_same_comparison(
502 serialize(unified_current_values), serialize(unified_filtered_data)
503 )
504
505 return (
506 False,
507 not is_same,
508 filtered_data,
509 {"before": unified_current_values, "after": unified_filtered_data},
510 )
511
512 # record does not exist
513 return False, True, filtered_data, diff
514
515 if state == "absent":
516 if mkey is None:
517 return (
518 False,
519 False,
520 filtered_data,
521 {"before": current_data["results"][0], "after": ""},
522 )
523
524 if is_existed:
525 return (
526 False,
527 True,
528 filtered_data,
529 {"before": current_data["results"][0], "after": ""},
530 )
531 return False, False, filtered_data, {}
532
533 return True, False, {"reason: ": "Must provide state parameter"}, {}
534 # pass post processed data to member operations
535 # no need to do underscore_to_hyphen since do_member_operation handles it by itself
536 data_copy = data.copy()
537 data_copy["user_fsso"] = filtered_data
538 fos.do_member_operation(
539 "user",
540 "fsso",
541 data_copy,
542 )
543
544 if state == "present" or state is True:
545 return fos.set(
546 "user", "fsso", data=converted_data, vdom=vdom, parameters=parameters
547 )
548
549 elif state == "absent":
550 return fos.delete(
551 "user",
552 "fsso",
553 mkey=converted_data["name"],
554 vdom=vdom,
555 parameters=parameters,
556 )
557 else:
558 fos._module.fail_json(msg="state must be present or absent!")
559
560
561 def is_successful_status(resp):
562 return (
563 "status" in resp
564 and resp["status"] == "success"
565 or "http_status" in resp
566 and resp["http_status"] == 200
567 or "http_method" in resp
568 and resp["http_method"] == "DELETE"
569 and resp["http_status"] == 404
570 )
571
572
573 def fortios_user(data, fos, check_mode):
574
575 if data["user_fsso"]:
576 resp = user_fsso(data, fos, check_mode)
577 else:
578 fos._module.fail_json(msg="missing task body: %s" % ("user_fsso"))
579 if isinstance(resp, tuple) and len(resp) == 4:
580 return resp
581 return (
582 not is_successful_status(resp),
583 is_successful_status(resp)
584 and (resp["revision_changed"] if "revision_changed" in resp else True),
585 resp,
586 {},
587 )
588
589
590 versioned_schema = {
591 "type": "list",
592 "elements": "dict",
593 "children": {
594 "name": {"v_range": [["v6.0.0", ""]], "type": "string", "required": True},
595 "type": {
596 "v_range": [["v6.2.0", ""]],
597 "type": "string",
598 "options": [
599 {"value": "default"},
600 {"value": "fortinac"},
601 {"value": "fortiems", "v_range": [["v6.2.0", "v6.2.7"]]},
602 {"value": "fortiems-cloud", "v_range": [["v6.2.0", "v6.2.7"]]},
603 ],
604 },
605 "server": {"v_range": [["v6.0.0", ""]], "type": "string"},
606 "port": {"v_range": [["v6.0.0", ""]], "type": "integer"},
607 "password": {"v_range": [["v6.0.0", ""]], "type": "string"},
608 "server2": {"v_range": [["v6.0.0", ""]], "type": "string"},
609 "port2": {"v_range": [["v6.0.0", ""]], "type": "integer"},
610 "password2": {"v_range": [["v6.0.0", ""]], "type": "string"},
611 "server3": {"v_range": [["v6.0.0", ""]], "type": "string"},
612 "port3": {"v_range": [["v6.0.0", ""]], "type": "integer"},
613 "password3": {"v_range": [["v6.0.0", ""]], "type": "string"},
614 "server4": {"v_range": [["v6.0.0", ""]], "type": "string"},
615 "port4": {"v_range": [["v6.0.0", ""]], "type": "integer"},
616 "password4": {"v_range": [["v6.0.0", ""]], "type": "string"},
617 "server5": {"v_range": [["v6.0.0", ""]], "type": "string"},
618 "port5": {"v_range": [["v6.0.0", ""]], "type": "integer"},
619 "password5": {"v_range": [["v6.0.0", ""]], "type": "string"},
620 "logon_timeout": {"v_range": [["v7.0.1", ""]], "type": "integer"},
621 "ldap_server": {"v_range": [["v6.0.0", ""]], "type": "string"},
622 "group_poll_interval": {"v_range": [["v6.2.0", ""]], "type": "integer"},
623 "ldap_poll": {
624 "v_range": [["v6.2.0", ""]],
625 "type": "string",
626 "options": [{"value": "enable"}, {"value": "disable"}],
627 },
628 "ldap_poll_interval": {"v_range": [["v6.2.0", ""]], "type": "integer"},
629 "ldap_poll_filter": {"v_range": [["v6.2.0", ""]], "type": "string"},
630 "user_info_server": {"v_range": [["v6.2.0", ""]], "type": "string"},
631 "ssl": {
632 "v_range": [["v6.2.0", ""]],
633 "type": "string",
634 "options": [{"value": "enable"}, {"value": "disable"}],
635 },
636 "sni": {"v_range": [["v7.2.0", ""]], "type": "string"},
637 "ssl_server_host_ip_check": {
638 "v_range": [["v7.0.2", ""]],
639 "type": "string",
640 "options": [{"value": "enable"}, {"value": "disable"}],
641 },
642 "ssl_trusted_cert": {"v_range": [["v6.2.0", ""]], "type": "string"},
643 "source_ip": {"v_range": [["v6.0.0", ""]], "type": "string"},
644 "source_ip6": {"v_range": [["v6.0.0", ""]], "type": "string"},
645 "interface_select_method": {
646 "v_range": [["v6.2.0", "v6.2.0"], ["v6.2.5", "v6.4.0"], ["v6.4.4", ""]],
647 "type": "string",
648 "options": [{"value": "auto"}, {"value": "sdwan"}, {"value": "specify"}],
649 },
650 "interface": {
651 "v_range": [["v6.2.0", "v6.2.0"], ["v6.2.5", "v6.4.0"], ["v6.4.4", ""]],
652 "type": "string",
653 },
654 "vrf_select": {"v_range": [["v7.6.1", ""]], "type": "integer"},
655 },
656 "v_range": [["v6.0.0", ""]],
657 }
658
659
660 def main():
661 module_spec = schema_to_module_spec(versioned_schema)
662 mkeyname = "name"
663 fields = {
664 "access_token": {"required": False, "type": "str", "no_log": True},
665 "enable_log": {"required": False, "type": "bool", "default": False},
666 "vdom": {"required": False, "type": "str", "default": "root"},
667 "member_path": {"required": False, "type": "str"},
668 "member_state": {
669 "type": "str",
670 "required": False,
671 "choices": ["present", "absent"],
672 },
673 "state": {"required": True, "type": "str", "choices": ["present", "absent"]},
674 "user_fsso": {
675 "required": False,
676 "type": "dict",
677 "default": None,
678 "options": {},
679 },
680 }
681 for attribute_name in module_spec["options"]:
682 fields["user_fsso"]["options"][attribute_name] = module_spec["options"][
683 attribute_name
684 ]
685 if mkeyname and mkeyname == attribute_name:
686 fields["user_fsso"]["options"][attribute_name]["required"] = True
687
688 module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
689 check_legacy_fortiosapi(module)
690
691 is_error = False
692 has_changed = False
693 result = None
694 diff = None
695
696 versions_check_result = None
697 if module._socket_path:
698 connection = Connection(module._socket_path)
699 if "access_token" in module.params:
700 connection.set_custom_option("access_token", module.params["access_token"])
701
702 if "enable_log" in module.params:
703 connection.set_custom_option("enable_log", module.params["enable_log"])
704 else:
705 connection.set_custom_option("enable_log", False)
706 fos = FortiOSHandler(connection, module, mkeyname, admin_passwd_header=False)
707 versions_check_result = check_schema_versioning(
708 fos, versioned_schema, "user_fsso"
709 )
710
711 is_error, has_changed, result, diff = fortios_user(
712 module.params, fos, module.check_mode
713 )
714
715 else:
716 module.fail_json(**FAIL_SOCKET_MSG)
717
718 if versions_check_result and versions_check_result["matched"] is False:
719 module.warn(
720 "Ansible has detected version mismatch between FortOS system and your playbook, see more details by specifying option -vvv"
721 )
722
723 if not is_error:
724 if versions_check_result and versions_check_result["matched"] is False:
725 module.exit_json(
726 changed=has_changed,
727 version_check_warning=versions_check_result,
728 meta=result,
729 diff=diff,
730 )
731 else:
732 module.exit_json(changed=has_changed, meta=result, diff=diff)
733 else:
734 if versions_check_result and versions_check_result["matched"] is False:
735 module.fail_json(
736 msg="Error in repo",
737 version_check_warning=versions_check_result,
738 meta=result,
739 )
740 else:
741 module.fail_json(msg="Error in repo", meta=result)
742
743
744 if __name__ == "__main__":
745 main()