
    f                     z    d Z ddlZddl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y)z0gpg.py - Collection of gpg key related functions    N)subp)gpgz--with-fingerprintz--no-default-keyringz--list-keysz	--keyringc                     	 t        j                   ddd| gd      \  }}|S # t         j                  $ r$}t        j                  d| |       d}Y d}~|S d}~ww xY w)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      sa    iiJ
C0$
 M	 %% 		:CGM	s   " AAAc                 J    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"    
 99e[)EBIII    c                     g }|j                  t               |s|j                  d       |j                  |        t        j                  |d      \  }}|rt        j                  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yyd3VV<hOMr   c                    t         j                  d| |       ddd|z  d| g}|g }d}d}t        |      }	 |d	z  }	 t        j                  |d
       t         j                  d| ||       y# t        j                  $ r}|}Y d}~nd}~ww xY w	 t        |      }t         j                  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-existent 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   T   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_keyr1   B   s    II6YG*.:M3
OCFE']F
!
	IIc4(IIA	 )) 	E		&\FIIH
 JJv 	%(+Y'FG 	+ s0   /A, ,B?BBAC 	C2C--C2c                     	 t        j                   dddd| gd       y# t         j                  $ r!}t        j                  d| |       Y d}~y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_keyr3   s   sO    >		Iw=t	
 %% >0#u==>s    AAAc                     t        |       }|s'	 t        | |       t        |       }	 t        |        |S |S # t        $ r t        j	                  d|         w xY w# t        |        w xY w)zget gpg keyid from keyserver)r+   zFailed to obtain gpg key %s)r   r1   r*   r
   	exceptionr3   )keyidr+   r   s      r   
getkeybyidr7   }   si    5!F	Ui0"5)F uM6M  	MM7?	
 us   7 !AA A()F))r#   r#   )zkeyserver.ubuntu.com)__doc__loggingr'   	cloudinitr   	getLogger__name__r
   r   r   r   r!   r1   r3   r7    r   r   <module>r>      sL    7   g!
J&.b>r   