
    M/e2                         d Z ddl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
 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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  ej>                  e       Z! G d d      Z"de#dee#ef   dee#ef   deee	e#ef   f   fdZ$ G d dejJ                        Z& G d d      Z' ee e'ejP                  e                ejP                  e <   y# e$ r	 dZdZdZY w xY w)z;Common code for DNS Authenticator Plugins built on Lexicon.    N)
ModuleType)Any)cast)Dict)List)Mapping)Optional)Tuple)Union)	HTTPError)RequestException)configuration)errors)
dns_common)Client)ConfigResolver)Providerc                       e Zd ZdZddZdedededdfdZdedededdfd	Zdeddfd
Zde	dede
ej                     fdZdedede
ej                     fdZy)LexiconClientz
    Encapsulates all communication with a DNS provider via Lexicon.

    .. deprecated:: 2.7.0
       Please use certbot.plugins.dns_common_lexicon.LexiconDNSAuthenticator instead.
    returnNc                     |  y N selfs    D/usr/lib/python3/dist-packages/certbot/plugins/dns_common_lexicon.py__init__zLexiconClient.__init__0   s        domainrecord_namerecord_contentc                     | j                  |       	 | j                  j                  d||       y# t        $ rA}t        j                  d|d       t        j                  dj                  |            d}~ww xY w)a  
        Add a TXT record using the supplied information.

        :param str domain: The domain to use to look up the managed zone.
        :param str record_name: The record name (typically beginning with '_acme-challenge.').
        :param str record_content: The record content (typically the challenge validation).
        :raises errors.PluginError: if an error occurs communicating with the DNS Provider API
        TXTrtypenamecontent'Encountered error adding TXT record: %sTexc_infoError adding TXT record: {0}N)	_find_domain_idprovidercreate_recordr   loggerdebugr   PluginErrorformatr   r   r    r!   es        r   add_txt_recordzLexiconClient.add_txt_record3   st     	V$	OMM''e+~'^ 	OLLBAPTLU$$%C%J%J1%MNN	Os   2 	A<<A77A<c                 2   	 | j                  |       	 | j
                  j                  d||       y# t        j                  $ r"}t        j	                  d|d       Y d}~yd}~ww xY w# t        $ r"}t        j	                  d|d       Y d}~yd}~ww xY w)a  
        Delete a TXT record using the supplied information.

        :param str domain: The domain to use to look up the managed zone.
        :param str record_name: The record name (typically beginning with '_acme-challenge.').
        :param str record_content: The record content (typically the challenge validation).
        :raises errors.PluginError: if an error occurs communicating with the DNS Provider  API
        7Encountered error finding domain_id during deletion: %sTr)   Nr#   r$   )Encountered error deleting TXT record: %s)r,   r   r1   r/   r0   r-   delete_recordr   r3   s        r   del_txt_recordzLexiconClient.del_txt_recordD   s    	  (	XMM''e+~'^ !! 	LLRTU"&  (	   	XLLDaRVLWW	Xs,   3 A+ A(A##A(+	B4BBc                    t        j                  |      }|D ]_  }	 t        | j                  d      r|| j                  j                  d<   n|| j                  _        | j                  j                           y t        j                  dj                  ||            # t        $ r }| j                  ||      }|r|Y d}~d}~wt        $ r }| j                  ||      }|r|Y d}~d}~ww xY w)z
        Find the domain_id for a given domain.

        :param str domain: The domain for which to find the domain_id.
        :raises errors.PluginError: if the domain_id cannot be found.
        optionsr   NAUnable to determine zone identifier for {0} using zone names: {1})r   base_domain_name_guesseshasattrr-   r<   r   authenticater   _handle_http_error	Exception_handle_general_errorr   r1   r2   r   r   domain_name_guessesdomain_namer4   result1result2s          r   r,   zLexiconClient._find_domain_idY   s     )AA&I. 	"K"4==)46ADMM))(3 ,7DMM(**,	".   !d"(&1D"EG 	G  "11![A!M  "44QD!M "s$   AB	C/(CC/C**C/r4   rF   c                 L    t        j                  dj                  ||            S Nz/Error determining zone identifier for {0}: {1}.r   r1   r2   r   r4   rF   s      r   rA   z LexiconClient._handle_http_error}   $    !!"S#)6+q#9; 	;r   c                     t        |      j                  d      s%t        j                  dj	                  ||            S y NzNo domain foundz9Unexpected error determining zone identifier for {0}: {1}str
startswithr   r1   r2   rL   s      r   rC   z#LexiconClient._handle_general_error   :    1v  !23%%&a'-vk1'=? ?r   r   N)__name__
__module____qualname____doc__r   rQ   r5   r:   r,   r   r	   r   r1   rA   rB   rC   r   r   r   r   r   (   s     OS Os OC OTX O"XS Xs XC XTX X*"Gc "Gd "GH;I ;C ;HVM_M_D` ;y s xPVPbPbGc r   r   lexicon_provider_namelexicon_optionsprovider_optionsr   c                     d| i}|j                  |       t        |j                  |       |S i }|j                  |       ||| <   t               j                  |      j                         S )a  
    Convenient function to build a Lexicon 2.x/3.x config object.

    :param str lexicon_provider_name: the name of the lexicon provider to use
    :param dict lexicon_options: options specific to lexicon
    :param dict provider_options: options specific to provider
    :return: configuration to apply to the provider
    :rtype: ConfigurationResolver or dict

    .. deprecated:: 2.7.0
       Please use certbot.plugins.dns_common_lexicon.LexiconDNSAuthenticator instead.
    provider_name)updater   	with_dictwith_env)rY   rZ   r[   config_dictprovider_configs        r   build_lexicon_configrc      sv     $34I"JK'+, +-/0-<)*))+6??AAr   c                   V    e Zd ZdZdej
                  def fdZee	j                  defd              Zedefd       Zded	ed
eddfdZdedefdZddZdedededdfdZdedededdfdZdedefdZdededeej0                     fdZdededeej0                     fdZ xZS )LexiconDNSAuthenticatorzr
    Base class for a DNS authenticator that uses Lexicon client
    as backend to execute DNS record updates
    configr&   c                 8    t         |   ||       g | _        |  y r   )superr   _provider_options)r   rf   r&   	__class__s      r   r   z LexiconDNSAuthenticator.__init__   s    &=?r   r   c                      y)z9
        The name of the Lexicon provider to use
        Nr   r   s    r   _provider_namez&LexiconDNSAuthenticator._provider_name   s    r   c                      y)zX
        Time to live to apply to the DNS records created by this Authenticator
        <   r   r   s    r   _ttlzLexiconDNSAuthenticator._ttl   s    
 r   creds_var_namecreds_var_labellexicon_provider_option_nameNc                 @    | j                   j                  |||f       y r   )ri   append)r   rp   rq   rr   s       r   _add_provider_optionz,LexiconDNSAuthenticator._add_provider_option   s"    %%_.JK	Mr   r   c                 V   t        | d      s| j                          d|d|d| j                  d| j                  | j                  | j                  D ci c]$  }|d   | j
                  j                  |d         & c}i}t               j                  |      j                         S c c}w )N_credentialsr   	delegatedr]   ttl   r   )
r?   _setup_credentialsrl   ro   ri   rw   confr   r_   r`   )r   r   itemdict_configs       r   _build_lexicon_configz-LexiconDNSAuthenticator._build_lexicon_config   s    t^,##% f T00499.2.D.D"F&* #'q'4+<+<+A+A$q'+J"J "F

 ))+6??AA"Fs   )B&c           
          | j                  dd| j                   d| j                  D ci c]  }|d   |d    c}      | _        y c c}w )NcredentialszCredentials INI file for z DNS authenticatorr      )keylabelrequired_variables)_configure_credentialsrl   ri   rw   )r   r}   s     r   r{   z*LexiconDNSAuthenticator._setup_credentials   sX     77-d.A.A-BBTU=A=S=STTQa 0T 8 
  Us   Avalidation_name
validationc                 H   | j                  |      }	 t        | j                  |            5 }|j                  d||       d d d        y # 1 sw Y   y xY w# t        $ rA}t
        j                  d|d       t        j                  dj                  |            d }~ww xY w)Nr#   r$   r(   Tr)   r+   )
_resolve_domainr   r   r.   r   r/   r0   r   r1   r2   )r   r   r   r   resolved_domain
operationsr4   s          r   _performz LexiconDNSAuthenticator._perform   s    ..v6	O22?CD `
((u?T^(_` ` ` 	OLLBAPTLU$$%C%J%J1%MNN	Os4   A AA AA A 	B! <BB!c                 |   	 | j                  |      }	 t        | j                  |            5 }|j                  d||       d d d        y # t        j                  $ r"}t        j	                  d|d       Y d }~y d }~ww xY w# 1 sw Y   y xY w# t        $ r"}t        j	                  d|d       Y d }~y d }~ww xY w)Nr7   Tr)   r#   r$   r8   )	r   r   r1   r/   r0   r   r   r9   r   )r   r   r   r   r   r4   r   s          r   _cleanupz LexiconDNSAuthenticator._cleanup   s    	"226:O	X22?CD `
((u?T^(_` ` !! 	LLRTU"&  (	` ` 	XLLDaRVLWW	XsK   A B BB BA<<BB	B B 	B;B66B;c                    t        j                  |      }|D ]*  }	 t        | j                  |            5  |cd d d        c S  t        j                  dj                  ||            # 1 sw Y   nxY w_# t        $ r }| j                  ||      }|r|Y d }~d }~wt        $ r }| j                  ||      }|r|Y d }~d }~ww xY w)Nr=   )r   r>   r   r   r   rA   rB   rC   r   r1   r2   rD   s          r   r   z'LexiconDNSAuthenticator._resolve_domain   s    (AA&I. 	"K" D66{CD '&' '		""   !d"(&1D"EG 	G' ' ' "11![A!M  "44QD!M "s9   A7A*	A7*A3	/A77	C BC'CCr4   rF   c                 L    t        j                  dj                  ||            S rJ   rK   rL   s      r   rA   z*LexiconDNSAuthenticator._handle_http_error
  rM   r   c                     t        |      j                  d      s%t        j                  dj	                  ||            S y rO   rP   rL   s      r   rC   z-LexiconDNSAuthenticator._handle_general_error  rS   r   rT   )rU   rV   rW   rX   r   NamespaceConfigrQ   r   propertyabcabstractmethodrl   intro   ru   r   r   r{   r   r   r   r   r	   r   r1   rA   rB   rC   __classcell__)rj   s   @r   re   re      sU   
?}<< ?C ?
    
 c  M3 M M;>MCGM
BC BN B"
Os OS Oc Od OXs XS Xc Xd XGc Gc G.;I ;C ;HVM_M_D` ;y s xPVPbPbGc r   re   c                   b    e Zd ZdZdefdZdedefdZdededdfd	Z	dedefd
Z
dee   fdZy)_DeprecationModulez
    Internal class delegating to a module, and displaying warnings when attributes
    related to deprecated attributes in the current module.
    modulec                 "    || j                   d<   y N_module)__dict__)r   r   s     r   r   z_DeprecationModule.__init__  s    #)i r   attrr   c                     |dv r&t        j                  | dt         dt        d       t	        | j
                  |      S )N)r   rc   z attribute in z/ module is deprecated and will be removed soon.rz   )
stacklevel)warningswarnrU   DeprecationWarninggetattrr   r   r   s     r   __getattr__z_DeprecationModule.__getattr__   sC    <<MMTF.
 ;6 6,< t||T**r   valueNc                 2    t        | j                  ||       y r   )setattrr   )r   r   r   s      r   __setattr__z_DeprecationModule.__setattr__'  s    dE*r   c                 0    t        | j                  |       y r   )delattrr   r   s     r   __delattr__z_DeprecationModule.__delattr__*  s    d#r   c                 4    dgt        | j                        z   S r   )dirr   r   s    r   __dir__z_DeprecationModule.__dir__-  s    {S...r   )rU   rV   rW   rX   r   r   rQ   r   r   r   r   r   r   r   r   r   r   r     sc    *z *+ + ++ +C +D +$ $ $/c /r   r   ))rX   r   loggingsystypesr   typingr   r   r   r   r   r	   r
   r   r   requests.exceptionsr   r   certbotr   r   certbot.pluginsr   lexicon.clientr   lexicon.configr   lexicon.interfacesr   ImportError	getLoggerrU   r/   r   rQ   rc   DNSAuthenticatorre   r   modulesr   r   r   <module>r      s#   A 
  
           ) 0 !  &%-+ 
		8	$] ]@B B*1#s(*;BOVWZ\_W_O`B#NDcN$BCB:mj99 mf/ /4 Z);CKK<Q)RSH e  FNHs   (C? ?DD