
                             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
Z
d	Zd
ZeZ G d de      Zd Zd Zy)z7Helper functions for interacting with the cloudkms API.    )absolute_import)division)unicode_literals)apis)
get_digest)mapsNcloudkmsv1c                   $    e Zd ZdZddZd Zd Zy)Clientz2A client to access cloudkms for binauthz purposes.Nc                     |t         }t        j                  t        |      | _        t        j
                  t        |      | _        y)zlCreates a Cloud KMS client.

    Args:
      api_version: If provided, the cloudkms API version to use.
    N)DEFAULT_VERSIONr   GetClientInstanceAPI_NAMEclientGetMessagesModulemessages)selfapi_versions     4lib/googlecloudsdk/api_lib/container/binauthz/kms.py__init__zClient.__init__%   s7     #k((;?DK**8[ADM    c                     | j                   j                  |      }| j                  j                  j	                  |      S )z4Retrieves the public key for given CryptoKeyVersion.)name)r   OCloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsGetPublicKeyRequestr   8projects_locations_keyRings_cryptoKeys_cryptoKeyVersionsGetPublicKey)r   key_refreqs      r   r   zClient.GetPublicKey1   s=    
--
g
g h C 	LLSr   c                    t        j                  |t        j                  |            }| j                  j                  || j                  j                  |            }| j                  j                  j                  |      S )a  Sign a string payload with an asymmetric KMS CryptoKeyVersion.

    Args:
      key_ref: The CryptoKeyVersion relative resource name to sign with.
      digest_algorithm: The name of the digest algorithm to use in the signing
          operation. May be one of 'sha256', 'sha384', 'sha512'.
      plaintext: The plaintext bytes to sign.

    Returns:
      An AsymmetricSignResponse.
    )digest)r   asymmetricSignRequest)
r   GetDigestOfFilesixBytesIOr   QCloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsAsymmetricSignRequestAsymmetricSignRequestr   r   AsymmetricSign)r   r   digest_algorithm	plaintextr!   r   s         r   r(   zClient.AsymmetricSign9   sx     ''#++i02F
--
i
i"mmAA B  j C
 	LLsr   )N)__name__
__module____qualname____doc__r   r   r(    r   r   r   r   "   s    :
Br   r   c                 H    | j                         j                  dd      d   S )zReturns the URI used as the default for KMS keys.

  This should look something like '//cloudkms.googleapis.com/v1/...'

  Args:
    key_ref: A CryptoKeyVersion Resource.

  Returns:
    The string URI.
  :   )SelfLinksplit)r   s    r   	GetKeyUrir5   P   s%     
				!	!#q	)!	,,r   c                 n    t         j                  D ]"  }|| j                  j                         v s |c S  y)zFReturns the digest name associated with the given CryptoKey Algorithm.N)r   DIGESTSr   lower)key_algorithmdigest_names     r   GetAlgorithmDigestTyper;   ^   s-    \\km((..00 "r   )r.   
__future__r   r   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.command_lib.kmsr   r   r$   r   V1r   objectr   r5   r;   r/   r   r   <module>rA      sE     > &  ' , 5 / 
	+V +\-r   