U
    w7e                  	   @   s   U d Z ddlZddlm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mZ ddlmZ dd	lmZ eZd
ddddddgZdZdddeegeededgdgdZeed< eeZ eeZeee	eddddZdS )zPhone Home: Post data to url    N)dedent)	templater
url_helperutil)Cloud)Config)
MetaSchemaget_meta_doc)ALL_DISTROS)PER_INSTANCEpub_key_dsapub_key_rsapub_key_ecdsapub_key_ed25519instance_idhostnamefqdnaj  This module can be used to post data to a remote host after boot is complete.
If the post url contains the string ``$INSTANCE_ID`` it will be replaced with
the id of the current instance. Either all data can be posted or a list of
keys to post. Available keys are:

    - ``pub_key_dsa``
    - ``pub_key_rsa``
    - ``pub_key_ecdsa``
    - ``pub_key_ed25519``
    - ``instance_id``
    - ``hostname``
    - ``fdqn``

Data is sent as ``x-www-form-urlencoded`` arguments.

**Example HTTP POST**:

.. code-block:: http

    POST / HTTP/1.1
    Content-Length: 1337
    User-Agent: Cloud-Init/21.4
    Accept-Encoding: gzip, deflate
    Accept: */*
    Content-Type: application/x-www-form-urlencoded

    pub_key_dsa=dsa_contents&pub_key_rsa=rsa_contents&pub_key_ecdsa=ecdsa_contents&pub_key_ed25519=ed25519_contents&instance_id=i-87018aed&hostname=myhost&fqdn=myhost.internal
Zcc_phone_homez
Phone HomezPost data to urlzt            phone_home:
                url: http://example.com/$INSTANCE_ID/
                post: all
            as              phone_home:
                url: http://example.com/$INSTANCE_ID/
                post:
                    - pub_key_dsa
                    - pub_key_rsa
                    - pub_key_ecdsa
                    - pub_key_ed25519
                    - instance_id
                    - hostname
                    - fqdn
                tries: 5
            
phone_home)idnametitleZdescriptionZdistros	frequencyZexamplesZactivate_by_schema_keysmeta)r   cfgcloudargsreturnc              	   C   s
  t |dkrt|d }n d|kr4td|  d S |d }d|krTtd|  d S |d }|dd}|d}zt|}W n* tt	fk
r   d	}t
td
| Y nX |dkrt}| | j|jddjd}ddddd}	|	 D ]@\}
}zt|||
< W q tk
r*   t
td| Y qX qi }|D ]2}||krR|| ||< nd ||< td| q6i }| D ]*\}}|d krd||< nt|||< qvd|d i}t||}z$tj|||d dt|jd W n& tk
r   t
td|| Y nX d S )Nr   r   z=Skipping module named %s, no 'phone_home' configuration foundurlzFSkipping module named %s, no 'url' found in 'phone_home' configurationZpostalltries
   z?Configuration entry 'tries' is not an integer, using %s insteadT)r   )r   r   r   z/etc/ssh/ssh_host_dsa_key.pubz/etc/ssh/ssh_host_rsa_key.pubz/etc/ssh/ssh_host_ecdsa_key.pubz!/etc/ssh/ssh_host_ed25519_key.pub)r   r   r   r   z1%s: failed to open, can not phone home that data!z=Requested key %s from 'post' configuration list not availablezN/AZINSTANCE_IDr         )dataZretriesZsec_betweenZssl_detailsz0Failed to post phone home data to %s in %s tries)lenr   Z	read_confLOGdebugZwarninggetint
ValueError	TypeErrorZlogexcPOST_LIST_ALLZget_instance_idZget_hostnamer   itemsZ	load_file	Exceptionstrr   Zrender_stringr   Zread_file_or_urlZfetch_ssl_detailspaths)r   r   r   r   Zph_cfgr   Z	post_listr   Zall_keysZpubkeysnpathZsubmit_keyskZreal_submit_keysvZ
url_params r4   @/usr/lib/python3/dist-packages/cloudinit/config/cc_phone_home.pyhandleo   s    

  


 

   r6   )__doc__Zloggingtextwrapr   Z	cloudinitr   r   r   Zcloudinit.cloudr   Zcloudinit.configr   Zcloudinit.config.schemar   r	   Zcloudinit.distrosr
   Zcloudinit.settingsr   r   r+   ZMODULE_DESCRIPTIONr   __annotations__Z	getLogger__name__r%   r.   listr6   r4   r4   r4   r5   <module>	   sH   
"
