o
    th#                  
   @   sl  U d Z ddlZddlZddlmZmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ eeZddd	d
dgdZdddddgddddddgddddddgddddddgddddddgddZdD ]Zed ee< qhdD ]Zed ee< qsg dZdeed d!gd"Zeed#< d$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0ed1e
d2ed3ed4df
d5d6Z dS )7zCA Certs: Add ca certificates.    N)	lifecyclesubputil)Cloud)Config)
MetaSchema)PER_INSTANCEz!/usr/local/share/ca-certificates/z#cloud-init-ca-cert-{cert_index}.crtz/etc/ca-certificates.confzupdate-ca-certificates)ca_cert_pathca_cert_local_pathca_cert_filenameca_cert_configca_cert_update_cmdz/etc/ssl/certs/z#cloud-init-ca-cert-{cert_index}.pemz+/etc/ca-certificates/conf.d/cloud-init.confzupdate-ca-bundlez/etc/pki/ca-trust/z/usr/share/pki/ca-trust-source/z+anchors/cloud-init-ca-cert-{cert_index}.crtzupdate-ca-trustz/etc/pki/trust/z/usr/share/pki/trust/z/etc/pki/tls/certs/zrehash_ca_certificates.sh)aoscfedorarhelopensusephoton)opensuse-microosopensuse-tumbleweedopensuse-leapsle_hpc	sle-microslesr   )	almalinuxcentos
cloudlinuxrockyr   )r   r   r   r   alpinedebianr   raspberry-pi-osr   r   r   r   r   r   r   r   ubuntur   cc_ca_certsca_certsca-certs)iddistros	frequencyactivate_by_schema_keysmetac                 C   s*   t | t}tj|d |d |d< |S )zReturn a distro-specific ca_certs config dictionary

    @param distro_name: String providing the distro class name.
    @returns: Dict of distro configurations for ca_cert.
    r
   r   ca_cert_full_path)DISTRO_OVERRIDESgetDEFAULT_CONFIGospathjoin)distro_namecfg r2   >/usr/lib/python3/dist-packages/cloudinit/config/cc_ca_certs.py_distro_ca_certs_configsn   s
   r4   c                 C   s   t j | d dd dS )z
    Updates the CA certificate cache on the current machine.

    @param distro_cfg: A hash providing _distro_ca_certs_configs function.
    r   F)captureN)r   
distro_cfgr2   r2   r3   update_ca_certs{   s   r8   c                 C   sH   |sdS t |dD ]\}}t|}| d j|d}tj||dd q	dS )a-  
    Adds certificates to the system. To actually apply the new certificates
    you must also call the appropriate distro-specific utility such as
    L{update_ca_certs}.

    @param distro_cfg: A hash providing _distro_ca_certs_configs function.
    @param certs: A list of certificate strings.
    N   r)   )
cert_indexi  )mode)	enumeratestrformatr   
write_file)r7   certsr:   ccert_file_contentscert_file_namer2   r2   r3   add_ca_certs   s   	rD   c                 C   sJ   | dv r
t | dS | dv r!t| | dv r#d}tjd|d dS dS dS )a.  
    Disables all default trusted CA certificates. For Alpine, Debian and
    Ubuntu to actually apply the changes you must also call
    L{update_ca_certs}.

    @param distro_name: String providing the distro class name.
    @param distro_cfg: A hash providing _distro_ca_certs_configs function.
    )r   r   )r   r   r   r   r    )r   r   r    z8ca-certificates ca-certificates/trust_new_crts select no)zdebconf-set-selections-)dataN)remove_default_ca_certsdisable_system_ca_certsr   )r0   r7   debconf_selr2   r2   r3   disable_default_ca_certs   s   		rJ   c                 C   s   | d }|rt j|sdS d}d}t |jrat|}g }| D ].}||kr1d}|| q#|dks;|d dv rA|| q#|sJ|| d}|d	|  q#tj	|d

|d
 dd dS dS )z
    For every entry in the CA_CERT_CONFIG file prefix the entry with a "!"
    in order to disable it.

    @param distro_cfg: A hash providing _distro_ca_certs_configs function.
    r   Nz;# Modified by cloud-init to deselect certs due to user-dataFT r   )#!rM   
wb)omode)r-   r.   existsstatst_sizer   load_text_file
splitlinesappendr?   r/   )r7   ca_cert_cfg_fnheader_commentadded_headerorig	out_linesliner2   r2   r3   rH      s.   


rH   c                 C   s:   | d du rdS t d t| d  t| d  dS )z
    Removes all default trusted CA certificates from the system.

    @param distro_cfg: A hash providing _distro_ca_certs_configs function.
    r	   NzDeleting system CA certificatesr
   )LOGdebugr   delete_dir_contentsr6   r2   r2   r3   rG      s
   
rG   namer1   cloudargsreturnc                 C   s  d|v rt jdddd nd|vrtd|  dS d|v r&d|v r&td	 |d|d}t|ts8td
t	|j
j}d|v rJt jdddd |d|ddr`td t|j
j| d|v ryt|d}|rytdt| t|| td t| dS )au  
    Call to handle ca_cert sections in cloud-config file.

    @param name: The module name "ca_cert" from cloud.cfg
    @param cfg: A nested dict containing the entire cloud config contents.
    @param cloud: The L{CloudInit} object in use.
    @param log: Pre-initialized Python logger object to use for logging.
    @param args: Any module arguments from cloud.cfg
    r#   zKey 'ca-certs'z22.1zUse 'ca_certs' instead.)
deprecateddeprecated_versionextra_messager"   z<Skipping module named %s, no 'ca_certs' key in configurationNzMFound both ca-certs (deprecated) and ca_certs config keys. Ignoring ca-certs.zunexpected type: {ca_cert_cfg}zremove-defaultszKey 'remove-defaults'zUse 'remove_defaults' instead.remove_defaultsFz'Disabling/removing default certificatestrustedzAdding %d certificateszUpdating certificates)r   	deprecater]   r^   warningr+   
isinstancedict	TypeErrorr4   distror`   rJ   r   get_cfg_option_listlenrD   r8   )r`   r1   ra   rb   ca_cert_cfgr7   trusted_certsr2   r2   r3   handle   sL   




rs   )!__doc__loggingr-   	cloudinitr   r   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   cloudinit.settingsr   	getLogger__name__r]   r,   r*   rn   r%   r(   __annotations__r4   r8   rD   rJ   rH   rG   r=   listrs   r2   r2   r2   r3   <module>   s~   
	&	'"