CasperSecurity

Current Path : /lib/python3/dist-packages/cloudinit/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/cloudinit/__pycache__/gpg.cpython-310.pyc

o

쑛h��@stdZddlZddlZddlZddlZddlZddlmZddlm	Z	m
Z
ddlmZe�
e�ZdZGdd�d�ZdS)	z0gpg.py - Collection of gpg key related functions�N)�TemporaryDirectory)�Dict�Optional)�subp�	GNUPGHOMEc@s�eZdZdd�Zdd�Zedeeeffdd��Zdd	�Z	d"dd�Z
d
edeefdd�Zd
edefdd�Z
d#dedefdd�Zd$d
ededd
fdd�Zd
edd
fdd�Z	d%dededeefdd�Zd"d d!�Zd
S)&�GPGcCsd|_i|_t�|_dS)NF)�gpg_started�_envr�temp_dir��self�r
�//usr/lib/python3/dist-packages/cloudinit/gpg.py�__init__szGPG.__init__cCs|S�Nr
rr
r
r�	__enter__sz
GPG.__enter__�returncCs&|jr|jSd|_t|jji|_|jS)awhen 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
�namerr
r
r�env"s
	zGPG.envcCs|��dSr)�cleanup)r�exc_typ�	exc_value�	tracebackr
r
r�__exit__1szGPG.__exit__NcCs4|��|jrtj�|jj�r|j��dSdSdS)z0cleanup the gpg temporary directory and kill gpgN)�kill_gpgr
�os�path�isdirrrrr
r
rr4s�zGPG.cleanup�keyc
CsVztjddd|gd|jd�jWStjy*}zt�d||�WYd}~dSd}~ww)z*Export gpg key, armoured key gets returned�gpgz--exportz--armourT��capture�
update_env�&Failed to export armoured key "%s": %sN)rr�stdout�ProcessExecutionError�LOG�debug�rr�errorr
r
r�
export_armour:s
����zGPG.export_armourcCstjddg|d|jd�jS)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�decoder#)rrr%)rrr
r
r�dearmorGs
��zGPG.dearmorF�key_filecCsLgd�}|s|�d�|�|�tj||jdd�\}}|r$t�d||�|S)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)r#r"r$)�appendrrr'�warning)rr/�human_output�cmdr%�stderrr
r
r�	list_keysPs

�z
GPG.list_keys��r7�	keyserverc	
Cs�t�d||�d}d}t|pg�}	|d7}ztjddd|d	|gd|jd
�t�d|||�WdStjyF}z|}WYd}~nd}~wwzt|�}t�d|j|�t�	|�Wnt
yq}ztd
||||f�|�d}~wwq)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'rNTr7r z--no-ttyz--keyserver=%sz--recv-keysr!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(�iterrrr&�next�	exit_code�time�sleep�
StopIteration�
ValueError)	rrr8�retries�trynumr*�sleeps�e�naplenr
r
r�recv_keyjs^������
������zGPG.recv_keyc
CsZztjdddd|gd|jd�WdStjy,}zt�d||�WYd}~dSd}~ww)	z0Delete the specified key from the local gpg ringr z--batchz--yesz
--delete-keysTr!zFailed delete key "%s": %sN)rrr&r'r1r)r
r
r�
delete_key�s���zGPG.delete_key�keyserver.ubuntu.com�keyidc	Csj|�|�}|s3z%z|j||d�|�|�}Wnty$t�d|��wW|�|�|S|�|�w|S)zget gpg keyid from keyserver)r8zFailed to obtain gpg key %s)r+rEr?r'�	exceptionrF)rrHr8�armourr
r
r�
getkeybyid�s
��
�zGPG.getkeybyidc
Cs�zK|jsWdSt�d�rtjgd�d|jd�j}WdStjgd�dddgd	�j}t�d
|�}dd�|D�}|r=t�d
|�|D]	}t	�
|tj�q?WdStj
ye}z
t�d|�WYd}~dSd}~ww)akilling 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)rLz--kill�allTr!)	�psz-ozppid,pid�-C�keyboxdrO�dirmngrrOz	gpg-agentrr7)r"�rcsz(?P<ppid>\d+)\s+(?P<pid>\d+)cSs$g|]}|ddkrt|d��qS)r�1r7)�int)�.0�pidr
r
r�
<listcomp>�sz GPG.kill_gpg.<locals>.<listcomp>z&Killing gpg-agent and dirmngr pids: %sz"Failed to clean up gpg process: %s)rr�whichrr%�re�findallr'r(r�kill�signal�SIGKILLr&r1)r�gpg_process_out�gpg_pids�
root_gpg_pids�gpg_pidrCr
r
rr�sF
����������zGPG.kill_gpg)rN)F)r6)rG)�__name__�
__module__�__qualname__rr�propertyr�strrrrrr+r.r5rErFrKrr
r
r
rrs*

	7���
�r)�__doc__�loggingrrYr\r<�tempfiler�typingrr�	cloudinitr�	getLoggerrbr'rrr
r
r
r�<module>s

Hacker Blog, Shell İndir, Sql İnjection, XSS Attacks, LFI Attacks, Social Hacking, Exploit Bot, Proxy Tools, Web Shell, PHP Shell, Alfa Shell İndir, Hacking Training Set, DDoS Script, Denial Of Service, Botnet, RFI Attacks, Encryption
Telegram @BIBIL_0DAY