
    f                         S r SSKrSSKJr  SSKJr  SSKrSSKJr  Sr	 " S S\5      r
S	 rS
 rS rS rS rS rS rS rS rg)z,Utilities for Binary Authorization commands.    N)docker_name)Error)urllibz/binaryauthorization.googleapis.com/attestationsc                       \ rS rSrSrSrg)BadImageUrlError   z@Raised when a container image URL cannot be parsed successfully. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r	       9lib/googlecloudsdk/command_lib/container/binauthz/util.pyr   r      s    Hr   r   c                    U=(       d    Sn[         R                  R                  U 5      nUR                  (       a)  UR                  (       d  [        SR                  U S95      eUR                  (       d.  [         R                  R                  SR                  U 5      5      nUR                  US9R                  5       R                  S5      $ )aj  Returns the passed `image_url` with the scheme replaced.

Args:
  image_url: The URL to replace (or strip) the scheme from. (string)
  scheme: The scheme of the returned URL.  If this is an empty string or
    `None`, the scheme is stripped and the leading `//` of the resulting URL
    will be stripped off.
Raises:
  BadImageUrlError: `image_url` isn't valid.
 zMImage URL '{image_url}' is invalid because it does not have a host component.)	image_urlz//{}scheme/)
r   parseurlparser   netlocr   format_replacegeturllstrip)r   r   
parsed_urls      r   ReplaceImageUrlSchemer    "   s     <R&||$$Y/* z00
	ViV02 2 
		&&v}}Y'?@J
 
		F		+	2	2	4	;	;C	@@r   c                    [        U SS9n [        R                  " U5      nSS[
        R                  " UR                  5       5      0SUR                  0SS.0$ ! [        R                   a  n[	        U5      eSnAff = f)	a  Creates a dict representing a JSON signature object to sign.

Args:
  container_image_url: See `containerregistry.client.docker_name.Digest` for
    artifact URL validation and parsing details.  `container_image_url` must
    be a fully qualified image URL with a valid sha256 digest.

Returns:
  Dictionary of nested dictionaries and strings, suitable for passing to
  `json.dumps` or similar.
r   )r   r   Ncriticalzdocker-referencezdocker-manifest-digestz)Google cloud binauthz container signature)identityimagetype)	r    r   DigestBadNameExceptionr   six	text_typeas_repositorydigest)container_image_urlurlrepo_digestes       r   MakeSignaturePayloadDictr0   D   s     	(;BG#$$S)K  #--0I0I0K"L '(:(: >

 
 
	%	% 
1
s   A A>.A99A>c                     [        U 5      n[        R                  " USSSSS9nSR                  U5      R	                  S5      $ )a  Creates a JSON bytestring representing a signature object to sign.

Args:
  container_image_url: See `containerregistry.client.docker_name.Digest` for
    artifact URL validation and parsing details.  `container_image_url` must
    be a fully qualified image URL with a valid sha256 digest.

Returns:
  A bytestring representing a JSON-encoded structure of nested dictionaries
  and strings.
T   ),z: )ensure_asciiindent
separators	sort_keysz{}
utf-8)r0   jsondumpsr   encode)r,   payload_dictpayloads      r   MakeSignaturePayloadr>   b   sJ     **=>, JJ' 
w		&	&w	//r   c                     [        U SS9n [        R                  " U5        U$ ! [        R                   a  n[	        U5      eSnAff = f)zEnsures the given URL has no scheme (e.g.

replaces "https://gcr.io/foo/bar" with "gcr.io/foo/bar" and leaves
"gcr.io/foo/bar" unchanged).

Args:
  artifact_url: A URL string.
Returns:
  The URL with the scheme removed.
r   r   N)r    r   r&   r'   r   )artifact_urlurl_without_schemer/   s      r   RemoveArtifactUrlSchemerB   ~   sR     -\"E )* 
 
	%	% 
1
s   $ AAAc                     [        U SS9n [        R                  " U5      nUR
                  $ ! [        R                   a  n[	        U5      eSnAff = f)zReturns the digest of an image given its url.

Args:
  artifact_url: An image url. e.g. "https://gcr.io/foo/bar@sha256:123"

Returns:
  The image digest. e.g. "sha256:123"
r   r   N)r    r   r&   r'   r   r+   )r@   rA   r+   r/   s       r   GetImageDigestrD      sX     -\"E  23F 
 
	%	% 
1
s   . AAAc           	          U R                  S5      nUR                  S5      nSR                  SS[        U5      -  US[        U5      -  U/5      $ )zPae encode input using the specified dsse type.

Args:
  dsse_type: DSSE envelope type.
  body: payload string.

Returns:
  Pae-encoded payload byte string.
r8       s   DSSEv1s   %d)r;   joinlen)	dsse_typebodydsse_type_bytes
body_bytess       r   	PaeEncoderM      sZ     $$W-/{{7#*	c/""c*o 
 r   c           	      L    SSSS0SU  Vs/ s H  nSU0PM	     sn0S.nU$ s  snf )zCreates a minimal PodSpec from a list of images.

Args:
  images: list of images being evaluated.

Returns:
  PodSpec object in JSON form.
v1Podnamer   
containersr$   )
apiVersionkindmetadataspecr	   )imagesr$   rV   s      r   GeneratePodSpecFromImagesrX      sG     
" v>ve'5)v>	
$ 
+	 ?s   !c                     U S   R                  S0 5      nUR                  [        S5      nU(       a  SR                  U/U-   5      U[        '   OSR                  U5      U[        '   X S   S'   U $ )a  Inlines attestations into a Kubernetes PodSpec.

Args:
  pod_spec: The PodSpec provided by the user.
  attestations: List of attestations returned by the policy evaluator in comma
    separated DSSE form.

Returns:
  Modified PodSpec with attestations inlined.
rU   annotationsNr3   )get$_BINAUTHZ_ATTESTATION_ANNOTATION_KEYrG   )pod_specattestationsrZ   existing_attestationss       r   AddInlineAttestationsToPodSpecr`      s{     $((;+%//*D 8;	,.9K45 9<8NK45(3:}%	/r   c                 ^    U S   S:w  a  [        U S   S   U5      U S   S'   U $ [        X5      $ )a  Inlines attestations into a Kubernetes resource.

Args:
  resource: The Kubernetes resource provided by the user.
  attestations: List of attestations returned by the policy evaluator in comma
    separated DSSE form.

Returns:
  Modified Kubernetes resource with attestations inlined.
rT   rP   rV   template)r`   )resourcer^   s     r   AddInlineAttestationsToResourcerd      sF     f#A$l$HVZ  O	'	??r   )r   r9   containerregistry.clientr   googlecloudsdk.core.exceptionsr   r(   	six.movesr   r\   r   r    r0   r>   rB   rD   rM   rX   r`   rd   r	   r   r   <module>rh      sc    3  0 0 
  6 %
Iu IAD<08*(*.2@r   