
    ~ g                     h    S SK r S SKJr  S SKJr  S SKJr  S SKJr  S SK	J
r
JrJr   " S S\
\5      rg)	    N)HTTPResponse)Any)request)URLError)PluginIndependentPlugin	PluginOptc                       \ rS rSrSrSrSr\" SSSS9/rS	r	S
r
SrSrSrS rS rS rS\4S jr\S\S\4S j5       rS r\S\S\4S j5       rSrg)GCP   z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                 \    U R                  S5      nUS   S:w  a  gU R                  U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_enabledGCP.check_enabled)   s4     g&?a%/11    c                 H    U R                  SS/S9  U R                  SS/S9  g)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   setup	GCP.setup4   s1     	.eW= 	yw7r   c                 d   U R                  SS/S9 n U R                  5       U l        U R                  5         UR	                  [
        R                  " U R                  SS95        S S S 5        g ! [         a$  nUR	                  [        U5      5         S nAN2S nAff = f! , (       d  f       g = f)Nzmetadata.jsonr   r      )indent)	collection_fileget_metadatametadatascrub_metadatawritejsondumpsRuntimeErrorstr)r   mfileerrs      r   collectGCP.collectB   s    !!/!@E& $ 1 1 3##%DJJt}}Q?@	 A@
   &CH%%& A@s/   B!AA00
B:BB!BB!!
B/returnc                     U R                  U R                  5      nUR                  5       R                  5       n[        R
                  " U5      $ )zY
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)   GCP.get_metadataL   s=    
 &&t':':; ..0zz-((r   urlc                 |    [         R                  " U SS0S9n[         R                  " U5       nUR                  S:w  a8  [	        SUR                   S3UR                  5       R                  5       -   5      eUsSSS5        $ ! , (       d  f       g= f! [         a  n[	        S[        U5      -   5      UeSnAff = f)	zN
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/   r9   r:   r   r0   )r?   reqr<   r2   s       r   r7   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 
B;B66B;c                   ^ ^^^^ T R                  S5      (       a  gT R                  S   S   mT R                  S   S   m[        T5      mS[        S[        4UUUUU 4S jjmT" T R                  5      T l        T R	                  T R                  S   S	   S
5        T R	                  T R                  S   S	   S5        g)z
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datar5   c                   > [        U [        5      (       aG  SU ;   a  TR                  U S'   U R                  5        VVs0 s H  u  pT" U5      T" U5      _M     snn$ [        U [        5      (       a  U  Vs/ s H  nT" U5      PM     sn$ [        U [
        5      (       a6  U R                  TTR                  5      R                  TTR                  5      $ [        U [        5      (       a  U T:X  a  TR                  $ U $ U $ s  snnf s  snf )Ntoken)
isinstancedictREDACTEDitemslistr0   replaceint)	rM   kvvalue
project_idproject_numberproject_number_intscrubr   s	       r   r]   !GCP.scrub_metadata.<locals>.scruby   s    $%%d? %)MMDM7;zz|D|tqa%(*|DD$%%267$e$77$$$||NDMMB#GJ>?$$$(,0B(Bt}}LLK E7s   D7D
attributeszssh-keyssshKeys)
get_optionr*   r0   r   safe_redact_key)r   rZ   r[   r\   r]   s   `@@@@r   r+   GCP.scrub_metadatai   s     ??:&&]]9-k:
!]]956HI/0	 	 	 	  dmm,T]]95lC'	)T]]95lC&	(r   dict_objkeyc                 ,    X!;   a  U R                   X'   gg)zRedact keys N)rR   )clsrd   re   s      r   rb   GCP.safe_redact_key   s     ?LLHM r   )r*   )__name__
__module____qualname____firstlineno__
short_descplugin_nameprofilesr	   option_listMETADATA_ROOTr8   rR   r*   r   r   r#   r3   rQ   r)   staticmethodr0   r   r7   r+   classmethodrb   __static_attributes__ 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   ru   r   r   <module>r{      s-     $   ! C CC)&# C)r   