o
    x[h°  ã                
   @   sÊ   U d 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 ddlmZ ddlmZ d	Zd
g d¢eg dœZeed< e e¡Zdede	dededdf
dd„Zdd„ Zdededefdd„ZdS )zSSH Import ID: Import SSH idé    N)ÚsubpÚutil)ÚCloud)ÚConfig)Ú
MetaSchema)Úug_util)ÚPER_INSTANCEzssh-import-idÚcc_ssh_import_id)ÚalpineÚcosÚdebianÚubuntu)ÚidÚdistrosÚ	frequencyÚactivate_by_schema_keysÚmetaÚnameÚcfgÚcloudÚargsÚreturnc                 C   s|  t |dƒst d¡ d S t t¡st d¡ d S |r3|d }g }t|ƒdkr,|dd … }t||ƒ d S t	 
||j¡\}}g }| ¡ D ]q\}}	g }
|	d rTt |dg ¡}
nz|	d }
W n tyi   t d|¡ Y qBw zt |
¡}
dd	„ |
D ƒ}
W n ty‡   t d
|¡ Y qBw t|
ƒs�qBzt|
|ƒ W qB ty³ } zt td||
¡ | |¡ W Y d }~qBd }~ww t|ƒr¼|d ‚d S )NÚssh_import_idzISkipping module named ssh_import_id, no 'ssh_import_id' directives found.zXssh-import-id is not installed, but module ssh_import_id is configured. Skipping module.r   é   Údefaultz+User %s is not configured for ssh_import_idc                 S   s   g | ]}t |ƒ‘qS © )Ústr)Ú.0Úir   r   úC/usr/lib/python3/dist-packages/cloudinit/config/cc_ssh_import_id.pyÚ
<listcomp>K   s    zhandle.<locals>.<listcomp>z5User %s is not correctly configured for ssh_import_idzssh-import-id failed for: %s %s)Úis_key_in_nested_dictÚLOGÚdebugr   ÚwhichÚSSH_IMPORT_ID_BINARYÚwarningÚlenÚimport_ssh_idsr   Únormalize_users_groupsÚdistroÚitemsr   Úget_cfg_option_listÚ	ExceptionÚ
uniq_mergeÚlogexcÚappend)r   r   r   r   ÚuserÚidsÚusersÚ_groupsÚelistÚuser_cfgÚ
import_idsÚexcr   r   r   Úhandle"   sh   
ÿ
ÿ
þ
ÿüÿ€üÿr9   c              
   C   sÜ   |r| st  d|| ¡ d S zt |¡ W n ty" } z|‚d }~ww t d¡r2ddd|tg|  }nt d¡r@dd|tg|  }nt  d¡ d S t  d|¡ z
tj|d	d
� W d S  tj	ym } z	t
 t d|¡ |‚d }~ww )Nz(empty user(%s) or ids(%s). not importingÚsudoz--preserve-env=https_proxyz-HuÚdoasz-uz:Neither sudo nor doas available! Unable to import SSH ids.zImporting SSH ids for user %s.F)Úcapturez*Failed to run command to import %s SSH ids)r"   r#   ÚpwdÚgetpwnamÚKeyErrorr   r$   r%   ÚerrorÚProcessExecutionErrorr   r/   )r2   r1   r8   Úcmdr   r   r   r(   a   sH   €ÿ
ûú
üû
€þr(   ÚconfigÚ
search_keyc                 C   sz   |   ¡ D ]6}||kr dS t| | tƒrt| | |ƒr dS t| | tƒr:| | D ]}t|tƒr9t||ƒr9  dS q)qdS )z•Search for key nested in config.

    Note: A dict embedded in a list of lists will not be found walked - but in
    this case we don't need it.
    TF)ÚkeysÚ
isinstanceÚdictr!   Úlist)rC   rD   Ú
config_keyÚitemr   r   r   r!       s   

€r!   )Ú__doc__Úloggingr=   Ú	cloudinitr   r   Úcloudinit.cloudr   Úcloudinit.configr   Úcloudinit.config.schemar   Úcloudinit.distrosr   Úcloudinit.settingsr   r%   r   Ú__annotations__Ú	getLoggerÚ__name__r"   r   rH   r9   r(   rG   Úboolr!   r   r   r   r   Ú<module>   s&   ü
??