Sindbad~EG File Manager

Current Path : /lib/python3.6/site-packages/sos/report/plugins/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/sos/report/plugins/__pycache__/gcp.cpython-36.opt-1.pyc

3

�f��@sbddlZddlmZddlmZddlmZddlmZddl	m
Z
mZmZGdd�de
e�Z
dS)	�N)�HTTPResponse)�Any)�request)�URLError)�Plugin�IndependentPlugin�	PluginOptc@s�eZdZdZdZdZedddd�gZdZd	Z	d
Z
dZdZd
d�Z
dd�Zdd�Zed�dd�Zeeed�dd��Zdd�Zeeed�dd��ZdS)�GCPzGoogle Cloud Platform�gcp�virtzkeep-piiFzyStop the plugin from removing PIIs like project name or organization ID from the metadata retrieved from Metadata server.)�defaultZdescz3http://metadata.google.internal/computeMetadata/v1/zBhttp://metadata.google.internal/computeMetadata/v1/?recursive=truez[--REDACTED--]NzDDMI: Google Google Compute Engine/Google Compute Engine, BIOS GooglecCs(|jd�}|ddkrdS|j|dkS)z�
        Checks if this plugin should be executed at all. In this case, it
        will check the `dmesg` command output to see if the system is
        running on a Google Cloud Compute instance.
        �dmesgZstatusrF�output)Zexec_cmd�
GOOGLE_DMI)�selfr
�r�/usr/lib/python3.6/gcp.py�
check_enabled)s
zGCP.check_enabledcCs$|jddgd�|jddgd�dS)z�
        Collect the following info:
         * Metadata from the Metadata server
         * `gcloud auth list` output
         * Any google services output from journal
        zgcloud auth listr
)�tagszgoogle*)ZunitsrN)Zadd_cmd_outputZadd_journal)rrrr�setup4s	z	GCP.setupcCs~|jddgd��d}y,|j�|_|j�|jtj|jdd��Wn0tk
rn}z|jt|��WYdd}~XnXWdQRXdS)Nz
metadata.jsonr
)r�)�indent)	Zcollection_file�get_metadata�metadata�scrub_metadata�write�json�dumps�RuntimeError�str)rZmfile�errrrr�collectBs
zGCP.collect)�returncCs"|j|j�}|j�j�}tj|�S)zq
        Retrieves metadata from the Metadata Server and transforms it into a
        dictionary object.
        )�_query_address�METADATA_QUERY�read�decoder�loads)r�responseZ
response_bodyrrrrLszGCP.get_metadata)�urlr"cCs�yTtj|ddid�}tj|��.}|jdkrHtd|j�d�|j�j���|SQRXWn4tk
r�}ztdt|��|�WYdd}~XnXdS)	zf
        Query the given url address with headers required by Google Metadata
        Server.
        zMetadata-FlavorZGoogle)Zheaders��z2Failed to communicate with Metadata Server (code: z): Nz,Failed to communicate with Metadata Server: )	rZRequestZurlopen�coderr%r&rr)r)Zreqr(r rrrr#Us
zGCP._query_addresscs��jd�rdS�jdd��jdd�t���ttd������fdd����j��_�j�jdd	d
��j�jdd	d�dS)a"
        Remove all PII information from metadata, unless a keep-pii option
        is specified.

        Note: PII information collected by this plugin, like
        project number, account names etc. might be required by Google
        Cloud Support for faster issue resolution.
        zkeep-piiNZprojectZ	projectIdZnumericProjectId)�datar"cs�t|t�r2d|kr�j|d<�fdd�|j�D�St|t�rN�fdd�|D�St|t�rp|j��j�j��j�St|t�r�|�kr��jS|S|S)N�tokencsi|]\}}�|��|��qSrr)�.0�k�v)�scrubrr�
<dictcomp>sz5GCP.scrub_metadata.<locals>.scrub.<locals>.<dictcomp>csg|]}�|��qSrr)r.�value)r1rr�
<listcomp>�sz5GCP.scrub_metadata.<locals>.scrub.<locals>.<listcomp>)�
isinstance�dict�REDACTED�items�listr�replace�int)r,)�
project_id�project_number�project_number_intr1rrrr1ys





z!GCP.scrub_metadata.<locals>.scrubZ
attributeszssh-keysZsshKeys)Z
get_optionrrr�safe_redact_key)rr)r<r=r>r1rrris	
zGCP.scrub_metadata)�dict_obj�keycCs||kr|j||<dS)z
 Redact keys N)r7)�clsr@rArrrr?�szGCP.safe_redact_key)r)�__name__�
__module__�__qualname__Z
short_descZplugin_nameZprofilesrZoption_listZ
METADATA_ROOTr$r7rrrrr!r6r�staticmethodrrr#r�classmethodr?rrrrr	s&

	'r	)rZhttp.clientrZtypingrZurllibrZurllib.errorrZsos.report.pluginsrrrr	rrrr�<module>s

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists