
                             d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlZddl	m
Z
  G d de      Zd	 Zd
 Zd Zd Zd Zd Zd Zy)zEThis package holds a handful of utilities for manipulating manifests.    )absolute_import)division)print_functionN)docker_namec                       e Zd ZdZy)BadManifestExceptionz?Exception type raised when a malformed manifest is encountered.N)__name__
__module____qualname____doc__     4lib/third_party/containerregistry/client/v2/util_.pyr   r      s    Gr   r   c                     | j                  d      }t        |      }|dz  dk(  rn'|dz  dk(  r|dz  }n|dz  dk(  r|dz  }nt        d      t        j                  |      j                  d      S )	a  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                 X    t        j                  t        | d               }|d   |d   fS )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                 h    t        | d         }| dd D ]  }|t        |      k7  st        d       |S )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                     t        j                  |       }|d   }t        |      dk  rt        d      |D ]  }d|vst        d       t	        |      \  }}t        |      }| d| |z   }||fS )aB  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   DetachSignaturesr4   L   s     **X&- \** 	_q
<
==c# !GHH 
 "?z!J=+,&q/&8
Z	((r   c                     | S )Nr   )r3   s    r   Signr6   l   s    	r   c                     t        |      \  }}| d| }t        |      }dj                  |t        j                  |d      |      S )z>Attach the provided signatures to the provided naked manifest.r   z*{prefix},"signatures":{signatures}{suffix}T)	sort_keys)prefixr)   r2   )r,   r   formatr#   dumps)r.   r)   r0   r1   r9   r2   s         r   _AttachSignaturesr<   s   sU     "?z!J=+Am$&,&	5	<	<J$7 
= 
 r   c                     t        |       \  }}t        j                  |      }|j                  |d<   |j                  |d<   t        j
                  |dd      }t        |      S )z@Rename this signed manifest to the provided name, and resign it.nametagTr   )r8   indent)r4   r#   r$   
repositoryr?   r;   r6   )r.   r>   r3   unused_signaturesr/   updated_unsigned_manifests         r   RenamerD      sd    )9()C&&**./-//-- #jjtA/ 
'	((r   )r   
__future__r   r   r   r   r#   os
subprocesscontainerregistry.clientr   	Exceptionr   r   r&   r,   r4   r6   r<   rD   r   r   r   <module>rJ      sU    L &  %   	  0H9 H<8>)@	)r   