U
    -eO                     @   sh  d dl Z d dlmZ d dlZz"d dlmZ d dlm  m	Z
 W n ek
rZ   dZdZ
Y nX d dlmZmZ dZdZdZdZdZe jje jjgZe jd	d
dd Ze jd	d
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z dd Z!dd  Z"d!d" Z#d#d$ Z$d%d& Z%d'd( Z&d)d* Z'd+d, Z(e jj)d-d.d/d0 Z*e jj)d1d.d2d3 Z+d4d5 Z,dS )6    N)	timedelta)InMemoryKmsClientverify_file_encryptedzencrypted_table.in_mem.parquets   0123456789112345
footer_keys   1234567890123450Zcol_keymodule)scopec               	   C   s<   t jt dddgt dddgt ddd	gd
} | S )N         abcxyz)r   r   r   )paTableZfrom_pydictarray)
data_table r   f/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/pyarrow/tests/parquet/test_encryption.pyr   0   s    r   c                  C   s   t jttddgid} | S )Nr   r   )r   column_keys)peEncryptionConfigurationFOOTER_KEY_NAMECOL_KEY_NAME)basic_encryption_configr   r   r   r   :   s     r   c           	      C   s   | t  }tjttddgidtdddd}tjttdtt	did	}d
d }t
|}t||||| t| tjtddd}t||||}||stdS )zDWrite an encrypted parquet, verify it's encrypted, and then read it.r   r   
AES_GCM_V1      @minutes   r   r   encryption_algorithmcache_lifetimedata_key_length_bitsUTF-8custom_kms_confc                 S   s   t | S Nr   Zkms_connection_configurationr   r   r   kms_factory[   s    z6test_encrypted_parquet_write_read.<locals>.kms_factoryr$   N)PARQUET_NAMEr   r   r   r   r   KmsConnectionConfig
FOOTER_KEYdecodeCOL_KEYCryptoFactorywrite_encrypted_parquetr   DecryptionConfigurationread_encrypted_parquetequalsAssertionError)	tempdirr   pathencryption_configkms_connection_configr,   crypto_factorydecryption_configresult_tabler   r   r   !test_encrypted_parquet_write_readD   sD     	  
    r@   c              	   C   sD   | ||}|d k	sttj| |j|d}|| W 5 Q R X d S )N)Zencryption_properties)file_encryption_propertiesr8   pqZParquetWriterschemaZwrite_table)r:   tabler;   r<   r=   rA   writerr   r   r   r4   l   s      r4   c                 C   sn   | ||}|d k	sttj| |d}|jdks4ttj| |d}t|jdksTttj| |d}|j	ddS )NZdecryption_propertiesr
   TZuse_threads)
file_decryption_propertiesr8   rB   read_metadataZnum_columnsread_schemalennamesParquetFileread)r:   r>   r<   r=   rH   metarC   resultr   r   r   r6   w   s(        r6   c           	   	   C   s   | t  }tjttddgidtdddd}tjttdtt	did	}d
d }t
|}t||||| t| tjtt	dttdid	}tjtddd}tjtdd t|||| W 5 Q R X dS )zYWrite an encrypted parquet, verify it's encrypted,
    and then read it using wrong keys.r   r   r   r   r   r!   r"   r&   r'   c                 S   s   t | S r)   r*   r+   r   r   r   r,      s    z@test_encrypted_parquet_write_read_wrong_key.<locals>.kms_factoryr-   zIncorrect master key usedmatchN)r.   r   r   r   r   r   r/   r0   r1   r2   r3   r4   r   r5   pytestraises
ValueErrorr6   )	r9   r   r:   r;   r<   r,   r=   Zwrong_kms_connection_configr>   r   r   r   +test_encrypted_parquet_write_read_wrong_key   sR     	  
     rV   c              	   C   s:   t | | tjtdd t| t   W 5 Q R X dS )zmWrite an encrypted parquet, verify it's encrypted,
    but then try to read it without decryption properties.no decryptionrQ   N)r@   rS   rT   IOErrorrB   rM   r.   rN   r9   r   r   r   r   0test_encrypted_parquet_read_no_decryption_config   s    
rZ   c              	   C   s6   t | | tjtdd t| t  W 5 Q R X dS )zwWrite an encrypted parquet, verify it's encrypted,
    but then try to read its metadata without decryption properties.rW   rQ   N)r@   rS   rT   rX   rB   rI   r.   rY   r   r   r   9test_encrypted_parquet_read_metadata_no_decryption_config   s    
r[   c              	   C   s6   t | | tjtdd t| t  W 5 Q R X dS )zuWrite an encrypted parquet, verify it's encrypted,
    but then try to read its schema without decryption properties.rW   rQ   N)r@   rS   rT   rX   rB   rJ   r.   rY   r   r   r   7test_encrypted_parquet_read_schema_no_decryption_config   s    
r\   c              	   C   st   | d }t jtd}t jttdttdid}dd }t |}t	j
tdd t||||| W 5 Q R X d	S )
zMWrite an encrypted parquet, but give only footer key,
    without column key.z)encrypted_table_no_col_key.in_mem.parquetr   r&   r'   c                 S   s   t | S r)   r*   r+   r   r   r   r,      s    z<test_encrypted_parquet_write_no_col_key.<locals>.kms_factoryz4Either column_keys or uniform_encryption must be setrQ   N)r   r   r   r/   r0   r1   r   r2   r3   rS   rT   OSErrorr4   r9   r   r:   r;   r<   r,   r=   r   r   r   'test_encrypted_parquet_write_no_col_key   s(      
 r`   c              	   C   sT   | d }|}t  }dd }t |}tjtdd t||||| W 5 Q R X dS )<Write an encrypted parquet, but raise KeyError in KmsClient.(encrypted_table_kms_error.in_mem.parquetc                 S   s   t | S r)   r*   r+   r   r   r   r,      s    z;test_encrypted_parquet_write_kms_error.<locals>.kms_factoryr   rQ   N)r   r/   r3   rS   rT   KeyErrorr4   r9   r   r   r:   r;   r<   r,   r=   r   r   r   &test_encrypted_parquet_write_kms_error   s    
 re   c              	      sj   | d }|}t  }G dd dt j  fdd}t |}tjtdd t||||| W 5 Q R X dS )	ra   rb   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	zJtest_encrypted_parquet_write_kms_specific_error.<locals>.ThrowingKmsClientzVA KmsClient implementation that throws exception in
        wrap/unwrap calls
        c                 S   s   t j|  || _dS )z%Create an InMemoryKmsClient instance.N)r   	KmsClient__init__configselfrh   r   r   r   rg     s    zStest_encrypted_parquet_write_kms_specific_error.<locals>.ThrowingKmsClient.__init__c                 S   s   t dd S )NCannot Wrap KeyrU   rj   	key_bytesmaster_key_identifierr   r   r   wrap_key  s    zStest_encrypted_parquet_write_kms_specific_error.<locals>.ThrowingKmsClient.wrap_keyc                 S   s   t dd S )NzCannot Unwrap Keyrl   rj   Zwrapped_keyro   r   r   r   
unwrap_key  s    zUtest_encrypted_parquet_write_kms_specific_error.<locals>.ThrowingKmsClient.unwrap_keyN__name__
__module____qualname____doc__rg   rp   rr   r   r   r   r   ThrowingKmsClient  s   rx   c                    s    | S r)   r   r+   rx   r   r   r,     s    zDtest_encrypted_parquet_write_kms_specific_error.<locals>.kms_factoryrk   rQ   N)r   r/   rf   r3   rS   rT   rU   r4   rd   r   ry   r   /test_encrypted_parquet_write_kms_specific_error  s    
 rz   c              	   C   sT   | d }|}t  }dd }t |}tjtdd t||||| W 5 Q R X dS )z@Write an encrypted parquet, but raise ValueError in kms_factory.0encrypted_table_kms_factory_error.in_mem.parquetc                 S   s   t dd S )NCannot create KmsClientrl   r+   r   r   r   r,   3  s    zCtest_encrypted_parquet_write_kms_factory_error.<locals>.kms_factoryr|   rQ   N)r   r/   r3   rS   rT   rU   r4   rd   r   r   r   .test_encrypted_parquet_write_kms_factory_error*  s    
 r}   c              	      sb   | d }|}t  }G dd d  fdd}t |}tt t||||| W 5 Q R X dS )z_Write an encrypted parquet, but use wrong KMS client type
    that doesn't implement KmsClient.r{   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	zOtest_encrypted_parquet_write_kms_factory_type_error.<locals>.WrongTypeKmsClientz4This is not an implementation of KmsClient.
        c                 S   s   |j | _d S r)   )r(   Zmaster_keys_mapri   r   r   r   rg   L  s    zXtest_encrypted_parquet_write_kms_factory_type_error.<locals>.WrongTypeKmsClient.__init__c                 S   s   d S r)   r   rm   r   r   r   rp   O  s    zXtest_encrypted_parquet_write_kms_factory_type_error.<locals>.WrongTypeKmsClient.wrap_keyc                 S   s   d S r)   r   rq   r   r   r   rr   R  s    zZtest_encrypted_parquet_write_kms_factory_type_error.<locals>.WrongTypeKmsClient.unwrap_keyNrs   r   r   r   r   WrongTypeKmsClientH  s   r~   c                    s    | S r)   r   r+   r~   r   r   r,   U  s    zHtest_encrypted_parquet_write_kms_factory_type_error.<locals>.kms_factoryN)r   r/   r3   rS   rT   	TypeErrorr4   rd   r   r   r   3test_encrypted_parquet_write_kms_factory_type_error>  s    
 r   c               
   C   s   dd } t jttddgidddtdd	dd
d}| | t jtd}tddgi|_d|_d|_d|_tdd	|_	d|_
d
|_| | d S )Nc                 S   sv   t | jkstddg| jt ks$td| jks2t| js<t| jrFttdd| j	ksZt| j
rdtd| jksrtd S )Nr   r   AES_GCM_CTR_V1      $@r      )r   r   r8   r   r   r#   plaintext_footerdouble_wrappingr   r$   internal_key_materialr%   )r;   r   r   r   !validate_encryption_configuration`  s    


zZtest_encrypted_parquet_encryption_configuration.<locals>.validate_encryption_configurationr   r   r   TFr   r   r   )r   r   r#   r   r   r$   r   r%   r]   )r   r   r   r   r   r   r#   r   r   r$   r   r%   )r   r;   Zencryption_config_1r   r   r   /test_encrypted_parquet_encryption_configuration_  s.    


r   c                  C   sR   t jtddd} tdd| jks&tt  }tdd|_tdd|jksNtd S )Nr   r   r-   )r   r5   r   r$   r8   )r>   Zdecryption_config_1r   r   r   /test_encrypted_parquet_decryption_configuration  s    r   c                  C   sZ   dd } t jddddddd	}| | t  }d|_d|_d|_ddd|_| | d S )
Nc                 S   sB   d| j kstd| jkstd| jks*tddd| jks>td S )N	Instance1URL1MyTokenkey_material_1key_material_2key1key2)kms_instance_idr8   kms_instance_urlkey_access_tokenr(   )r<   r   r   r   validate_kms_connection_config  s    zPtest_encrypted_parquet_kms_configuration.<locals>.validate_kms_connection_configr   r   r   r   r   r   )r   r   r   r(   )r   r/   r   r   r   r(   )r   r<   Zkms_connection_config_1r   r   r   (test_encrypted_parquet_kms_configuration  s$    r   zNPlaintext footer - reading plaintext column subset reads encrypted columns too)reasonc                 C   sh   | t  }tjttddgiddd}tjttdttdid}dd	 }t	|}t
||||| d
S )zWrite an encrypted parquet, with plaintext footer
    and with single wrapping,
    verify it's encrypted, and then read plaintext columns.r   r   TF)r   r   r   r   r&   r'   c                 S   s   t | S r)   r*   r+   r   r   r   r,     s    zStest_encrypted_parquet_write_read_plain_footer_single_wrapping.<locals>.kms_factoryN)r.   r   r   r   r   r/   r0   r1   r2   r3   r4   r_   r   r   r   >test_encrypted_parquet_write_read_plain_footer_single_wrapping  s,       
 r   z'External key material not supported yetc                 C   sT   | t  }tjti dd}tjttdid}dd }t|}t||||| dS )zWrite an encrypted parquet, with external key
    material.
    Currently it's not implemented, so should throw
    an exceptionF)r   r   r   r&   r'   c                 S   s   t | S r)   r*   r+   r   r   r   r,     s    z:test_encrypted_parquet_write_external.<locals>.kms_factoryN)	r.   r   r   r   r/   r0   r1   r3   r4   r_   r   r   r   %test_encrypted_parquet_write_external  s    
 r   c                 C   s   | t  }|}tjttdttdid}dd }t|}t	||||| t
| tjtddd}tdD ]D}	|||}
|
d	k	sttj||
d
}|jdd}||sptqpd	S )z`Write an encrypted parquet, verify it's encrypted,
    and then read it multithreaded in a loop.r&   r'   c                 S   s   t | S r)   r*   r+   r   r   r   r,     s    z0test_encrypted_parquet_loop.<locals>.kms_factoryr   r   r-   2   NrF   TrG   )r.   r   r/   r   r0   r1   r   r2   r3   r4   r   r5   r   rangerH   r8   rB   rM   rN   r7   )r9   r   r   r:   r;   r<   r,   r=   r>   irH   rP   r?   r   r   r   test_encrypted_parquet_loop  s>      
   r   )-rS   datetimer   Zpyarrowr   Zpyarrow.parquetZparquetrB   Zpyarrow.parquet.encryptionZ
encryptionr   ImportErrorZ pyarrow.tests.parquet.encryptionr   r   r.   r0   r   r2   r   markZparquet_encryptionZ
pytestmarkZfixturer   r   r@   r4   r6   rV   rZ   r[   r\   r`   re   rz   r}   r   r   r   r   Zxfailr   r   r   r   r   r   r   <module>   sR   


	

	(1	
	$!#

'
