
    Vh<(                         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mZ dd
lmZ d Z G d d      Zy# e$ r Y 9w xY w)z@
Common Amazon Certificate Manager facts shared between modules
    )BotoCoreError)ClientError)to_bytes)camel_dict_to_snake_dict   )is_boto3_error_code)AWSRetry)ansible_dict_to_boto3_tag_list)boto3_tag_list_to_ansible_dictc                       fd}|S )Nc                     |j                  dd       }|j                  dd       }|j                  dg       }	  | i |S # t        |      $ r Y y t        t        f$ r }|s |j	                  ||       Y d }~y d }~ww xY w)Nmoduleerrorignore_error_codesmsg)popr   r   r   fail_json_aws)argskwargsr   r   r   efuncs         g/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/amazon/aws/plugins/module_utils/acm.pyrunnerz(acm_catch_boto_exception.<locals>.runner"   s    Hd+

7D)#ZZ(<bA	/((("#56 	{+ 	/   ..	/s   A A?A?A::A? )r   r   s   ` r   acm_catch_boto_exceptionr   !   s    / M    c                      e Zd ZdZd Ze ej                  ddg      d               Ze ej                  ddg      dd              Z	e ej                  ddd	g      d
               Z
e ej                  ddd	g      d               Ze ej                  ddd	g      d               Ze ej                  ddg      d               Z ej                  ddd	g      d        Zd ZdddZddddddZd ZdddddZy)ACMServiceManagerzHandles ACM Facts Servicesc                 >    || _         |j                  d      | _        y )Nacm)r   client)selfr   s     r   __init__zACMServiceManager.__init__6   s    mmE*r      RequestInProgressException)delaycatch_extra_error_codesc                 <    | j                   j                  |       y )NCertificateArn)r"   delete_certificate)r#   arns     r   delete_certificate_with_backoffz1ACMServiceManager.delete_certificate_with_backoff:   s     	&&c&:r   Nc                     | j                   j                  d      }ddg dii}|r||d<    |j                  di |j                         d   S )Nlist_certificatesIncludeskeyTypes)RSA_1024RSA_2048RSA_3072RSA_4096EC_prime256v1EC_secp384r1EC_secp521r1CertificateStatusesCertificateSummaryListr   )r"   get_paginatorpaginatebuild_full_result)r#   statuses	paginatorr   s       r   list_certificates_with_backoffz0ACMServiceManager.list_certificates_with_backoff?   sg     KK--.AB	  

 ,4F()!y!!+F+==?@XYYr   ResourceNotFoundExceptionc                 P    | j                   j                  |      }|d   |d   dS )Nr*   CertificateCertificateChain)rD   rE   )r"   get_certificate)r#   certificate_arnresponses      r   get_certificate_with_backoffz.ACMServiceManager.get_certificate_with_backoffU   s1    
 ;;..o.N'6HUgLhiir   c                 @    | j                   j                  |      d   S )Nr*   rD   )r"   describe_certificater#   rG   s     r   !describe_certificate_with_backoffz3ACMServiceManager.describe_certificate_with_backoff^   s"    
 {{///OP]^^r   c                 @    | j                   j                  |      d   S )Nr*   Tags)r"   list_tags_for_certificaterL   s     r   "list_certificate_tags_with_backoffz4ACMServiceManager.list_certificate_tags_with_backoffe   s"    
 {{44O4TU[\\r   c                     t        |      t        |      d}|r||d<   |r||d<    | j                  j                  di |d   S )N)rD   
PrivateKeyr+   rE   r   )r   r"   import_certificate)r#   certificateprivate_keycertificate_chainr-   paramss         r   import_certificate_with_backoffz1ACMServiceManager.import_certificate_with_backoffl   sT     "*+!6h{F[\'*F#$):F%&-t{{--778HIIr   c                 T    t        |      }| j                  j                  ||       y )N)r+   rO   )r
   r"   add_tags_to_certificate)r#   r-   tagsaws_tagss       r   tag_certificate_with_backoffz.ACMServiceManager.tag_certificate_with_backoffy   s%     2$7++3X+Nr   c                     y	 t        fdD              xr t        fdD              S # t        t        f$ r'}| j                  j	                  |d       Y d }~y d }~ww xY w)NTc              3   &   K   | ]  }|v  
 y wNr   ).0k	cert_tagss     r   	<genexpr>z0ACMServiceManager._match_tags.<locals>.<genexpr>   s     8!qI~8s   c              3   L   K   | ]  }j                  |      |   k(    y wra   )get)rb   rc   rd   ref_tagss     r   re   z0ACMServiceManager._match_tags.<locals>.<genexpr>   s'     @sefqAQU]^_U`A`@ss   !$zACM tag filtering errr   )all	TypeErrorAttributeErrorr   r   )r#   rh   rd   r   s    `` r   _match_tagszACMServiceManager._match_tags   sf    	F8x88sS@sjr@s=ss>* 	FKK%%a-D%EE	Fs   *2 A(A##A()r-   c                    |/t        |      dk  r| j                  j                  d       |d   }d| }| j                  || j                  |       y )N   z+Missing required certificate arn to delete.r      zCouldn't delete certificate )r   r   )lenr   	fail_jsonr.   )r#   r-   r   r   s       r   r,   z$ACMServiceManager.delete_certificate   sV    ;4y1}%%*W%Xq'C.se4,,SE,Rr   )domain_namer?   r-   	only_tagsc                D   | j                  || j                  d      }fd}t        t        ||            }	g }
|	D ]  }| j	                  |d   | j                  d|d    dg      }|/|d	   d
vr=| j                  |d   | j                  d|d    dg      }|b|j                  |       t        |      }| j                  |d   | j                  d|d    dg      }|t        |      }| j                  ||      s||d<   |
j                  |        |
S )al  
        Returns a list of certificates
        if domain_name is specified, returns only certificates with that domain
        if an ARN is specified, returns only that certificate
        only_tags is a dict, e.g. {'key':'value'}. If specified this function will return
        only certificates which contain all those tags (key exists, value matches).
        zCouldn't obtain certificates)r?   r   r   c                 2    r	| d   k7  ryr	| d   k7  ryy)N
DomainNameFr+   Tr   )certr-   rr   s    r   _filter_certificatez?ACMServiceManager.get_certificates.<locals>._filter_certificate   s+    tL1[@t,-4r   r+   z0Couldn't obtain certificate metadata for domain rv   rB   )r   r   r   Status)PENDING_VALIDATIONVALIDATION_TIMED_OUTFAILEDz,Couldn't obtain certificate data for domain z Couldn't obtain tags for domain r\   )rA   r   listfilterrM   rI   updater   rQ   r   rl   append)r#   rr   r?   r-   rs   r   r   all_certificatesrx   certificatesresultsrU   	cert_data	cert_infor\   s    ` `           r   get_certificatesz"ACMServiceManager.get_certificates   s     >>dkk9W ? 
	 F#68HIJ' $	&K>>,-{{HUaIbHcd$?#@	 ? I   "*bb == 01;;HUaIbHcd(C'D	 > 	 $  +0;I::,-{{8\9R8ST$?#@	 ; D |1$7D##It4 $IfNN9%I$	&J r   c                     || j                   j                  d       d| }| j                  || j                   |      }|d   S )z
        returns the domain name of a certificate (encoded in the public cert)
        for a given ARN A cert with that ARN must already exist
        zEInternal error with ACM domain fetching, no certificate ARN specifiedr   z)Couldn't obtain certificate data for arn )rG   r   r   rv   )r   rq   rM   )r#   r-   r   r   r   s        r   get_domain_of_certz$ACMServiceManager.get_domain_of_cert   sU    
 ;KK!!&m!n;C5A::3W[WbWbjo:p	&&r   )r-   rW   r\   c                   |}||||| j                   dd} | j                  d
i |}|r'||k7  r"| j                   j                  d| d|        	 | j                  ||       |S # t        t
        f$ r}		 | j                  |       nU# t        t
        f$ rC | j                   j                  d| d       | j                   j                  |	d| d	       Y nw xY w| j                   j                  |	d|        Y d }	~	|S d }	~	ww xY w)NzCouldn't upload new certificate)rU   rV   rW   r-   r   r   z"ARN changed with ACM update, from z to r   zCertificate zG exists, and is not tagged. So Ansible will not see it on the next run.zCouldn't tag certificate z, couldn't delete it eitherr   )	r   rY   rq   r^   r   r   r.   warnr   )
r#   rU   rV   r-   rW   r\   r   original_arnrX   r   s
             r   rT   z$ACMServiceManager.import_certificate   s;    '&!2kk6
 3d22<V<SL0 KK!!(J<.X\]`\a&b!c
	P--c48 
 {+ 	Po44S9!;/ o  "3%'no ))!3LSEQl1m)n	o
 KK%%a/H-N%OO
	Ps7   A& &D6BDACDC#DDra   )__name__
__module____qualname____doc__r$   r   r	   jittered_backoffr.   rA   rI   rM   rQ   rY   r^   rl   r,   r   r   rT   r   r   r   r   r   3   s   $+ XQA]@^_; ` ; XQA]@^_Z ` Z( X*FHc)dj j
 X*FHc)d_ _ X*FHc)d] ] XQA]@^_J ` J X*FHc)dOOF -1 S 374[_ ;z	' GK^bim  r   r   N)r   botocore.exceptionsr   r   ImportErroransible.module_utils._textr   0ansible.module_utils.common.dict_transformationsr   botocorer   retriesr	   taggingr
   r   r   r   r   r   r   <module>r      sN   	1/ 0 U )  3 3$F F=  		s   A A
	A
