
    ܍~cH                     |    d Z ddlZddlmZ ddlmZ  ej        e          Zg dZ	d Z
d Zdd	ZddZd ZddZdS )z0gpg.py - Collection of gpg key related functions    N)log)subp)gpgz--with-fingerprintz--no-default-keyringz--list-keysz	--keyringc                     	 t          j         ddd| gd          \  }}n:# t           j        $ r(}t                              d| |           d}Y d}~nd}~ww xY w|S )z*Export gpg key, armoured key gets returnedr   z--exportz--armourTcapture&Failed to export armoured key "%s": %sN)r   ProcessExecutionErrorLOGdebug)keyarmour_errors       //usr/lib/python3/dist-packages/cloudinit/gpg.pyexport_armourr      s    iJ
C0$
 
 
 %   		:CGGG Ms     AAAc                 >    t          j         ddg| d          j        S )z~Dearmor gpg key, dearmored key gets returned

    note: man gpg(1) makes no mention of an --armour spelling, only --armor
    r   z	--dearmorF)datadecode)r   stdout)r   s    r   dearmorr   '   s#    
 9e[)EBBBII    Fc                    g }|                     t                     |s|                    d           |                    |            t          j        |d          \  }}|rt                              d| |           |S )zList keys from a keyring with fingerprints. Default to a stable machine
    parseable format.

    @param key_file: a string containing a filepath to a key
    @param human_output: return output intended for human parsing
    z--with-colonsTr   r	   )extendGPG_LISTappendr   r   warning)key_filehuman_outputcmdr   stderrs        r   listr"   /   s     CJJx $

?###JJxyd333VV P<hOOOMr      r$   c                    t                               d| |           ddd|z  d| g}|g }d}d}t          |          }	 |d	z  }	 t          j        |d
           t                               d| ||           dS # t          j        $ r}|}Y d}~nd}~ww xY w	 t          |          }t                               d|j        |           t          j        |           n)# t          $ r}t          d| |||fz            |d}~ww xY w)a  Receive gpg key from the specified keyserver.

    Retries are done by default because keyservers can be unreliable.
    Additionally, there is no way to determine the difference between
    a non-existant key and a failure.  In both cases gpg (at least 2.2.4)
    exits with status 2 and stderr: "keyserver receive failed: No data"
    It is assumed that a key provided to cloud-init exists on the keyserver
    so re-trying makes better sense than failing.

    @param key: a string key fingerprint (as passed to gpg --recv-keys).
    @param keyserver: the keyserver to request keys from.
    @param retries: an iterable of sleep lengths for retries.
                    Use None to indicate no retries.z&Importing key '%s' from keyserver '%s'r   z--no-ttyz--keyserver=%sz--recv-keysNr   Tr$   r   z/Imported key '%s' from keyserver '%s' on try %dz6Import failed with exit code %d, will try again in %ssz@Failed to import key '%s' from keyserver '%s' after %d tries: %s)r   r   iterr   r
   next	exit_codetimesleepStopIteration
ValueError)	r   	keyserverretriesr    trynumr   sleepsenaplens	            r   recv_keyr3   B   sj    II6YGGG*.:M3
OCFE']]F!
	Ic4((((IIA	   F) 	 	 	EEEEEE		&\\FIIH  
 Jv 	 	 	%(+Y'FG  	+s1   3A: :B	BBAC 
D(C??Dc                     	 t          j         dddd| gd           dS # t           j        $ r'}t                              d| |           Y d}~dS d}~ww xY w)	z0Delete the specified key from the local gpg ringr   z--batchz--yesz--delete-keysTr   zFailed delete key "%s": %sN)r   r
   r   r   )r   r   s     r   
delete_keyr5   s   s    >	Iw=t	
 	
 	
 	
 	
 	
 % > > >0#u=========>s    AAAkeyserver.ubuntu.comc                 
   t          |           }|sq	 t          | |           t          |           }n*# t          $ r t                              d|             w xY w	 t          |            n# t          |            w xY w|S )zget gpg keyid from keyserver)r-   zFailed to obtain gpg key %s)r   r3   r,   r   	exceptionr5   )keyidr-   r   s      r   
getkeybyidr:   }   s    5!!F 		Ui0000"5))FF 	 	 	MM7???	  uJuMs    4 A/ 'AA/ /B )F)r#   )r6   )__doc__r)   	cloudinitr   loggingr   	getLogger__name__r   r   r   r   r"   r3   r5   r:    r   r   <module>rA      s    7 6  $ $ $ $ $ $      g!!  
 
 
J J J   &. . . .b> > >     r   