
    Vh(                         d dl mZmZmZ eZddgddZdZdZdZ	d d	l
Z
d d
lmZmZ ddlmZmZmZ 	 d d	lZdZd	Zd Zd Zd Zd Zd Zd Zd Zedk(  r e        y	y	# e$ r dZ e
j,                         ZY =w xY w)    )absolute_importdivisionprint_functionz1.1stableinterface	certified)metadata_versionstatussupported_bya  
module: user
author:
  - Paul Arthur (@flowerysong)
  - Aljaz Kosir (@aljazkosir)
  - Tadej Borovsak (@tadeboro)
short_description: Manage Sensu users
description:
  - Create, update, activate or deactivate Sensu user.
  - For more information, refer to the Sensu documentation at
    U(https://docs.sensu.io/sensu-go/latest/reference/rbac/#users).
version_added: 1.0.0
extends_documentation_fragment:
  - sensu.sensu_go.requirements
  - sensu.sensu_go.auth
  - sensu.sensu_go.name
requirements:
  - bcrypt (when managing Sensu Go 5.21.0 or newer)
seealso:
  - module: sensu.sensu_go.user_info
options:
  state:
    description:
      - Desired state of the user.
      - Users cannot actually be deleted, only deactivated.
    type: str
    choices: [ enabled, disabled ]
    default: enabled
  password:
    description:
      - Password for the user.
      - Required if user with a desired name does not exist yet on the backend
        and I(password_hash) is not set.
      - If both I(password) and I(password_hash) are set, I(password_hash) is
        ignored and calculated from the I(password) if required.
    type: str
  password_hash:
    description:
      - Bcrypt password hash for the user.
      - Use C(sensuctl user hash-password PASSWORD) to generate a hash.
      - Required if user with a desired name does not exist yet on the backend
        and I(password) is not set.
      - If both I(password) and I(password_hash) are set, I(password_hash) is
        ignored and calculated from the I(password) if required.
      - Sensu Go < 5.21.0 does not support creating/updating users using
        hashed passwords. Use I(password) parameter if you need to manage such
        Sensu Go installations.
      - At the moment, change detection does not work properly when using
        password hashes because the Sensu Go backend does not expose enough
        information via its API.
    type: str
    version_added: 1.8.0
  groups:
    description:
      - List of groups user belongs to.
    type: list
    elements: str
a  
- name: Create a user
  sensu.sensu_go.user:
    auth:
      url: http://localhost:8080
    name: awesome_username
    password: hidden_password?
    groups:
      - dev
      - prod

- name: Use pre-hashed password
  sensu.sensu_go.user:
    auth:
      url: http://localhost:8080
    name: awesome_username
    password_hash: $5f$14$.brXRviMZpbaleSq9kjoUuwm67V/s4IziOLGHjEqxJbzPsreQAyNm

- name: Deactivate a user
  sensu.sensu_go.user:
    name: awesome_username
    state: disabled
a  
object:
  description: Object representing Sensu user.
  returned: success
  type: dict
  sample:
    disabled: false
    groups:
      - ops
      - dev
    password: USER_PASSWORD
    password_hash: $5f$14$.brXRviMZpbaleSq9kjoUuwm67V/s4IziOLGHjEqxJbzPsreQAyNm
    username: alice
N)AnsibleModulemissing_required_lib   )	argumentserrorsutilsTFc                 L    dt        fd| j                         D              S )N)passwordpassword_hashc              3   6   K   | ]  \  }}|vs||f  y w)N ).0kvmasked_keyss      g/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/sensu/sensu_go/plugins/modules/user.py	<genexpr>z-_simulate_backend_response.<locals>.<genexpr>   s%      1aa{.BAs   	)dictitems)payloadr   s    @r   _simulate_backend_responser      s(    /K "==?      c           
         | j                  ||      ry|s| j                  dk  r&t        j                  | |dz   t	        ||             y
t
        r8t        j                  |j                  d      t        j                               }nt        j                  t        d            t        j                  | |dz   t	        ||j                  d      	             y
)NF5.21.0z	/password)usernamer   zutf-8bcrypt/reset_passwordasciir#   r   T)validate_auth_dataversionr   putr   
HAS_BCRYPTr$   hashpwencodegensaltr   RequirementsErrorr   decode)clientpathr#   r   
check_modehashs         r   update_passwordr5      s       84>>H$IIfd[0$!H3   }}X__W%=v~~?OP../CH/MNNIIfd%66!W1E9  r    c                     | j                   dk  rt        j                  d      |s%t        j                  | |dz   t        ||             y)Nr"   z2Sensu Go < 5.21.0 does not support password hashesr%   r'   T)r)   r   
SensuErrorr   r*   r   )r1   r2   r#   r   r3   s        r   update_password_hashr8      sP    ~~ @
 	
 		&$!22D]5
 	 r    c                 0   t        |      j                  |      }t        |      j                  |      }|sG|D ]  }t        j                  | |dz   |z   d        ! |D ]  }t        j                  | |dz   |z            t        |      t        |      z   dkD  S )Nz/groups/r   )set
differencer   r*   deletelen)r1   r2   
old_groups
new_groupsr3   	to_deleteto_addgs           r   update_groupsrC      s    J**:6I_''
3F  	;AIIfdZ/!3T:	; 	8ALL
!2Q!67	8 y>CK'!++r    c                     ||k7  }|s6|r4|rt        j                  | |       |S t        j                  | |dz   d        |S )Nz
/reinstate)r   r<   r*   )r1   r2   old_disablednew_disabledr3   changeds         r   update_staterH      sE    l*G'LL& N IIfd\148Nr    c                    | >|rdt        |      fS t        j                  |||       dt        j                  ||      fS d}d|v rt	        |||d   |d   |      xs |}nd|v rt        |||d   |d   |      xs |}d|v r)t        ||| j                  d      xs g |d   |      xs |}d|v rt        ||| d   |d   |      xs |}|r|t        | fi t        |      fS |t        j                  ||      fS )NTFr   r#   r   groupsdisabled)	r   r   r*   getr5   r8   rC   rH   r   )remote_objectr1   r2   r   r3   rG   s         r   syncrN      ss   3G<<<		&$(UYYvt,,, G W!D'*-wz/B
   	 
G	#&D'*-w/G
   	
 7D-++H5;Hz
   	
 WD-
3WZ5H
   	
  
7@
 
 	
 EIIfd+++r    c                     t        dt        t        j                  dd      t        dddg      t        d      t        d      t        d	d
                  } t        j                  | j
                  d         }t        j                  d d| j
                  d         }	 t        j                  ||      }0| j
                  d   !| j
                  d   | j                  d       t        j                  | j
                  ddd      }| j
                  d   |d<   | j
                  d   dk(  |d<   	 t        ||||| j                        \  }}| j!                  ||       y # t        j                  $ r%}| j                  t        |             Y d }~d }~ww xY w# t        j                  $ r%}| j                  t        |             Y d }~y d }~ww xY w)NTauthnameenabledrK   )defaultchoices)no_logFliststr)typeelements)stater   r   rJ   )supports_check_modeargument_specusers)msgr   r   z3Cannot create new user without a password or a hashrJ   r#   rZ   )rG   object)r   r   r   get_specget_sensu_clientparamsr   build_core_v2_pathrL   r   Error	fail_jsonrW   get_spec_payloadrN   r3   	exit_json)moduler1   r2   rM   er   rG   users           r   mainrk     s    vv.!"J/   e
F( ''f(=>F##D'6==3HID%		&$/
 	MM*%-MM/*2E 	 	
 ((z?HG !--/GJ --0J>GJ%64&2C2C
 	6- << %SV$$%. << %SV$$%s0   #E% 5/F  %F8FF G3GG__main__)
__future__r   r   r   rX   __metaclass__ANSIBLE_METADATADOCUMENTATIONEXAMPLESRETURN	tracebackansible.module_utils.basicr   r   module_utilsr   r   r   r$   r+   BCRYPT_IMPORT_ERRORImportError
format_excr   r5   r8   rC   rH   rN   rk   __name__r   r    r   <module>rz      s    A @  ! 9v0
  J 3 31J0$, 	8,v2%j zF G  1J.)..01s   A A76A7