
    VhQ*                         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
m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 d dlZd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionz1.1preview	community)metadata_versionstatussupported_byaa  
---
module: cyberark_credential
short_description: Credential retrieval using AAM Central Credential Provider.
author:
    - Edward Nunez (@enunez-cyberark)
    - CyberArk BizDev (@cyberark-bizdev)
    - Erasmo Acosta (@erasmix)
    - James Stutes (@JimmyJamCABD)
version_added: '1.0.0'
description:
    - Creates a URI for retrieving a credential from a password object stored
      in the Cyberark Vault.  The request uses the Privileged Account Security
      Web Services SDK through the Central Credential Provider by requesting
      access with an Application ID.

options:
    api_base_url:
        type: str
        required: true
        description:
            - A string containing the base URL of the server hosting the
              Central Credential Provider.
    validate_certs:
        type: bool
        required: false
        default: true
        description:
            - If C(false), SSL certificate chain will not be validated.  This
              should only set to C(true) if you have a root CA certificate
              installed on each node.
    app_id:
        type: str
        required: true
        description:
            - A string containing the Application ID authorized for retrieving
              the credential.
    query:
        type: str
        required: true
        description:
            - A string containing details of the object being queried;
            - Possible parameters could be Safe, Folder, Object
            - (internal account name), UserName, Address, Database,
            - PolicyID.
    connection_timeout:
        type: int
        required: false
        default: '30'
        description:
            - An integer value of the allowed time before the request returns
              failed.
    query_format:
        type: str
        required: false
        default: Exact
        choices: [Exact, Regexp]
        description:
            - The format for which your Query will be received by the CCP.
    fail_request_on_password_change:
        type: bool
        required: false
        default: false
        description:
            - A boolean parameter for completing the request in the middle of
              a password change of the requested credential.
    client_cert:
        type: str
        required: false
        description:
            - A string containing the file location and name of the client
              certificate used for authentication.
    client_key:
        type: str
        required: false
        description:
            - A string containing the file location and name of the private
              key of the client certificate used for authentication.
    reason:
        type: str
        required: false
        description:
            - Reason for requesting credential if required by policy;
            - It must be specified if the Policy managing the object
            - requires it.
a  
- name: credential retrieval basic
  cyberark.pas.cyberark_credential:
    api_base_url: "http://10.10.0.1"
    app_id: "TestID"
    query: "Safe=test;UserName=admin"
    register: result

- name: credential retrieval advanced
  cyberark.pas.cyberark_credential:
    api_base_url: "https://components.cyberark.local"
    validate_certs: true
    client_cert: /etc/pki/ca-trust/source/client.pem
    client_key: /etc/pki/ca-trust/source/priv-key.pem
    app_id: "TestID"
    query: "Safe=test;UserName=admin"
    connection_timeout: 60
    query_format: Exact
    fail_request_on_password_change: true
    reason: "requesting credential for Ansible deployment"
    register: result
a	  
changed:
    description:
        - Identify if the playbook run resulted in a change to the account in
          any way.
    returned: always
    type: bool
failed:
    description: Whether playbook run resulted in a failure of any kind.
    returned: always
    type: bool
status_code:
    description: Result HTTP Status code.
    returned: success
    type: int
    sample: "200, 201, -1, 204"
result:
    description: A json dump of the resulting action.
    returned: success
    type: complex
    contains:
        Address:
            description: The target address of the credential being queried
            type: str
            returned: if required
        Content:
            description: The password for the object being queried
            type: str
            returned: always
        CreationMethod:
            description: This is how the object was created in the Vault
            type: str
            returned: always
        DeviceType:
            description:
                - An internal File Category for more granular management of
                  Platforms.
            type: str
            returned: always
        Folder:
            description:
                - The folder within the Safe where the credential is stored.
            type: str
            returned: always
        Name:
            description:
                - The Cyberark unique object ID of the credential being
                  queried.
            type: str
            returned: always
        PasswordChangeInProcess:
            description: If the password has a change flag placed by the CPM
            type: bool
            returned: always
        PolicyID:
            description: Whether or not SSL certificates should be validated.
            type: str
            returned: if assigned to a policy
        Safe:
            description: The safe where the queried credential is stored
            type: str
            returned: always
        Username:
            description: The username of the credential being queried
            type: str
            returned: if required
        LogonDomain:
            description: The Address friendly name resolved by the CPM
            type: str
            returned: if populated
        CPMDisabled:
            description:
                - A description of why this vaulted credential is not being
                  managed by the CPM.
            type: str
            returned: if CPM management is disabled and a reason is given
)to_text)AnsibleModule)open_url)	HTTPError)quote)HTTPExceptionNc                    | j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }d }d }	d}
d	| j                   v r| j                   d	   }d
| j                   v r| j                   d
   }	d| j                   v r| j                   d   }
|
dt        |      dt        |      d|d|d|}d| j                   v r/| j                   d    t        | j                   d         }|d|z  z   }d }d }	 t        ||z   d|||	      }|j                         dk(  r6	 t        j                  |j                               }||j                         fS | j                  d|z          y # t        t        f$ r8}| j                  d||dt        |      |j                         Y d }~d }~wt        $ r.}| j                  d||dt        |      d       Y d }~d }~ww xY w# t        $ r)}| j                  dt        |      z  d       Y d }~d }~ww xY w)Napi_base_urlvalidate_certsapp_idqueryconnection_timeoutquery_formatfail_request_on_password_changez/AIMWebService/api/Accountsclient_cert
client_keypathz?AppId=z&Query=z&ConnectionTimeout=z&QueryFormat=z&FailRequestOnPasswordChange=reasonz
&reason=%sGET)methodr   r   r   zError while retrieving credential.Please validate parameters provided, and permissions for the application and provider in CyberArk.
*** end_point=z
 ==> )msgstatus_codez9Unknown error while retrieving credential.
*** end_point=
   z9Error obtain cyberark credential result from http body
%szerror in end_point=>)r   )paramsr   r   r   r   	fail_jsonr   code	Exceptiongetcodejsonloadsread)moduler   r   r   r   r   r   r   r   r   r   	end_pointr   resultresponsehttp_exceptionunknown_exceptionexcs                     t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cyberark/pas/plugins/modules/cyberark_credential.pyretrieve_credentialr4      sy    ==0L]]#34N]]8$FMM'"E';<==0L&,mm4U&V#KJ(D%mmM2v}}$]]<0
}}V$ 	fe'I 6== V]]8%<%Hv}}X./v 55	FH 
9$)#!
@ S 	ZZ0F ((*++ 	3i?@O }% 
  GN,C	E '++ 	 		
 		
  	
  G4E,FH  	 	
 	
	
   	T3<!   	s<   .F  #H  H/.G""H.$HH	I(IIc                      dddddddddddddddddddd	gdd
ddddddddddddddd
} t        | d      }t        |      \  }}|j                  d||       y )NTstr)requiredtypeFint   )r7   r8   defaultExactRegexp)r7   r8   choicesr;   bool)r8   r;   )r8   r7   )r8   r7   no_log)
r   r   r   r   r   r   r   r   r   r   )argument_specsupports_check_mode)changedr.   r    )r   r4   	exit_json)fieldsr,   r.   r    s       r3   mainrF   4  s     &*59#U3"E2$e4+0%BO*	
 ,

 $*d; %59$%4H'F, TJF/7V[
U6{K    __main__)
__future__r   r   r   r8   __metaclass__ANSIBLE_METADATADOCUMENTATIONEXAMPLESRETURNansible.module_utils._textr   ansible.module_utils.basicr   ansible.module_utils.urlsr   +ansible.module_utils.six.moves.urllib.errorr   +ansible.module_utils.six.moves.urllib.parser   *ansible.module_utils.six.moves.http_clientr   r)   r4   rF   __name__ rG   r3   <module>rW      s|   
 C B  k Un.L
\ / 4 . A = D \A~L> zF rG   