o
    th                  
   @   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debianzraspberry-pi-os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|
sqBz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 )zSearch 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&   
??