Ë
    ìmxib  ã                   ó²   — d dl Z d dlmc mZ d dlmZ d dlZ G d„ dej                  «      Z	de
dee
eef   fd„ZdZ G d	„ d
ej                  «      Zd„ Zd„ Zy)é    N)ÚFileSystemKeyMaterialStorec                   ó"   — e Zd ZdZd„ Zd„ Zd„ Zy)ÚInMemoryKmsClientzRThis is a mock class implementation of KmsClient, built for testing
    only.
    c                 ód   — t         j                  j                  | «       |j                  | _        y)z%Create an InMemoryKmsClient instance.N)ÚpeÚ	KmsClientÚ__init__Úcustom_kms_confÚmaster_keys_map)ÚselfÚconfigs     úW/home/htdocs/ttos/venv/lib/python3.12/site-packages/pyarrow/tests/parquet/encryption.pyr	   zInMemoryKmsClient.__init__   s"   € ä
�‰×Ñ˜dÔ#Ø%×5Ñ5ˆÕó    c                 ó’   — | j                   |   j                  d«      }dj                  ||g«      }t        j                  |«      }|S )z`Not a secure cipher - the wrapped key
        is just the master key concatenated with key bytesúutf-8r   )r   ÚencodeÚjoinÚbase64Ú	b64encode)r   Ú	key_bytesÚmaster_key_identifierÚmaster_key_bytesÚwrapped_keyÚresults         r   Úwrap_keyzInMemoryKmsClient.wrap_key!   sM   € ð  ×/Ñ/Ð0EÑF×MÑMØóÐà—h‘hÐ 0°)Ð<Ó=ˆÜ×!Ñ! +Ó.ˆØˆr   c                 óØ   — || j                   vrt        d|«      ‚| j                   |   }t        j                  |«      }|dd }|dd }||j	                  d«      k(  r|S t        d||«      ‚)zGNot a secure cipher - just extract the key from
        the wrapped keyzUnknown master keyNé   r   zIncorrect master key used)r   Ú
ValueErrorr   Ú	b64decodeÚdecode)r   r   r   Úexpected_master_keyÚdecoded_wrapped_keyr   Údecrypted_keys          r   Ú
unwrap_keyzInMemoryKmsClient.unwrap_key*   sŒ   € ð !¨×(<Ñ(<Ñ<ÜÐ1Ð3HÓIÐIØ"×2Ñ2Ð3HÑIÐÜ$×.Ñ.¨{Ó;ÐØ.¨s°Ð3ÐØ+¨B¨CÐ0ˆØÐ#3×#:Ñ#:¸7Ó#CÒCØ Ð ÜÐ4Ø)¨=ó:ð 	:r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r$   © r   r   r   r      s   „ ñò6ò
ó:r   r   r   Úreturnc                 óÜ   — t        j                  d«      }|j                  | «      x}r9|j                  «       \  }}}t	        |«      }t        j                  |«      }|||fS t        d| «      ‚)z…Parses a wrapped key string into a tuple: (key id, version, key) given
    input in the form: <key id>:v<version>:<bas64 encoded key>z(.+?):v([0-9]+?):(.+)zCannot parse wrapped key)ÚreÚcompileÚ	fullmatchÚgroupsÚintr   r   r   )r   ÚptnÚmÚidÚversionÚb64keyÚkeys          r   Úparse_wrapped_keyr7   9   sm   € ô �*‰*Ð,Ó
-€CØ�M‰M˜+Ó&Ð&€qÐ&ØŸh™h›jÑˆˆG�VÜ�g“,ˆÜ×Ñ˜vÓ&ˆØ�G˜SÐ!Ð!äÐ3°[ÓAÐAr   Úmaster_key_versionc                   óV   — e Zd ZdZdd„Zedefd„«       Zdede	de	fd„Z
d	e	de	defd
„Zy)ÚMockVersioningKmsClientae  This is a mock class implementation of KmsClient, built for testing
    only.

    During tests that involve CryptoFactory.rotate_master_keys, separate
    instances of this client will be created when writing, rotating keys, and
    reading back parquet data. To help unit tests verify that external key
    material was stored correctly at each step, this client wraps keys with a
    master_key_identifier and a version number. To ensure each client wraps
    with the correct version, the current version is persisted in the
    key_access_token attribute of the KmsConnectionConfig shared by all clients
    r*   Nc                 óP   — t         j                  j                  | «       || _        y ©N)r   r   r	   Úconnection_config)r   r=   s     r   r	   z MockVersioningKmsClient.__init__V   s   € Ü
�‰×Ñ˜dÔ#Ø!2ˆÕr   c                 ó@   — t        | j                  j                  «      S r<   )r0   r=   Úkey_access_token)r   s    r   r8   z*MockVersioningKmsClient.master_key_versionZ   s   € ä�4×)Ñ)×:Ñ:Ó;Ð;r   r   r   c                 ór   — t        j                  |«      j                  d«      }|› d| j                  › d|› �S )Nr   z:vú:)r   r   r    r8   )r   r   r   r5   s       r   r   z MockVersioningKmsClient.wrap_key^   s<   € Ü×!Ñ! )Ó,×3Ñ3°GÓ<ˆØ'Ð(¨¨4×+BÑ+BÐ*CÀ1ÀVÀHÐMÐMr   r   c                 óH   — t        |«      \  }}}||k7  rt        d||«      ‚|S )Nz"Mismatched master key identifiers:)r7   r   )r   r   r   Úkey_idÚ_r6   s         r   r$   z"MockVersioningKmsClient.unwrap_keyb   s8   € ô +¨;Ó7‰ˆ��3ØÐ*Ò*ÜÐAØ#Ð%:ó<ð <àˆ
r   )r*   N)r%   r&   r'   r(   r	   Úpropertyr0   r8   ÚbytesÚstrr   r$   r)   r   r   r:   r:   I   sf   „ ñ
ó3ð ð< Cò <ó ð<ðN %ð NÀð NÈó Nðàðð $'ðð ,1ôr   r:   c                 óx   — t        | d«      5 }|j                  d«      }|dk(  sJ ‚	 ddd«       y# 1 sw Y   yxY w)z–Verify that the file is encrypted by looking at its first 4 bytes.
    If it's the magic string PARE
    then this is a parquet with encrypted footer.Úrbé   s   PAREN)ÚopenÚread)ÚpathÚfileÚ	magic_strs      r   Úverify_file_encryptedrP   m   s@   € ô 
ˆd�DÓ	ð $˜TØ—I‘I˜a“Lˆ	à˜GÒ#Ð#Ñ#÷$÷ $ñ $ús   �0°9c                 ó®   — t        j                  | «      }t        «       }|j                  «       D ]"  }|j	                  |«      }|||j
                  <   Œ$ |S )z‚Reads an external key material store given a parquet file path and
    returns a dict mapping master_key_id to KeyMaterial objects)r   Úfor_fileÚdictÚget_key_id_setÚget_key_materialÚmaster_key_id)rM   ÚstoreÚkeysr3   Úkey_materials        r   Úread_external_keys_to_dictrZ   w   sZ   € ô '×/Ñ/°Ó5€EÜ‹6€DØ×"Ñ"Ó$ò 8ˆØ×-Ñ-¨bÓ1ˆØ+7ˆˆ\×'Ñ'Ò(ð8ð €Kr   )r   Úpyarrow.parquet.encryptionÚparquetÚ
encryptionr   Úpyarrow._parquet_encryptionr   r,   r   r   rG   Útupler0   rF   r7   ÚMASTER_KEY_VERSIONr:   rP   rZ   r)   r   r   ú<module>ra      sk   ðó" ß 'Ð 'Ý BÛ 	ô:˜Ÿ™ô :ðD
B 3ð 
B¨5°°c¸5°Ñ+Aó 
Bð *Ð ô!˜bŸl™lô !òH$ór   