
    Vh*                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ d dlmZ d dlmc mc mc mc mZ  G d	 d
e      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
---
module: zabbix_mfa

short_description: Create/update/delete Zabbix MFA method


description:
    - This module allows you to create, update and delete Zabbix MFA method.

author:
    - ONODERA Masaru(@masa-orca)

requirements:
    - "python >= 3.11"

version_added: 3.1.0

options:
    name:
        description:
            - Name of this MFA method
        type: str
        required: true
    method_type:
        description:
            - A type of this MFA method
        type: str
        choices:
            - "totp"
            - "duo_universal_prompt"
    hash_function:
        description:
            - Type of the hash function for generating TOTP codes.
            - Required when C(method_type=totp).
        type: str
        choices:
            - "sha-1"
            - "sha-256"
            - "sha-512"
    code_length:
        description:
            - Verification code length.
            - Required when C(method_type=totp).
        type: int
        choices:
            - 6
            - 8
    api_hostname:
        description:
            - API hostname provided by the Duo authentication service.
            - Required when C(method_type=duo_universal_prompt).
        type: str
    clientid:
        description:
            - Client ID provided by the Duo authentication service.
            - Required when C(method_type=duo_universal_prompt).
        type: str
    client_secret:
        description:
            - Client secret provided by the Duo authentication service.
            - Required when C(method_type=duo_universal_prompt).
        type: str
    state:
        description:
            - State of this MFA.
        type: str
        choices: ['present', 'absent']
        default: 'present'


notes:
    - Only Zabbix >= 7.0 is supported.
    - This module returns changed=true when I(method_type) is C(duo_universal_prompt) as Zabbix API
      will not return any sensitive information back for module to compare.

extends_documentation_fragment:
    - community.zabbix.zabbix

a{  
# If you want to use Username and Password to be authenticated by Zabbix Server
- name: Set credentials to access Zabbix Server API
  ansible.builtin.set_fact:
    ansible_user: Admin
    ansible_httpapi_pass: zabbix

# If you want to use API token to be authenticated by Zabbix Server
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens
- name: Set API token
  ansible.builtin.set_fact:
    ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895

- name: Create a 'Zabbix TOTP' MFA method
  # set task level variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: 'zabbixeu'  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_mfa:
    name: Zabbix TOTP
    method_type: totp
    hash_function: sha-1
    code_length: 6
z
msg:
    description: The result of the creating operation
    returned: success
    type: str
    sample: 'Successfully created MFA method'
)AnsibleModule)
ZabbixBase)LooseVersionNc                   >     e Zd Zd fd	Zd Zd Zd Zd Zd Z xZ	S )MFAc                     t         t        |   |||       t        | j                        t        d      k  r|j                  d       y y )Nz7.0z:This module doesn't support Zabbix versions lower than 7.0msg)superr
   __init__r   _zbx_api_version	fail_json)selfmodulezbxzapi_wrapper	__class__s       o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_mfa.pyr   zMFA.__init__   sJ    c4!&#|<--.e1DDP   E    c                     	 | j                   j                  j                  dd|id      }d }|D ]  }|d   |k(  s|} |S # t        $ r)}| j                  j                  d|z         Y d }~y d }~ww xY w)Nextendname)outputsearchzFailed to get MFA method: %sr   )_zapimfaget	Exception_moduler   )r   mfa_namemfasr   _mfaes         r   get_mfazMFA.get_mfa   s    	::>>%%&%x0D C LH,C J 	LL""2Q6 #  	s   9A A 	A4A//A4c                 d   	 |d   g}| j                   j                  r| j                   j                  d       | j                  j                  j                  |       | j                   j                  dd       y # t        $ r)}| j                   j                  d|z         Y d }~y d }~ww xY w)NmfaidTchangedz Successfully deleted MFA method.r+   r   zFailed to delete MFA method: %sr   )r"   
check_mode	exit_jsonr   r   deleter!   r   )r   r   	parameterr&   s       r   
delete_mfazMFA.delete_mfa   s    	WI||&&&&t&4JJNN!!),LL"""D #   	LL""59 #  	s   A:A= =	B/B**B/c                 "   i }||d<   t        t        j                  g d|            |d<   |dk(  r4t        t        j                  g d|            |d<   t        |      |d<   |S t        |      |d<   t        |      |d	<   t        |      |d
<   |S )Nr   )Ntotpduo_universal_prompttyper3   )Nsha-1sha-256sha-512hash_functioncode_lengthapi_hostnameclientidclient_secret)strzabbix_utilshelper_to_numeric_value)	r   r   method_typer9   r:   r;   r<   r=   r0   s	            r   _convert_to_parameterzMFA._convert_to_parameter   s    	 	& D D
 !
 	& 6!),\-Q-Q . *Io& (+;'7Im$
  ),L(9In%$'MIj!),]);Io&r   c           	         | j                  |||||||      }	 | j                  j                  r| j                  j                  d       | j                  j
                  j                  |       | j                  j                  dd       y # t        $ r)}	| j                  j                  d|	z         Y d }	~	y d }	~	ww xY w)NTr*   z Successfully created MFA method.r,   zFailed to create MFA method: %sr   )	rB   r"   r-   r.   r   r   creater!   r   )
r   r   rA   r9   r:   r;   r<   r=   r0   r&   s
             r   
create_mfazMFA.create_mfa   s    ..t[-Q\^jlt  wD  E	
	||&&&&t&4JJNN!!),LL"""D #   	LL""59 #  	s   A4B 	C B;;C c	           	      h   	 | j                  |||||||      }	|	j                  d|d   i       |dk(  rVt        j                  |ddg      d   }i }
t        j                  |	||
       |
i k(  r| j
                  j                  d       | j
                  j                  r| j
                  j                  d	       | j                  j                  j                  |	       | j
                  j                  d	d
       y # t        $ r)}| j
                  j                  d|z         Y d }~y d }~ww xY w)Nr)   r3   r;   r<   )del_keysr   Fr*   Tz Successfully updated MFA method.r,   zFailed to update MFA method: %sr   )rB   updater?   helper_normalize_datahelper_compare_dictionariesr"   r.   r-   r   r   r!   r   )r   current_mfar   rA   r9   r:   r;   r<   r=   r0   
differencer&   s               r   
update_mfazMFA.update_mfa   s2   	224mU`bnpx  {H  IIg{7';<=v%*@@>:*F  
88KQ[\"$LL**5*9||&&&&t&4JJNN!!),LL"""D #   	LL""59 #  	s   C<C? ?	D1D,,D1)NN)
__name__
__module____qualname__r   r'   r1   rB   rE   rM   __classcell__)r   s   @r   r
   r
      s!    $8r   r
   c                  X   t        j                         } | j                  t        t        dd      t        dddg      t        dg d      t        dd	d
g      t        d      t        d      t        dd      t        ddddg                   t	        | ddddggddg dggdgddddddd      }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }	t        |      }
|
j                  |      }|	dk(  r'|r|
j                  |       y|j                  d       y|r|
j                  ||||||||       y|
j                  |||||||       y) zMain ansible module functionr>   T)r5   requiredr3   r4   )r5   choices)r6   r7   r8   int      )r5   )r5   no_logpresentabsent)r5   defaultrT   )r   rA   r9   r:   r;   r<   r=   staterA   r9   r:   )r;   r<   r=   )r9   r;   )r9   r:   r;   r<   r=   )argument_specrequired_ifmutually_exclusiverequired_bysupports_check_moder   r;   r<   r=   r\   Fr*   N)r?   zabbix_common_argument_specrH   dictr   paramsr
   r'   r1   r.   rM   rE   )r]   r   r   rA   r9   r:   r;   r<   r=   r\   mfa_class_objr   s               r   mainrf      s    !<<>M540*  A 5)u%E$7!"H-1	
B # #! &
( .
 +()%*
 !? FD == D--.KMM/2M--.K==0L}}Z(HMM/2MMM'"EKM



%C$$S)U+$$S$]KYegoq~$$T;{T`bjlyzr   __main__)
__future__r   r   r   r5   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   >ansible_collections.community.zabbix.plugins.module_utils.baser   #ansible.module_utils.compat.versionr   Aansible_collections.community.zabbix.plugins.module_utils.helpers	communityzabbixpluginsmodule_utilshelpersr?   r
   rf   rN    r   r   <module>rw      si    A @Ob<
 5 U < X X Xh* hV\{~ zF r   