a
    `5                     @   s   d Z ddlm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 eeZejjZd"d	d
Zd#ddZd$ddZdd Zdd Zdd Zg dZdd Zdd Zdd Zdd Zd d! Z dS )%z=Decide which plugins to use for authentication & installation    )print_functionN)errors)
interfaces)os)util<How would you like to authenticate and install certificates?c                 C   s   t | |||tjtjfS )zPick configurator plugin.)pick_pluginr   IAuthenticator
IInstallerconfigdefaultpluginsquestion r   E/usr/lib/python3/dist-packages/certbot/_internal/plugins/selection.pypick_configurator   s    
r   +How would you like to install certificates?c                 C   s   t | |||tjfS )zPick installer plugin.)r   r   r
   r   r   r   r   pick_installer   s    r   4How would you like to authenticate with the ACME CA?c                 C   s   t | |||tjfS )zPick authentication plugin.)r   r   r	   r   r   r   r   pick_authenticator"   s    r   c                    s   t | \}  sdS | fdd}||  |tjf}t|dkrXtd  |rt	|
 d }td| || S td  dS )	a9  
    Get an unprepared interfaces.IInstaller object.

    :param certbot.interfaces.IConfig config: Configuration
    :param certbot._internal.plugins.disco.PluginsRegistry plugins:
        All plugins registered as entry points.

    :returns: Unprepared installer plugin or None
    :rtype: IPlugin or None
    Nc                    s
   |   S NZ
check_nameZp_epreq_instr   r   <lambda>9       z*get_unprepared_installer.<locals>.<lambda>   zfFound multiple installers with the name %s, Certbot is unable to determine which one to use. Skipping.r   zSelecting plugin: %sz:Could not select or initialize the requested installer %s.)cli_plugin_requestsfilterinitverifyr   r
   lenr   PluginSelectionErrorlistvaluesloggerdebug)r   r   _Z
installersinstr   r   r   get_unprepared_installer*   s&    

r+   c           	         s    dur|  fdd}n| jr,td| |}||  ||}|  |	 }t
|dkrtd| ttt||}|du rdS | S t
|dkrt| d }td| |jrdS | S td	 dS dS )
a  Pick plugin.

    :param certbot.interfaces.IConfig: Configuration
    :param str default: Plugin name supplied by user or ``None``.
    :param certbot._internal.plugins.disco.PluginsRegistry plugins:
        All plugins registered as entry points.
    :param str question: Question to be presented to the user in case
        multiple candidates are found.
    :param list ifaces: Interfaces that plugins must provide.

    :returns: Initialized plugin.
    :rtype: IPlugin

    Nc                    s
   |   S r   r   r   r   r   r   r   Y   r   zpick_plugin.<locals>.<lambda>a  Missing command line flags. For non-interactive execution, you will need to specify a plugin on the command line.  Run with '--help plugins' to see a list of options, and see https://eff.org/letsencrypt-plugins for more detail on what the plugins do and how to use them.r   zMultiple candidate plugins: %sr   zSingle candidate plugin: %szNo candidate plugin)r    Znoninteractive_moder   MissingCommandlineFlagZvisibleifacesr!   r"   prepareZ	availabler#   r'   r(   choose_pluginr%   sixZ
itervaluesr&   misconfigured)	r   r   r   r   r.   ZfilteredZverifiedprepared	plugin_epr   r,   r   r   H   s2    


r   c           	      C   s   dd | D }t dd | D }ttj}dtjv rl|ddhkrldd t| D d	 }|j|||d
\}}n|j||dd\}}|tj	kr| | }|j
rttjjd| dd q|S q dS q dS )zAllow the user to choose their plugin.

    :param list prepared: List of `~.PluginEntryPoint`.
    :param str question: Question to be presented to the user.

    :returns: Plugin entry point chosen by the user.
    :rtype: `~.PluginEntryPoint`

    c                 S   s    g | ]}|j |jrd nd qS )z [Misconfigured] )Zdescription_with_namer2   .0r4   r   r   r   
<listcomp>   s   z!choose_plugin.<locals>.<listcomp>c                 s   s   | ]}|j V  qd S r   namer6   r   r   r   	<genexpr>   r   z choose_plugin.<locals>.<genexpr>ZCERTBOT_AUTOapachenginxc                 S   s   g | ]\}}|j d kr|qS )r<   r9   )r7   npr   r   r   r8      r   r   r,   T)Zforce_interactivezxThe selected plugin encountered an error while parsing your server configuration and cannot be used. The error was:

{0}F)pauseN)setz_utilr   ZIDisplayr   environ	enumerateZmenudisplay_utilZOKr2   Znotificationformatr/   )	r3   r   ZoptsnamesZdispZ
apache_idxcodeindexr4   r   r   r   r0   ~   s(    



r0   )webrootmanual
standalonedns-cloudflaredns-cloudxnsdns-digitaloceandns-dnsimpledns-dnsmadeeasy
dns-gehirn
dns-google
dns-linode
dns-luadns	dns-nsonedns-ovhdns-rfc2136dns-route53dns-sakuracloudc                 C   sB   |r| |jnd| _|r&| |jnd| _td| j| j dS )zFUpdate the config entries to reflect the plugins we actually selected.Nz0Plugins selected: Authenticator %s, Installer %s)Z	find_initr:   authenticator	installerr'   info)r   r   Zauthr*   r   r   r   record_chosen_plugins   s
    r^   c                 C   s8  t | \}}d}|dkrd}|dkr`d }}ddlm} |tv rh|shd|tj|}	t|	nd	 }}|d
krvd}n|dv rd}| j	rt
d| d }
}|dkr||krt| || }
}n&|s|rt| |||}|rt| ||}
t
d|
| |r
|s
td|| |r"|
s"td|| t| ||
| ||
fS )a;  
    Figure out which configurator we're going to use, modifies
    config.authenticator and config.installer strings to reflect that choice if
    necessary.

    :raises errors.PluginSelectionError if there was a problem

    :returns: (an `IAuthenticator` or None, an `IInstaller` or None)
    :rtype: tuple
    NenhancezMWhich installer would you like to use to configure the selected enhancements?runTr   cli_commandzWith the {0} plugin, you probably want to use the "certonly" command, eg:{1}{1}    {2} certonly --{0}{1}{1}(Alternatively, add a --installer flag. See https://eff.org/letsencrypt-plugins{1} and "--help plugins" for more information.)FZcertonly)Zinstallr_   zRSpecifying an authenticator doesn't make sense when running Certbot with verb "%s"z*Selected authenticator %s and installer %sr\   r[   )r   certbot._internal.clirb   noninstaller_pluginsrF   r   linesepr   r-   r[   r'   Zwarningr   r   r   r(   diagnose_configurator_problemr^   )r   r   Zverbreq_authr   Zinstaller_questionZ	need_instZ	need_authrb   msgr[   r\   r   r   r   choose_configurator_plugins   sF    ri   c                 C   s6   |s| S | r2| |kr2d}t |t| t||S )z
    Setting configurators multiple ways is okay, as long as they all agree
    :param str previously: previously identified request for the installer/authenticator
    :param str requested: the request currently being processed
    zIToo many flags setting configurators/installers/authenticators {0} -> {1})r   r$   rF   repr)Z
previouslyZnowrh   r   r   r   set_configurator   s    rk   c                 C   s  | j  }}t|| j}t|| j}| jr<t|d}t|d}| jrVt|d}t|d}| jrft|d}| jrvt|d}| jrt|d}| j	rt|d}| j
rt|d}| jrt|d}| jrt|d	}| jrt|d
}| jrt|d}| jrt|d}| jrt|d}| jrt|d}| jr,t|d}| jr>t|d}| jrPt|d}| jrbt|d}| jrtt|d}td|| ||fS )z
    Figure out which plugins the user requested with CLI and config options

    :returns: (requested authenticator string or None, requested installer string or None)
    :rtype: tuple
    r=   r<   rL   rJ   rK   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   z+Requested authenticator %s and installer %s)Zconfiguratorrk   r\   r[   r=   r<   rL   rJ   rK   Zdns_cloudflareZdns_cloudxnsZdns_digitaloceanZdns_dnsimpleZdns_dnsmadeeasyZ
dns_gehirnZ
dns_googleZ
dns_linodeZ
dns_luadnsZ	dns_nsoneZdns_ovhZdns_rfc2136Zdns_route53Zdns_sakuracloudr'   r(   )r   r   rg   r   r   r   r     sZ    





















r   c                 C   sd   |r,||vrd |}qVd ||| j}n*| dkrLddlm} d |}n
d | }t|dS )	a>  
    Raise the most helpful error message about a plugin being unavailable

    :param str cfg_type: either "installer" or "authenticator"
    :param str requested: the plugin that was requested
    :param .PluginsRegistry plugins: available plugins

    :raises error.PluginSelectionError: if there was a problem
    z8The requested {0} plugin does not appear to be installedzkThe {0} plugin is not working; there may be problems with your existing configuration.
The error was: {1!r}r\   r   ra   zCertbot doesn't know how to automatically configure the web server on this system. However, it can still get a certificate for you. Please run "{0} certonly" to do so. You'll need to manually configure your web server to use the resulting certificate.z/{0} could not be determined or is not installedN)rF   Zproblemrc   rb   r   r$   )Zcfg_typeZ	requestedr   rh   rb   r   r   r   rf   8  s    

rf   )r   )r   )r   )!__doc__Z
__future__r   Zloggingr1   Zzope.componentzopeZcertbotr   r   Zcertbot.compatr   Zcertbot.displayr   rE   Z	getLogger__name__r'   Z	componentZ
getUtilityrB   r   r   r   r+   r   r0   rd   r^   ri   rk   r   rf   r   r   r   r   <module>   s2   
 
 
 
6'?7