Debian Codegraph @

ansible_collections/fortinet/fortios/plugins/modules/fortios_user_radius.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_radius
23 short_description: Configure RADIUS 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 radius 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_radius:
88 description:
89 - Configure RADIUS 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_processing:
103 description:
104 - Account key processing operation. The FortiGate will keep either the whole domain or strip the domain from the subject identity.
105 type: str
106 choices:
107 - 'same'
108 - 'strip'
109 accounting_server:
110 description:
111 - Additional accounting servers.
112 type: list
113 elements: dict
114 suboptions:
115 id:
116 description:
117 - ID (0 - 4294967295). see <a href='#notes'>Notes</a>.
118 required: true
119 type: int
120 interface:
121 description:
122 - Specify outgoing interface to reach server. Source system.interface.name.
123 type: str
124 interface_select_method:
125 description:
126 - Specify how to select outgoing interface to reach server.
127 type: str
128 choices:
129 - 'auto'
130 - 'sdwan'
131 - 'specify'
132 port:
133 description:
134 - RADIUS accounting port number.
135 type: int
136 secret:
137 description:
138 - Secret key.
139 type: str
140 server:
141 description:
142 - Server CN domain name or IP address.
143 type: str
144 source_ip:
145 description:
146 - Source IP address for communications to the RADIUS server.
147 type: str
148 status:
149 description:
150 - Status.
151 type: str
152 choices:
153 - 'enable'
154 - 'disable'
155 vrf_select:
156 description:
157 - VRF ID used for connection to server.
158 type: int
159 acct_all_servers:
160 description:
161 - Enable/disable sending of accounting messages to all configured servers .
162 type: str
163 choices:
164 - 'enable'
165 - 'disable'
166 acct_interim_interval:
167 description:
168 - Time in seconds between each accounting interim update message.
169 type: int
170 all_usergroup:
171 description:
172 - Enable/disable automatically including this RADIUS server in all user groups.
173 type: str
174 choices:
175 - 'disable'
176 - 'enable'
177 auth_type:
178 description:
179 - Authentication methods/protocols permitted for this RADIUS server.
180 type: str
181 choices:
182 - 'auto'
183 - 'ms_chap_v2'
184 - 'ms_chap'
185 - 'chap'
186 - 'pap'
187 ca_cert:
188 description:
189 - CA of server to trust under TLS. Source vpn.certificate.ca.name.
190 type: str
191 call_station_id_type:
192 description:
193 - Calling & Called station identifier type configuration , this option is not available for 802.1x authentication.
194 type: str
195 choices:
196 - 'legacy'
197 - 'IP'
198 - 'MAC'
199 class:
200 description:
201 - Class attribute name(s).
202 type: list
203 elements: dict
204 suboptions:
205 name:
206 description:
207 - Class name.
208 required: true
209 type: str
210 client_cert:
211 description:
212 - Client certificate to use under TLS. Source vpn.certificate.local.name.
213 type: str
214 delimiter:
215 description:
216 - Configure delimiter to be used for separating profile group names in the SSO attribute .
217 type: str
218 choices:
219 - 'plus'
220 - 'comma'
221 group_override_attr_type:
222 description:
223 - RADIUS attribute type to override user group information.
224 type: str
225 choices:
226 - 'filter-Id'
227 - 'class'
228 h3c_compatibility:
229 description:
230 - Enable/disable compatibility with the H3C, a mechanism that performs security checking for authentication.
231 type: str
232 choices:
233 - 'enable'
234 - 'disable'
235 interface:
236 description:
237 - Specify outgoing interface to reach server. Source system.interface.name.
238 type: str
239 interface_select_method:
240 description:
241 - Specify how to select outgoing interface to reach server.
242 type: str
243 choices:
244 - 'auto'
245 - 'sdwan'
246 - 'specify'
247 mac_case:
248 description:
249 - MAC authentication case .
250 type: str
251 choices:
252 - 'uppercase'
253 - 'lowercase'
254 mac_password_delimiter:
255 description:
256 - MAC authentication password delimiter .
257 type: str
258 choices:
259 - 'hyphen'
260 - 'single-hyphen'
261 - 'colon'
262 - 'none'
263 mac_username_delimiter:
264 description:
265 - MAC authentication username delimiter .
266 type: str
267 choices:
268 - 'hyphen'
269 - 'single-hyphen'
270 - 'colon'
271 - 'none'
272 name:
273 description:
274 - RADIUS server entry name.
275 required: true
276 type: str
277 nas_id:
278 description:
279 - Custom NAS identifier.
280 type: str
281 nas_id_type:
282 description:
283 - NAS identifier type configuration .
284 type: str
285 choices:
286 - 'legacy'
287 - 'custom'
288 - 'hostname'
289 nas_ip:
290 description:
291 - IP address used to communicate with the RADIUS server and used as NAS-IP-Address and Called-Station-ID attributes.
292 type: str
293 password_encoding:
294 description:
295 - Password encoding.
296 type: str
297 choices:
298 - 'auto'
299 - 'ISO-8859-1'
300 password_renewal:
301 description:
302 - Enable/disable password renewal.
303 type: str
304 choices:
305 - 'enable'
306 - 'disable'
307 radius_coa:
308 description:
309 - Enable to allow a mechanism to change the attributes of an authentication, authorization, and accounting session after it is
310 authenticated.
311 type: str
312 choices:
313 - 'enable'
314 - 'disable'
315 radius_port:
316 description:
317 - RADIUS service port number.
318 type: int
319 require_message_authenticator:
320 description:
321 - Require message authenticator in authentication response.
322 type: str
323 choices:
324 - 'enable'
325 - 'disable'
326 rsso:
327 description:
328 - Enable/disable RADIUS based single sign on feature.
329 type: str
330 choices:
331 - 'enable'
332 - 'disable'
333 rsso_context_timeout:
334 description:
335 - Time in seconds before the logged out user is removed from the "user context list" of logged on users.
336 type: int
337 rsso_endpoint_attribute:
338 description:
339 - RADIUS attributes used to extract the user end point identifier from the RADIUS Start record.
340 type: str
341 choices:
342 - 'User-Name'
343 - 'NAS-IP-Address'
344 - 'Framed-IP-Address'
345 - 'Framed-IP-Netmask'
346 - 'Filter-Id'
347 - 'Login-IP-Host'
348 - 'Reply-Message'
349 - 'Callback-Number'
350 - 'Callback-Id'
351 - 'Framed-Route'
352 - 'Framed-IPX-Network'
353 - 'Class'
354 - 'Called-Station-Id'
355 - 'Calling-Station-Id'
356 - 'NAS-Identifier'
357 - 'Proxy-State'
358 - 'Login-LAT-Service'
359 - 'Login-LAT-Node'
360 - 'Login-LAT-Group'
361 - 'Framed-AppleTalk-Zone'
362 - 'Acct-Session-Id'
363 - 'Acct-Multi-Session-Id'
364 rsso_endpoint_block_attribute:
365 description:
366 - RADIUS attributes used to block a user.
367 type: str
368 choices:
369 - 'User-Name'
370 - 'NAS-IP-Address'
371 - 'Framed-IP-Address'
372 - 'Framed-IP-Netmask'
373 - 'Filter-Id'
374 - 'Login-IP-Host'
375 - 'Reply-Message'
376 - 'Callback-Number'
377 - 'Callback-Id'
378 - 'Framed-Route'
379 - 'Framed-IPX-Network'
380 - 'Class'
381 - 'Called-Station-Id'
382 - 'Calling-Station-Id'
383 - 'NAS-Identifier'
384 - 'Proxy-State'
385 - 'Login-LAT-Service'
386 - 'Login-LAT-Node'
387 - 'Login-LAT-Group'
388 - 'Framed-AppleTalk-Zone'
389 - 'Acct-Session-Id'
390 - 'Acct-Multi-Session-Id'
391 rsso_ep_one_ip_only:
392 description:
393 - Enable/disable the replacement of old IP addresses with new ones for the same endpoint on RADIUS accounting Start messages.
394 type: str
395 choices:
396 - 'enable'
397 - 'disable'
398 rsso_flush_ip_session:
399 description:
400 - Enable/disable flushing user IP sessions on RADIUS accounting Stop messages.
401 type: str
402 choices:
403 - 'enable'
404 - 'disable'
405 rsso_log_flags:
406 description:
407 - Events to log.
408 type: list
409 elements: str
410 choices:
411 - 'protocol-error'
412 - 'profile-missing'
413 - 'accounting-stop-missed'
414 - 'accounting-event'
415 - 'endpoint-block'
416 - 'radiusd-other'
417 - 'none'
418 rsso_log_period:
419 description:
420 - Time interval in seconds that group event log messages will be generated for dynamic profile events.
421 type: int
422 rsso_radius_response:
423 description:
424 - Enable/disable sending RADIUS response packets after receiving Start and Stop records.
425 type: str
426 choices:
427 - 'enable'
428 - 'disable'
429 rsso_radius_server_port:
430 description:
431 - UDP port to listen on for RADIUS Start and Stop records.
432 type: int
433 rsso_secret:
434 description:
435 - RADIUS secret used by the RADIUS accounting server.
436 type: str
437 rsso_validate_request_secret:
438 description:
439 - Enable/disable validating the RADIUS request shared secret in the Start or End record.
440 type: str
441 choices:
442 - 'enable'
443 - 'disable'
444 secondary_secret:
445 description:
446 - Secret key to access the secondary server.
447 type: str
448 secondary_server:
449 description:
450 - Secondary RADIUS CN domain name or IP address.
451 type: str
452 secret:
453 description:
454 - Pre-shared secret key used to access the primary RADIUS server.
455 type: str
456 server:
457 description:
458 - Primary RADIUS server CN domain name or IP address.
459 type: str
460 server_identity_check:
461 description:
462 - Enable/disable RADIUS server identity check (verify server domain name/IP address against the server certificate).
463 type: str
464 choices:
465 - 'enable'
466 - 'disable'
467 source_ip:
468 description:
469 - Source IP address for communications to the RADIUS server.
470 type: str
471 source_ip_interface:
472 description:
473 - Source interface for communication with the RADIUS server. Source system.interface.name.
474 type: str
475 sso_attribute:
476 description:
477 - RADIUS attribute that contains the profile group name to be extracted from the RADIUS Start record.
478 type: str
479 choices:
480 - 'User-Name'
481 - 'NAS-IP-Address'
482 - 'Framed-IP-Address'
483 - 'Framed-IP-Netmask'
484 - 'Filter-Id'
485 - 'Login-IP-Host'
486 - 'Reply-Message'
487 - 'Callback-Number'
488 - 'Callback-Id'
489 - 'Framed-Route'
490 - 'Framed-IPX-Network'
491 - 'Class'
492 - 'Called-Station-Id'
493 - 'Calling-Station-Id'
494 - 'NAS-Identifier'
495 - 'Proxy-State'
496 - 'Login-LAT-Service'
497 - 'Login-LAT-Node'
498 - 'Login-LAT-Group'
499 - 'Framed-AppleTalk-Zone'
500 - 'Acct-Session-Id'
501 - 'Acct-Multi-Session-Id'
502 sso_attribute_key:
503 description:
504 - Key prefix for SSO group value in the SSO attribute.
505 type: str
506 sso_attribute_value_override:
507 description:
508 - Enable/disable override old attribute value with new value for the same endpoint.
509 type: str
510 choices:
511 - 'enable'
512 - 'disable'
513 status_ttl:
514 description:
515 - 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
516 time (0 = cache disabled).
517 type: int
518 switch_controller_acct_fast_framedip_detect:
519 description:
520 - Switch controller accounting message Framed-IP detection from DHCP snooping (seconds).
521 type: int
522 switch_controller_nas_ip_dynamic:
523 description:
524 - Enable/Disable switch-controller nas-ip dynamic to dynamically set nas-ip.
525 type: str
526 choices:
527 - 'enable'
528 - 'disable'
529 switch_controller_service_type:
530 description:
531 - RADIUS service type.
532 type: list
533 elements: str
534 choices:
535 - 'login'
536 - 'framed'
537 - 'callback-login'
538 - 'callback-framed'
539 - 'outbound'
540 - 'administrative'
541 - 'nas-prompt'
542 - 'authenticate-only'
543 - 'callback-nas-prompt'
544 - 'call-check'
545 - 'callback-administrative'
546 tertiary_secret:
547 description:
548 - Secret key to access the tertiary server.
549 type: str
550 tertiary_server:
551 description:
552 - Tertiary RADIUS CN domain name or IP address.
553 type: str
554 timeout:
555 description:
556 - Time in seconds to retry connecting server.
557 type: int
558 tls_min_proto_version:
559 description:
560 - Minimum supported protocol version for TLS connections .
561 type: str
562 choices:
563 - 'default'
564 - 'SSLv3'
565 - 'TLSv1'
566 - 'TLSv1-1'
567 - 'TLSv1-2'
568 - 'TLSv1-3'
569 transport_protocol:
570 description:
571 - Transport protocol to be used .
572 type: str
573 choices:
574 - 'udp'
575 - 'tcp'
576 - 'tls'
577 use_management_vdom:
578 description:
579 - Enable/disable using management VDOM to send requests.
580 type: str
581 choices:
582 - 'enable'
583 - 'disable'
584 username_case_sensitive:
585 description:
586 - Enable/disable case sensitive user names.
587 type: str
588 choices:
589 - 'enable'
590 - 'disable'
591 vrf_select:
592 description:
593 - VRF ID used for connection to server.
594 type: int
595 """
596
597 EXAMPLES = """
598 - name: Configure RADIUS server entries.
599 fortinet.fortios.fortios_user_radius:
600 vdom: "{{ vdom }}"
601 state: "present"
602 access_token: "<your_own_value>"
603 user_radius:
604 account_key_cert_field: "othername"
605 account_key_processing: "same"
606 accounting_server:
607 -
608 id: "6"
609 interface: "<your_own_value> (source system.interface.name)"
610 interface_select_method: "auto"
611 port: "0"
612 secret: "<your_own_value>"
613 server: "192.168.100.40"
614 source_ip: "84.230.14.43"
615 status: "enable"
616 vrf_select: "0"
617 acct_all_servers: "enable"
618 acct_interim_interval: "0"
619 all_usergroup: "disable"
620 auth_type: "auto"
621 ca_cert: "<your_own_value> (source vpn.certificate.ca.name)"
622 call_station_id_type: "legacy"
623 class:
624 -
625 name: "default_name_22"
626 client_cert: "<your_own_value> (source vpn.certificate.local.name)"
627 delimiter: "plus"
628 group_override_attr_type: "filter-Id"
629 h3c_compatibility: "enable"
630 interface: "<your_own_value> (source system.interface.name)"
631 interface_select_method: "auto"
632 mac_case: "uppercase"
633 mac_password_delimiter: "hyphen"
634 mac_username_delimiter: "hyphen"
635 name: "default_name_32"
636 nas_id: "<your_own_value>"
637 nas_id_type: "legacy"
638 nas_ip: "<your_own_value>"
639 password_encoding: "auto"
640 password_renewal: "enable"
641 radius_coa: "enable"
642 radius_port: "0"
643 require_message_authenticator: "enable"
644 rsso: "enable"
645 rsso_context_timeout: "28800"
646 rsso_endpoint_attribute: "User-Name"
647 rsso_endpoint_block_attribute: "User-Name"
648 rsso_ep_one_ip_only: "enable"
649 rsso_flush_ip_session: "enable"
650 rsso_log_flags: "protocol-error"
651 rsso_log_period: "0"
652 rsso_radius_response: "enable"
653 rsso_radius_server_port: "1813"
654 rsso_secret: "<your_own_value>"
655 rsso_validate_request_secret: "enable"
656 secondary_secret: "<your_own_value>"
657 secondary_server: "<your_own_value>"
658 secret: "<your_own_value>"
659 server: "192.168.100.40"
660 server_identity_check: "enable"
661 source_ip: "84.230.14.43"
662 source_ip_interface: "<your_own_value> (source system.interface.name)"
663 sso_attribute: "User-Name"
664 sso_attribute_key: "<your_own_value>"
665 sso_attribute_value_override: "enable"
666 status_ttl: "300"
667 switch_controller_acct_fast_framedip_detect: "2"
668 switch_controller_nas_ip_dynamic: "enable"
669 switch_controller_service_type: "login"
670 tertiary_secret: "<your_own_value>"
671 tertiary_server: "<your_own_value>"
672 timeout: "5"
673 tls_min_proto_version: "default"
674 transport_protocol: "udp"
675 use_management_vdom: "enable"
676 username_case_sensitive: "enable"
677 vrf_select: "0"
678 """
679
680 RETURN = """
681 build:
682 description: Build number of the fortigate image
683 returned: always
684 type: str
685 sample: '1547'
686 http_method:
687 description: Last method used to provision the content into FortiGate
688 returned: always
689 type: str
690 sample: 'PUT'
691 http_status:
692 description: Last result given by FortiGate on last operation applied
693 returned: always
694 type: str
695 sample: "200"
696 mkey:
697 description: Master key (id) used in the last call to FortiGate
698 returned: success
699 type: str
700 sample: "id"
701 name:
702 description: Name of the table used to fulfill the request
703 returned: always
704 type: str
705 sample: "urlfilter"
706 path:
707 description: Path of the table used to fulfill the request
708 returned: always
709 type: str
710 sample: "webfilter"
711 revision:
712 description: Internal revision number
713 returned: always
714 type: str
715 sample: "17.0.2.10658"
716 serial:
717 description: Serial number of the unit
718 returned: always
719 type: str
720 sample: "FGVMEVYYQT3AB5352"
721 status:
722 description: Indication of the operation's result
723 returned: always
724 type: str
725 sample: "success"
726 vdom:
727 description: Virtual domain used
728 returned: always
729 type: str
730 sample: "root"
731 version:
732 description: Version of the FortiGate
733 returned: always
734 type: str
735 sample: "v5.6.3"
736 """
737 from ansible.module_utils.basic import AnsibleModule
738 from ansible.module_utils.connection import Connection
739 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
740 FortiOSHandler,
741 )
742 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
743 check_legacy_fortiosapi,
744 )
745 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
746 schema_to_module_spec,
747 )
748 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
749 check_schema_versioning,
750 )
751 from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import (
752 FAIL_SOCKET_MSG,
753 )
754 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.data_post_processor import (
755 remove_invalid_fields,
756 )
757 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
758 is_same_comparison,
759 )
760 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
761 serialize,
762 )
763 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
764 find_current_values,
765 )
766 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
767 unify_data_format,
768 )
769
770
771 def filter_user_radius_data(json):
772 option_list = [
773 "account_key_cert_field",
774 "account_key_processing",
775 "accounting_server",
776 "acct_all_servers",
777 "acct_interim_interval",
778 "all_usergroup",
779 "auth_type",
780 "ca_cert",
781 "call_station_id_type",
782 "class",
783 "client_cert",
784 "delimiter",
785 "group_override_attr_type",
786 "h3c_compatibility",
787 "interface",
788 "interface_select_method",
789 "mac_case",
790 "mac_password_delimiter",
791 "mac_username_delimiter",
792 "name",
793 "nas_id",
794 "nas_id_type",
795 "nas_ip",
796 "password_encoding",
797 "password_renewal",
798 "radius_coa",
799 "radius_port",
800 "require_message_authenticator",
801 "rsso",
802 "rsso_context_timeout",
803 "rsso_endpoint_attribute",
804 "rsso_endpoint_block_attribute",
805 "rsso_ep_one_ip_only",
806 "rsso_flush_ip_session",
807 "rsso_log_flags",
808 "rsso_log_period",
809 "rsso_radius_response",
810 "rsso_radius_server_port",
811 "rsso_secret",
812 "rsso_validate_request_secret",
813 "secondary_secret",
814 "secondary_server",
815 "secret",
816 "server",
817 "server_identity_check",
818 "source_ip",
819 "source_ip_interface",
820 "sso_attribute",
821 "sso_attribute_key",
822 "sso_attribute_value_override",
823 "status_ttl",
824 "switch_controller_acct_fast_framedip_detect",
825 "switch_controller_nas_ip_dynamic",
826 "switch_controller_service_type",
827 "tertiary_secret",
828 "tertiary_server",
829 "timeout",
830 "tls_min_proto_version",
831 "transport_protocol",
832 "use_management_vdom",
833 "username_case_sensitive",
834 "vrf_select",
835 ]
836
837 json = remove_invalid_fields(json)
838 dictionary = {}
839
840 for attribute in option_list:
841 if attribute in json and json[attribute] is not None:
842 dictionary[attribute] = json[attribute]
843
844 return dictionary
845
846
847 def flatten_single_path(data, path, index):
848 if (
849 not data
850 or index == len(path)
851 or path[index] not in data
852 or (not data[path[index]] and not isinstance(data[path[index]], list))
853 ):
854 return
855
856 if index == len(path) - 1:
857 data[path[index]] = " ".join(str(elem) for elem in data[path[index]])
858 if len(data[path[index]]) == 0:
859 data[path[index]] = None
860 elif isinstance(data[path[index]], list):
861 for value in data[path[index]]:
862 flatten_single_path(value, path, index + 1)
863 else:
864 flatten_single_path(data[path[index]], path, index + 1)
865
866
867 def flatten_multilists_attributes(data):
868 multilist_attrs = [
869 ["switch_controller_service_type"],
870 ["rsso_log_flags"],
871 ]
872
873 for attr in multilist_attrs:
874 flatten_single_path(data, attr, 0)
875
876 return data
877
878
879 def underscore_to_hyphen(data):
880 new_data = None
881 if isinstance(data, list):
882 new_data = []
883 for i, elem in enumerate(data):
884 new_data.append(underscore_to_hyphen(elem))
885 elif isinstance(data, dict):
886 new_data = {}
887 for k, v in data.items():
888 new_data[k.replace("_", "-")] = underscore_to_hyphen(v)
889 else:
890 return data
891 return new_data
892
893
894 def user_radius(data, fos, check_mode=False):
895
896 state = None
897 vdom = data["vdom"]
898 parameters = None
899 state = data.get("state", None)
900 user_radius_data = data["user_radius"]
901
902 filtered_data = filter_user_radius_data(user_radius_data)
903 filtered_data = flatten_multilists_attributes(filtered_data)
904 converted_data = underscore_to_hyphen(filtered_data)
905
906 # check_mode starts from here
907 if check_mode:
908 diff = {
909 "before": "",
910 "after": filtered_data,
911 }
912 mkeyname = fos.get_mkeyname(None, None)
913 mkey = fos.get_mkey("user", "radius", filtered_data, vdom=vdom)
914 current_data = fos.get(
915 "user", "radius", vdom=vdom, mkey=mkey, parameters=parameters
916 )
917 is_existed = (
918 current_data
919 and current_data.get("http_status") == 200
920 and (
921 mkeyname
922 and isinstance(current_data.get("results"), list)
923 and len(current_data["results"]) > 0
924 or not mkeyname
925 and current_data["results"] # global object response
926 )
927 )
928
929 # 2. if it exists and the state is 'present' then compare current settings with desired
930 if state == "present" or state is True or state is None:
931 # for non global modules, mkeyname must exist and it's a new module when mkey is None
932 if mkeyname is not None and mkey is None:
933 return False, True, filtered_data, diff
934
935 # if mkey exists then compare each other
936 # record exits and they're matched or not
937 copied_filtered_data = filtered_data.copy()
938 copied_filtered_data.pop(mkeyname, None)
939 unified_filtered_data = unify_data_format(copied_filtered_data)
940
941 current_data_results = current_data.get("results", {})
942 current_config = (
943 current_data_results[0]
944 if mkeyname
945 and isinstance(current_data_results, list)
946 and len(current_data_results) > 0
947 else current_data_results
948 )
949 if is_existed:
950 unified_current_values = find_current_values(
951 unified_filtered_data,
952 unify_data_format(current_config),
953 )
954
955 is_same = is_same_comparison(
956 serialize(unified_current_values), serialize(unified_filtered_data)
957 )
958
959 return (
960 False,
961 not is_same,
962 filtered_data,
963 {"before": unified_current_values, "after": unified_filtered_data},
964 )
965
966 # record does not exist
967 return False, True, filtered_data, diff
968
969 if state == "absent":
970 if mkey is None:
971 return (
972 False,
973 False,
974 filtered_data,
975 {"before": current_data["results"][0], "after": ""},
976 )
977
978 if is_existed:
979 return (
980 False,
981 True,
982 filtered_data,
983 {"before": current_data["results"][0], "after": ""},
984 )
985 return False, False, filtered_data, {}
986
987 return True, False, {"reason: ": "Must provide state parameter"}, {}
988 # pass post processed data to member operations
989 # no need to do underscore_to_hyphen since do_member_operation handles it by itself
990 data_copy = data.copy()
991 data_copy["user_radius"] = filtered_data
992 fos.do_member_operation(
993 "user",
994 "radius",
995 data_copy,
996 )
997
998 if state == "present" or state is True:
999 return fos.set(
1000 "user", "radius", data=converted_data, vdom=vdom, parameters=parameters
1001 )
1002
1003 elif state == "absent":
1004 return fos.delete(
1005 "user",
1006 "radius",
1007 mkey=converted_data["name"],
1008 vdom=vdom,
1009 parameters=parameters,
1010 )
1011 else:
1012 fos._module.fail_json(msg="state must be present or absent!")
1013
1014
1015 def is_successful_status(resp):
1016 return (
1017 "status" in resp
1018 and resp["status"] == "success"
1019 or "http_status" in resp
1020 and resp["http_status"] == 200
1021 or "http_method" in resp
1022 and resp["http_method"] == "DELETE"
1023 and resp["http_status"] == 404
1024 )
1025
1026
1027 def fortios_user(data, fos, check_mode):
1028
1029 if data["user_radius"]:
1030 resp = user_radius(data, fos, check_mode)
1031 else:
1032 fos._module.fail_json(msg="missing task body: %s" % ("user_radius"))
1033 if isinstance(resp, tuple) and len(resp) == 4:
1034 return resp
1035 return (
1036 not is_successful_status(resp),
1037 is_successful_status(resp)
1038 and (resp["revision_changed"] if "revision_changed" in resp else True),
1039 resp,
1040 {},
1041 )
1042
1043
1044 versioned_schema = {
1045 "type": "list",
1046 "elements": "dict",
1047 "children": {
1048 "name": {"v_range": [["v6.0.0", ""]], "type": "string", "required": True},
1049 "server": {"v_range": [["v6.0.0", ""]], "type": "string"},
1050 "secret": {"v_range": [["v6.0.0", ""]], "type": "string"},
1051 "secondary_server": {"v_range": [["v6.0.0", ""]], "type": "string"},
1052 "secondary_secret": {"v_range": [["v6.0.0", ""]], "type": "string"},
1053 "tertiary_server": {"v_range": [["v6.0.0", ""]], "type": "string"},
1054 "tertiary_secret": {"v_range": [["v6.0.0", ""]], "type": "string"},
1055 "timeout": {"v_range": [["v6.0.0", ""]], "type": "integer"},
1056 "status_ttl": {"v_range": [["v7.4.0", ""]], "type": "integer"},
1057 "all_usergroup": {
1058 "v_range": [["v6.0.0", ""]],
1059 "type": "string",
1060 "options": [{"value": "disable"}, {"value": "enable"}],
1061 },
1062 "use_management_vdom": {
1063 "v_range": [["v6.0.0", ""]],
1064 "type": "string",
1065 "options": [{"value": "enable"}, {"value": "disable"}],
1066 },
1067 "switch_controller_nas_ip_dynamic": {
1068 "v_range": [["v7.4.2", ""]],
1069 "type": "string",
1070 "options": [{"value": "enable"}, {"value": "disable"}],
1071 },
1072 "nas_ip": {"v_range": [["v6.0.0", ""]], "type": "string"},
1073 "nas_id_type": {
1074 "v_range": [["v7.2.4", ""]],
1075 "type": "string",
1076 "options": [
1077 {"value": "legacy"},
1078 {"value": "custom"},
1079 {"value": "hostname"},
1080 ],
1081 },
1082 "call_station_id_type": {
1083 "v_range": [["v7.4.1", ""]],
1084 "type": "string",
1085 "options": [{"value": "legacy"}, {"value": "IP"}, {"value": "MAC"}],
1086 },
1087 "nas_id": {"v_range": [["v7.2.4", ""]], "type": "string"},
1088 "acct_interim_interval": {"v_range": [["v6.0.0", ""]], "type": "integer"},
1089 "radius_coa": {
1090 "v_range": [["v6.0.0", ""]],
1091 "type": "string",
1092 "options": [{"value": "enable"}, {"value": "disable"}],
1093 },
1094 "radius_port": {"v_range": [["v6.0.0", ""]], "type": "integer"},
1095 "h3c_compatibility": {
1096 "v_range": [["v6.0.0", ""]],
1097 "type": "string",
1098 "options": [{"value": "enable"}, {"value": "disable"}],
1099 },
1100 "auth_type": {
1101 "v_range": [["v6.0.0", ""]],
1102 "type": "string",
1103 "options": [
1104 {"value": "auto"},
1105 {"value": "ms_chap_v2"},
1106 {"value": "ms_chap"},
1107 {"value": "chap"},
1108 {"value": "pap"},
1109 ],
1110 },
1111 "source_ip": {"v_range": [["v6.0.0", ""]], "type": "string"},
1112 "source_ip_interface": {"v_range": [["v7.6.0", ""]], "type": "string"},
1113 "username_case_sensitive": {
1114 "v_range": [["v6.0.0", ""]],
1115 "type": "string",
1116 "options": [{"value": "enable"}, {"value": "disable"}],
1117 },
1118 "group_override_attr_type": {
1119 "v_range": [["v6.4.0", ""]],
1120 "type": "string",
1121 "options": [{"value": "filter-Id"}, {"value": "class"}],
1122 },
1123 "class": {
1124 "type": "list",
1125 "elements": "dict",
1126 "children": {
1127 "name": {
1128 "v_range": [["v6.0.0", ""]],
1129 "type": "string",
1130 "required": True,
1131 }
1132 },
1133 "v_range": [["v6.0.0", ""]],
1134 },
1135 "password_renewal": {
1136 "v_range": [["v6.0.0", ""]],
1137 "type": "string",
1138 "options": [{"value": "enable"}, {"value": "disable"}],
1139 },
1140 "require_message_authenticator": {
1141 "v_range": [["v7.6.1", ""]],
1142 "type": "string",
1143 "options": [{"value": "enable"}, {"value": "disable"}],
1144 },
1145 "password_encoding": {
1146 "v_range": [["v6.0.0", ""]],
1147 "type": "string",
1148 "options": [{"value": "auto"}, {"value": "ISO-8859-1"}],
1149 },
1150 "mac_username_delimiter": {
1151 "v_range": [["v7.2.1", ""]],
1152 "type": "string",
1153 "options": [
1154 {"value": "hyphen"},
1155 {"value": "single-hyphen"},
1156 {"value": "colon"},
1157 {"value": "none"},
1158 ],
1159 },
1160 "mac_password_delimiter": {
1161 "v_range": [["v7.2.1", ""]],
1162 "type": "string",
1163 "options": [
1164 {"value": "hyphen"},
1165 {"value": "single-hyphen"},
1166 {"value": "colon"},
1167 {"value": "none"},
1168 ],
1169 },
1170 "mac_case": {
1171 "v_range": [["v7.2.1", ""]],
1172 "type": "string",
1173 "options": [{"value": "uppercase"}, {"value": "lowercase"}],
1174 },
1175 "acct_all_servers": {
1176 "v_range": [["v6.0.0", ""]],
1177 "type": "string",
1178 "options": [{"value": "enable"}, {"value": "disable"}],
1179 },
1180 "switch_controller_acct_fast_framedip_detect": {
1181 "v_range": [["v6.4.0", ""]],
1182 "type": "integer",
1183 },
1184 "interface_select_method": {
1185 "v_range": [["v6.2.0", "v6.2.0"], ["v6.2.5", ""]],
1186 "type": "string",
1187 "options": [{"value": "auto"}, {"value": "sdwan"}, {"value": "specify"}],
1188 },
1189 "interface": {
1190 "v_range": [["v6.2.0", "v6.2.0"], ["v6.2.5", ""]],
1191 "type": "string",
1192 },
1193 "vrf_select": {"v_range": [["v7.6.1", ""]], "type": "integer"},
1194 "switch_controller_service_type": {
1195 "v_range": [["v6.4.0", "v6.4.0"], ["v6.4.4", ""]],
1196 "type": "list",
1197 "options": [
1198 {"value": "login"},
1199 {"value": "framed"},
1200 {"value": "callback-login"},
1201 {"value": "callback-framed"},
1202 {"value": "outbound"},
1203 {"value": "administrative"},
1204 {"value": "nas-prompt"},
1205 {"value": "authenticate-only"},
1206 {"value": "callback-nas-prompt"},
1207 {"value": "call-check"},
1208 {"value": "callback-administrative"},
1209 ],
1210 "multiple_values": True,
1211 "elements": "str",
1212 },
1213 "transport_protocol": {
1214 "v_range": [["v7.4.0", ""]],
1215 "type": "string",
1216 "options": [{"value": "udp"}, {"value": "tcp"}, {"value": "tls"}],
1217 },
1218 "tls_min_proto_version": {
1219 "v_range": [["v7.4.0", ""]],
1220 "type": "string",
1221 "options": [
1222 {"value": "default"},
1223 {"value": "SSLv3"},
1224 {"value": "TLSv1"},
1225 {"value": "TLSv1-1"},
1226 {"value": "TLSv1-2"},
1227 {"value": "TLSv1-3", "v_range": [["v7.4.1", ""]]},
1228 ],
1229 },
1230 "ca_cert": {"v_range": [["v7.4.0", ""]], "type": "string"},
1231 "client_cert": {"v_range": [["v7.4.0", ""]], "type": "string"},
1232 "server_identity_check": {
1233 "v_range": [["v7.4.0", ""]],
1234 "type": "string",
1235 "options": [{"value": "enable"}, {"value": "disable"}],
1236 },
1237 "account_key_processing": {
1238 "v_range": [["v7.4.1", ""]],
1239 "type": "string",
1240 "options": [{"value": "same"}, {"value": "strip"}],
1241 },
1242 "account_key_cert_field": {
1243 "v_range": [["v7.4.1", ""]],
1244 "type": "string",
1245 "options": [
1246 {"value": "othername"},
1247 {"value": "rfc822name"},
1248 {"value": "dnsname"},
1249 {"value": "cn", "v_range": [["v7.4.4", ""]]},
1250 ],
1251 },
1252 "rsso": {
1253 "v_range": [["v6.0.0", ""]],
1254 "type": "string",
1255 "options": [{"value": "enable"}, {"value": "disable"}],
1256 },
1257 "rsso_radius_server_port": {"v_range": [["v6.0.0", ""]], "type": "integer"},
1258 "rsso_radius_response": {
1259 "v_range": [["v6.0.0", ""]],
1260 "type": "string",
1261 "options": [{"value": "enable"}, {"value": "disable"}],
1262 },
1263 "rsso_validate_request_secret": {
1264 "v_range": [["v6.0.0", ""]],
1265 "type": "string",
1266 "options": [{"value": "enable"}, {"value": "disable"}],
1267 },
1268 "rsso_secret": {"v_range": [["v6.0.0", ""]], "type": "string"},
1269 "rsso_endpoint_attribute": {
1270 "v_range": [["v6.0.0", ""]],
1271 "type": "string",
1272 "options": [
1273 {"value": "User-Name"},
1274 {"value": "NAS-IP-Address"},
1275 {"value": "Framed-IP-Address"},
1276 {"value": "Framed-IP-Netmask"},
1277 {"value": "Filter-Id"},
1278 {"value": "Login-IP-Host"},
1279 {"value": "Reply-Message"},
1280 {"value": "Callback-Number"},
1281 {"value": "Callback-Id"},
1282 {"value": "Framed-Route"},
1283 {"value": "Framed-IPX-Network"},
1284 {"value": "Class"},
1285 {"value": "Called-Station-Id"},
1286 {"value": "Calling-Station-Id"},
1287 {"value": "NAS-Identifier"},
1288 {"value": "Proxy-State"},
1289 {"value": "Login-LAT-Service"},
1290 {"value": "Login-LAT-Node"},
1291 {"value": "Login-LAT-Group"},
1292 {"value": "Framed-AppleTalk-Zone"},
1293 {"value": "Acct-Session-Id"},
1294 {"value": "Acct-Multi-Session-Id"},
1295 ],
1296 },
1297 "rsso_endpoint_block_attribute": {
1298 "v_range": [["v6.0.0", ""]],
1299 "type": "string",
1300 "options": [
1301 {"value": "User-Name"},
1302 {"value": "NAS-IP-Address"},
1303 {"value": "Framed-IP-Address"},
1304 {"value": "Framed-IP-Netmask"},
1305 {"value": "Filter-Id"},
1306 {"value": "Login-IP-Host"},
1307 {"value": "Reply-Message"},
1308 {"value": "Callback-Number"},
1309 {"value": "Callback-Id"},
1310 {"value": "Framed-Route"},
1311 {"value": "Framed-IPX-Network"},
1312 {"value": "Class"},
1313 {"value": "Called-Station-Id"},
1314 {"value": "Calling-Station-Id"},
1315 {"value": "NAS-Identifier"},
1316 {"value": "Proxy-State"},
1317 {"value": "Login-LAT-Service"},
1318 {"value": "Login-LAT-Node"},
1319 {"value": "Login-LAT-Group"},
1320 {"value": "Framed-AppleTalk-Zone"},
1321 {"value": "Acct-Session-Id"},
1322 {"value": "Acct-Multi-Session-Id"},
1323 ],
1324 },
1325 "sso_attribute": {
1326 "v_range": [["v6.0.0", ""]],
1327 "type": "string",
1328 "options": [
1329 {"value": "User-Name"},
1330 {"value": "NAS-IP-Address"},
1331 {"value": "Framed-IP-Address"},
1332 {"value": "Framed-IP-Netmask"},
1333 {"value": "Filter-Id"},
1334 {"value": "Login-IP-Host"},
1335 {"value": "Reply-Message"},
1336 {"value": "Callback-Number"},
1337 {"value": "Callback-Id"},
1338 {"value": "Framed-Route"},
1339 {"value": "Framed-IPX-Network"},
1340 {"value": "Class"},
1341 {"value": "Called-Station-Id"},
1342 {"value": "Calling-Station-Id"},
1343 {"value": "NAS-Identifier"},
1344 {"value": "Proxy-State"},
1345 {"value": "Login-LAT-Service"},
1346 {"value": "Login-LAT-Node"},
1347 {"value": "Login-LAT-Group"},
1348 {"value": "Framed-AppleTalk-Zone"},
1349 {"value": "Acct-Session-Id"},
1350 {"value": "Acct-Multi-Session-Id"},
1351 ],
1352 },
1353 "sso_attribute_key": {"v_range": [["v6.0.0", ""]], "type": "string"},
1354 "sso_attribute_value_override": {
1355 "v_range": [["v6.0.0", ""]],
1356 "type": "string",
1357 "options": [{"value": "enable"}, {"value": "disable"}],
1358 },
1359 "rsso_context_timeout": {"v_range": [["v6.0.0", ""]], "type": "integer"},
1360 "rsso_log_period": {"v_range": [["v6.0.0", ""]], "type": "integer"},
1361 "rsso_log_flags": {
1362 "v_range": [["v6.0.0", ""]],
1363 "type": "list",
1364 "options": [
1365 {"value": "protocol-error"},
1366 {"value": "profile-missing"},
1367 {"value": "accounting-stop-missed"},
1368 {"value": "accounting-event"},
1369 {"value": "endpoint-block"},
1370 {"value": "radiusd-other"},
1371 {"value": "none"},
1372 ],
1373 "multiple_values": True,
1374 "elements": "str",
1375 },
1376 "rsso_flush_ip_session": {
1377 "v_range": [["v6.0.0", ""]],
1378 "type": "string",
1379 "options": [{"value": "enable"}, {"value": "disable"}],
1380 },
1381 "rsso_ep_one_ip_only": {
1382 "v_range": [["v6.0.0", ""]],
1383 "type": "string",
1384 "options": [{"value": "enable"}, {"value": "disable"}],
1385 },
1386 "delimiter": {
1387 "v_range": [["v7.2.0", ""]],
1388 "type": "string",
1389 "options": [{"value": "plus"}, {"value": "comma"}],
1390 },
1391 "accounting_server": {
1392 "type": "list",
1393 "elements": "dict",
1394 "children": {
1395 "id": {
1396 "v_range": [["v6.0.0", ""]],
1397 "type": "integer",
1398 "required": True,
1399 },
1400 "status": {
1401 "v_range": [["v6.0.0", ""]],
1402 "type": "string",
1403 "options": [{"value": "enable"}, {"value": "disable"}],
1404 },
1405 "server": {"v_range": [["v6.0.0", ""]], "type": "string"},
1406 "secret": {"v_range": [["v6.0.0", ""]], "type": "string"},
1407 "port": {"v_range": [["v6.0.0", ""]], "type": "integer"},
1408 "source_ip": {"v_range": [["v6.0.0", ""]], "type": "string"},
1409 "interface_select_method": {
1410 "v_range": [["v6.2.0", "v6.2.0"], ["v6.2.5", ""]],
1411 "type": "string",
1412 "options": [
1413 {"value": "auto"},
1414 {"value": "sdwan"},
1415 {"value": "specify"},
1416 ],
1417 },
1418 "interface": {
1419 "v_range": [["v6.2.0", "v6.2.0"], ["v6.2.5", ""]],
1420 "type": "string",
1421 },
1422 "vrf_select": {"v_range": [["v7.6.1", ""]], "type": "integer"},
1423 },
1424 "v_range": [["v6.0.0", ""]],
1425 },
1426 },
1427 "v_range": [["v6.0.0", ""]],
1428 }
1429
1430
1431 def main():
1432 module_spec = schema_to_module_spec(versioned_schema)
1433 mkeyname = "name"
1434 fields = {
1435 "access_token": {"required": False, "type": "str", "no_log": True},
1436 "enable_log": {"required": False, "type": "bool", "default": False},
1437 "vdom": {"required": False, "type": "str", "default": "root"},
1438 "member_path": {"required": False, "type": "str"},
1439 "member_state": {
1440 "type": "str",
1441 "required": False,
1442 "choices": ["present", "absent"],
1443 },
1444 "state": {"required": True, "type": "str", "choices": ["present", "absent"]},
1445 "user_radius": {
1446 "required": False,
1447 "type": "dict",
1448 "default": None,
1449 "options": {},
1450 },
1451 }
1452 for attribute_name in module_spec["options"]:
1453 fields["user_radius"]["options"][attribute_name] = module_spec["options"][
1454 attribute_name
1455 ]
1456 if mkeyname and mkeyname == attribute_name:
1457 fields["user_radius"]["options"][attribute_name]["required"] = True
1458
1459 module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
1460 check_legacy_fortiosapi(module)
1461
1462 is_error = False
1463 has_changed = False
1464 result = None
1465 diff = None
1466
1467 versions_check_result = None
1468 if module._socket_path:
1469 connection = Connection(module._socket_path)
1470 if "access_token" in module.params:
1471 connection.set_custom_option("access_token", module.params["access_token"])
1472
1473 if "enable_log" in module.params:
1474 connection.set_custom_option("enable_log", module.params["enable_log"])
1475 else:
1476 connection.set_custom_option("enable_log", False)
1477 fos = FortiOSHandler(connection, module, mkeyname, admin_passwd_header=False)
1478 versions_check_result = check_schema_versioning(
1479 fos, versioned_schema, "user_radius"
1480 )
1481
1482 is_error, has_changed, result, diff = fortios_user(
1483 module.params, fos, module.check_mode
1484 )
1485
1486 else:
1487 module.fail_json(**FAIL_SOCKET_MSG)
1488
1489 if versions_check_result and versions_check_result["matched"] is False:
1490 module.warn(
1491 "Ansible has detected version mismatch between FortOS system and your playbook, see more details by specifying option -vvv"
1492 )
1493
1494 if not is_error:
1495 if versions_check_result and versions_check_result["matched"] is False:
1496 module.exit_json(
1497 changed=has_changed,
1498 version_check_warning=versions_check_result,
1499 meta=result,
1500 diff=diff,
1501 )
1502 else:
1503 module.exit_json(changed=has_changed, meta=result, diff=diff)
1504 else:
1505 if versions_check_result and versions_check_result["matched"] is False:
1506 module.fail_json(
1507 msg="Error in repo",
1508 version_check_warning=versions_check_result,
1509 meta=result,
1510 )
1511 else:
1512 module.fail_json(msg="Error in repo", meta=result)
1513
1514
1515 if __name__ == "__main__":
1516 main()