
    Vh7T                         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mZmZmZ d	 Z G d
 de      Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: ecs_domain
author:
  - Chris Trufan (@ctrufan)
version_added: '1.0.0'
short_description: Request validation of a domain with the Entrust Certificate Services (ECS) API
description:
  - Request validation or re-validation of a domain with the Entrust Certificate Services (ECS) API.
  - Requires credentials for the L(Entrust Certificate Services,https://www.entrustdatacard.com/products/categories/ssl-certificates)
    (ECS) API.
  - If the domain is already in the validation process, no new validation will be requested, but the validation data (if applicable)
    will be returned.
  - If the domain is already in the validation process but the O(verification_method) specified is different than the current
    O(verification_method), the O(verification_method) will be updated and validation data (if applicable) will be returned.
  - If the domain is an active, validated domain, the return value of C(changed) will be false, unless RV(domain_status=EXPIRED),
    in which case a re-validation will be performed.
  - If O(verification_method=dns), details about the required DNS entry will be specified in the return parameters RV(dns_contents),
    RV(dns_location), and RV(dns_resource_type).
  - If O(verification_method=web_server), details about the required file details will be specified in the return parameters
    RV(file_contents) and RV(file_location).
  - If O(verification_method=email), the email address(es) that the validation email(s) were sent to will be in the return
    parameter RV(emails). This is purely informational. For domains requested using this module, this will always be a list
    of size 1.
notes:
  - There is a small delay (typically about 5 seconds, but can be as long as 60 seconds) before obtaining the random values
    when requesting a validation while O(verification_method=dns) or O(verification_method=web_server). Be aware of that if
    doing many domain validation requests.
extends_documentation_fragment:
  - community.crypto.attributes
  - community.crypto.ecs_credential
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
  idempotent:
    support: partial
    details:
      - Under which conditions the module is idempotent still needs to be determined.
        If you are using this module and have more information, please contribute to
        the documentation!
options:
  client_id:
    description:
      - The client ID to request the domain be associated with.
      - If no client ID is specified, the domain will be added under the primary client with ID of 1.
    type: int
    default: 1
  domain_name:
    description:
      - The domain name to be verified or reverified.
    type: str
    required: true
  verification_method:
    description:
      - The verification method to be used to prove control of the domain.
      - If O(verification_method=email) and the value O(verification_email) is specified, that value is used for the email
        validation. If O(verification_email) is not provided, the first value present in WHOIS data will be used. An email
        will be sent to the address in O(verification_email) with instructions on how to verify control of the domain.
      - If O(verification_method=dns), the value RV(dns_contents) must be stored in location RV(dns_location), with a DNS
        record type of RV(dns_resource_type). To prove domain ownership, update your DNS records so the text string returned
        by RV(dns_contents) is available at RV(dns_location).
      - If O(verification_method=web_server), the contents of return value RV(file_contents) must be made available on a web
        server accessible at location RV(file_location).
      - If O(verification_method=manual), the domain will be validated with a manual process. This is not recommended.
    type: str
    choices: ['dns', 'email', 'manual', 'web_server']
    required: true
  verification_email:
    description:
      - Email address to be used to verify domain ownership.
      - 'Email address must be either an email address present in the WHOIS data for O(domain_name), or one of the following
        constructed emails: admin@O(domain_name), administrator@O(domain_name), webmaster@O(domain_name), hostmaster@O(domain_name),
        postmaster@O(domain_name).'
      - Note that if O(domain_name) includes subdomains, the top level domain should be used. For example, if requesting validation
        of example1.ansible.com, or test.example2.ansible.com, and you want to use the "admin" preconstructed name, the email
        address should be admin@ansible.com.
      - If using the email values from the WHOIS data for the domain or its top level namespace, they must be exact matches.
      - If O(verification_method=email) but O(verification_email) is not provided, the first email address found in WHOIS
        data for the domain will be used.
      - To verify domain ownership, domain owner must follow the instructions in the email they receive.
      - Only allowed if O(verification_method=email).
    type: str
seealso:
  - module: community.crypto.x509_certificate
    description: Can be used to request certificates from ECS, with O(community.crypto.x509_certificate#module:provider=entrust).
  - module: community.crypto.ecs_certificate
    description: Can be used to request a Certificate from ECS using a verified domain.
al  
---
- name: Request domain validation using email validation for client ID of 2.
  community.crypto.ecs_domain:
    domain_name: ansible.com
    client_id: 2
    verification_method: email
    verification_email: admin@ansible.com
    entrust_api_user: apiusername
    entrust_api_key: a^lv*32!cd9LnT
    entrust_api_client_cert_path: /etc/ssl/entrust/ecs-client.crt
    entrust_api_client_cert_key_path: /etc/ssl/entrust/ecs-client.key

- name: Request domain validation using DNS. If domain is already valid, request revalidation if expires within 90 days
  community.crypto.ecs_domain:
    domain_name: ansible.com
    verification_method: dns
    entrust_api_user: apiusername
    entrust_api_key: a^lv*32!cd9LnT
    entrust_api_client_cert_path: /etc/ssl/entrust/ecs-client.crt
    entrust_api_client_cert_key_path: /etc/ssl/entrust/ecs-client.key

- name: Request domain validation using web server validation, and revalidate if fewer than 60 days remaining of EV eligibility.
  community.crypto.ecs_domain:
    domain_name: ansible.com
    verification_method: web_server
    entrust_api_user: apiusername
    entrust_api_key: a^lv*32!cd9LnT
    entrust_api_client_cert_path: /etc/ssl/entrust/ecs-client.crt
    entrust_api_client_cert_key_path: /etc/ssl/entrust/ecs-client.key

- name: Request domain validation using manual validation.
  community.crypto.ecs_domain:
    domain_name: ansible.com
    verification_method: manual
    entrust_api_user: apiusername
    entrust_api_key: a^lv*32!cd9LnT
    entrust_api_client_cert_path: /etc/ssl/entrust/ecs-client.crt
    entrust_api_client_cert_key_path: /etc/ssl/entrust/ecs-client.key
a  
domain_status:
  description: Status of the current domain. Will be one of V(APPROVED), V(DECLINED), V(CANCELLED), V(INITIAL_VERIFICATION),
    V(DECLINED), V(CANCELLED), V(RE_VERIFICATION), V(EXPIRED), V(EXPIRING).
  returned: changed or success
  type: str
  sample: APPROVED
verification_method:
  description: Verification method used to request the domain validation. If C(changed) will be the same as O(verification_method)
    input parameter.
  returned: changed or success
  type: str
  sample: dns
file_location:
  description: The location that ECS will be expecting to be able to find the file for domain verification, containing the
    contents of RV(file_contents).
  returned: O(verification_method) is V(web_server)
  type: str
  sample: http://ansible.com/.well-known/pki-validation/abcd.txt
file_contents:
  description: The contents of the file that ECS will be expecting to find at RV(file_location).
  returned: O(verification_method) is V(web_server)
  type: str
  sample: AB23CD41432522FF2526920393982FAB
emails:
  description:
    - The list of emails used to request validation of this domain.
    - Domains requested using this module will only have a list of size 1.
  returned: O(verification_method) is V(email)
  type: list
  sample: [admin@ansible.com, administrator@ansible.com]
dns_location:
  description: The location that ECS will be expecting to be able to find the DNS entry for domain verification, containing
    the contents of RV(dns_contents).
  returned: changed and if O(verification_method) is V(dns)
  type: str
  sample: _pki-validation.ansible.com
dns_contents:
  description: The value that ECS will be expecting to find in the DNS record located at RV(dns_location).
  returned: changed and if O(verification_method) is V(dns)
  type: str
  sample: AB23CD41432522FF2526920393982FAB
dns_resource_type:
  description: The type of resource record that ECS will be expecting for the DNS record located at RV(dns_location).
  returned: changed and if O(verification_method) is V(dns)
  type: str
  sample: TXT
client_id:
  description: Client ID that the domain belongs to. If the input value O(client_id) is specified, this will always be the
    same as O(client_id).
  returned: changed or success
  type: int
  sample: 1
ov_eligible:
  description: Whether the domain is eligible for submission of "OV" certificates. Will never be V(false) if RV(ev_eligible)
    is V(true).
  returned: success and RV(domain_status) is V(APPROVED), V(RE_VERIFICATION), V(EXPIRING), or V(EXPIRED).
  type: bool
  sample: true
ov_days_remaining:
  description: The number of days the domain remains eligible for submission of "OV" certificates. Will never be less than
    the value of RV(ev_days_remaining).
  returned: success and RV(ov_eligible) is V(true) and RV(domain_status) is V(APPROVED), V(RE_VERIFICATION) or V(EXPIRING).
  type: int
  sample: 129
ev_eligible:
  description: Whether the domain is eligible for submission of "EV" certificates. Will never be V(true) if RV(ov_eligible)
    is V(false).
  returned: success and RV(domain_status) is V(APPROVED), V(RE_VERIFICATION) or V(EXPIRING), or V(EXPIRED).
  type: bool
  sample: true
ev_days_remaining:
  description: The number of days the domain remains eligible for submission of "EV" certificates. Will never be greater than
    the value of RV(ov_days_remaining).
  returned: success and RV(ev_eligible) is V(true) and RV(domain_status) is V(APPROVED), V(RE_VERIFICATION) or V(EXPIRING).
  type: int
  sample: 94
N)AnsibleModule)	to_native)	ECSClientRestOperationExceptionSessionConfigurationExceptionecs_client_argument_specc                     d }| rKt         j                   j                  | d      }|t         j                   j                         z
  j                  }|S )Nz%Y-%m-%dT%H:%M:%SZ)datetimestrptimenowdays)expiry_datedays_remainingexpiry_datetimes      o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/crypto/plugins/modules/ecs_domain.pycalculate_days_remainingr      sH    N"++44[BVW)H,=,=,A,A,CCII    c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)	EcsDomainz4
    Entrust Certificate Services domain class.
    c                    d| _         d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _	        d | _
        d | _        d | _        d | _        d | _        	 t        |j                  d   |j                  d   |j                  d   |j                  d   |j                  d         | _        	 | j                  j)                          y # t         $ r4}|j#                  dj%                  t'        |            	       Y d }~Td }~ww xY w# t*        $ r>}|j#                  d
j%                  t'        |j,                              	       Y d }~y d }~ww xY w)NFentrust_api_userentrust_api_keyentrust_api_client_cert_path entrust_api_client_cert_key_pathentrust_api_specification_path)r   r   entrust_api_certentrust_api_cert_keyr   z*Failed to initialize Entrust Provider: {0}msgzYPlease verify credential information. Received exception when testing ECS connection: {0})changeddomain_statusverification_methodfile_locationfile_contentsdns_locationdns_contentsdns_resource_typeemailsov_eligibleov_days_remaining
ev_eligbleev_days_remaining
ecs_clientr   paramsr
   	fail_jsonformatr   GetAppVersionr	   message)selfmodulees      r   __init__zEcsDomain.__init__   sa   !#' !!  !%!%!% $( 	'!'/A!B &.? @!'/M!N%+]]3U%V/5}}40DO	OO))+ - 	@GG	RSU   	 & 	ovvaii(   	s1   +AC D 	D&*DD	E$&4EE$c                    |j                  d      r|d   j                         | _        |d   | _        |j                  d      | _        t        |j                  d            | _        |j                  d      | _        t        |j                  d            | _        |d   | _	        | j                  dk(  r9|j                  d	      r(|d	   d
   | _
        |d	   d   | _        |d	   d   | _        y | j                  dk(  r,|j                  d      r|d   d   | _        |d   d   | _        y | j                  dk(  r|j                  d      r|d   | _        y y y )NverificationMethodverificationStatus
ovEligibleovExpiry
evEligibleevExpiryclientIddns	dnsMethodrecordDomain
recordTyperecordValue
web_serverwebServerMethodfileLocationfileContentsemailemailMethod)getlowerr%   r$   r,   r   r-   ev_eligibler/   	client_idr(   r*   r)   r&   r'   r+   )r6   domain_detailss     r   set_domain_detailszEcsDomain.set_domain_details&  si   23'56J'K'Q'Q'SD$+,@A)--l;!9z*"
 *--l;!9z*"
 (
3##u,1C1CK1P .{ ;N KD%3K%@%ND" .{ ;M JD%%5.:L:L;
 "00A!B>!RD!/0A!B>!RD%%0^5G5G5V(7DK 6W0r   c                    	 | j                   j                  |j                  d   |j                  d         }| j                  |       | j                  dk7  r| j                  dk7  r| j                  dk7  ry| j                  dk(  s| j                  dk(  r| j
                  |j                  d   k7  ry| j                  d	k(  ryy
# t        $ r Y yw xY w)NrP   domain_namerA   domainAPPROVEDINITIAL_VERIFICATIONRE_VERIFICATIONFr%   EXPIRINGT)r0   	GetDomainr1   rR   r$   r%   r	   )r6   r7   rQ   s      r   checkzEcsDomain.check@  s    	!__66{3FMM-<X 7 N ##N3""j0&&*@@&&*;; ""&<<%%):: ++v}}=R/SS !!Z/% 		s   A5C 8:C 3C 	CCc                 N   | j                  |      sRi }|j                  d   j                         |d<   |j                  d   dk(  r3i }|j                  d   rd|d<   |j                  d   |d<   nd|d<   ||d<   | j                  s|j                  d	   |d
<   	 | j                  s+| j                  j                  |j                  d   |       n8| j                  j                  |j                  d   |j                  d	   |       t        j                  d       | j                  j                  |j                  d   |j                  d	         }|j                  d   dk(  s|j                  d   dk(  rt        d      D ]z  }|j                  d   dk(  r*|j                  d      s'|d   d   | j                  k7  s= n>|j                  d   dk(  sR|j                  d      sd|d   d   | j                  k7  sz n t        j                  d       | j                  j                  |j                  d   |j                  d	         }d| _        | j                  |       y y # t         $ r5}|j#                  dj%                  |j&                               Y d }~y d }~ww xY w)Nr%   r;   rK   verification_email	SPECIFIEDemailSourceINCLUDE_WHOISrL   rT   
domainNamerP   )rA   Body)rA   rV   rc      rU   rB   rG      rC   rF   rH   rJ   
   Tz:Failed to request domain validation from Entrust (ECS) {0}r!   )r\   r1   upperr$   r0   	AddDomainReverifyDomaintimesleepr[   rangerM   r)   r'   r#   rR   r	   r2   r3   r5   )r6   r7   bodyrL   resultir8   s          r   request_domainzEcsDomain.request_domain]  s   zz&!D)/7L)M)S)S)UD%&}}23w> ==!561<K.+1==9M+NK(1@K.&1]#%%%+]]=%A\"3))OO--!'{!;$ .  OO22!'{!;%}}];! 3  

122#]];7!==7 3  MM"78EA}}%:;|K"1X &!==)>?5H &

; 7$*;$7$F#'#4#4%5 !&#]]+@A\Q &

+< =$*+<$=n$M#'#5#5%6 !&&  JJrN!__66!'{!;%}}]; 7 F  $''/y "z *   T[[		 !  s8   DI& *I&  I& I& 'I& =A'I& &	J$/+JJ$c                    | j                   | j                  | j                  d}| j                  r| j                  |d<   | j                  | j                  |d<   | j
                  r| j
                  |d<   | j                  | j                  |d<   | j                  r| j                  |d<   | j                  r| j                  |d<   | j                  dk(  r/| j                  |d	<   | j                  |d
<   | j                  |d<   |S | j                  dk(  r | j                  |d<   | j                  |d<   |S | j                  dk(  r| j                  |d<   |S )N)r#   rP   r$   r%   r,   r-   rO   r/   r+   rB   r(   r)   r*   rG   r&   r'   rK   )r#   rP   r$   r%   r,   r-   rO   r/   r+   r(   r)   r*   r&   r'   )r6   rn   s     r   dumpzEcsDomain.dump  s]   ||!//
 ##,0,D,DF()'$($4$4F=!!!*.*@*@F&''$($4$4F=!!!*.*@*@F&';;#{{F8##u,%)%6%6F>"%)%6%6F>"*.*@*@F&'  %%5&*&8&8F?#&*&8&8F?#  %%0#{{F8r   N)	__name__
__module____qualname____doc__r9   rR   r\   rp   rr    r   r   r   r      s#    *X84:CJr   r   c            
      |    t        t        dd      t        dd      t        ddg d      t        d	      
      S )Nint   )typedefaultstrT)r{   required)rB   rK   manualrG   )r{   r~   choices)r{   )rP   rT   r%   r^   )dictrw   r   r   ecs_domain_argument_specr     s=    E1-ed3 /W
  U+ r   c                  ~   t               } | j                  t                      t        | d      }|j                  d   r@|j                  d   dk7  r.|j                  dj                  |j                  d                t        |      }|j                  |       |j                         } |j                  di | y )	NF)argument_specsupports_check_moder^   r%   rK   zGThe verification_email field is invalid when verification_method="{0}".r!   rw   )r   updater   r   r1   r2   r3   r   rp   rr   	exit_json)ecs_argument_specr7   rV   rn   s       r   mainr     s    02578'!F 	*+MM/0G;Y``34 	 	
 vF
&![[]FFvr   __main__)
__future__r   r   r   r{   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   rj   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   Aansible_collections.community.crypto.plugins.module_utils.ecs.apir   r	   r
   r   r   objectr   r   r   rs   rw   r   r   <module>r      sy    A @ Xt'RM
^   4 A K K\0 zF r   