ó
    x[hú  ã                   ó”   • S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSKJ	r	J
r
  SSKJr  \R                  " \5      rSr " S S5      rg)	z0gpg.py - Collection of gpg key related functionsé    N)ÚTemporaryDirectory)ÚDictÚOptional)ÚsubpÚ	GNUPGHOMEc                   óê   • \ rS rSrS rS r\S\\\4   4S j5       r	S r
SS jrS	\S\\   4S
 jrS	\S\4S jrSS\S\4S jjrSS	\S\SS4S jjrS	\SS4S jr SS\S\S\\   4S jjrSS jrSrg)ÚGPGé   c                 ó>   • SU l         0 U l        [        5       U l        g )NF)Úgpg_startedÚ_envr   Útemp_dir©Úselfs    Ú//usr/lib/python3/dist-packages/cloudinit/gpg.pyÚ__init__ÚGPG.__init__   s   € Ø ˆÔØˆŒ	Ü*Ó,ˆ�ó    c                 ó   • U $ ©N© r   s    r   Ú	__enter__ÚGPG.__enter__   s   € Øˆr   Úreturnc                 ó¤   • U R                   (       a  U R                   $ SU l        [        U R                  R                  0U l         U R                   $ )zòwhen this env property gets invoked, set up our temporary
directory, and also set gpg_started to tell the cleanup()
method whether or not

why put this here and not in __init__? pytest seems unhappy
and it's not obvious how to work around it
T)r   r   ÚHOMEr   Únamer   s    r   ÚenvÚGPG.env"   s=   € ð �9�9Ø—9‘9ÐØˆÔÜ˜4Ÿ=™=×-Ñ-Ð.ˆŒ	Ø�y‰yÐr   c                 ó$   • U R                  5         g r   )Úcleanup)r   Úexc_typÚ	exc_valueÚ	tracebacks       r   Ú__exit__ÚGPG.__exit__1   s   € Ø�‰�r   Nc                 óî   • U R                  5         U R                  (       aT  [        R                  R	                  U R                  R
                  5      (       a  U R                  R                  5         ggg)z0cleanup the gpg temporary directory and kill gpgN)Úkill_gpgr   ÚosÚpathÚisdirr   r!   r   s    r   r!   ÚGPG.cleanup4   sE   € à�‰ŒØ�=�=œRŸW™WŸ]™]¨4¯=©=×+=Ñ+=×>Ñ>Ø�M‰M×!Ñ!Õ#ð ?ˆ=r   Úkeyc                 óÎ   •  [         R                   " SSSU/SU R                  S9R                  $ ! [         R                   a   n[        R                  SX5         SnAgSnAff = f)z*Export gpg key, armoured key gets returnedÚgpgz--exportz--armourT©ÚcaptureÚ
update_envú&Failed to export armoured key "%s": %sN)r   r   ÚstdoutÚProcessExecutionErrorÚLOGÚdebug©r   r-   Úerrors      r   Úexport_armourÚGPG.export_armour:   se   € ð	LÜ—9’9Ø˜
 J°Ð4ØØŸ8™8ñ÷ ‰fð	øô
 ×)Ñ)ó 	Lä�I‰IÐ>À×KÐKØûð	Lús   ‚-0 °A$ÁAÁA$c                 ó\   • [         R                   " SS/USU R                  S9R                  $ )zvDearmor gpg key, dearmored key gets returned

note: man gpg(1) makes no mention of an --armour spelling, only --armor
r/   z	--dearmorF)ÚdataÚdecoder2   )r   r   r4   )r   r-   s     r   ÚdearmorÚGPG.dearmorG   s,   € ô
 �yŠyØ�KÐ  s°5ÀTÇXÁXñ
ç
‰&ð	r   Úkey_filec                 óÜ   • / SQnU(       d  UR                  S5        UR                  U5        [        R                  " X0R                  SS9u  pEU(       a  [        R	                  SX5        U$ )zÒList 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
)r/   z--no-optionsz--with-fingerprintz--no-default-keyringz--list-keysz	--keyringz--with-colonsT)r2   r1   r3   )Úappendr   r   r6   Úwarning)r   rA   Úhuman_outputÚcmdr4   Ústderrs         r   Ú	list_keysÚGPG.list_keysP   sW   € ò
ˆö Ø�J‰J�Ô'à�
‰
�8ÔÜŸš 3·8±8ÀTÑJ‰ˆÞÜ�K‰KØ8¸(ôð ˆr   Ú	keyserverc                 ó  • [         R                  SX5        SnSn[        U=(       d    / 5      n US-  n [        R                  " SSSU-  S	U/SU R                  S
9  [         R                  SUUU5        g! [        R
                   a  nUn SnAOSnAff = f [        U5      n[         R                  SUR                  U5        [        R                  " U5        O#! [         a  n[        SXXE4-  5      UeSnAff = fMÓ  )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   NTé   r/   z--no-ttyz--keyserver=%sz--recv-keysr0   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)r6   r7   Úiterr   r   r5   ÚnextÚ	exit_codeÚtimeÚsleepÚStopIterationÚ
ValueError)	r   r-   rJ   ÚretriesÚtrynumr9   ÚsleepsÚeÚnaplens	            r   Úrecv_keyÚGPG.recv_keyj   s  € ô 	�	‰	Ð:¸CÔKØˆØˆÜ�g—m Ó$ˆØØ�a‰KˆFðÜ—	’	àØ"Ø(¨9Ñ4Ø%Øðð !Ø#Ÿx™xò
ô —	‘	ØEØØØô	ð øÜ×-Ñ-ó Ø•ûðúðÜ˜f›�Ü—	‘	ØLØ—O‘OØôô
 —
’
˜6Õ"øÜ ó Ü ð)Ø,/¸FÐ+JñKóð ðûðúñ? s1   ¶A A7 Á7BÂBÂBÂAC Ã
C>Ã(C9Ã9C>c                 ó¾   •  [         R                   " SSSSU/SU R                  S9  g! [         R                   a   n[        R	                  SX5         SnAgSnAff = f)	z0Delete the specified key from the local gpg ringr/   z--batchz--yesz--delete-keysTr0   zFailed delete key "%s": %sN)r   r   r5   r6   rD   r8   s      r   Ú
delete_keyÚGPG.delete_key¡   sX   € ð	BÜ�IŠIØ˜	 7¨O¸SÐAØØŸ8™8óøô
 ×)Ñ)ó 	BÜ�K‰KÐ4°c×AÑAûð	Bús   ‚%( ¨A¼AÁAÚkeyidc                 ó  • U R                  U5      nU(       d5   U R                  XS9  U R                  U5      n U R                  U5        U$ U$ ! [         a    [        R	                  SU5        e f = f! U R                  U5        f = f)zget gpg keyid from keyserver)rJ   zFailed to obtain gpg key %s)r:   rY   rS   r6   Ú	exceptionr\   )r   r^   rJ   Úarmours       r   Ú
getkeybyidÚGPG.getkeybyid¬   sƒ   € ð ×#Ñ# EÓ*ˆÞð	'Ø—‘˜e�Ñ9Ø×+Ñ+¨EÓ2‘ð —‘ Ô&Øˆˆvˆøô ó Ü—‘Ð;¸UÔCØðûð —‘ Õ&ús   š A Á"A2Á2A5 Á5Bc                 óŽ  •  U R                   (       d  g[        R                  " S5      (       a-  [        R                  " / SQSU R                  S9R                  ng[        R                  " / SQSSS/S	9R                  n[
        R                  " S
U5      nU Vs/ s H  o3S   S:X  d  M  [        US   5      PM     nnU(       a  [        R                  SU5        U H(  n[        R                  " U[        R                  5        M*     gs  snf ! [        R                   a   n[        R                  SU5         SnAgSnAff = f)zçkilling with gpgconf is best practice, but when it isn't available
failover is possible

GH: 4344 - stop gpg-agent/dirmgr daemons spawned by gpg
key imports. Daemons spawned by cloud-config.service on systemd
v253 report (running)
NÚgpgconf)re   z--killÚallTr0   )	Úpsz-ozppid,pidú-CÚkeyboxdrh   Údirmngrrh   z	gpg-agentr   rL   )r1   Úrcsz(?P<ppid>\d+)\s+(?P<pid>\d+)Ú1z&Killing gpg-agent and dirmngr pids: %sz"Failed to clean up gpg process: %s)r   r   Úwhichr   r4   ÚreÚfindallÚintr6   r7   r)   ÚkillÚsignalÚSIGKILLr5   rD   )r   Úgpg_process_outÚgpg_pidsÚpidÚroot_gpg_pidsÚgpg_pidrW   s          r   r(   ÚGPG.kill_gpg¾   s  € ð&	AØ×#×#ØÜ�zŠz˜)×$Ñ$Ü"&§)¢)Ú0Ø Ø#Ÿx™xñ#÷ ‘&ñ	  ô #'§)¢)ò
ð !Ø˜A˜ñ#÷ ‘&ð  ô Ÿ:š:Ø3°_ó�ñ ,4ó!Ú+3 C¸1±vÀ±}“K”C˜˜A™–K©8ð ð !ö !Ü—I‘IØ@À-ôó  -�GÜ—G’G˜G¤V§^¡^Ö4ò  -ùò!øô ×)Ñ)ó 	AÜ�K‰KÐ<¸a×@Ñ@ûð	Aús<   ‚D ”AD Á?D ÂDÂ+DÂ=AD ÄD ÄEÄ$D?Ä?E)r   r   r   )r   N)F))rL   rL   )zkeyserver.ubuntu.com)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r   r   Úpropertyr   Ústrr   r%   r!   r   r:   r?   rH   rY   r\   rb   r(   Ú__static_attributes__r   r   r   r	   r	      sÔ   † ò-ò
ð ð�T˜#˜s˜(‘^ó ó ðòô$ð ð ¨°#©ô ð˜3ð  3ô ñ #ð ¸cõ ñ45˜Cð 5¨Cð 5ÀDõ 5ðn	B˜cð 	B dô 	Bð ,BñØðØ%(ðà	�#‰õ÷$.Ar   r	   )Ú__doc__Úloggingr)   rn   rr   rP   Útempfiler   Útypingr   r   Ú	cloudinitr   Ú	getLoggerrz   r6   r   r	   r   r   r   Ú<module>r‡      sE   ðñ 7ã Û 	Û 	Û Û Ý 'ß !å à×Ò˜Ó!€à€÷SAò SAr   