
    -_g                     h    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e
e      Zy)	    N)HTTPResponse)Any)request)URLError)PluginIndependentPlugin	PluginOptc                       e Zd ZdZdZdZ eddd      gZdZd	Z	d
Z
dZdZd Zd Zd ZdefdZededefd       Zd Zededefd       Zy)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--]NzDDMI: Google Google Compute Engine/Google Compute Engine, BIOS Googlec                 X    | j                  d      }|d   dk7  ry| 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   s     8/usr/lib/python3/dist-packages/sos/report/plugins/gcp.pycheck_enabledzGCP.check_enabled)   s4     g&?a%/11    c                 T    | j                  ddg       | j                  ddg       y)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   s    r   setupz	GCP.setup4   s1     	.eW= 	yw7r   c                 b   | j                  ddg      5 }	 | j                         | _        | j                          |j	                  t        j                  | j                  d             d d d        y # t        $ r$}|j	                  t        |             Y d }~1d }~ww xY w# 1 sw Y   y xY w)Nzmetadata.jsonr   r      )indent)	collection_fileget_metadatametadatascrub_metadatawritejsondumpsRuntimeErrorstr)r   mfileerrs      r   collectzGCP.collectB   s    !!/!@ 	&E& $ 1 1 3##%DJJt}}Q?@		& 	&
   &CH%%&	& 	&s/   B%AA55	B">BB%B""B%%B.returnc                     | j                  | j                        }|j                         j                         }t	        j
                  |      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_bodys      r   r&   zGCP.get_metadataL   s=    
 &&t':':; ..0zz-((r   urlc                 p   	 t        j                  | ddi      }t        j                  |      5 }|j                  dk7  r8t	        d|j                   d|j                         j                         z         |cddd       S # 1 sw Y   yxY w# t        $ r}t	        dt        |      z         |d}~ww xY w)	zf
        Query the given url address with headers required by Google Metadata
        Server.
        zMetadata-FlavorGoogle)headers   z2Failed to communicate with Metadata Server (code: z): Nz,Failed to communicate with Metadata Server: )	r   Requesturlopencoder,   r5   r6   r   r-   )r:   reqr8   r/   s       r   r3   zGCP._query_addressU   s    	//#0A8/LMC%  ==C'&""*--5 ..012 2         	>SIK	s5   .B A	B9	B BB B 	B5B00B5c                 r     j                  d      ry j                  d   d    j                  d   d   t              dt        dt        f fd  j                         _         j	                   j                  d   d	   d
        j	                   j                  d   d	   d       y)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datar1   c                    t        | t              rCd| v rj                  | d<   | j                         D ci c]  \  }} |       |       c}}S t        | t              r| D cg c]
  } |       c}S t        | t
              r6| j                  j                        j                  j                        S t        | t              r| k(  rj                  S | S | S c c}}w c c}w )Ntoken)
isinstancedictREDACTEDitemslistr-   replaceint)	rG   kvvalue
project_idproject_numberproject_number_intscrubr   s	       r   rW   z!GCP.scrub_metadata.<locals>.scruby   s    $%d? %)MMDM7;zz|Dtq!a%(*DD$%267e77$$||NDMMB#GJ>?$$(,0B(Bt}}LLK E7s   C&)C,
attributeszssh-keyssshKeys)
get_optionr'   r-   r   safe_redact_key)r   rT   rU   rV   rW   s   `@@@@r   r(   zGCP.scrub_metadatai   s     ??:&]]9-k:
!]]956HI/0	 	 	 	  dmm,T]]95lC'	)T]]95lC&	(r   dict_objkeyc                 ,    ||v r| j                   ||<   yy)z Redact keys N)rL   )clsr\   r]   s      r   r[   zGCP.safe_redact_key   s     (?LLHSM r   )__name__
__module____qualname__
short_descplugin_nameprofilesr	   option_listMETADATA_ROOTr4   rL   r'   r   r   r!   r0   rK   r&   staticmethodr-   r   r3   r(   classmethodr[    r   r   r   r      s    (JKH 	*e*	+K JM'NHH/J	28&)d ) C L  &%(N )t )# ) )r   r   )r*   http.clientr   typingr   urllibr   urllib.errorr   sos.report.pluginsr   r   r	   r   rj   r   r   <module>rp      s-     $   ! C CC)&# C)r   