3 \me @sUdZddlZddlZddlZddlmZddlmZddlm Z m Z ddl m Z ddl mZddlmZmZdd lmZdd lmZejeZd Zd d deegeededggdZeeeZdddZdddZeee eddddZdS)z%Seed Random: Provide random seed dataN)BytesIO)dedent)subputil)Cloud)Config) MetaSchema get_meta_doc) ALL_DISTROS) PER_INSTANCEaPAll 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. Zcc_seed_randomz Seed RandomzProvide 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 )idnametitle descriptionZdistrosZ frequencyZexamplesZactivate_by_schema_keyscCsf|sdS| s|jd kr$tj|S|jd kr:tj|S|jd krVtj|dddStd |dS) Nrawbase64b64gzipgzF)quietdecodez Unknown random_seed encoding: %s)r)rr)rr)lowerr encode_textrZ b64decodeZ decomp_gzipIOError)dataencodingr$/usr/lib/python3.6/cc_seed_random.py_decodePs    rcCsr| r|rtdn|s&tjddS|d}tj|s^|rNtdj|dntjd|dStj||dddS) Nz"no command found but required=truezno command providedrz+command '{cmd}' not found but required=true)cmdz'command '%s' not found for seed_commandF)envZcapture) ValueErrorLOGdebugrZwhichformat)commandrequiredr!r rrrhandle_random_seed_command]s     r()r cfgcloudargsreturnc Cs|jdi}|jdd}|jdd}t}|rF|jt||jdd|jj}|rnd|krn|jtj|d|j}t |rt j d|t ||tj |||jd d} |jd d } y$t jj} || d <t| | | d Wn6tk r} zt jd| | | WYdd} ~ XnXdS)NZ random_seedfilez /dev/urandomrrr)rz0%s: adding %s bytes of random seed entropy to %sr&Zcommand_requiredFZRANDOM_SEED_FILE)r&r'r!z'handling random command [%s] failed: %s)getrwriterZ datasourcemetadatarrgetvaluelenr#r$Z append_fileosenvironcopyr(r"Zwarning) r r)r*r+ZmycfgZ seed_pathZ seed_dataZseed_bufr0r&Zreqr!errrhandleps4        r7)N)N) __doc__rZloggingr3iortextwraprZ cloudinitrrZcloudinit.cloudrZcloudinit.configrZcloudinit.config.schemarr Zcloudinit.distrosr Zcloudinit.settingsr Z getLogger__name__r#ZMODULE_DESCRIPTIONmetarr(strlistr7rrrr s8