U
    -_g                     @   sb   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZmZ G dd de
eZdS )	    N)HTTPResponse)Any)request)URLError)PluginIndependentPlugin	PluginOptc                   @   s   e Zd ZdZdZdZeddddgZdZd	Z	d
Z
dZdZdd Zdd Zdd ZedddZeeedddZdd ZeeedddZdS )GCPzGoogle Cloud Platformgcp)Zvirtkeep-piiFzyStop the plugin from removing PIIs like project name or organization ID from the metadata retrieved from Metadata server.)defaultZdescz3http://metadata.google.internal/computeMetadata/v1/zBhttp://metadata.google.internal/computeMetadata/v1/?recursive=truez[--REDACTED--]NzDDMI: Google Google Compute Engine/Google Compute Engine, BIOS Googlec                 C   s(   |  d}|d dkrdS | j|d kS )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.
        dmesgZstatusr   Foutput)Zexec_cmd
GOOGLE_DMI)selfr    r   8/usr/lib/python3/dist-packages/sos/report/plugins/gcp.pycheck_enabled)   s    
zGCP.check_enabledc                 C   s$   | j ddgd | jddgd dS )z
        Collect the following info:
         * Metadata from the Metadata server
         * `gcloud auth list` output
         * Any google services output from journal
        zgcloud auth listr
   tagszgoogle*)Zunitsr   N)Zadd_cmd_outputZadd_journalr   r   r   r   setup4   s    	z	GCP.setupc                 C   s~   | j ddgdd}z,|  | _|   |tj| jdd W n0 tk
rn } z|t| W 5 d }~X Y nX W 5 Q R X d S )Nzmetadata.jsonr
   r      )indent)	Zcollection_fileget_metadatametadatascrub_metadatawritejsondumpsRuntimeErrorstr)r   Zmfileerrr   r   r   collectB   s    
zGCP.collect)returnc                 C   s"   |  | j}|  }t|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_bodyr   r   r   r   L   s    zGCP.get_metadata)urlr$   c              
   C   s   zbt j| ddid}t |<}|jdkrHtd|j d|   |W  5 Q R  W S Q R X W n4 tk
r } ztdt| |W 5 d}~X Y nX dS )	zf
        Query the given url address with headers required by Google Metadata
        Server.
        zMetadata-FlavorZGoogle)Zheaders   z2Failed to communicate with Metadata Server (code: z): z,Failed to communicate with Metadata Server: N)	r   ZRequestZurlopencoder    r'   r(   r   r!   )r+   Zreqr*   r"   r   r   r   r%   U   s     


zGCP._query_addressc                    s    drdS jd d  jd d tttd fddj_jd d	 d
 jd d	 d dS )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.
        r   NZprojectZ	projectIdZnumericProjectId)datar$   c                    s   t | tr2d| krj| d< fdd|  D S t | trNfdd| D S t | trp| j jS t | tr| krjS | S | S )Ntokenc                    s   i | ]\}} | |qS r   r   ).0kvscrubr   r   
<dictcomp>   s      z5GCP.scrub_metadata.<locals>.scrub.<locals>.<dictcomp>c                    s   g | ]} |qS r   r   )r0   valuer3   r   r   
<listcomp>   s     z5GCP.scrub_metadata.<locals>.scrub.<locals>.<listcomp>)
isinstancedictREDACTEDitemslistr!   replaceint)r.   Z
project_idZproject_numberZproject_number_intr4   r   r   r   r4   y   s    



 
z!GCP.scrub_metadata.<locals>.scrubZ
attributeszssh-keysZsshKeys)Z
get_optionr   r!   r   safe_redact_keyr   r   r?   r   r   i   s    	
zGCP.scrub_metadata)dict_objkeyc                 C   s   ||kr| j ||< dS )z Redact keys N)r:   )clsrA   rB   r   r   r   r@      s    zGCP.safe_redact_key)__name__
__module____qualname__Z
short_descZplugin_nameZprofilesr   Zoption_listZMETADATA_ROOTr&   r:   r   r   r   r   r#   r9   r   staticmethodr!   r   r%   r   classmethodr@   r   r   r   r   r	      s*   
	'r	   )r   Zhttp.clientr   typingr   Zurllibr   Zurllib.errorr   Zsos.report.pluginsr   r   r   r	   r   r   r   r   <module>   s   