
                             S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	J
r
   " S S\5      rS	 rS
 rS rS rS rS rS rg)zEThis package holds a handful of utilities for manipulating manifests.    )absolute_import)division)print_functionN)docker_namec                       \ rS rSrSrSrg)BadManifestException   z?Exception type raised when a malformed manifest is encountered. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r
       4lib/third_party/containerregistry/client/v2/util_.pyr   r      s    Gr   r   c                     U R                  S5      n[        U5      nUS-  S:X  a  O)US-  S:X  a  US-  nOUS-  S:X  a  US-  nO[        S5      e[        R                  " U5      R                  S5      $ )	an  Perform a JOSE-style base64 decoding of the supplied message.

This is based on the docker/libtrust version of the similarly named
function found here:
  https://github.com/docker/libtrust/blob/master/util.go

Args:
  message: a JOSE-style base64 url-encoded message.
Raises:
  BadManifestException: a malformed message was supplied.
Returns:
  The decoded message.
utf8   r      s   ==      =zMalformed JOSE Base64 encoding.)encodelenr   base64urlsafe_b64decodedecode)message	bytes_msgls      r   _JoseBase64UrlDecoder!       s{     nnV$)	)n!UaZ1uzI1uzI
@
AA		!	!)	,	3	3F	;;r   c                 Z    [         R                  " [        U S   5      5      nUS   US   4$ )zDExtract the length and encoded suffix denoting the protected region.	protectedformatLength
formatTail)jsonloadsr!   )	signaturer#   s     r   _ExtractProtectedRegionr)   <   s0    jj-i.DEF)
N
#Y|%<	==r   c                 n    [        U S   5      nU SS  H  nU[        U5      :w  d  M  [        S5      e   U$ )zHVerify that the signatures agree on the protected region and return one.r      Nz'Signatures disagree on protected region)r)   r   )
signaturespsigs      r   _ExtractCommonProtectedRegionr/   B   sC     jm,!^c#C(( !JKK  
(r   c                     [         R                  " U 5      nUS   n[        U5      S:  a  [        S5      eU H  nSU;  d  M  [        S5      e   [	        U5      u  pE[        U5      nU SU U-   nXr4$ )a2  Detach the signatures from the signed manifest and return the two halves.

Args:
  manifest: a signed JSON manifest.
Raises:
  BadManifestException: the provided manifest was improperly signed.
Returns:
  a pair consisting of the manifest with the signature removed and a list of
  the removed signatures.
r,   r+   zExpected a signed manifest.r#   z$Signature is missing "protected" keyr   )r&   r'   r   r   r/   r!   )manifestjson_manifestr,   r.   format_lengthformat_tailsuffixunsigned_manifests           r   DetachSignaturesr7   L   s     **X&- \** 	_q
<
==c# !GHH 
 "?z!J=,&q/&8
	((r   c                     U $ )Nr
   )r6   s    r   Signr9   l   s    	r   c                     [        U5      u  p#U SU n[        U5      nSR                  U[        R                  " USS9US9$ )z>Attach the provided signatures to the provided naked manifest.r   z*{prefix},"signatures":{signatures}{suffix}T)	sort_keys)prefixr,   r5   )r/   r!   formatr&   dumps)r1   r,   r3   r4   r<   r5   s         r   _AttachSignaturesr?   s   sS     "?z!J=Am$&,&	5	<	<J$7 
= 
 r   c                     [        U 5      u  p#[        R                  " U5      nUR                  US'   UR                  US'   [        R
                  " USSS9n[        U5      $ )z@Rename this signed manifest to the provided name, and resign it.nametagTr   )r;   indent)r7   r&   r'   
repositoryrB   r>   r9   )r1   rA   r6   unused_signaturesr2   updated_unsigned_manifests         r   RenamerG      sa    )9()C&**./-//-- #jjtA/ 
'	((r   )r   
__future__r   r   r   r   r&   os
subprocesscontainerregistry.clientr   	Exceptionr   r!   r)   r/   r7   r9   r?   rG   r
   r   r   <module>rM      sU    L &  %   	  0H9 H<8>)@	)r   