
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\mex              	   @   s   U d Z ddlZddlZddlm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 dd	lmZ d
dddgZdZdZdddeeeedgg dZeeeZ ejeZeee	eddddZdd ZeeedddZ dS )zSSH Import ID: Import SSH id    N)dedent)subputil)Cloud)Config)
MetaSchemaget_meta_doc)ug_util)PER_INSTANCEZalpineZcosZdebianZubuntuzssh-import-ida2  This module imports SSH keys from either a public keyserver, usually launchpad
or github using ``ssh-import-id``. Keys are referenced by the username they are
associated with on the keyserver. The keyserver can be specified by prepending
either ``lp:`` for launchpad or ``gh:`` for github to the username.
Zcc_ssh_import_idzSSH Import IDzImport SSH idzi            ssh_import_id:
             - user
             - gh:user
             - lp:user
            )idnametitledescriptiondistrosZ	frequencyZexamplesZactivate_by_schema_keys)r   cfgcloudargsreturnc          "   C   s  t |dstjd d S tjts0tjd d S t|dkrn|d }g }t|dkr`|dd  }t|| d S t	j
||j\}}g }x|j D ]\}}	g }
|	d rtj|dg }
n0y|	d }
W n" tk
r   tjd| wY nX ytj|
}
dd	 |
D }
W n$ tk
r"   tjd
| wY nX t|
s0qyt|
| W q tk
r| } z tjtd||
 |j| W Y d d }~X qX qW t|r|d d S )NZ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   &/usr/lib/python3.6/cc_ssh_import_id.py
<listcomp>a   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_BINARYZwarninglenimport_ssh_idsr	   Znormalize_users_groupsZdistroitemsr   Zget_cfg_option_list	ExceptionZ
uniq_mergelogexcappend)r   r   r   r   useridsZusersZ_groupsZelistZuser_cfgZ
import_idsexcr   r   r   handle8   sV    




 
r*   c             C   s   |o| st jd||  d S ytj| W n& tk
rN } z
|W Y d d }~X nX tjdrnddd|tg|  }n*tjdrdd|tg|  }nt jd d S t jd| ytj|d	d
 W n6 tj	k
r } zt
jt d| |W Y d d }~X nX d S )Nz(empty user(%s) or ids(%s). not importingZsudoz--preserve-env=https_proxyz-HuZdoasz-uz:Neither sudo nor doas available! Unable to import SSH ids.zImporting SSH ids for user %s.F)Zcapturez*Failed to run command to import %s SSH ids)r   r   pwdgetpwnamKeyErrorr   r   r    errorZProcessExecutionErrorr   r%   )r(   r'   r)   cmdr   r   r   r"   w   s8    


r"   )config
search_keyr   c             C   sz   xt| j  D ]h}||krdS t| | tr:t| | |r:dS t| | tr
x(| | D ]}t|trRt||rRdS qRW q
W 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)r0   r1   Z
config_keyitemr   r   r   r      s    

r   )!__doc__Zloggingr+   textwrapr   Z	cloudinitr   r   Zcloudinit.cloudr   Zcloudinit.configr   Zcloudinit.config.schemar   r   Zcloudinit.distrosr	   Zcloudinit.settingsr
   r   r    ZMODULE_DESCRIPTIONmetaZ	getLogger__name__r   r   r5   r*   r"   r4   boolr   r   r   r   r   <module>   s4   
??