
    n                     z   d Z ddlmZ ddlmZ ddlmZ 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 ddlmZ ddlmZ ddlmZ dj'                  ej(                        Z ej,                  ej.                  j0                        ej2                   G d dej4                                      Z G d de      Zy)zFImplements the command to list vulnerabilities from Artifact Registry.    )absolute_import)division)unicode_literalsN)
exceptions)GetLatestScan)GetVulnerabilities)base)docker_util)endpoint_util)flags)format_utilaA       table[box, title="%TITLE%"](
      occurrence.vulnerability.shortDescription:label=CVE,
      occurrence.vulnerability.effectiveSeverity:label=EFFECTIVE_SEVERITY,
      occurrence.vulnerability.cvssScore:label=CVSS:sort=-1:reverse,
      occurrence.vulnerability.packageIssue.fixAvailable:label=FIX_AVAILABLE,
      occurrence.vulnerability.vexAssessment.state:label=VEX_STATUS,
      occurrence.vulnerability.packageIssue.affectedPackage:sort=3:label=PACKAGE,
      occurrence.vulnerability.packageIssue.packageType:label=PACKAGE_TYPE,
      vexScope,
      {}
    )
    c                   <    e Zd ZdZdddZed        Zd Zd Zd Z	y	)
ListzMCommand for listing vulnerabilities. To see all fields, use --format=json.
  z{description}z        To list vulnerabilities for an artifact, run:

          $ {command} us-east1-docker.pkg.dev/project123/repository123/someimage@sha256:49765698074d6d7baa82f
      )DESCRIPTIONEXAMPLESc                    t        j                         j                  |        t        j                         j                  |        t        j                         j                  |        | j
                  j                  dg       y )Nz%occurrence.vulnerability.packageIssue)r   GetListURIArgAddToParserGetOptionalAALocationFlag&GetVulnerabilitiesOccurrenceFilterFlagdisplay_info
AddFlatten)parsers    -lib/surface/artifacts/vulnerabilities/list.pyArgsz	List.Args>   sb    	%%f-	##%11&9	002>>vF
""$K#LM
    c                    |j                   }|j                  }| j                  |j                        \  }}t	        j
                  |      5  |dj                  ||      }t        ||      }t        |||      }d d d        | j                  |       t              }g }t        |      dk  ri S |D ]  }	d}
|	j                  r~|	j                  j                  rh|	j                  j                  j                  rH|	j                  j                  j                  j                  d      }|d   j!                  d      rd}
nd}
|j#                  t%        |	|
              |S # 1 sw Y   xY w)	Nz{}/locations/{}    /zimage-IMAGEDIGEST)locationoccurrence_filterreplaceTagsURIr   
WithRegionformatr   r   setTitlelistlenvulnerabilityvexAssessmentnoteNamesplit
startswithappendVulnerabilityEntry)selfargsr$   r%   resourceprojectlatest_scanoccurrencesresultsocc	vex_scopetokenss               r   RunzList.RunF   sE   }}H..((2Hg		!	!(	+		#**7H=!'84k&w:KLk	 
,
 	MM$${#KG
;!ii


----66""0099??D":  *))nn'Y78  N/ 
,	+s   .E!!E*c           
      H   |}|j                  d      sdj                  |      }t        j                  t        j
                  |      }|r4|d   d   }d|d   d   v }|rt	        j                  |      \  }}}t	        j                  |      \  }}|rt	        j                  |j                  j                  |j                         j                  |j                  j                         dj                  |j                  j                  |j                  j                                    }|j                  }	dj                  |j                               }
t        j                  t        j
                  |
|d      }||	fS t        j                   dj                  |            )Nzhttps://z
https://{}r   zgcr.ior   z{}/{}zReceived invalid URI {})r1   r)   refindallr
   DOCKER_URI_REGEXConvertGCRImageStringDockerUrlToVersionGcrDockerVersiondocker_repor7   GetDockerStringreplacereposubar_exceptionsInvalidInputValueError)r4   original_uriupdated_urifoundresource_uri_stris_gcr_imageversionr7   docker_html_str_digests              r   r&   zList.replaceTagsc   s{   K!!*- ''4kJJ{33[AEq!58A;&f	!,!B!B"
!Q #556FGneW	..%%##%--!!113%%**%%--	
 g+2273J3J3LMFF

&
&
 

	k '!!

.
.!((6 r   c                    d}|r"|j                   |j                   j                  d}n*|j                   j                  d d }dj                  |      }t        j	                  d|      }|j                         j                  |       y )Nr   zScan status unknownizLatest scan was at {}z%TITLE%)	discoverylastScanTimer)   DEFAULT_LIST_FORMATrH   GetDisplayInfo	AddFormat)r4   r5   r8   titlelast_scan_timelist_formats         r   r*   zList.setTitle   s~    E  (  --5#e",,99$3?n%,,^<e%--i?K##K0r   N)
__name__
__module____qualname____doc__detailed_helpstaticmethodr   r>   r&   r*    r   r   r   r   /   s<     %
-  :#J1r   r   c                   6    e Zd ZdZd Zed        Zed        Zy)r3   zHolder for an entry of vulnerability list results.

  Properties:
    occurrence: Vulnerability occurrence.
    vex_scope: Scope of the VEX statement.
  c                      || _         || _        y N)_occurrence
_vex_scope)r4   
occurrencer<   s      r   __init__zVulnerabilityEntry.__init__   s    !DDOr   c                     | j                   S rh   )ri   r4   s    r   rk   zVulnerabilityEntry.occurrence   s    r   c                     | j                   S rh   )rj   rn   s    r   r<   zVulnerabilityEntry.vex_scope   s    ??r   N)r_   r`   ra   rb   rl   propertyrk   r<   re   r   r   r3   r3      s4         r   r3   )rb   
__future__r   r   r   r@    googlecloudsdk.api_lib.artifactsr   rK   0googlecloudsdk.api_lib.artifacts.vulnerabilitiesr   r   googlecloudsdk.callioper	   $googlecloudsdk.command_lib.artifactsr
   r   r   r   r)   "CONTAINER_ANALYSIS_METADATA_FORMATrY   ReleaseTracksReleaseTrackGADefaultUniverseOnlyListCommandr   objectr3   re   r   r   <module>r}      s    M &  ' 	 H J O ( < > 6 < 	{==>  D%%(()c14 c1  *c1L r   