o
    e}E                     @   s   d Z ddlZddlZddlZddlZddlZddl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eeZG d	d
 d
eZdS )z.
This module provides a client class for KMS.
    N)BceBaseClient)required)bce_v1_signer)bce_http_client)handler)http_methodsc                   @   s  e Zd ZdZd7ddZd7ddZ		d8ddZeee	fee	fee	fe
d		d9ddZee
dd:ddZee	efe	efdd7ddZee	efe	efdd7ddZee	efe	efdd;ddZee	efe
dd7ddZee	efdd7d d!Zee	efdd7d"d#Zee	efe
d$d7d%d&Zee	efdd7d'd(Zee	efdd7d)d*Zee	efd	+	d<d-d.Zee	efe	efe	efe	efd/	d=d0d1Zee	efe	efe	efe	efe	efed2	d=d3d4Zee	efe	efe	efe	efe	efed2	d=d5d6ZdS )>	KmsClientz
    sdk client
    Nc                 C   s   t | | d S N)r   __init__)selfconfig r   PD:\Projects\ConvertPro\env\Lib\site-packages\baidubce/services/kms/kms_client.pyr
   &   s   zKmsClient.__init__c                 C   s(   |d u r| j S t| j }|| |S r	   )r   copyZmerge_non_none_values)r   r   Z
new_configr   r   r   _merge_config)   s
   
zKmsClient._merge_configc              
   C   sJ   |  |}|d u rtj}|d u rddd}t|tjtj|g|||||S )Ns   */*s   application/json;charset=utf-8)s   Accepts   Content-Type)r   r   Z
parse_jsonr   send_requestr   signZparse_error)r   Zhttp_methodpathbodyheadersparamsr   Zbody_parserr   r   r   _send_request1   s   

zKmsClient._send_request)protectedBykeySpecoriginrotateCycleENCRYPT_DECRYPTr   c                 C   sd   d}i }	d|	d< i }
|r||
d< ||
d< ||
d< ||
d< ||
d< ||
d	< | j tj|t|
|	|d
S )a.  
        create a master key with the specified options.
        :type description: string
        :param description: a description about the master key

        :type protectedBy: constants.ProtectedBy
        :param protectedBy: the protect level about the master key, you can choose HSM or SOFTWARE

        :type keySpec: constants.KeySpec
        :param keySpec:  key specification about the master key. now you can choose the BAIDU_AES_256, 
        AES_128, AES_256, RSA_1024, RSA_2048, RSA_4096

        :type keyUsage: string
        :param keyUsage:  default "ENCRYPT_DECRYPT"

        :type origin: constants.Origin
        :param origin:  origin of the master key. you can choose BAIDU_KMS or EXTERNAL

        :type rotateCycle: int
        :param rotateCycle: rotateCycle of the master key.
           /s	   CreateKeyactiondescriptionr   r   r   keyUsager   r   r   r   r   POSTjsondumps)r   r   r   r   r   r    r   r   r   r   r   r   r   r   create_masterKey?   s   zKmsClient.create_masterKey)limit c                 C   @   d}i }d|d< i }||d< ||d< | j tj|t|||dS )z
        list your masterkey 
        :type limit: int
        :param limit: the number of masterKey you want list

        :type marker: string
        :param marker: the marker keyid , kms will search from the marker, default ""
        r   s   ListKeysr   r'   markerr!   r"   )r   r'   r*   r   r   r   r   r   r   r   list_masterKeye   s   
zKmsClient.list_masterKey)keyId	plaintextc                 C   f   d}i }d|d< i }||d< ||d< zt | W n ty$   tdw | jtj|t|||dS )z
        encrypt the plaintext
        :type keyId: string
        :param keyId: indicate kms will use which masterkey to encrypt
        
        :type plaintext: string
        :param plaintext: the plaintext need encrypted by kms
        r   s   Encryptr   r,   r-   please input base64 stringr!   base64	b64decode	TypeErrorr   r   r#   r$   r%   )r   r,   r-   r   r   r   r   r   r   r   encryptx      
zKmsClient.encrypt)r,   
ciphertextc                 C   r.   )z
        decrypt the ciphertext
        :type keyId: string
        :param keyId: indicate kms will use which masterkey to decrypt

        :type ciphertext: string
        :param ciphertext:  the ciphertext need decrypted by kms
        r   s   Decryptr   r,   r6   r/   r!   r0   )r   r,   r6   r   r   r   r   r   r   r   decrypt   r5   zKmsClient.decrypt)r,   r   c                 C   s`   d}i }d|d< i }||d< |dkr|dkrt d||d< ||d	< | jtj|t|||d
S )aA  
        generate a data key by master key
        :type keyId: string
        :param keyId: indicate kms will use which masterkey to generate data key

        :type keySpec: string
        :param keySpec: AES_128 or AES_256

        :type numberOfBytes: int
        :param numberOfBytes: The length of data key
        r   s   GenerateDataKeyr   r,   ZAES_128ZAES_256z only support AES_128 and AES_256r   numberOfBytesr!   
ValueErrorr   r   r#   r$   r%   )r   r,   r   r9   r   r   r   r   r   r   r   generate_dataKey   s   zKmsClient.generate_dataKey)r,   r   c                 C   r)   )z
        update your master key rptation
        :type keyId: string
        :type rotateCycle: int
        :param keyId: the keyId of masterkey will be enable
        :param rotateCycle: the rotatecycle of masterkey
        r   s   EnableRotationr   r,   r   r!   r"   )r   r,   r   r   r   r   r   r   r   r   updaterotation_masterKey   s   	z"KmsClient.updaterotation_masterKey)r,   c                 C   8   d}i }d|d< i }||d< | j tj|t|||dS )z
        enable your master key
        :type keyId: string
        :param keyId: the keyId of masterkey will be enable
        r   s	   EnableKeyr   r,   r!   r"   r   r,   r   r   r   r   r   r   r   enable_masterKey      zKmsClient.enable_masterKeyc                 C   r>   )z
        disable your master key
        :type keyId: string
        :param keyId: the keyId of masterkey will be diable
        r   s
   DisableKeyr   r,   r!   r"   r?   r   r   r   disable_masterKey   rA   zKmsClient.disable_masterKey)r,   pendingWindowInDaysc                 C   sX   d}i }d|d< i }||d< |dks|dk rt d||d< | jtj|t|||d	S )
a
  
        schedule delete master key
        :type keyId: string
        :param keyId: the keyId of masterkey will be deleted

        :type pendingWindowInDays: int
        :pram pendingWindowInDays: kms will wait pendingWindowInDays day then delete the key
        r   s   ScheduleKeyDeletionr   r,         z-please input pendingWindowInDays >=7 and <=30rC   r!   r:   )r   r,   rC   r   r   r   r   r   r   r   scheduleDelete_masterKey   s   
z"KmsClient.scheduleDelete_masterKeyc                 C   r>   )z
        cancel delete master key
        :type keyId: string
        :param keyId: the keyId of masterkey will cancel delete
        r   s   CancelKeyDeletionr   r,   r!   r"   r?   r   r   r   cancelDelete_masterKey  rA   z KmsClient.cancelDelete_masterKeyc                 C   r>   )zs
        descript the master key
        :type keyId: string
        :param keyId: the keyId of masterkey 
        r   s   DescribeKeyr   r,   r!   r"   r?   r   r   r   describe_masterKey  rA   zKmsClient.describe_masterKeyRSAES_PKCS1_V1_5RSA_2048c           	      C   s   d}i }d|d< i }||d< |dkrt d||d< |dkr"t d	||d
< |dkr6|dkr6|dkr6td||d< | jtj|t|||dS )aY  
        get parameters for import
        :type keyId: string
        :param keyId: the keyId of masterkey

        :type wrappingAlgorithm: string
        :param wrappingAlgorithm: the algorithm for user encrypt local key

        :type wrappingKeySpec:string
        :param wrappingKeySpec: the pubkey spec for user encrypt local key
        r   s   GetParametersForImportr   r,   rI   zonly support RSAES_PKCS1_V1_5wrappingAlgorithmrJ   zonly support RSA_2048wrappingKeySpecZRAW_HEXBASE64ZPEMz%only support RAW_HEX or BASE64 or PEMpublicKeyEncodingr!   )r3   r;   r   r   r#   r$   r%   )	r   r,   rN   rK   rL   r   r   r   r   r   r   r   get_parameters_for_import"  s"   z#KmsClient.get_parameters_for_import)r,   importTokenencryptedKeyr   c           
      C   sX   d}i }d|d< i }	||	d< ||	d< ||	d< ||	d< ||	d< | j tj|t|	||d	S )
a  
        import symmetric key
        :type keyId: string
        :param keyId: the keyId of masterkey

        :type importToken: string
        :param importToken: token from import parameter

        :type encryptedKey: string
        :param encryptedKey: the symmetric key encrypted by pubkey

        :type keySpec: string
        :param keySpec: the import key spec

        :type keyUsage: string
        :param keyUsage: default "ENCRYPT_DECRYPT"
        r   s	   ImportKeyr   r,   rP   rQ   r   r    r!   r"   )
r   r,   rP   rQ   r   r    r   r   r   r   r   r   r   import_symmetricMasterKeyA  s   z#KmsClient.import_symmetricMasterKey)r,   rP   asymmetricKeySpecasymmetricKeyUsageencryptedKeyEncryptionKeyZasymmetricKeyc                 K   s\  d}i }	d|	d< i }
||
d< ||
d< ||
d< ||
d< ||
d< i |
d	< |d
 du r,t d|d
 |
d	 d
< |d du r>t d|d |
d	 d< |d du rPt d|d |
d	 d< |d du rbt d|d |
d	 d< |d du rtt d|d |
d	 d< |d du rt d|d |
d	 d< |d du rt d|d |
d	 d< | jtj|t|
|	|dS )a2  
        import asymmetric key
        :type keyId: string
        :param keyId: the keyId of masterkey

        :type importToken: string
        :param importToken: token from import parameter

        :type asymmetricKeySpec: string
        :param asymmetricKeySpec: the import key spec

        :type encryptedKeyEncryptionKey: string
        :param encryptedKeyEncryptionKey: EncryptionKey

        :type asymmetricKey: **args
        :param asymmetricKey: include publicKeyDer encryptedD encryptedP encryptedQ encryptedDp encryptedDq encryptedQinv
        r      ImportAsymmetricKeyr   r,   rP   rS   rT   rU   ZencryptedRsaKeypublicKeyDerN%arg "publicKeyDer" should not be NoneZ
encryptedDz#arg "encryptedD" should not be NoneZ
encryptedPz#arg "encryptedP" should not be NoneZ
encryptedQz#arg "encryptedQ" should not be NoneZencryptedDpz$arg "encryptedDp" should not be NoneZencryptedDqz$arg "encryptedDq" should not be NoneZencryptedQinvz&arg "encryptedQinv" should not be Noner!   r:   r   r,   rP   rS   rU   rT   r   kwargsr   r   r   r   r   r   import_asymmetricMasterKeya  sD   z$KmsClient.import_asymmetricMasterKeyc                 K   s   d}i }	d|	d< i }
||
d< ||
d< ||
d< ||
d< ||
d< i |
d	< |d
 du r,t d|d
 |
d	 d
< |d du r>t d|d |
d	 d< | jtj|t|
|	|dS )a  
        import asymmetric key
        :type keyId: string
        :param keyId: the keyId of masterkey

        :type importToken: string
        :param importToken: token from import parameter

        :type asymmetricKeySpec: string
        :param asymmetricKeySpec: the import key spec

        :type encryptedKeyEncryptionKey: string
        :param encryptedKeyEncryptionKey: EncryptionKey

        :type asymmetricKey: **args
        :param asymmetricKey: include publicKeyDer encryptedPrivateKey
        r   rV   r   r,   rP   rS   rT   rU   ZencryptedSm2KeyrW   NrX   ZencryptedPrivateKeyz,arg "encryptedPrivateKey" should not be Noner!   r:   rY   r   r   r   import_asymmetricSM2MasterKey  s&   z'KmsClient.import_asymmetricSM2MasterKeyr	   )NNNNN)r   r   N)r(   N)r8   N)rI   rJ   N)r   N)__name__
__module____qualname____doc__r
   r   r   r   bytesstrintr&   r+   r4   r7   r<   r=   r@   rB   rF   rG   rH   rO   rR   objectr[   r\   r   r   r   r   r   "   sn    

	
%5r   )r`   r   r$   loggingrandomstringuuidZbaidubce.bce_base_clientr   Zbaidubce.utilsr   Zbaidubce.authr   Zbaidubce.httpr   r   r   r1   	getLoggerr]   Z_loggerr   r   r   r   r   <module>   s    
