
    f              
          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d<    ee      Z  ej0                  e      Zd ZddZd Z	 ddZdede
dede ddf
dZ!y)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``.
cc_ssh_authkey_fingerprintszSSH AuthKey Fingerprintsz!Log fingerprints of user SSH keyszno_ssh_fingerprints: truezauthkey_hash: sha512)idnametitledescriptiondistros	frequencyexamplesactivate_by_schema_keysmetac                 n    g }t        dt        |       d      D ]  }|j                  | ||dz            |S )Nr      )rangelenappend)bin_hashsplit_upis      N/usr/lib/python3/dist-packages/cloudinit/config/cc_ssh_authkey_fingerprints.py_split_hashr    ,   s?    H1c(mQ' -QU+,-O    c                     | sy	 t        j                  |      }|j                  t        j                  |              dj                  t        |j                                     S # t        t        f$ r Y yw xY w)N :?)
hashlibnewupdatebase64	b64decodejoinr    	hexdigest	TypeError
ValueError)b64_text	hash_methhashers      r   _gen_fingerprintr2   3   sh    Y'f&&x01xxF$4$4$6788z"  	s   A A& &A87A8c                     t        | j                  | j                  | j                  | j                  g      rE| j                  r9| j                  j                         j                         t        j                  v ryy)NTF)	anykeytyper)   commentoptionslowerstripr   VALID_KEY_TYPES)entrys    r   _is_printable_keyr<   B   sT    
EMM5<<FGMM##%++-1I1IIr!   c                 d   |s!|d| d}t        j                  |dd       y dd|z  dd	g}t        |      }|D ]g  }t        |      s|j                  xs d
t        |j                  |      xs d
|j                  xs d
|j                  xs d
g}	|j                  |	       i |j                         }
|
j                         }t        t        |t                    }t        j                  d|d| d|      g}|j                  |       |D ]  }t        j                  ||ddd       ! y )Nz3no authorized SSH keys fingerprints found for user z.
TF)consolestderrKeytypezFingerprint (%s)OptionsComment-)keyzAuthorized keys from z
 for user +
)textr?   r>   )r   	multi_logr   r<   r5   r2   r)   r7   r6   add_row
get_string
splitlinesr   maxcenterextend)userkey_fnkey_entriesr0   prefixmessage
tbl_fieldstblr;   row	authtbl_sauthtbl_linesmax_lenlineslines                  r   _pprint_key_entriesr\   L   s7     t 	 	wU;i(	J j
!C U#$ y9@S$$	C KK  I((*M#m-.G5;TB	
E 
LL 
#T*5$	

r!   r   cfgcloudargsreturnc                    t        j                  |j                  dd            rt        j	                  d|        y t        j
                  |dd      }t        j                  ||j                        \  }}|j                         D ]d  \  }}|j                  d      s|j                  d      rt        j	                  d|       ?t        j                  |      \  }	}
t        ||	|
|       f y )	Nno_ssh_fingerprintsFz>Skipping module named %s, logging of SSH fingerprints disabledauthkey_hashsha256no_create_homesystemzXSkipping printing of ssh fingerprints for user '%s' because no home directory is created)r   is_truegetLOGdebugget_cfg_option_strr
   normalize_users_groupsdistroitemsr   extract_authorized_keysr\   )r   r]   r^   r_   r0   users_groups	user_name_cfgrP   rQ   s              r   handlert   w   s    ||CGG159:		L	
 	''^XFI55c5<<HUG"[[] 
GD88$%();II/
  ( @ @ KIv{IF
Gr!   )rd   )rd   z	ci-info: )"__doc__r)   r&   logging	cloudinitr   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   r   cloudinit.distrosr	   r
   cloudinit.settingsr   cloudinit.simpletabler   MODULE_DESCRIPTIONr   __annotations__	getLogger__name__ri   r    r2   r<   r\   strlistrt    r!   r   <module>r      s    B    $ ! # < 2 + -  (&0%}#  "j  t
g! ;F(
VG G6 G% Gt G Gr!   