
    $+                     ~    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  G d
 de      Zy)z+API helpers for interacting with attestors.    )absolute_import)division)unicode_literals)
list_pager)apis)util)
exceptions)mapsc                   v    e Zd ZdZddZd ZddZd Zd Zd Z	dd	Z
dd
Z	 ddZd Z	 ddZddZd Zd Zy)Clientz(A client for interacting with attestors.Nc                 z    t        j                  |      | _        t        j                  |      | _        || _        y N)r   GetClientInstanceclientGetMessagesModulemessagesapi_version)selfr   s     :lib/googlecloudsdk/api_lib/container/binauthz/attestors.py__init__zClient.__init__    s.    ((5DK**;7DM"D    c                     | j                   j                  j                  | j                  j	                  |j                                     S )zGet the specified attestor.name)r   projects_attestorsGetr   .BinaryauthorizationProjectsAttestorsGetRequestRelativeName)r   attestor_refs     r   r   z
Client.Get%   sC    ;;))--DD**, 	E 	
 r   c                     t        j                  | j                  j                  | j                  j                  |j                               |xs d|dd      S )z7List the attestors associated with the current project.)parentd   	attestorspageSize)
batch_sizelimitfieldbatch_size_attribute)r   YieldFromListr   r   r   /BinaryauthorizationProjectsAttestorsListRequestr   )r   project_refr&   	page_sizes       r   ListzClient.List,   sW    ##&&EE++- 	F 	0#') )r   c                     | j                   t        j                  k(  r| j                  j                  S | j                  j
                  S r   )r   r   V1r   UserOwnedGrafeasNoteUserOwnedDrydockNoter   s    r   _GetNoteClasszClient._GetNoteClass7   s:    477* MM.. 0MM..0r   c                 D    | j                   t        j                  k(  rdS dS )NuserOwnedGrafeasNoteuserOwnedDrydockNote)r   r   r/   r2   s    r   GetNotePropertyNamezClient.GetNotePropertyName<   s&    477* # $"$r   c                 l    | j                   t        j                  k(  r|j                  S |j                  S )z7Return the Attestor's version-dependent Note attribute.)r   r   r/   r5   r6   )r   attestors     r   GetNoteAttrzClient.GetNoteAttrA   s,    477"******r   c                    |j                  t        j                        }| j                  j                  j                  | j                  j                   | j                  j                  d|j                         |d| j                          | j                         |j                               i|j                         |j                                     S )z8Create an attestors associated with the current project.)r   description)noteReference)r9   
attestorIdr!    )Parentr   PROJECTS_COLLECTIONr   r   Creater   1BinaryauthorizationProjectsAttestorsCreateRequestAttestorr   r7   r3   Name)r   r   note_refr<   r+   s        r   rB   zClient.CreateH   s    %%d&>&>?K;;))00GG+T]]++ !..0' ++-/Ct/A/A/C"*"7"7"90  $((*++- 	H 
	
 r   c                 d   | j                  |      }t        d | j                  |      j                  D              }||v r.t	        j
                  dj                  |j                              t        d | j                  |      j                  D              | j                  |      j                  j                  | j                  j                  ||             | j                  j                  j                  |      }t        fd| j                  |      j                  D              S )a|  Add a PGP key to an attestor.

    Args:
      attestor_ref: ResourceSpec, The attestor to be updated.
      pgp_pubkey_content: The contents of the PGP public key file.
      comment: The comment on the public key.

    Returns:
      The added public key.

    Raises:
      AlreadyExistsError: If a public key with the same key content was found on
          the attestor.
    c              3   4   K   | ]  }|j                     y wr   )asciiArmoredPgpPublicKey.0
public_keys     r   	<genexpr>z#Client.AddPgpKey.<locals>.<genexpr>i   s      A?J 	++?   z4Provided public key already present on attestor [{}]c              3   4   K   | ]  }|j                     y wr   idrJ   s     r   rM   z#Client.AddPgpKey.<locals>.<genexpr>q         A?J 	?rN   )rI   commentc              3   >   K   | ]  }|j                   vr|  y wr   rP   rK   rL   existing_idss     r   rM   z#Client.AddPgpKey.<locals>.<genexpr>|   %      .GJ==, 	G   )r   setr:   
publicKeysr	   AlreadyExistsErrorformatr   appendr   AttestorPublicKeyr   r   Updatenext)r   r   pgp_pubkey_contentrS   r9   existing_pub_keysupdated_attestorrV   s          @r   	AddPgpKeyzClient.AddPgpKeyX   s&    xx%H A**84??A A ..))
@
G
Gmm   A**84??A AL 	X))00''%7 	( 	
 {{55<<XF .**+;<GG. . .r   c           	      L   | j                  |      }t        d | j                  |      j                  D              |3|v r/t	        j
                  dj                  ||j                              | j                  |      j                  j                  | j                  j                  || j                  j                  ||      |             | j                  j                  j                  |      }t        fd| j                  |      j                  D              S )a  Add a key to an attestor.

    Args:
      attestor_ref: ResourceSpec, The attestor to be updated.
      pkix_pubkey_content: The PEM-encoded PKIX public key.
      pkix_sig_algorithm: The PKIX public key signature algorithm.
      id_override: If provided, the key ID to use instead of the API-generated
          one.
      comment: The comment on the public key.

    Returns:
      The added public key.

    Raises:
      AlreadyExistsError: If a public key with the same key content was found on
          the attestor.
    c              3   4   K   | ]  }|j                     y wr   rP   rJ   s     r   rM   z$Client.AddPkixKey.<locals>.<genexpr>   rR   rN   z8Public key with ID [{}] already present on attestor [{}])publicKeyPemsignatureAlgorithm)rQ   pkixPublicKeyrS   c              3   >   K   | ]  }|j                   vr|  y wr   rP   rU   s     r   rM   z$Client.AddPkixKey.<locals>.<genexpr>   rW   rX   )r   rY   r:   rZ   r	   r[   r\   r   r]   r   r^   PkixPublicKeyr   r   r_   r`   )	r   r   pkix_pubkey_contentpkix_sig_algorithmid_overriderS   r9   rc   rV   s	           @r   
AddPkixKeyzClient.AddPkixKey   s   & xx%H A**84??A AL ;,#>))
D
K
K8==*+ + 	X))00''--550#5 6 7  	( 	 {{55<<XF .**+;<GG. . .r   c                    | j                  |      }t        d | j                  |      j                  D              }||vr.t	        j
                  dj                  |j                              | j                  |      j                  D cg c]  }|j                  |k7  r| c}| j                  |      _        | j                  j                  j                  |       yc c}w )zRemove a key on an attestor.

    Args:
      attestor_ref: ResourceSpec, The attestor to be updated.
      pubkey_id: The ID of the key to remove.

    Raises:
      NotFoundError: If an expected public key could not be located by ID.
    c              3   4   K   | ]  }|j                     y wr   rP   rJ   s     r   rM   z#Client.RemoveKey.<locals>.<genexpr>   rR   rN   -No matching public key found on attestor [{}]N)r   rY   r:   rZ   r	   NotFoundErrorr\   r   rQ   r   r   r_   )r   r   	pubkey_idr9   rV   rL   s         r   	RemoveKeyzClient.RemoveKey   s     xx%H A**84??A AL $$$
9
@
@mm  **84??-'?J==I% 	?-'DX)
 	KK""))(3-'s   C!c                    | j                  |      }| j                  |      j                  D cg c]  }|j                  k(  r| }}|s.t	        j
                  dj                  |j                              t        |      dkD  r.t	        j                  dj                  |j                              |d   }|(|j                  st	        j                  d      ||_
        |||_        | j                  j                  j                  |      }	t!        fd| j                  |	      j                  D              S c c}w )a$  Update a key on an attestor.

    Args:
      attestor_ref: ResourceSpec, The attestor to be updated.
      pubkey_id: The ID of the key to update.
      pgp_pubkey_content: The contents of the public key file.
      comment: The comment on the public key.

    Returns:
      The updated public key.

    Raises:
      NotFoundError: If an expected public key could not be located by ID.
      InvalidStateError: If multiple public keys matched the provided ID.
      InvalidArgumentError: If a non-PGP key is updated with pgp_pubkey_content.
    rr      z4Multiple matching public keys found on attestor [{}]r   z7Cannot update a non-PGP PublicKey with a PGP public keyc              3   @   K   | ]  }|j                   k(  r|  y wr   rP   )rK   rL   rt   s     r   rM   z#Client.UpdateKey.<locals>.<genexpr>   s%      'GJ==I% 	Gs   )r   r:   rZ   rQ   r	   rs   r\   r   lenInvalidStateErrorrI   InvalidArgumentErrorrS   r   r   r_   r`   )
r   r   rt   ra   rS   r9   rL   existing_keysexisting_keyrc   s
     `       r   	UpdateKeyzClient.UpdateKey   sK   $ xx%H **84??'?J==I% 	?  '
 $$
9
@
@mm  =A((
@
G
Gmm  !#L%22--EG 	G.@l+$l{{55<<XF '**+;<GG' ' '3's   E c                     | j                  |      }|||_        | j                  j                  j	                  |      S )zUpdate an attestor.

    Args:
      attestor_ref: ResourceSpec, The attestor to be updated.
      description: string, If provided, the new attestor description.

    Returns:
      The updated attestor.
    )r   r<   r   r   r_   )r   r   r<   r9   s       r   r_   zClient.Update   s;     xx%H(h;;))00::r   c                     | j                   j                  |j                               }| j                  j                  j                  |       y)zDelete the specified attestor.r   N)r   1BinaryauthorizationProjectsAttestorsDeleteRequestr   r   r   Delete)r   r   reqs      r   r   zClient.Delete  sB    
--
I
I&&( J C 	KK""))#.r   c                 |   | j                   j                  j                  }t        j                  }|j
                  j                  |j                  |j                  j                  |j                  |j                  j                  |j                  |j                  j                  |j                  |j                  j                  |j                  |j                  j                  |j                  |j                   j                  |j                   |j"                  j                  |j"                  |j$                  j                  |j&                  |j(                  j                  |j*                  i
}	 ||j                     S # t,        $ r/ t/        j0                  dj3                  |j                              w xY w)zDConvert a KMS SignatureAlgorithm into a Binauthz SignatureAlgorithm.z3Unsupported PkixPublicKey signature algorithm: "{}")r   rk   !SignatureAlgorithmValueValuesEnumkms_mapsALGORITHM_ENUMRSA_SIGN_PSS_2048_SHA256r   RSA_PSS_2048_SHA256RSA_SIGN_PSS_3072_SHA256RSA_PSS_3072_SHA256RSA_SIGN_PSS_4096_SHA256RSA_PSS_4096_SHA256RSA_SIGN_PSS_4096_SHA512RSA_PSS_4096_SHA512RSA_SIGN_PKCS1_2048_SHA256RSA_SIGN_PKCS1_3072_SHA256RSA_SIGN_PKCS1_4096_SHA256RSA_SIGN_PKCS1_4096_SHA512EC_SIGN_P256_SHA256ECDSA_P256_SHA256EC_SIGN_P384_SHA384ECDSA_P384_SHA384KeyErrorr	   r{   r\   )r   kms_algorithmbinauthz_enumkms_enumalg_maps        r    ConvertFromKmsSignatureAlgorithmz'Client.ConvertFromKmsSignatureAlgorithm  sh   MM//QQM&&H))..--))..--))..--))..--++0044++0044++0044++0044$$))++$$))++)G,#]''(( #++
?
F
F  "# ##s   4F 8F;r   )NN)__name__
__module____qualname____doc__r   r   r-   r3   r7   r:   rB   rd   ro   ru   r~   r_   r   r   r?   r   r   r   r      sZ    0#
	)0
$
+ '.T ,0*.X48 GK0'd;"/#r   r   N)r   
__future__r   r   r   apitools.base.pyr   )googlecloudsdk.api_lib.container.binauthzr   r   -googlecloudsdk.command_lib.container.binauthzr	   googlecloudsdk.command_lib.kmsr
   r   objectr   r?   r   r   <module>r      s0     2 &  ' ' : : D ;U#V U#r   