o
    h                     @   s<  d dl Z d dlmZ d dlZd dlmZ d dlZd dlZdZ	zd dl
mZ d dlmZ W n ey9   dZdZY nw zd dlmZ d dlm  mZ W n eyV   dZ	Y nw ejjZdZdZdZd	Zd
d Zdd Zdd Zdd Zdd Z ejj!e	dddd Z"ejj!e	 dddd Z#ejj!e	dddd Z$dS )    N)	timedeltaFInMemoryKmsClientTs   0123456789112345
footer_keys   1234567890123450col_keyc                   C   s   t g dg dg ddS )N)i      r   i  r   )   r	      r
      d   )FlamingoParrotDogHorsezBrittle stars	Centipede)yearn_legsanimal)patable r   r   Y/var/www/vscode/kcb/lib/python3.10/site-packages/pyarrow/tests/test_dataset_encryption.pycreate_sample_table4   s   r   c                   C   s$   t jtdtddgidtddddS )	NFr   r   
AES_GCM_V1g      @)minutes   )r   plaintext_footercolumn_keysencryption_algorithmcache_lifetimedata_key_length_bits)peEncryptionConfigurationFOOTER_KEY_NAMECOL_KEY_NAMEr   r   r   r   r   create_encryption_configE   s   
r&   c                   C   s   t jddS )Ni,  )r    )r"   DecryptionConfigurationr   r   r   r   create_decryption_configQ   s   r(   c                   C   s    t jttdttdidS )NzUTF-8)custom_kms_conf)r"   KmsConnectionConfigr$   
FOOTER_KEYdecoder%   COL_KEYr   r   r   r   create_kms_connection_configU   s
   

r.   c                 C   s   t | S Nr   )kms_connection_configurationr   r   r   kms_factory^   s   r1   z+Parquet Encryption is not currently enabled)reasonc                  C   sJ  t  } t }t }t }tt}t|||}t	|||}t
j }|j|d}t }	|	d tj| d|||	d t
j }tjtdd tjd||	d W d    n1 s\w   Y  tj|d}
t
jj|
d	}tjd||	d}| | sJ |||}tj|d
}
t
jj|
d	}tjd||	d}| | sJ d S )Nencryption_config/sample_dataset)database_dirformatfile_options
filesystemzno decryption)matchr9   r;   decryption_configdefault_fragment_scan_options)decryption_properties)r   r&   r(   r.   r"   CryptoFactoryr1   dsParquetEncryptionConfigParquetDecryptionConfigr   datasetParquetFileFormatmake_write_optionsfs_MockFileSystem
create_dirwrite_datasetpytestraisesIOErrorParquetFragmentScanOptionsequalsto_tablefile_decryption_properties)r   r4   r?   kms_connection_configcrypto_factoryparquet_encryption_cfgparquet_decryption_cfgpformatwrite_optionsmockfspq_scan_optsrG   rB   r   r   r   "test_dataset_encryption_decryptionb   sR   



	r]   z'Parquet Encryption is currently enabledc                  C   sF   t j } tt | jdd}W d   dS 1 sw   Y  dS )zTest write_dataset with ParquetFileFormat and test if an exception is thrown
    if you try to set encryption_config using make_write_optionsz
some valuer3   N)r   rG   rH   rN   rO   NotImplementedErrorrI   )rY   _r   r   r   -test_write_dataset_parquet_without_encryption   s   
	"r`   c                     sB  G dd dt j d} tjjtjdd t| D t dgdgd}t  }t 	 fd	d
}t j
dddgidddd}t|||}t||t  }tj|d}tj|d}|j|d}	||}
t }|d d}tj||||	|d |d }tj||
|d }||ksJ tj|||d}| }||ksJ dS )z9Test encryption and decryption of a large number of rows.c                   @   s8   e Zd ZdededefddZdededefddZd	S )
z;test_large_row_encryption_decryption.<locals>.NoOpKmsClient	key_bytesr_   returnc                 S      t |}|S r/   )base64	b64encode)selfra   r_   br   r   r   wrap_key      
zDtest_large_row_encryption_decryption.<locals>.NoOpKmsClient.wrap_keywrapped_keyc                 S   rc   r/   )rd   	b64decode)rf   rj   r_   rg   r   r   r   
unwrap_key   ri   zFtest_large_row_encryption_decryption.<locals>.NoOpKmsClient.unwrap_keyN)__name__
__module____qualname__bytesstrrh   rl   r   r   r   r   NoOpKmsClient   s    rr   i  c                 S   s   g | ]}t   qS r   )random).0r_   r   r   r   
<listcomp>   s    z8test_large_row_encryption_decryption.<locals>.<listcomp>)typefoo)namesc                    s     S r/   r   )r_   rr   r   r   <lambda>   s    z6test_large_row_encryption_decryption.<locals>.<lambda>UNIMPORTANT_KEYTF   )r   r   double_wrappingr   r!   r>   r@   r3   r5   zlarge-row-test-dataset)r9   r:   r;   z/part-0.parquet)rB   r;   r=   N)r"   	KmsClientr   Tablefrom_arraysarrayrangefloat32r*   rC   r#   rD   rE   rF   r'   rQ   rH   rI   rT   rJ   rK   rL   rM   pqParquetFilereadrG   rS   )	row_countr   
kms_configrV   r4   
pqe_config
pqd_configscan_optionsfile_formatrZ   rT   r[   path	file_path	new_tablerG   r   ry   r   $test_large_row_encryption_decryption   sZ   	



r   )%rd   datetimer   rs   
pyarrow.fsrJ   pyarrowr   rN   encryption_unavailablepyarrow.parquetparquetr   pyarrow.datasetrG   rD   ImportError pyarrow.tests.parquet.encryptionr   pyarrow.parquet.encryption
encryptionr"   mark
pytestmarkr+   r$   r-   r%   r   r&   r(   r.   r1   skipifr]   r`   r   r   r   r   r   <module>   sT   	
9
