
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\me              	   @   s   U d Z ddlZddlZddl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mZ ddlmZ dd	lmZ d
Zdddeegeddgg dZeeeZ ejeZdd ZdddZdd ZdddZee
eeddddZ dS ) z;SSH AuthKey Fingerprints: Log fingerprints of user SSH keys    N)ssh_utilutil)Cloud)Config)
MetaSchemaget_meta_doc)ALL_DISTROSug_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``.
Zcc_ssh_authkey_fingerprintszSSH AuthKey Fingerprintsz!Log fingerprints of user SSH keyszno_ssh_fingerprints: truezauthkey_hash: sha512)idnametitledescriptionZdistrosZ	frequencyZexamplesZactivate_by_schema_keysc             C   s8   g }x.t dt| dD ]}|j| ||d   qW |S )Nr      )rangelenappend)Zbin_hashZsplit_upi r   1/usr/lib/python3.6/cc_ssh_authkey_fingerprints.py_split_hash,   s    r   sha256c             C   sR   | sdS y,t j|}|jtj|  djt|j S  tt	fk
rL   dS X d S )N :?)
hashlibnewupdatebase64Z	b64decodejoinr   Z	hexdigest	TypeError
ValueError)Zb64_text	hash_methZhasherr   r   r   _gen_fingerprint3   s    
r$   c             C   s:   t | j| j| j| jgr6| jr6| jj j tjkr6dS dS )NTF)	anykeytyper   commentoptionslowerstripr   ZVALID_KEY_TYPES)entryr   r   r   _is_printable_keyB   s
    r,   	ci-info: c             C   s   |s$d|| f }t j|ddd d S dd| ddg}t|}xH|D ]@}t|rB|jpVd	t|j|pdd	|jpld	|jptd	g}	|j	|	 qBW |j
 }
|
j }tt|td
}t jd|| f d|g}|j| x$|D ]}t jd||f ddd qW d S )Nz9%sno authorized SSH keys fingerprints found for user %s.
TF)consolestderrZKeytypezFingerprint (%s)ZOptionsComment-)keyz#Authorized keys from %s for user %s+z%s%s
)textr/   r.   )r   Z	multi_logr   r,   r&   r$   r   r(   r'   Zadd_rowZ
get_string
splitlinesr   maxcenterextend)userkey_fnkey_entriesr#   prefixmessageZ
tbl_fieldsZtblr+   rowZ	authtbl_sZauthtbl_linesZmax_lenlinesliner   r   r   _pprint_key_entriesL   s8    




rA   )r   cfgcloudargsreturnc             C   s   t j|jddr"tjd|  d S t j|dd}tj||j\}}xR|j	 D ]F\}}|jdsh|jdrvtjd| qLt
j|\}	}
t||	|
| qLW d S )	NZno_ssh_fingerprintsFz>Skipping module named %s, logging of SSH fingerprints disabledZauthkey_hashr   Zno_create_homesystemzXSkipping printing of ssh fingerprints for user '%s' because no home directory is created)r   Zis_truegetLOGdebugZget_cfg_option_strr	   Znormalize_users_groupsZdistroitemsr   Zextract_authorized_keysrA   )r   rB   rC   rD   r#   ZusersZ_groupsZ	user_nameZ_cfgr:   r;   r   r   r   handlew   s    rK   )r   )r   r-   )!__doc__r   r   ZloggingZ	cloudinitr   r   Zcloudinit.cloudr   Zcloudinit.configr   Zcloudinit.config.schemar   r   Zcloudinit.distrosr   r	   Zcloudinit.settingsr
   Zcloudinit.simpletabler   ZMODULE_DESCRIPTIONmetaZ	getLogger__name__rH   r   r$   r,   rA   strlistrK   r   r   r   r   <module>   s6   


*