
    Vh                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dl
mZ d dl
mZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ  e       Z e	j.                  dd      Z G d d      Z G d de      Zy)    )absolute_importdivisionprint_functiona  
author: Unknown (!UNKNOWN)
name: cyberarkpassword
short_description: get secrets from CyberArk AIM
requirements:
  - CyberArk AIM tool installed
description:
  - Get secrets from CyberArk AIM.
options:
  _command:
    description: Cyberark CLI utility.
    type: string
    env:
      - name: AIM_CLIPASSWORDSDK_CMD
    default: '/opt/CARKaim/sdk/clipasswordsdk'
  appid:
    description: Defines the unique ID of the application that is issuing the password request.
    type: string
    required: true
  query:
    description: Describes the filter criteria for the password retrieval.
    type: string
    required: true
  output:
    description:
      - Specifies the desired output fields separated by commas.
      - 'They could be: Password, PassProps.<property>, PasswordChangeInProcess.'
    type: string
    default: 'password'
  _extra:
    description: For extra_params values please check parameters for clipasswordsdk in CyberArk's "Credential Provider and
      ASCP Implementation Guide".
notes:
  - For Ansible on Windows, please change the -parameters (C(-p), C(-d), and C(-o)) to /parameters (C(/p), C(/d), and C(/o)) and change the
    location of C(CLIPasswordSDK.exe).
aq  
- name: passing options to the lookup
  ansible.builtin.debug:
    msg: '{{ lookup("community.general.cyberarkpassword", cyquery) }}'
  vars:
    cyquery:
      appid: "app_ansible"
      query: "safe=CyberArk_Passwords;folder=root;object=AdminPass"
      output: "Password,PassProps.UserName,PassProps.Address,PasswordChangeInProcess"


- name: used in a loop
  ansible.builtin.debug:
    msg: "{{item}}"
  with_community.general.cyberarkpassword:
    appid: 'app_ansible'
    query: 'safe=CyberArk_Passwords;folder=root;object=AdminPass'
    output: 'Password,PassProps.UserName,PassProps.Address,PasswordChangeInProcess'
aS  
_result:
  description: A list containing one dictionary.
  type: list
  elements: dictionary
  contains:
    password:
      description:
        - The actual value stored.
    passprops:
      description: Properties assigned to the entry.
      type: dictionary
    passwordchangeinprocess:
      description: Did the password change?
N)PIPE)Popen)AnsibleError)
LookupBase)to_bytes	to_native)DisplayAIM_CLIPASSWORDSDK_CMDz/opt/CARKaim/sdk/clipasswordsdkc                       e Zd ZddZd Zy)CyberarkPasswordNc                    || _         || _        || _        g | _        |j	                         D ]@  \  }}| j                  j                  d       | j                  j                  | d|        B | j                   t        d      | j                  t        d      | j                  d| _        d| _        y | j                  j                         | _        d| _        y )N-p=z+CyberArk Error: No Application ID specifiedz(CyberArk Error: No Vault query specifiedpasswords   @#@)	appidqueryoutputextra_parmsitemsappendr   lowerb_delimiter)selfr   r   r   kwargskeyvalues          u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/lookup/cyberarkpassword.py__init__zCyberarkPassword.__init__c   s    

  ,,. 	6JC##D)##se1UG$45	6 ::LMM::IJJ;;$DK " ++++-DK!    c           
         i }	 t         ddd| j                   dd| j                   d| j                  d| j                  g
}|j                  | j                         d}|D cg c]  }t        |       }}t        |t        t        t              j                         \  }}|rt        |      }|rt        d	| d
      |r|j                  d      r|d d }| j                  j                  d      }|j                  | j                        }	t        t        |            D ]P  }
||
   j!                  d      r&d|vri |d<   ||
   dd  }t#        |	|
         |d   |<   =t#        |	|
         |||
   <   R 	 |gS c c}w # t$        j&                  $ r}t        |j                        d }~wt(        $ r+}t        d|j*                   d|j,                   d
      d }~ww xY w)NGetPasswordr   zAppDescs.AppID=zQuery=z-oz-dr"   )stdoutstderrstdinz	ERROR =>     
,z
passprops.	passprops
   zMERROR - AIM not installed or clipasswordsdk not in standard location. ERROR=(z) => )CLIPASSWORDSDK_CMDr   r   r   r   extendr   r
   r   r   communicater   endswithsplitrangelen
startswithr   
subprocessCalledProcessErrorOSErrorerrnostrerror)r   result_dict	all_parmsb_credentialvb_all_params
tmp_output	tmp_erroroutput_namesoutput_valuesioutput_prop_namees                r    getzCyberarkPassword.get   s   &	L"

|4tzzl+dkkd&&(I T--.L1:;AHQK;L;$),tDX\$]$i$i$k!J	'
3"Yyk#;<< 5 5e <+CR0;;,,S1L(..t/?/?@M3|,- O?--l;"+535K0'3Ars';$AJ=YZK[A\K,-=>3<]1=M3NKQ0O }; <0 ,, 	)qxx(( 	L!nopovovnww|}~  ~H  ~H  }I  IJ   K  L  L	Ls7   AE? #E:5DE? :E? ?GF''G3&GG)NNN)__name__
__module____qualname__r!   rG    r"   r    r   r   a   s    "8,r"   r   c                       e Zd ZdZddZy)LookupModulez
    USAGE:

    Nc                 "   t         j                  |        t        |t              rMg }|D ]D  }t         j                  d|        t	        di |}|j                  |j                                F |S t	        di |}|j                         }|S )NzTerm: rK   )displayvvvv
isinstancelistr   r   rG   )r   terms	variablesr   return_valuestermcyberark_connresults           r    runzLookupModule.run   s    w eT"M :vdV_- 0 84 8$$]%6%6%89: ! ,5u5M"&&(FMr"   )N)rH   rI   rJ   __doc__rY   rK   r"   r    rM   rM      s    
r"   rM   )
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURNosr6   r   r   ansible.errorsr   ansible.plugins.lookupr	   +ansible.module_utils.common.text.convertersr
   r   ansible.utils.displayr   rO   getenvr.   r   rM   rK   r"   r    <module>rg      sx    C B#J(
  
    ' - K )
)RYY79Z[ J JZ: r"   