
    f'%              
          U d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlmZ  ej"                  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ZdD ]
  Zed   ee<    dZg dZdddeee ed      gd d!gd"Zeed#<    ee      Z d$ Zd% Zd& Zd' Zd( Zd) Z d*e!d+ed,e	d-e"d.df
d/Z#y)0zCA Certs: Add ca certificates.    N)dedent)subputil)Cloud)Config)
MetaSchemaget_meta_doc)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/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/ssl/certs/z/etc/pki/tls/certs/zrehash_ca_certificates.sh)fedorarhelopensusephoton)opensuse-microosopensuse-tumbleweedopensuse-leapsle_hpc	sle-microslesr   a/  This module adds CA certificates to the system's CA store and updates any
related files using the appropriate OS-specific utility. The default CA
certificates can be disabled/deleted from use by the system with the
configuration option ``remove_defaults``.

.. note::
    certificates must be specified using valid yaml. in order to specify a
    multiline certificate, the yaml multiline list syntax must be used

.. note::
    Alpine Linux requires the ca-certificates package to be installed in
    order to provide the ``update-ca-certificates`` command.
)alpinedebianr   r   r   r   r   r   r   r   r   ubuntur   cc_ca_certszCA CertificateszAdd ca certificatesa              ca_certs:
              remove_defaults: true
              trusted:
                - single_line_cert
                - |
                  -----BEGIN CERTIFICATE-----
                  YOUR-ORGS-TRUSTED-CA-CERT-HERE
                  -----END CERTIFICATE-----
            ca_certsca-certs)idnametitledescriptiondistros	frequencyexamplesactivate_by_schema_keysmetac                     t         j                  | t              }t        j                  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cfgs     >/usr/lib/python3/dist-packages/cloudinit/config/cc_ca_certs.py_distro_ca_certs_configsr4   |   sF     

{N
;C!ww|| !3'9#: C J    c                 8    t        j                   | d   d       y)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_cfgs    r3   update_ca_certsr:      s     	IIj-.>r5   c                     |syt        |d      D ]=  \  }}t        |      }| d   j                  |      }t        j                  ||d       ? y)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)r9   certsr=   ccert_file_contentscert_file_names         r3   add_ca_certsrG      sb     "5!, H
A V#$78??! @ 
 	(:GHr5   c                     | dv rt        |       y| dv r*t        |       | dv rd}t        j                  d|       yyy)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   z8ca-certificates ca-certificates/trust_new_crts select no)zdebconf-set-selections-)dataN)remove_default_ca_certsdisable_system_ca_certsr   )r1   r9   debconf_sels      r3   disable_default_ca_certsrN      sR     ((
+	6	6
+..O  II5KH	 / 
7r5   c                    | d   }|rt         j                  j                  |      syd}d}t        j                  |      j                  rt        j                  |      }g }|j                         D ]b  }||k(  rd}|j                  |       |dk(  s|d   dv r|j                  |       :|s|j                  |       d}|j                  d	|z          d t        j                  |d
j                  |      d
z   d       yy)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   )#!rR   
wb)omode)r.   r/   existsstatst_sizer   load_text_file
splitlinesappendrB   r0   )r9   ca_cert_cfg_fnheader_commentadded_headerorig	out_lineslines          r3   rL   rL      s       01N!? 	F  L	ww~&&"">2	OO% 
	-D~%#  &tAw*4  &#$$^4#'L  t,
	- 	DIIi047t	
 'r5   c                     | d   yt         j                  d       t        j                  | d          t        j                  | d          y)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_contentsr8   s    r3   rK   rK      sF     .!)II/0Z78Z(<=>r5   r!   r2   cloudargsreturnc                    d|v rt        j                  ddd       nd|vrt        j                  d|        yd|v rd|v rt        j	                  d	       |j                  d|j                  d            }t        |j                  j                        }d
|v rt        j                  ddd       |j                  d|j                  d
d            r5t        j                  d       t        |j                  j                  |       d|v rCt        j                  |d      }|r+t        j                  dt        |             t        ||       t        j                  d       t        |       y)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remove-defaultszKey 'remove-defaults'zUse 'remove_defaults' instead.remove_defaultsFz'Disabling/removing default certificatestrustedzAdding %d certificateszUpdating certificates)r   	deprecaterc   rd   warningr,   r4   distror!   rN   get_cfg_option_listlenrG   r:   )r!   r2   rf   rg   ca_cert_cfgr9   trusted_certss          r3   handlerv      sF    S'%3	

 
3			J	
 	SZ3."	
 ''*cggj&9:K)%,,*;*;<J K'.%:	

 ;??+<eD 			;< !2!2J? K00iHII.M0BC]3 II%&Jr5   )$__doc__loggingr.   textwrapr   	cloudinitr   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   r	   cloudinit.settingsr
   	getLogger__name__rc   r-   r+   rq   MODULE_DESCRIPTIONr$   r(   __annotations__r4   r:   rG   rN   rL   rK   r@   listrv    r5   r3   <module>r      s  
 %  	    ! # < +g! ==134 -?I01 -?I01 *5I78 *3A:;- > <F  0
;V< " "%		
 !+J7+j 0 t

?H*I*$
N?6  6 6 6 % 6 t 6  6 r5   