o
    ɶd/                     @   s   d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZmZmZ d dlmZmZmZ eejG dd	 d	eZG d
d deZeejG dd deZG dd deZG dd deZdS )    )sixN)
algorithms)ServiceError)KeyShapeGenerateKeyDetailsDecryptDataDetails)KmsCryptoClientKmsManagementClientKmsVaultClient)convert_to_strverify_crc32_checksumraise_runtime_error_fromc                   @   s,   e Zd ZdZejdd Zejdd ZdS )MasterKeyProviderzn
    An abstract base class defining methods to vend MasterKeys
    for use in encryption and decryption.
    c                 C      dS )zv
        Returns the primary master key for this MasterKeyProvider.

        :rtype: oci.encryption.MasterKey
        N selfr   r   @usr/lib/python3.10/site-packages/oci/encryption/key_providers.pyget_primary_master_key      z(MasterKeyProvider.get_primary_master_keyc                 K   r   )zz
        Returns a specific master key based on the arguments provided.

        :rtype: oci.encryption.MasterKey
        Nr   )r   kwargsr   r   r   get_master_key$   r   z MasterKeyProvider.get_master_keyN)__name__
__module____qualname____doc__abcabstractmethodr   r   r   r   r   r   r      s    
r   c                   @   s&   e Zd ZdddZdd Zdd ZdS )	KMSMasterKeyProviderNc                 K   s^   |durt |dkrtdd| _|r|d | _|d| _|| _| js+| js-tddS dS )ak  
        :param dict config: (required)
            An OCI config dict used to create underlying clients to talk to OCI KMS.
            Note, the 'region' in this config must match the region that the key / vault
            exist in otherwise they will not be found.

        :param list[KMSMasterKey] kms_master_keys: (optional)
            A list of KMSMasterKeys. Currently a max of 1 master key is supported.
            For decryption, you can use a KMSMasterKeyProvder with no master keys.

        :param signer: (optional)
            The signer to use when signing requests made by the service client. The default is to use a :py:class:`~oci.signer.Signer` based on the values
            provided in the config parameter.

            One use case for this parameter is for `Instance Principals authentication <https://docs.cloud.oracle.com/Content/Identity/Tasks/callingservicesfrominstances.htm>`__
            by passing an instance of :py:class:`~oci.auth.signers.InstancePrincipalsSecurityTokenSigner` as the value for this keyword argument
        :type signer: :py:class:`~oci.signer.AbstractBaseSigner`
        N   zGOnly one KMS master key is currently supported for KMSMasterKeyProviderr   signer+Either a config or signer must be passed in)len
ValueErrorprimary_master_keygetr    config)r   r&   Zkms_master_keysr   r   r   r   __init__/   s   
zKMSMasterKeyProvider.__init__c                 C      | j S N)r$   r   r   r   r   r   R   s   z+KMSMasterKeyProvider.get_primary_master_keyc                 K   s^   | ds	td| jr| j | dkr| jS | j}| jr$| j|d< tdd|i|}|S )a8  
        Get a KMSMasterKey based on the provided parameters.

        If this key provider already has the KMSMasterKey that was requested, it will return it.
        If it does not have a representation of the KMSMasterKey locally, it will attempt to
        retrieve it from KMS.

        :param str master_key_id: (required)
            The OCID of this master key

        :param str vault_id: (optional)
            The OCID of the vault this master key resides in

        :param str region: (optional)
            The region this master key resides in
        master_key_idz/keyword argument master_key_id must not be Noner    r&   Nr   )r%   r#   r$   get_identifierr&   r    KMSMasterKey)r   r   Zmaster_key_configZkms_master_keyr   r   r   r   U   s   

z#KMSMasterKeyProvider.get_master_keyr)   )r   r   r   r'   r   r   r   r   r   r   r   .   s    
#r   c                   @   s:   e Zd ZdZejdd Zejdd Zejdd ZdS )		MasterKeyzz
    An abstract base class representing a MasterKey resource to be used in
    encryption and decryption operations.
    c                 C   r   )a  
        Generates a data encryption key (DEK) based on the algorithm provided using
        this MasterKey.  The returned DataEncryptionKey includes a copy of the
        DEK encrypted under this MasterKey.

        :param oci.encryption.algorithms.Algorithm algorithm: (required)
            The algorithm the key will be used for.

        :rtype: oci.encryption.key_providers.DataEncryptionKey
        Nr   )r   	algorithmr   r   r   generate_data_encryption_key}   s   z&MasterKey.generate_data_encryption_keyc                 C   r   )
        Decrypts and returns bytes that were encrypted under this master key.

        :param bytes bytes_to_decrypt: (required)
            The bytes to decrypt using this MasterKey.

        :rtype: bytes
        Nr   )r   bytes_to_decryptr   r   r   decrypt   s   
zMasterKey.decryptc                 C   r   )zP
        Returns an identifier for this MasterKey.

        :rtype: str
        Nr   r   r   r   r   r+      r   zMasterKey.get_identifierN)	r   r   r   r   r   r   r/   r2   r+   r   r   r   r   r-   w   s    

r-   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
r,   c           	   
   K   s  |s| dstdd| _| dr| d| _nd|v r$|d | _n| dr0| dj| _t|fi |}|j| j z||j}W n tyf } zdj	|| jd}t
|| W Y d}~nd}~ww t|fd|ji|| _t|fd|ji|| _|| _|j| _dS )a  
        Represents a MasterKey contained in the OCI Key Management Service.

        :param dict config: (required)
            An OCI config dict used to create underlying clients to talk to OCI KMS.
            Note, the 'region' in this config must match the region that the key / vault
            exist in otherwise they will not be found.

        :param str master_key_id: (required)
            The OCID of the KMS master key

        :param str vault_id: (required)
            The OCID of the vault containing the master key

        :param signer: (optional)
            The signer to use when signing requests made by the service client. The default is to use a :py:class:`~oci.signer.Signer` based on the values
            provided in the config parameter.

            One use case for this parameter is for `Instance Principals authentication <https://docs.cloud.oracle.com/Content/Identity/Tasks/callingservicesfrominstances.htm>`__
            by passing an instance of :py:class:`~oci.auth.signers.InstancePrincipalsSecurityTokenSigner` as the value for this keyword argument
        :type signer: :py:class:`~oci.signer.AbstractBaseSigner`

        :param str region: (optional)
            The region this master key resides in
        r    r!   NregionzFFailed to access vaultId: {vault_id} while targeting region: {region}.)vault_idr3   Zservice_endpoint)r%   r#   r3   r
   Zbase_clientZ
set_regionZ	get_vaultdatar   formatr   r	   Zmanagement_endpointZkms_management_clientr   Zcrypto_endpointkms_crypto_clientr*   idr4   )	r   r&   r*   r4   r   Zkms_vault_clientZvaultservice_errormessager   r   r   r'      sL   

zKMSMasterKey.__init__c           	   
   C   s   t  }|jjtjjkrtdt j|_|j|_t	 }d|_
| j|_||_z	| j|j}W n  tyM } zdj| j| jd}t|| W Y d }~nd }~ww t|j}t|j}t|||jdS )NzNOnly AES is currently supported for client side encryption with KMS master keyTzlFailed to generate data encryption key using masterKeyId: {master_key_id} while targeting vault: {vault_id}.r*   r4   )plaintext_key_bytesencrypted_key_bytesplaintext_key_checksum)r   r.   namer   ZAESr#   ZALGORITHM_AESZkey_lenlengthr   Zinclude_plaintext_keyr*   key_idZ	key_shaper7   r/   r5   r   r6   r4   r   base64	b64decode	plaintext
ciphertextDataEncryptionKeyplaintext_checksum)	r   r.   Zdek_key_shapeZgenerate_key_detailsZgenerated_keyr9   r:   Zdek_plaintext_bytesZdek_ciphertext_bytesr   r   r   r/      s@   z)KMSMasterKey.generate_data_encryption_keyc              
   C   s   t  }tt||_| j|_z	| j|j	}W n  t
y8 } zdj| j| jd}t|| W Y d}~nd}~ww tt|j|j |jS )r0   zkFailed to decrypt data encryption key using masterKeyId: {master_key_id} while targeting vault: {vault_id}.r;   N)r   r   rB   	b64encoderE   r*   rA   r7   r2   r5   r   r6   r4   r   r   rC   rD   rG   )r   r1   Zdecrypt_data_detailsZdecrypted_datar9   r:   r   r   r   r2     s(   	
zKMSMasterKey.decryptc                 C   r(   )zm
        Returns the OCID of this master key in the OCI Key Management Service.

        :rtype: str
        )r*   r   r   r   r   r+   '  s   zKMSMasterKey.get_identifierN)r   r   r   r'   r/   r2   r+   r   r   r   r   r,      s
    ?%!r,   c                   @   s   e Zd ZdZ	dddZdS )rF   zP
    Represents a data encryption key used to encrypt and decrypt payloads.
    Nc                 C   s*   || _ || _|| _| jrt|| dS dS )aX  
        :param bytes plaintext_key_bytes:
            The bytes of the data encryption key in plaintext

        :param bytes encrypted_key_bytes:
            The bytes of the data encrypted key encrypted under a master key

        :param str plaintext_key_checksum:
            The crc32 checsum of the plaintext data encryption key
        N)r<   r=   r>   r   )r   r<   r=   r>   r   r   r   r'   4  s   zDataEncryptionKey.__init__r)   )r   r   r   r   r'   r   r   r   r   rF   0  s    rF   )Zoci._vendorr   r   rB   Z&cryptography.hazmat.primitives.ciphersr   Zoci.exceptionsr   Zoci.key_management.modelsr   r   r   Zoci.key_managementr   r	   r
   Zoci.encryption.internal.utilsr   r   r   Zadd_metaclassABCMetaobjectr   r   r-   r,   rF   r   r   r   r   <module>   s    

I) 