
                         2   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 ej"                   ej$                  ej&                  j(                         G d dej*                                      Zy)z.Implements the command to upload an SBOM file.    )absolute_import)division)unicode_literals)arg_parsers)base)
exceptions)endpoint_util)flags)	sbom_util)util)log)
propertiesc                   0    e Zd ZdZdddZed        Zd Zy)Loadz6Upload an SBOM file and create a reference occurrence.z{description}a            To upload an SBOM file at /path/to/sbom.json for a Docker image in Artifact Registry:

          $ {command} --source=/path/to/sbom.json               --uri=us-west1-docker.pkg.dev/my-project/my-repository/busy-box@sha256:abcxyz

          To upload an SBOM file at /path/to/sbom.json for a Docker image with a KMS key version to sign the created SBOM reference:

          $ {command} --source=/path/to/sbom.json               --uri=us-west1-docker.pkg.dev/my-project/my-repository/busy-box@sha256:abcxyz               --kms-key-version=projects/my-project/locations/us-west1/keyRings/my-key-ring/cryptoKeys/my-key/cryptoKeyVersions/1

          To upload an SBOM file at /path/to/sbom.json for a Docker image from a Docker registry:

          $ {command} --source=/path/to/sbom.json               --uri=my-docker-registry/my-image@sha256:abcxyz               --destination=gs://my-cloud-storage-bucket
          )DESCRIPTIONEXAMPLESc                 J   | j                  ddddd       | j                  dddd	
       | j                  ddddt        j                  dd             | j                  dddddt        j                  dd             t        j                         j                  |        y)z[Set up arguments for this command.

    Args:
      parser: An argparse.ArgumentPaser.
    z--sourceSOURCET.zThe SBOM file for uploading.)metavarrequireddefaulthelpz--uriARTIFACT_URIa              The URI of the artifact the SBOM is generated from.
            The URI can be a Docker image from any Docker registries. A URI provided with a tag (e.g. `[IMAGE]:[TAG]`) will be resolved into a URI with a digest (`[IMAGE]@sha256:[DIGEST]`).
            When passing an image which is not from Artifact Registry or Container Registry with a tag, only public images can be resolved.
            Also, when passing an image which is not from Artifact Registry or Container Registry, the `--destination` flag is required.
            )r   r   r   z--kms-key-versionNa#              Cloud KMS key version to sign the SBOM reference.
            The key version provided should be the resource ID in the format of
            `projects/[KEY_PROJECT_ID]/locations/[LOCATION]/keyRings/[RING_NAME]/cryptoKeys/[KEY_NAME]/cryptoKeyVersions/[KEY_VERSION]`.
            FzX^projects/[^/]+/locations/[^/]+/keyRings/[^/]+/cryptoKeys/[^/]+/cryptoKeyVersions/[^/]+$zMust be in format of 'projects/[KEY_PROJECT_ID]/locations/[LOCATION]/keyRings/[RING_NAME]/cryptoKeys/[KEY_NAME]/cryptoKeyVersions/[KEY_VERSION]')r   r   r   typez--destinationDESTINATIONz            The storage path will be used to store the SBOM file.
            Currently only supports Cloud Storage paths start with 'gs://'.
        z	^gs://.*$z(Must be in format of gs://[STORAGE_PATH])r   r   r   r   r   )add_argumentr   RegexpValidatorr
   GetOptionalAALocationFlagAddToParser)parsers    "lib/surface/artifacts/sbom/load.pyArgsz	Load.Args;   s     +   	  
 
 ((gO
  $  ((D
   
##%11&9    c                    t        j                  |j                        }t        j                  dj                  |j                  |j                               |j                  st        j                  dd      t        j                  |j                        }t        j                  dj                  |j                  |j                  |j                  |j                  j!                  dd                   t         j"                  |j$                  k(  r"|j&                  st        j                  dd      t        j(                  |j                  |||j&                  	      }t        j                  d
j                  |             |j                  }|so|j                  xs2 t*        j,                  j.                  j                  j1                         }t        j                  dj                  |j                  |             t3        j4                  ||j                        }t7        j8                  |j                        5  t        j:                  |||||j<                        }ddd       t        j                  dj                               t        j>                  jA                  dj                  |jC                                      y# 1 sw Y   jxY w)zRun the load command.z3Successfully loaded the SBOM file. Format: {0}-{1}.r   z--uri is required.zFProcessed artifact. Project: {0}, Location: {1}, URI: {2}, Digest {3}.sha256 r   zT--destination is required for images not in Artifact Registry or Container Registry.)sourceartifactsbomgcs_pathzUploaded the SBOM file at {0}zUFailed to get project_id from the artifact URI {0}. Using project {1} for occurrence.)r)   
project_idstorager*   kms_key_versionNzWrote reference occurrence {0}.z1Uploaded the SBOM file under the resource URI {}.)"r   ParseJsonSbomr(   r   infoformatsbom_formatversionurir   InvalidArgumentExceptionProcessArtifactprojectlocationresource_uridigestsgetARTIFACT_TYPE_OTHERartifact_typedestinationUploadSbomToGCSr   VALUEScore	GetOrFailr   	GetParentr	   
WithRegionWriteReferenceOccurrencer.   statusPrintGetOccurrenceResourceUri)selfargssaremote_pathr,   parentoccurrence_ids           r"   RunzLoad.Runv   s*    	,AHH=DDMM199	
 88//

 
 	!!$((+AHHC
&IIqzz1>>199==23N
	 	%%8  //
%  ++{{!!	K HH,33K@A
 J<<M:#4#4#9#9#A#A#K#K#Mj	hh-F488Z(	 ^^J6F		!	!$--	088..m 
1 HH.55mDEJJ;BB&&(	
 
1	0s   %KKN)__name__
__module____qualname____doc__detailed_helpstaticmethodr#   rP    r$   r"   r   r       s3     ? %-, 8: 8:tIr$   r   N)rT   
__future__r   r   r   googlecloudsdk.callioper   r   r   $googlecloudsdk.command_lib.artifactsr	   r
   r   r   googlecloudsdk.corer   r   DefaultUniverseOnlyReleaseTracksReleaseTrackGACommandr   rW   r$   r"   <module>ra      sw    5 &  ' / ( . > 6 : 5 # * D%%(()]4<< ] * ]r$   