Debian Codegraph @

ansible_collections/fortinet/fortios/plugins/modules/fortios_system_saml.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_system_saml
23 short_description: Global settings for SAML authentication 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 system feature and saml 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 system_saml:
80 description:
81 - Global settings for SAML authentication.
82 default: null
83 type: dict
84 suboptions:
85 artifact_resolution_url:
86 description:
87 - SP artifact resolution URL.
88 type: str
89 binding_protocol:
90 description:
91 - IdP Binding protocol.
92 type: str
93 choices:
94 - 'post'
95 - 'redirect'
96 cert:
97 description:
98 - Certificate to sign SAML messages. Source certificate.local.name.
99 type: str
100 default_login_page:
101 description:
102 - Choose default login page.
103 type: str
104 choices:
105 - 'normal'
106 - 'sso'
107 default_profile:
108 description:
109 - Default profile for new SSO admin. Source system.accprofile.name.
110 type: str
111 entity_id:
112 description:
113 - SP entity ID.
114 type: str
115 idp_artifact_resolution_url:
116 description:
117 - IDP artifact resolution URL.
118 type: str
119 idp_cert:
120 description:
121 - IDP certificate name. Source certificate.remote.name.
122 type: str
123 idp_entity_id:
124 description:
125 - IDP entity ID.
126 type: str
127 idp_single_logout_url:
128 description:
129 - IDP single logout URL.
130 type: str
131 idp_single_sign_on_url:
132 description:
133 - IDP single sign-on URL.
134 type: str
135 life:
136 description:
137 - Length of the range of time when the assertion is valid (in minutes).
138 type: int
139 portal_url:
140 description:
141 - SP portal URL.
142 type: str
143 require_signed_resp_and_asrt:
144 description:
145 - Require both response and assertion from IDP to be signed when FGT acts as SP .
146 type: str
147 choices:
148 - 'enable'
149 - 'disable'
150 role:
151 description:
152 - SAML role.
153 type: str
154 choices:
155 - 'identity-provider'
156 - 'service-provider'
157 server_address:
158 description:
159 - Server address.
160 type: str
161 service_providers:
162 description:
163 - Authorized service providers.
164 type: list
165 elements: dict
166 suboptions:
167 assertion_attributes:
168 description:
169 - Customized SAML attributes to send along with assertion.
170 type: list
171 elements: dict
172 suboptions:
173 name:
174 description:
175 - Name.
176 required: true
177 type: str
178 type:
179 description:
180 - Type.
181 type: str
182 choices:
183 - 'username'
184 - 'email'
185 - 'profile-name'
186 idp_artifact_resolution_url:
187 description:
188 - IDP artifact resolution URL.
189 type: str
190 idp_entity_id:
191 description:
192 - IDP entity ID.
193 type: str
194 idp_single_logout_url:
195 description:
196 - IDP single logout URL.
197 type: str
198 idp_single_sign_on_url:
199 description:
200 - IDP single sign-on URL.
201 type: str
202 name:
203 description:
204 - Name.
205 required: true
206 type: str
207 prefix:
208 description:
209 - Prefix.
210 type: str
211 sp_artifact_resolution_url:
212 description:
213 - SP artifact resolution URL.
214 type: str
215 sp_binding_protocol:
216 description:
217 - SP binding protocol.
218 type: str
219 choices:
220 - 'post'
221 - 'redirect'
222 sp_cert:
223 description:
224 - SP certificate name. Source certificate.remote.name.
225 type: str
226 sp_entity_id:
227 description:
228 - SP entity ID.
229 type: str
230 sp_portal_url:
231 description:
232 - SP portal URL.
233 type: str
234 sp_single_logout_url:
235 description:
236 - SP single logout URL.
237 type: str
238 sp_single_sign_on_url:
239 description:
240 - SP single sign-on URL.
241 type: str
242 single_logout_url:
243 description:
244 - SP single logout URL.
245 type: str
246 single_sign_on_url:
247 description:
248 - SP single sign-on URL.
249 type: str
250 status:
251 description:
252 - Enable/disable SAML authentication .
253 type: str
254 choices:
255 - 'enable'
256 - 'disable'
257 tolerance:
258 description:
259 - Tolerance to the range of time when the assertion is valid (in minutes).
260 type: int
261 """
262
263 EXAMPLES = """
264 - name: Global settings for SAML authentication.
265 fortinet.fortios.fortios_system_saml:
266 vdom: "{{ vdom }}"
267 system_saml:
268 artifact_resolution_url: "<your_own_value>"
269 binding_protocol: "post"
270 cert: "<your_own_value> (source certificate.local.name)"
271 default_login_page: "normal"
272 default_profile: "<your_own_value> (source system.accprofile.name)"
273 entity_id: "<your_own_value>"
274 idp_artifact_resolution_url: "<your_own_value>"
275 idp_cert: "<your_own_value> (source certificate.remote.name)"
276 idp_entity_id: "<your_own_value>"
277 idp_single_logout_url: "<your_own_value>"
278 idp_single_sign_on_url: "<your_own_value>"
279 life: "30"
280 portal_url: "<your_own_value>"
281 require_signed_resp_and_asrt: "enable"
282 role: "identity-provider"
283 server_address: "<your_own_value>"
284 service_providers:
285 -
286 assertion_attributes:
287 -
288 name: "default_name_21"
289 type: "username"
290 idp_artifact_resolution_url: "<your_own_value>"
291 idp_entity_id: "<your_own_value>"
292 idp_single_logout_url: "<your_own_value>"
293 idp_single_sign_on_url: "<your_own_value>"
294 name: "default_name_27"
295 prefix: "<your_own_value>"
296 sp_artifact_resolution_url: "<your_own_value>"
297 sp_binding_protocol: "post"
298 sp_cert: "<your_own_value> (source certificate.remote.name)"
299 sp_entity_id: "<your_own_value>"
300 sp_portal_url: "<your_own_value>"
301 sp_single_logout_url: "<your_own_value>"
302 sp_single_sign_on_url: "<your_own_value>"
303 single_logout_url: "<your_own_value>"
304 single_sign_on_url: "<your_own_value>"
305 status: "enable"
306 tolerance: "5"
307 """
308
309 RETURN = """
310 build:
311 description: Build number of the fortigate image
312 returned: always
313 type: str
314 sample: '1547'
315 http_method:
316 description: Last method used to provision the content into FortiGate
317 returned: always
318 type: str
319 sample: 'PUT'
320 http_status:
321 description: Last result given by FortiGate on last operation applied
322 returned: always
323 type: str
324 sample: "200"
325 mkey:
326 description: Master key (id) used in the last call to FortiGate
327 returned: success
328 type: str
329 sample: "id"
330 name:
331 description: Name of the table used to fulfill the request
332 returned: always
333 type: str
334 sample: "urlfilter"
335 path:
336 description: Path of the table used to fulfill the request
337 returned: always
338 type: str
339 sample: "webfilter"
340 revision:
341 description: Internal revision number
342 returned: always
343 type: str
344 sample: "17.0.2.10658"
345 serial:
346 description: Serial number of the unit
347 returned: always
348 type: str
349 sample: "FGVMEVYYQT3AB5352"
350 status:
351 description: Indication of the operation's result
352 returned: always
353 type: str
354 sample: "success"
355 vdom:
356 description: Virtual domain used
357 returned: always
358 type: str
359 sample: "root"
360 version:
361 description: Version of the FortiGate
362 returned: always
363 type: str
364 sample: "v5.6.3"
365 """
366 from ansible.module_utils.basic import AnsibleModule
367 from ansible.module_utils.connection import Connection
368 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
369 FortiOSHandler,
370 )
371 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
372 check_legacy_fortiosapi,
373 )
374 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
375 schema_to_module_spec,
376 )
377 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
378 check_schema_versioning,
379 )
380 from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import (
381 FAIL_SOCKET_MSG,
382 )
383 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.data_post_processor import (
384 remove_invalid_fields,
385 )
386 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
387 is_same_comparison,
388 )
389 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
390 serialize,
391 )
392 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
393 find_current_values,
394 )
395 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
396 unify_data_format,
397 )
398
399
400 def filter_system_saml_data(json):
401 option_list = [
402 "artifact_resolution_url",
403 "binding_protocol",
404 "cert",
405 "default_login_page",
406 "default_profile",
407 "entity_id",
408 "idp_artifact_resolution_url",
409 "idp_cert",
410 "idp_entity_id",
411 "idp_single_logout_url",
412 "idp_single_sign_on_url",
413 "life",
414 "portal_url",
415 "require_signed_resp_and_asrt",
416 "role",
417 "server_address",
418 "service_providers",
419 "single_logout_url",
420 "single_sign_on_url",
421 "status",
422 "tolerance",
423 ]
424
425 json = remove_invalid_fields(json)
426 dictionary = {}
427
428 for attribute in option_list:
429 if attribute in json and json[attribute] is not None:
430 dictionary[attribute] = json[attribute]
431
432 return dictionary
433
434
435 def underscore_to_hyphen(data):
436 new_data = None
437 if isinstance(data, list):
438 new_data = []
439 for i, elem in enumerate(data):
440 new_data.append(underscore_to_hyphen(elem))
441 elif isinstance(data, dict):
442 new_data = {}
443 for k, v in data.items():
444 new_data[k.replace("_", "-")] = underscore_to_hyphen(v)
445 else:
446 return data
447 return new_data
448
449
450 def system_saml(data, fos, check_mode=False):
451
452 state = None
453 vdom = data["vdom"]
454 parameters = None
455 state = data.get("state", None)
456 system_saml_data = data["system_saml"]
457
458 filtered_data = filter_system_saml_data(system_saml_data)
459 converted_data = underscore_to_hyphen(filtered_data)
460
461 # check_mode starts from here
462 if check_mode:
463 diff = {
464 "before": "",
465 "after": filtered_data,
466 }
467 mkeyname = fos.get_mkeyname(None, None)
468 mkey = fos.get_mkey("system", "saml", filtered_data, vdom=vdom)
469 current_data = fos.get(
470 "system", "saml", vdom=vdom, mkey=mkey, parameters=parameters
471 )
472 is_existed = (
473 current_data
474 and current_data.get("http_status") == 200
475 and (
476 mkeyname
477 and isinstance(current_data.get("results"), list)
478 and len(current_data["results"]) > 0
479 or not mkeyname
480 and current_data["results"] # global object response
481 )
482 )
483
484 # 2. if it exists and the state is 'present' then compare current settings with desired
485 if state == "present" or state is True or state is None:
486 # for non global modules, mkeyname must exist and it's a new module when mkey is None
487 if mkeyname is not None and mkey is None:
488 return False, True, filtered_data, diff
489
490 # if mkey exists then compare each other
491 # record exits and they're matched or not
492 copied_filtered_data = filtered_data.copy()
493 copied_filtered_data.pop(mkeyname, None)
494 unified_filtered_data = unify_data_format(copied_filtered_data)
495
496 current_data_results = current_data.get("results", {})
497 current_config = (
498 current_data_results[0]
499 if mkeyname
500 and isinstance(current_data_results, list)
501 and len(current_data_results) > 0
502 else current_data_results
503 )
504 if is_existed:
505 unified_current_values = find_current_values(
506 unified_filtered_data,
507 unify_data_format(current_config),
508 )
509
510 is_same = is_same_comparison(
511 serialize(unified_current_values), serialize(unified_filtered_data)
512 )
513
514 return (
515 False,
516 not is_same,
517 filtered_data,
518 {"before": unified_current_values, "after": unified_filtered_data},
519 )
520
521 # record does not exist
522 return False, True, filtered_data, diff
523
524 if state == "absent":
525 if mkey is None:
526 return (
527 False,
528 False,
529 filtered_data,
530 {"before": current_data["results"][0], "after": ""},
531 )
532
533 if is_existed:
534 return (
535 False,
536 True,
537 filtered_data,
538 {"before": current_data["results"][0], "after": ""},
539 )
540 return False, False, filtered_data, {}
541
542 return True, False, {"reason: ": "Must provide state parameter"}, {}
543 # pass post processed data to member operations
544 # no need to do underscore_to_hyphen since do_member_operation handles it by itself
545 data_copy = data.copy()
546 data_copy["system_saml"] = filtered_data
547 fos.do_member_operation(
548 "system",
549 "saml",
550 data_copy,
551 )
552
553 return fos.set(
554 "system", "saml", data=converted_data, vdom=vdom, parameters=parameters
555 )
556
557
558 def is_successful_status(resp):
559 return (
560 "status" in resp
561 and resp["status"] == "success"
562 or "http_status" in resp
563 and resp["http_status"] == 200
564 or "http_method" in resp
565 and resp["http_method"] == "DELETE"
566 and resp["http_status"] == 404
567 )
568
569
570 def fortios_system(data, fos, check_mode):
571
572 if data["system_saml"]:
573 resp = system_saml(data, fos, check_mode)
574 else:
575 fos._module.fail_json(msg="missing task body: %s" % ("system_saml"))
576 if isinstance(resp, tuple) and len(resp) == 4:
577 return resp
578 return (
579 not is_successful_status(resp),
580 is_successful_status(resp)
581 and (resp["revision_changed"] if "revision_changed" in resp else True),
582 resp,
583 {},
584 )
585
586
587 versioned_schema = {
588 "v_range": [["v6.2.0", ""]],
589 "type": "dict",
590 "children": {
591 "status": {
592 "v_range": [["v6.2.0", ""]],
593 "type": "string",
594 "options": [{"value": "enable"}, {"value": "disable"}],
595 },
596 "role": {
597 "v_range": [["v6.2.0", ""]],
598 "type": "string",
599 "options": [{"value": "identity-provider"}, {"value": "service-provider"}],
600 },
601 "default_login_page": {
602 "v_range": [["v6.2.0", ""]],
603 "type": "string",
604 "options": [{"value": "normal"}, {"value": "sso"}],
605 },
606 "default_profile": {"v_range": [["v6.2.0", ""]], "type": "string"},
607 "cert": {"v_range": [["v6.2.0", ""]], "type": "string"},
608 "binding_protocol": {
609 "v_range": [["v6.2.3", "v6.2.3"], ["v7.0.0", ""]],
610 "type": "string",
611 "options": [
612 {"value": "post"},
613 {"value": "redirect", "v_range": [["v7.0.0", ""]]},
614 ],
615 },
616 "entity_id": {
617 "v_range": [["v6.2.0", "v7.0.5"], ["v7.2.0", "v7.2.0"], ["v7.4.1", ""]],
618 "type": "string",
619 },
620 "idp_entity_id": {"v_range": [["v6.2.0", ""]], "type": "string"},
621 "idp_single_sign_on_url": {"v_range": [["v6.2.0", ""]], "type": "string"},
622 "idp_single_logout_url": {"v_range": [["v6.2.0", ""]], "type": "string"},
623 "idp_cert": {"v_range": [["v6.2.0", ""]], "type": "string"},
624 "server_address": {"v_range": [["v6.2.0", ""]], "type": "string"},
625 "require_signed_resp_and_asrt": {
626 "v_range": [["v7.6.5", ""]],
627 "type": "string",
628 "options": [{"value": "enable"}, {"value": "disable"}],
629 },
630 "tolerance": {"v_range": [["v6.2.0", ""]], "type": "integer"},
631 "life": {"v_range": [["v6.2.0", ""]], "type": "integer"},
632 "service_providers": {
633 "type": "list",
634 "elements": "dict",
635 "children": {
636 "name": {
637 "v_range": [["v6.2.0", ""]],
638 "type": "string",
639 "required": True,
640 },
641 "prefix": {"v_range": [["v6.2.0", ""]], "type": "string"},
642 "sp_binding_protocol": {
643 "v_range": [["v6.2.3", "v6.2.3"], ["v7.0.0", ""]],
644 "type": "string",
645 "options": [
646 {"value": "post"},
647 {"value": "redirect", "v_range": [["v7.0.0", ""]]},
648 ],
649 },
650 "sp_cert": {"v_range": [["v6.2.0", ""]], "type": "string"},
651 "sp_entity_id": {"v_range": [["v6.2.0", ""]], "type": "string"},
652 "sp_single_sign_on_url": {
653 "v_range": [["v6.2.0", ""]],
654 "type": "string",
655 },
656 "sp_single_logout_url": {"v_range": [["v6.2.0", ""]], "type": "string"},
657 "sp_portal_url": {"v_range": [["v6.2.0", ""]], "type": "string"},
658 "assertion_attributes": {
659 "type": "list",
660 "elements": "dict",
661 "children": {
662 "name": {
663 "v_range": [["v6.2.0", ""]],
664 "type": "string",
665 "required": True,
666 },
667 "type": {
668 "v_range": [["v6.2.0", ""]],
669 "type": "string",
670 "options": [
671 {"value": "username"},
672 {"value": "email"},
673 {"value": "profile-name", "v_range": [["v6.4.0", ""]]},
674 ],
675 },
676 },
677 "v_range": [["v6.2.0", ""]],
678 },
679 "idp_entity_id": {
680 "v_range": [["v6.2.0", "v7.0.5"], ["v7.2.0", "v7.2.0"]],
681 "type": "string",
682 },
683 "idp_single_sign_on_url": {
684 "v_range": [["v6.2.0", "v7.0.5"], ["v7.2.0", "v7.2.0"]],
685 "type": "string",
686 },
687 "idp_single_logout_url": {
688 "v_range": [["v6.2.0", "v7.0.5"], ["v7.2.0", "v7.2.0"]],
689 "type": "string",
690 },
691 "sp_artifact_resolution_url": {
692 "v_range": [["v6.2.3", "v6.2.3"]],
693 "type": "string",
694 },
695 "idp_artifact_resolution_url": {
696 "v_range": [["v6.2.3", "v6.2.3"]],
697 "type": "string",
698 },
699 },
700 "v_range": [["v6.2.0", ""]],
701 },
702 "portal_url": {
703 "v_range": [["v6.2.0", "v7.0.5"], ["v7.2.0", "v7.2.0"]],
704 "type": "string",
705 },
706 "single_sign_on_url": {
707 "v_range": [["v6.2.0", "v7.0.5"], ["v7.2.0", "v7.2.0"]],
708 "type": "string",
709 },
710 "single_logout_url": {
711 "v_range": [["v6.2.0", "v7.0.5"], ["v7.2.0", "v7.2.0"]],
712 "type": "string",
713 },
714 "artifact_resolution_url": {
715 "v_range": [["v6.2.3", "v6.2.3"]],
716 "type": "string",
717 },
718 "idp_artifact_resolution_url": {
719 "v_range": [["v6.2.3", "v6.2.3"]],
720 "type": "string",
721 },
722 },
723 }
724
725
726 def main():
727 module_spec = schema_to_module_spec(versioned_schema)
728 mkeyname = None
729 fields = {
730 "access_token": {"required": False, "type": "str", "no_log": True},
731 "enable_log": {"required": False, "type": "bool", "default": False},
732 "vdom": {"required": False, "type": "str", "default": "root"},
733 "member_path": {"required": False, "type": "str"},
734 "member_state": {
735 "type": "str",
736 "required": False,
737 "choices": ["present", "absent"],
738 },
739 "system_saml": {
740 "required": False,
741 "type": "dict",
742 "default": None,
743 "options": {},
744 },
745 }
746 for attribute_name in module_spec["options"]:
747 fields["system_saml"]["options"][attribute_name] = module_spec["options"][
748 attribute_name
749 ]
750 if mkeyname and mkeyname == attribute_name:
751 fields["system_saml"]["options"][attribute_name]["required"] = True
752
753 module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
754 check_legacy_fortiosapi(module)
755
756 is_error = False
757 has_changed = False
758 result = None
759 diff = None
760
761 versions_check_result = None
762 if module._socket_path:
763 connection = Connection(module._socket_path)
764 if "access_token" in module.params:
765 connection.set_custom_option("access_token", module.params["access_token"])
766
767 if "enable_log" in module.params:
768 connection.set_custom_option("enable_log", module.params["enable_log"])
769 else:
770 connection.set_custom_option("enable_log", False)
771 fos = FortiOSHandler(connection, module, mkeyname, admin_passwd_header=False)
772 versions_check_result = check_schema_versioning(
773 fos, versioned_schema, "system_saml"
774 )
775
776 is_error, has_changed, result, diff = fortios_system(
777 module.params, fos, module.check_mode
778 )
779
780 else:
781 module.fail_json(**FAIL_SOCKET_MSG)
782
783 if versions_check_result and versions_check_result["matched"] is False:
784 module.warn(
785 "Ansible has detected version mismatch between FortOS system and your playbook, see more details by specifying option -vvv"
786 )
787
788 if not is_error:
789 if versions_check_result and versions_check_result["matched"] is False:
790 module.exit_json(
791 changed=has_changed,
792 version_check_warning=versions_check_result,
793 meta=result,
794 diff=diff,
795 )
796 else:
797 module.exit_json(changed=has_changed, meta=result, diff=diff)
798 else:
799 if versions_check_result and versions_check_result["matched"] is False:
800 module.fail_json(
801 msg="Error in repo",
802 version_check_warning=versions_check_result,
803 meta=result,
804 )
805 else:
806 module.fail_json(msg="Error in repo", meta=result)
807
808
809 if __name__ == "__main__":
810 main()