3 \me @sUdZddlZddlZddlZddlmZmZddlmZddl m Z ddl m Z m Z ddlmZmZddlmZdd lmZd Zd d d eegeddggdZe e eZejeZddZdddZddZdddZee eeddddZ dS) z;SSH AuthKey Fingerprints: Log fingerprints of user SSH keysN)ssh_utilutil)Cloud)Config) MetaSchema get_meta_doc) ALL_DISTROSug_util) PER_INSTANCE) SimpleTablezWrite fingerprints of authorized keys for each user to log. This is enabled by default, but can be disabled using ``no_ssh_fingerprints``. The hash type for the keys can be specified, but defaults to ``sha256``. Zcc_ssh_authkey_fingerprintszSSH AuthKey Fingerprintsz!Log fingerprints of user SSH keyszno_ssh_fingerprints: truezauthkey_hash: sha512)idnametitle descriptionZdistrosZ frequencyZexamplesZactivate_by_schema_keyscCs8g}x.tdt|dD]}|j|||dqW|S)Nr)rangelenappend)Zbin_hashZsplit_upir1/usr/lib/python3.6/cc_ssh_authkey_fingerprints.py _split_hash,srsha256c CsR|sdSy,tj|}|jtj|djt|jStt fk rLdSXdS)N:?) hashlibnewupdatebase64Z b64decodejoinrZ hexdigest TypeError ValueError)Zb64_text hash_methZhasherrrr_gen_fingerprint3s r$cCs:t|j|j|j|jgr6|jr6|jjjtjkr6dSdS)NTF) anykeytypercommentoptionslowerstriprZVALID_KEY_TYPES)entryrrr_is_printable_keyBs r, ci-info: cCs|s$d||f}tj|ddddSdd|ddg}t|}xH|D]@}t|rB|jpVd t|j|pdd |jpld |jptd g} |j | qBW|j } | j } t t | t d } tjd ||fd | g} | j| x$| D]}tjd ||fdddqWdS)Nz9%sno authorized SSH keys fingerprints found for user %s. TF)consolestderrZKeytypezFingerprint (%s)ZOptionsComment-)keyz#Authorized keys from %s for user %s+z%s%s )textr/r.)rZ multi_logr r,r&r$rr(r'Zadd_rowZ get_string splitlinesrmaxcenterextend)userkey_fn key_entriesr#prefixmessageZ tbl_fieldsZtblr+rowZ authtbl_sZ authtbl_linesZmax_lenlineslinerrr_pprint_key_entriesLs8      rA)r cfgcloudargsreturnc Cstj|jddr"tjd|dStj|dd}tj||j\}}xR|j D]F\}}|jdsh|jdrvtjd|qLt j |\} } t || | |qLWdS) NZno_ssh_fingerprintsFz>Skipping module named %s, logging of SSH fingerprints disabledZ authkey_hashrZno_create_homesystemzXSkipping printing of ssh fingerprints for user '%s' because no home directory is created) rZis_truegetLOGdebugZget_cfg_option_strr Znormalize_users_groupsZdistroitemsrZextract_authorized_keysrA) r rBrCrDr#ZusersZ_groupsZ user_nameZ_cfgr:r;rrrhandlewsrK)r)rr-)!__doc__rrZloggingZ cloudinitrrZcloudinit.cloudrZcloudinit.configrZcloudinit.config.schemarrZcloudinit.distrosrr Zcloudinit.settingsr Zcloudinit.simpletabler ZMODULE_DESCRIPTIONmetaZ getLogger__name__rHrr$r,rAstrlistrKrrrrs6        *