File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/acme.cpython-310.pyc
o
�N�gK2 � @ s� d Z ddlZddlZddlZddlZddlmZ e�e �Z
ejj�
g d��ZdZejj�� r2de Zdd� Zd d
� Zdd� Zd d
d�Z d!dd�Zdd� Zdd� Zdd� Zdd� Zd dd�Zd dd�ZdS )"ar
ACME / Let's Encrypt module
===========================
.. versionadded:: 2016.3.0
This module currently looks for certbot script in the $PATH as
- certbot,
- lestsencrypt,
- certbot-auto,
- letsencrypt-auto
eventually falls back to /opt/letsencrypt/letsencrypt-auto
.. note::
Installation & configuration of the Let's Encrypt client can for example be done using
https://github.com/saltstack-formulas/letsencrypt-formula
.. warning::
Be sure to set at least accept-tos = True in cli.ini!
Most parameters will fall back to cli.ini defaults if None is given.
DNS plugins
-----------
This module currently supports the CloudFlare certbot DNS plugin. The DNS
plugin credentials file needs to be passed in using the
``dns_plugin_credentials`` argument.
Make sure the appropriate certbot plugin for the wanted DNS provider is
installed before using this module.
� N)�SaltInvocationError)ZcertbotZletsencryptzcertbot-autozletsencrypt-autoz!/opt/letsencrypt/letsencrypt-autoz/etc/letsencrypt/live/z
/usr/localc C s t dudfS )z6
Only work when letsencrypt-auto is installed
NzKThe ACME execution module cannot be loaded: letsencrypt-auto not installed.)�LEA� r r �E/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/acme.py�__virtual__= s �r c C s t j�t| |� d��S )z;
Return expected path of a Let's Encrypt live cert
z.pem)�os�path�join�LE_LIVE)�nameZ cert_typer r r �
_cert_fileG s r c C s` t | d�}dtv rtd |��dd�}nd|� d�}d|� d�}ttd |d
d��}tj�|�S )zY
Return the expiry date of a cert
:rtype: datetime
:return: Expiry date
�cert�
tls.cert_info� not_afterr �openssl x509 -in z -noout -enddatezdate --date="$(z | cut -d= -f2)" +%sz cmd.shell�quiet�Zoutput_loglevel)r �__salt__�get�float�datetime�
fromtimestamp)r � cert_file�expiry�openssl_cmdZstrptime_sux_cmdr r r �_expiresN s
r c C s$ t | �}|dur|tj|d� }|S )z�
Date before a certificate should be renewed
:param str name: Name of the certificate
:param int window: days before expiry date to renew
:rtype: datetime
:return: First renewal date
N)�days)r r � timedelta)r �windowr r r r � _renew_byc s r F�root�0640c C s� t dddg}|du r| }dg}t|d�}td |�s#t�d|� d }nt||�r5t�d
|� |�d� d}|r?|�d
|� �� |rI|�d|� �� |rW|rRd dd�S |�d� |rk|�d� |durj|�d|� �� n$||v r�|dkr�|�d� |�d|� �� nd d|� d�d�S |�d� |r�|�d|� �� |r�|�d|� �� |�d| � �� |dur�|D ]
}|�d|� �� q�|r�|�d|� �� |
r�|�d|
� �� |r�|�d|� �� |r�|�d|� �� |r�|�d |� �� td! d"�|��}|d# d$k�r1d%|d& v �r&|�d'� td! d"�|��}|d# d$k�r%d d(�| |d& �d�S nd d(�| |d& �d�S d)|d* v �rAd+|� d,�}d}n|�rMd+|� d-�}d}nd+|� d.�}d}|t |�i |d/�}td0 t|d1�||| |
dd2�\}}|S )3aF
Obtain/renew a certificate from an ACME CA, probably Let's Encrypt.
:param name: Common Name of the certificate (DNS name of certificate)
:param aliases: subjectAltNames (Additional DNS names on certificate)
:param email: e-mail address for interaction with ACME provider
:param webroot: True or a full path to use to use webroot. Otherwise use standalone mode
:param test_cert: Request a certificate from the Happy Hacker Fake CA (mutually
exclusive with 'server')
:param renew: True/'force' to force a renewal, or a window of renewal before
expiry in days
:param keysize: RSA key bits
:param server: API endpoint to talk to
:param owner: owner of the private key file
:param group: group of the private key file
:param mode: mode of the private key file
:param certname: Name of the certificate to save
:param preferred_challenges: A sorted, comma delimited list of the preferred
challenge to use during authorization with the most preferred challenge
listed first.
:param tls_sni_01_port: Port used during tls-sni-01 challenge. This only affects
the port Certbot listens on. A conforming ACME server will still attempt
to connect on port 443.
:param tls_sni_01_address: The address the server listens to during tls-sni-01
challenge.
:param http_01_port: Port used in the http-01 challenge. This only affects
the port Certbot listens on. A conforming ACME server will still attempt
to connect on port 80.
:param https_01_address: The address the server listens to during http-01 challenge.
:param dns_plugin: Name of a DNS plugin to use (currently only 'cloudflare'
or 'digitalocean')
:param dns_plugin_credentials: Path to the credentials file if required by
the specified DNS plugin
:param dns_plugin_propagate_seconds: Number of seconds to wait for DNS propogations
before asking ACME servers to verify the DNS record. (default 10)
:rtype: dict
:return: Dictionary with 'result' True/False/None, 'comment' and certificate's
expiry date ('not_after')
CLI Example:
.. code-block:: bash
salt 'gitlab.example.com' acme.cert dev.example.com "[gitlab.example.com]" test_cert=True renew=14 webroot=/opt/gitlab/embedded/service/gitlab-rails/public
Zcertonlyz--non-interactivez--agree-tosNZ
cloudflarer
�file.file_existsz#Certificate %s does not exist (yet)FzCertificate %s will be renewedz--renew-by-defaultTz --server z--cert-name z(Use either server or test_cert, not both)�result�commentz--test-certz--authenticator webrootz--webroot-path z--dns-cloudflarez--dns-cloudflare-credentials zDNS plugin 'z' is not supportedz--authenticator standalonez--email z--rsa-key-size z
--domains z--preferred-challenges z--tls-sni-01-port z--tls-sni-01-address z--http-01-port z--http-01-address zcmd.run_all� �retcoder �expand�stderrz--expandz&Certificate {} renewal failed with:
{}zno action taken�stdoutzCertificate z
unchangedz renewedz obtained)r$ r Zchangesr# zfile.check_permsZprivkey)�follow_symlinks)
r r r �log�debug�
needs_renewal�appendr �format�expires)r �aliases�emailZwebrootZ test_certZrenewZkeysizeZserver�owner�group�modeZcertnameZpreferred_challengesZtls_sni_01_portZtls_sni_01_addressZhttp_01_portZhttp_01_addressZ
dns_pluginZdns_plugin_credentials�cmdZsupported_dns_pluginsr Zdns�resr$ r# �ret�_r r r r
s s� D
�
�
�
��� ���
�r
c C s dd� t d t�dd� D �S )z�
Return a list of active certificates
CLI Example:
.. code-block:: bash
salt 'vhost.example.com' acme.certs
c S s&