
    J%                     J   d 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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 ej0                   ej2                  ej4                  j6                         G d dej8                                      Zy)zCImplements the command to upload Generic artifacts to a repository.    )absolute_import)division)unicode_literals)
exceptions)
list_pager)apis)base)docker_util)endpoint_util)flags)requests)vex_util)
propertiesc                   V    e Zd ZdZdddZdZdZed        Zd Z	d Z
d	 Zd
 Zd Zd Zy)LoadVexa  Load VEX data from a CSAF file into Artifact Analysis.

  Command loads VEX data from a Common Security Advisory Framework (CSAF) file
  into Artifact Analysis as VulnerabilityAssessment Notes. VEX data tells
  Artifact Analysis whether vulnerabilities are relevant and how.
  z{description}a         To load a CSAF security advisory file given an artifact in Artifact Registry and the file on disk, run:

        $ {command} --uri=us-east1-docker.pkg.dev/project123/repository123/someimage@sha256:49765698074d6d7baa82f --source=/path/to/vex/file

To load a CSAF security advisory file given an artifact with a tag and a file on disk, run:

        $ {command} --uri=us-east1-docker.pkg.dev/project123/repository123/someimage:latest --source=/path/to/vex/file
    )DESCRIPTIONEXAMPLESNc                     | j                  ddd       | j                  ddd       | j                  ddd	       t        j                         j                  |        y )
Nz--uriTa  The path of the artifact in Artifact Registry. A 'gcr.io' image can also be used if redirection is enabled in Artifact Registry. Make sure 'artifactregistry.projectsettings.get' permission is granted to the current gcloud user to verify the redirection status.)requiredhelpz--sourcezThe path of the VEX file.z	--projectFz2The parent project to load security advisory into.)add_argumentr   GetOptionalAALocationFlagAddToParser)parsers    1lib/surface/artifacts/vulnerabilities/load_vex.pyArgszLoadVex.Args;   sx    
	  
 (  
 A  
 
##%11&9
    c                    t        j                  |j                        5  t        j                  dd      | _        | j
                  j                  | _        ddd       |j                  }t        j                  |      }t        j                  |      rIt        j                  |      \  }}|j                         }|r|j                         nd}|j                  }nt        j                   |      rt        j"                  |      \  }}}t%        j&                         }t%        j(                  |      }	|	j*                  |j,                  j.                  j0                  k7  r9t3        j4                  d      t3        j4                  dj7                  |            |j                  xs |}
|j8                  }t        j:                  |||      \  }}| j=                  ||
||j                         y# 1 sw Y   xY w)z(Run the generic artifact upload command.containeranalysisv1NzyThis command only supports Artifact Registry. You can enable redirection to use gcr.io repositories in Artifact Registry.z%{} is not an Artifact Registry image.)r   
WithRegionlocationr   GetClientInstance	ca_clientMESSAGES_MODULEca_messagesurir   RemoveHTTPSr
   IsARDockerImageDockerUrlToImageGetDockerStringproject
IsGCRImageParseGCRUrlar_requestsGetMessagesGetProjectSettingslegacyRedirectionStateProjectSettings%LegacyRedirectionStateValueValuesEnumREDIRECTION_FROM_GCR_IO_ENABLEDar_exceptionsInvalidInputValueErrorformatsourceParseVexFile
writeNotes)selfargsr'   imageversion	image_uriversion_uriimage_projectmessagessettingsr,   filenamenotesgeneric_uris                 r   RunzLoadVex.RunU   s   		!	!$--	0--.A4Hdn77d 
1 ((C


s
#C""3'"33C8neW'')i18G++-dkmmm				$.6.B.B3.G+mY((*h//>h

)
)%%KKkkl 22L
 	

 00
1
8
8
=  ll+mG{{H!..xKPE;OOE7K?
? 
1	0s   7G00G:c                    g }g }| j                  ||      }|D ]  }| j                  j                  dj                  ||j                              }		 | j
                  j                  j                  |	       d}
|
r|j                  |       u|j                  |        | j                  |||       | j                  |||       | j                  ||||       y # t        j                  $ r d}
Y yw xY w)N{}/notes/{}nameTF)parentr&   (ContaineranalysisProjectsNotesGetRequestr8   keyr$   projects_notesGetapitools_exceptionsHttpNotFoundErrorappendbatchWriteNotesupdateNotesdeleteNotes)r<   rF   r,   r'   r"   notes_to_createnotes_to_updaterM   noteget_requestnote_existss              r   r;   zLoadVex.writeNotesx   s    OO[[(+F$$MM##FDHH5 N k%%))+6 
t$t$  	'8<_gx8
 	UGS(3 !22 s   'CC43C4c                   
 d }
fd} |t         j                  j                  j                  j	                               
 ||      D ]  }|s y | j
                  j                  j                         }||_        | j
                  j                  |      }| j
                  j                  | j                  ||      |      }	| j                  j                  j                  |	        y )Nc                     	 t        |       }|dk  s|dkD  rt        j                  d      |S # t        $ r t        j                  d      w xY w)Nz.max_notes_per_batch_request must be an integer   i  z6max_notes_per_batch_request must be between 1 and 1000)int
ValueErrorr6   r7   )note_limit_strmax_notes_per_batch_requests     r   $validate_max_notes_per_batch_requestzELoadVex.batchWriteNotes.<locals>.validate_max_notes_per_batch_request   si    
&).&9#
 
%q	(,G$,N22D
 	
 )(  
22<
 	

s	   . Ac              3   z   K   g }| D ](  }|j                  |       t        |      k(  s#| g }* |r| y y w)N)rT   len)rF   notes_chunkrZ   rc   s      r   chunkedz(LoadVex.batchWriteNotes.<locals>.chunked   sM     k$4 {::
+	  
 
s   ';;)rF   )rM   batchCreateNotesRequest)r   VALUES	artifactsrc   rQ   r&   BatchCreateNotesRequest
NotesValueadditionalProperties0ContaineranalysisProjectsNotesBatchCreateRequestrM   r$   rP   BatchCreate)r<   rF   r,   r"   rd   rh   rg   
note_valuebatch_requestrequestrc   s             @r   rU   zLoadVex.batchWriteNotes   s    )
 #G##??CCE# u~##;;FFHj(3j%&&>> ? m 


K
K[[(3&3 L   nn##//8 &r   c                    |sy | j                  ||      }|D ]h  }| j                  j                  dj                  ||j                        |j
                        }| j                  j                  j                  |       j y )NrJ   )rL   rZ   )	rM   r&   *ContaineranalysisProjectsNotesPatchRequestr8   rO   valuer$   rP   Patch)r<   rF   r,   r"   rM   rZ   patch_requests          r   rV   zLoadVex.updateNotes   sv    [[(+F



E
E ''9:: F   nn##))-8 r   c                    | j                   j                  dj                  |      | j                  ||            }t	        j
                  | j                  j                  |dd      }t               }|D ]m  }|j                  j                  j                  j                  }	|j                  j                  j                  j                  }
|	|k(  s]|j                  |
       o |D ]r  }|j                  j                  j                  }||vs(| j                   j!                  |j"                        }| j                  j                  j%                  |       t y )Nz1vulnerability_assessment.product.generic_uri="{}")filterrM   rF   pageSize)servicers   fieldbatch_size_attributerK   )r&   )ContaineranalysisProjectsNotesListRequestr8   rM   r   YieldFromListr$   rP   setrv   vulnerabilityAssessmentproduct
genericUri
assessmentvulnerabilityIdadd+ContaineranalysisProjectsNotesDeleteRequestrL   Delete)r<   
file_notesr,   r'   r"   list_requestdb_notescves_in_file	file_notefile_urifile_vulnerabilitydb_notedb_vulnerabilitydelete_requests                 r   rW   zLoadVex.deleteNotes   s6   ##MMBII#N{{7H- N L ''--'	H 5L	88@@KKh
//
1
1
<
<
L
L  
S+,   

)
)
4
4
D
D  
	-HH\\ I  	
 	%%,,^< r   c                 L    |dj                  ||      S dj                  |      S )Nzprojects/{}/locations/{}zprojects/{})r8   )r<   r,   r"   s      r   rM   zLoadVex.parent   s,    '..wAA((r   )__name__
__module____qualname____doc__detailed_helpr$   r&   staticmethodr   rH   r;   rU   rV   rW   rM    r   r   r   r   "   sW     %- )+ 2!F4249l9=B)r   r   N)r   
__future__r   r   r   apitools.base.pyr   rR   r    googlecloudsdk.api_lib.artifactsr6   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper	   $googlecloudsdk.command_lib.artifactsr
   r   r   r   r/   r   googlecloudsdk.corer   DefaultUniverseOnlyReleaseTracksReleaseTrackGACommandr   r   r   r   <module>r      s~    J &  ' > ' H , ( < > 6 H 9 * D%%(()T)dll T) * T)r   