o
    ZhO                     @   sf  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y,   dZdZ
Y n	w 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   s6   t jt g dt g dt g dd} | S )N)         )abc)xyz)paTableZfrom_pydictarray)
data_table r   \/var/www/html/lang_env/lib/python3.10/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||||}||sQJ 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      t | S Nr   Zkms_connection_configurationr   r   r   kms_factory[      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)	tempdirr   pathencryption_configkms_connection_configr-   crypto_factorydecryption_configresult_tabler   r   r   !test_encrypted_parquet_write_readD   s6   	


rA   c                 C   sZ   | ||}|d usJ tj| |j|d}|| W d    d S 1 s&w   Y  d S )N)Zencryption_properties)file_encryption_propertiespqZParquetWriterschemaZwrite_table)r;   tabler<   r=   r>   rB   writerr   r   r   r6   l   s   "r6   c                 C   sn   | ||}|d usJ tj| |d}|jdksJ tj| |d}t|jdks*J tj| |d}|jddS )NZdecryption_propertiesr
   TZuse_threads)	file_decryption_propertiesrC   read_metadataZnum_columnsread_schemalennamesParquetFileread)r;   r?   r=   r>   rI   metarD   resultr   r   r   r8   w   s    r8   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 d   dS 1 smw   Y  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   r)   r*   r+   r,   r   r   r   r-      r.   z@test_encrypted_parquet_write_read_wrong_key.<locals>.kms_factoryr/   zIncorrect master key usedmatchN)r0   r   r   r   r   r   r1   r2   r3   r4   r5   r6   r   r7   pytestraises
ValueErrorr8   )	r:   r   r;   r<   r=   r-   r>   Zwrong_kms_connection_configr?   r   r   r   +test_encrypted_parquet_write_read_wrong_key   sD   	




"rW   c                 C   sP   t | | tjtdd t| t   W d   dS 1 s!w   Y  dS )zmWrite an encrypted parquet, verify it's encrypted,
    but then try to read it without decryption properties.no decryptionrR   N)rA   rT   rU   IOErrorrC   rN   r0   rO   r:   r   r   r   r   0test_encrypted_parquet_read_no_decryption_config   s   
"r[   c                 C   L   t | | tjtdd t| t  W d   dS 1 sw   Y  dS )zwWrite an encrypted parquet, verify it's encrypted,
    but then try to read its metadata without decryption properties.rX   rR   N)rA   rT   rU   rY   rC   rJ   r0   rZ   r   r   r   9test_encrypted_parquet_read_metadata_no_decryption_config   s   
"r]   c                 C   r\   )zuWrite an encrypted parquet, verify it's encrypted,
    but then try to read its schema without decryption properties.rX   rR   N)rA   rT   rU   rY   rC   rK   r0   rZ   r   r   r   7test_encrypted_parquet_read_schema_no_decryption_config   s   
"r^   c                 C   s   | 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 d	   d	S 1 s>w   Y  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   r)   r*   r+   r,   r   r   r   r-      r.   z<test_encrypted_parquet_write_no_col_key.<locals>.kms_factoryz4Either column_keys or uniform_encryption must be setrR   N)r   r   r   r1   r2   r3   r   r4   r5   rT   rU   OSErrorr6   r:   r   r;   r<   r=   r-   r>   r   r   r   'test_encrypted_parquet_write_no_col_key   s$   


"rb   c                 C   j   | d }|}t  }dd }t |}tjtdd t||||| W d   dS 1 s.w   Y  dS )<Write an encrypted parquet, but raise KeyError in KmsClient.(encrypted_table_kms_error.in_mem.parquetc                 S   r)   r*   r+   r,   r   r   r   r-      s   z;test_encrypted_parquet_write_kms_error.<locals>.kms_factoryr   rR   N)r   r1   r5   rT   rU   KeyErrorr6   r:   r   r   r;   r<   r=   r-   r>   r   r   r   &test_encrypted_parquet_write_kms_error   s   
"rh   c                    s   | d }|}t  }G dd dt j  fdd}t |}tjtdd t||||| W d   dS 1 s9w   Y  dS )	rd   re   c                   @   (   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selfrl   r   r   r   rk     s   
zStest_encrypted_parquet_write_kms_specific_error.<locals>.ThrowingKmsClient.__init__c                 S      t d)NCannot Wrap KeyrV   rn   	key_bytesmaster_key_identifierr   r   r   wrap_key  r.   zStest_encrypted_parquet_write_kms_specific_error.<locals>.ThrowingKmsClient.wrap_keyc                 S   ro   )NzCannot Unwrap Keyrq   rn   Zwrapped_keyrt   r   r   r   
unwrap_key  r.   zUtest_encrypted_parquet_write_kms_specific_error.<locals>.ThrowingKmsClient.unwrap_keyN__name__
__module____qualname____doc__rk   ru   rw   r   r   r   r   ThrowingKmsClient  s
    r}   c                        | S r*   r   r,   r}   r   r   r-     s   zDtest_encrypted_parquet_write_kms_specific_error.<locals>.kms_factoryrp   rR   N)r   r1   rj   r5   rT   rU   rV   r6   rg   r   r   r   /test_encrypted_parquet_write_kms_specific_error  s   
"r   c                 C   rc   )z@Write an encrypted parquet, but raise ValueError in kms_factory.0encrypted_table_kms_factory_error.in_mem.parquetc                 S   ro   )NCannot create KmsClientrq   r,   r   r   r   r-   3  r.   zCtest_encrypted_parquet_write_kms_factory_error.<locals>.kms_factoryr   rR   N)r   r1   r5   rT   rU   rV   r6   rg   r   r   r   .test_encrypted_parquet_write_kms_factory_error*  s   
"r   c                    sx   | d }|}t  }G dd d  fdd}t |}tt t||||| W d   dS 1 s5w   Y  dS )z_Write an encrypted parquet, but use wrong KMS client type
    that doesn't implement KmsClient.r   c                   @   ri   )	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_maprm   r   r   r   rk   L  s   zXtest_encrypted_parquet_write_kms_factory_type_error.<locals>.WrongTypeKmsClient.__init__c                 S      d S r*   r   rr   r   r   r   ru   O     zXtest_encrypted_parquet_write_kms_factory_type_error.<locals>.WrongTypeKmsClient.wrap_keyc                 S   r   r*   r   rv   r   r   r   rw   R  r   zZtest_encrypted_parquet_write_kms_factory_type_error.<locals>.WrongTypeKmsClient.unwrap_keyNrx   r   r   r   r   WrongTypeKmsClientH  s
    r   c                    r~   r*   r   r,   r   r   r   r-   U  r.   zHtest_encrypted_parquet_write_kms_factory_type_error.<locals>.kms_factoryN)r   r1   r5   rT   rU   	TypeErrorr6   rg   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J ddg| jt ksJ d| jksJ | jsJ | jr#J tdd| jks-J | j	r2J d| j
ks9J d S )Nr   r   AES_GCM_CTR_V1      $@r      )r   r   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J t  }tdd|_tdd|jks'J d S )Nr   r   r/   )r   r7   r   r$   )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J d| jksJ d| jksJ ddd| jksJ d S )N	Instance1URL1MyTokenkey_material_1key_material_2key1key2kms_instance_id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   r1   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   r)   r*   r+   r,   r   r   r   r-     r.   zStest_encrypted_parquet_write_read_plain_footer_single_wrapping.<locals>.kms_factoryN)r0   r   r   r   r   r1   r2   r3   r4   r5   r6   ra   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   r)   r*   r+   r,   r   r   r   r-     r.   z:test_encrypted_parquet_write_external.<locals>.kms_factoryN)	r0   r   r   r   r1   r2   r3   r5   r6   ra   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	usFJ tj||
d
}|jdd}||sZJ q8d	S )z`Write an encrypted parquet, verify it's encrypted,
    and then read it multithreaded in a loop.r&   r'   c                 S   r)   r*   r+   r,   r   r   r   r-     r.   z0test_encrypted_parquet_loop.<locals>.kms_factoryr   r   r/   2   NrG   TrH   )r0   r   r1   r   r2   r3   r   r4   r5   r6   r   r7   r   rangerI   rC   rN   rO   r9   )r:   r   r   r;   r<   r=   r-   r>   r?   irI   rQ   r@   r   r   r   test_encrypted_parquet_loop  s6   


r   )-rT   datetimer   Zpyarrowr   Zpyarrow.parquetZparquetrC   Zpyarrow.parquet.encryptionZ
encryptionr   ImportErrorZ pyarrow.tests.parquet.encryptionr   r   r0   r2   r   r4   r   markZparquet_encryptionZ
pytestmarkZfixturer   r   rA   r6   r8   rW   r[   r]   r^   rb   rh   r   r   r   r   r   r   Zxfailr   r   r   r   r   r   r   <module>   sV   


	
	(1	
	$!#

'
