o
    id                     @   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d Zdd Zdd ZdefddZededefddZdd ZededefddZdS )GCPzGoogle Cloud Platformgcp)virtkeep-piiFzyStop the plugin from removing PIIs like project name or organization ID from the metadata retrieved from Metadata server.)defaultdescz3http://metadata.google.internal/computeMetadata/v1/zBhttp://metadata.google.internal/computeMetadata/v1/?recursive=truez[--REDACTED--]zDDMI: Google Google Compute Engine/Google Compute Engine, BIOS Googlec                 C   s(   |  d}|d dkrdS | j|d v 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.
        dmesgstatusr   Foutput)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*)unitsr   N)add_cmd_outputadd_journalr   r   r   r   setup3   s   	z	GCP.setupc                 C   s   | j ddgdA}z|  | _|   |tj| jdd W n ty8 } z|t| W Y d }~nd }~ww W d    d S W d    d S 1 sLw   Y  d S )Nzmetadata.jsonr
   r      )indent)	collection_fileget_metadatametadatascrub_metadatawritejsondumpsRuntimeErrorstr)r   mfileerrr   r   r   collectA   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   responseresponse_bodyr   r   r   r"   K   s   
zGCP.get_metadataurlc              
   C   sv   zt j| ddid}t |}W n ty$ } ztdt| d}~ww |jdkr9td|j d|   |S )	zf
        Query the given url address with headers required by Google Metadata
        Server.
        zMetadata-FlavorGoogle)headersz,Failed to communicate with Metadata Server: N   z2Failed to communicate with Metadata Server (code: z): )	r   Requesturlopenr   r(   r)   coder0   r1   )r5   reqr3   r+   r   r   r   r.   T   s$   


zGCP._query_addressc                    s    drdS jd d  jd d tdtdtf 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   Nproject	projectIdnumericProjectIddatar-   c                    s   t | trd| v rj| d< fdd|  D S t | tr'fdd| D S t | tr8| j jS t | trF| krD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   )rB   valuerE   r   r   
<listcomp>~   s    z5GCP.scrub_metadata.<locals>.scrub.<locals>.<listcomp>)
isinstancedictREDACTEDitemslistr)   replaceint)r@   
project_idproject_numberproject_number_intrF   r   r   r   rF   v   s   





z!GCP.scrub_metadata.<locals>.scrub
attributeszssh-keyssshKeys)
get_optionr#   r)   r   safe_redact_keyr   r   rQ   r   r$   f   s   
	zGCP.scrub_metadatadict_objkeyc                 C   s   ||v r| j ||< d S d S )N)rL   )clsrY   rZ   r   r   r   rX      s   zGCP.safe_redact_keyN)__name__
__module____qualname__
short_descplugin_nameprofilesr   option_listMETADATA_ROOTr/   rL   r   r   r   r,   rK   r"   staticmethodr)   r   r.   r$   classmethodrX   r   r   r   r   r	      s*    
	'r	   )r&   http.clientr   typingr   urllibr   urllib.errorr   sos.report.pluginsr   r   r   r	   r   r   r   r   <module>   s   