o
    w7e                  
   @   s  U d 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
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	eZdZdddeegeededgg dZeed< eeZ dddZdddZdededede ddf
ddZ!dS ) z%Seed Random: Provide random seed data    N)BytesIO)dedent)log)subputil)Cloud)Config)
MetaSchemaget_meta_doc)ALL_DISTROS)PER_INSTANCEaP  All cloud instances started from the same image will produce very similar
data when they are first booted as they are all starting with the same seed
for the kernel's entropy keyring. To avoid this, random seed data can be
provided to the instance either as a string or by specifying a command to run
to generate the data.

Configuration for this module is under the ``random_seed`` config key. If
the cloud provides its own random seed data, it will be appended to ``data``
before it is written to ``file``.

If the ``command`` key is specified, the given command will be executed.  This
will happen after ``file`` has been populated.  That command's environment will
contain the value of the ``file`` key as ``RANDOM_SEED_FILE``. If a command is
specified that cannot be run, no error will be reported unless
``command_required`` is set to true.
cc_seed_randomzSeed RandomzProvide random seed dataz            random_seed:
              file: /dev/urandom
              data: my random string
              encoding: raw
              command: ['sh', '-c', 'dd if=/dev/urandom of=$RANDOM_SEED_FILE']
              command_required: true
            a_              # To use 'pollinate' to gather data from a remote entropy
            # server and write it to '/dev/urandom', the following
            # could be used:
            random_seed:
              file: /dev/urandom
              command: ["pollinate", "--server=http://local.polinate.server"]
              command_required: true
            )idnametitledescriptiondistros	frequencyexamplesactivate_by_schema_keysmetac                 C   s`   | sdS |r|  dv rt| S |  dv rt| S |  dv r*tj| dd dS td| )N    )raw)base64b64)gzipgzF)quietdecodez Unknown random_seed encoding: %s)lowerr   encode_textr   	b64decodedecomp_gzipIOError)dataencoding r&   A/usr/lib/python3/dist-packages/cloudinit/config/cc_seed_random.py_decodeP   s   

r(   c                 C   sl   | s|rt d| std d S | d }t|s,|r$t dj|dtd| d S tj| |dd d S )	Nz"no command found but required=truezno command providedr   z+command '{cmd}' not found but required=true)cmdz'command '%s' not found for seed_commandF)envcapture)
ValueErrorLOGdebugr   whichformat)commandrequiredr*   r)   r&   r&   r'   handle_random_seed_command]   s   


r3   r   cfgcloudargsreturnc              
   C   s  | di }| dd}| dd}t }|r#|t|| dd |jj}|r7d|v r7|t|d  | }t	|rOt
d| t	|| t|| | d	d }	| d
d}
ztj }||d< t|	|
|d W d S  ty } z	t
d|	| |d }~ww )Nrandom_seedfilez/dev/urandomr$   r   r%   )r%   z0%s: adding %s bytes of random seed entropy to %sr1   command_requiredFRANDOM_SEED_FILE)r1   r2   r*   z'handling random command [%s] failed: %s)getr   writer(   
datasourcemetadatar   r    getvaluelenr-   r.   append_fileosenvironcopyr3   r,   warning)r   r4   r5   r6   mycfg	seed_path	seed_dataseed_bufr?   r1   reqr*   er&   r&   r'   handlep   s:   
rM   )N)"__doc__r   rC   ior   textwrapr   	cloudinitr   loggingr   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar	   r
   cloudinit.distrosr   cloudinit.settingsr   	getLogger__name__r-   MODULE_DESCRIPTIONr   __annotations__r(   r3   strlistrM   r&   r&   r&   r'   <module>   sB   

!

"