3 ܓi6@sVddlZddlmZddlmZddlmZddlmZm Z m Z Gdddee Z dS)N)Any)request)URLError)PluginIndependentPlugin PluginOptc@seZdZdZdZdZeddddgZdZd Z d Z d Z d d Z ddZ ddZedddZeedddZddZeeedddZd S)GCPzGoogle Cloud Platformgcpvirtzkeep-piiFzyStop the plugin from removing PIIs like project name or organization ID from the metadata retrieved from Metadata server.)defaultZdescz(/sys/devices/virtual/dmi/id/product_namezBhttp://metadata.google.internal/computeMetadata/v1/?recursive=truez[--REDACTED--]NcCs@y&t|jdd}d|jkSQRXWntk r:dSXdS)zo Checks if this plugin should be executed based on the presence of GCE entry in sysfs. zutf-8)encodingzGoogle Compute EngineNF)open PRODUCT_PATHreadOSError)selfZsys_filer/usr/lib/python3.6/gcp.py check_enabled$s zGCP.check_enabledcCs$|jddgd|jddgddS)z Collect the following info: * `gcloud auth list` output * Any google services output from journal zgcloud auth listr )tagszgoogle*)ZunitsrN)Zadd_cmd_outputZ add_journal)rrrrsetup/sz GCP.setupcCs|jddgdd}y,|j|_|j|jtj|jddWn0tk rn}z|jt|WYdd}~XnXWdQRXd}|j j j d}|r|j |dS)Nz metadata.jsonr )r)indentzAcurl -v --connect-timeout 10https://rhui.googlecloud.com/zone.txtzgoogle-rhui-client) Zcollection_file get_metadatametadatascrub_metadatawritejsondumps RuntimeErrorstrZpolicyZpackage_managerZall_pkgs_by_name_regexZcollect_cmd_output)rZmfileerrZ curl_commandZ rhui_packagerrrcollect<s *z GCP.collect)returncCs|j|j}tj|S)zq Retrieves metadata from the Metadata Server and transforms it into a dictionary object. )_query_addressMETADATA_QUERYrloads)rZ response_bodyrrrrOs zGCP.get_metadata)urlr#cCsy\tj|ddid}tj|6}|jdkrHtd|jd|jj|jjSQRXWn4tk r}ztdt||WYdd}~XnXdS) zf Query the given url address with headers required by Google Metadata Server. zMetadata-FlavorZGoogle)Zheadersz2Failed to communicate with Metadata Server (code: z): Nz,Failed to communicate with Metadata Server: ) rZRequestZurlopencoderrdecoderr )rr'ZreqZresponser!rrrr$Ws    zGCP._query_addresscsjdrdSjddjddtttdfdd j_jjdd d jjdd 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#cst|tr2d|krj|d<fdd|jDSt|trNfdd|DSt|trp|jjjjSt|tr|krjS|S|S)Ntokencsi|]\}}||qSrr).0kv)scrubrr sz5GCP.scrub_metadata..scrub..csg|] }|qSrr)r-value)r0rr sz5GCP.scrub_metadata..scrub..) isinstancedictREDACTEDitemslistr replaceint)r+) project_idproject_numberproject_number_intr0rrrr0zs      z!GCP.scrub_metadata..scrubZ attributeszssh-keysZsshKeys)Z get_optionrr rsafe_redact_key)rr)r;r<r=r0rrrjs  zGCP.scrub_metadata)dict_objkeycCs||kr|j||<dS)z Redact keys N)r6)clsr?r@rrrr>szGCP.safe_redact_key)r )__name__ __module__ __qualname__Z short_descZ plugin_nameZprofilesrZ option_listrr%r6rrrr"r5rr r$r classmethodr>rrrrrs"   'r) rZtypingrZurllibrZ urllib.errorrZsos.report.pluginsrrrrrrrrs