
    <>ib                         S SK r S SKJs  Jr  S SKJr  S SKr " S S\R                  5      r	S\
S\\
\\4   4S jrSr " S	 S
\R                  5      rS rS rg)    N)FileSystemKeyMaterialStorec                   *    \ rS rSrSrS rS rS rSrg)InMemoryKmsClient   zJThis is a mock class implementation of KmsClient, built for testing
only.
c                 d    [         R                  R                  U 5        UR                  U l        g)z%Create an InMemoryKmsClient instance.N)pe	KmsClient__init__custom_kms_confmaster_keys_map)selfconfigs     _/var/www/html/land-tabula/venv/lib/python3.13/site-packages/pyarrow/tests/parquet/encryption.pyr
   InMemoryKmsClient.__init__   s"    
d#%55    c                     U R                   U   R                  S5      nSR                  X1/5      n[        R                  " U5      nU$ )zXNot 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_keyInMemoryKmsClient.wrap_key!   sK      //0EFMMhh 0<=!!+.r   c                     X R                   ;  a  [        SU5      eU R                   U   n[        R                  " U5      nUSS nUSS nX5R	                  S5      :X  a  U$ [        SXV5      e)z?Not 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_keyInMemoryKmsClient.unwrap_key*   s     !(<(<<13HII"223HI$..{;.s3+BC0#:#:7#CC  4): 	:r   )r   N)	__name__
__module____qualname____firstlineno____doc__r
   r   r'   __static_attributes__ r   r   r   r      s    6
:r   r   r   returnc                     [         R                  " S5      nUR                  U 5      =n(       a8  UR                  5       u  p4n[	        U5      n[
        R                  " U5      nX4U4$ [        SU 5      e)zParses 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_keyr=   9   si     **,
-CMM+&&q&hhjVg,v&S!!3[AAr   master_key_versionc                   f    \ rS rSrSrSS jr\S\4S j5       rS\	S\
S\
4S	 jrS
\
S\
S\	4S jrSrg)MockVersioningKmsClientI   aA  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
r0   Nc                 N    [         R                  R                  U 5        Xl        g N)r   r	   r
   connection_config)r   rD   s     r   r
    MockVersioningKmsClient.__init__V   s    
d#!2r   c                 @    [        U R                  R                  5      $ rC   )r6   rD   key_access_token)r   s    r   r>   *MockVersioningKmsClient.master_key_versionZ   s    4))::;;r   r   r   c                 t    [         R                  " U5      R                  S5      nU SU R                   SU 3$ )Nr   z:v:)r   r   r#   r>   )r   r   r   r;   s       r   r    MockVersioningKmsClient.wrap_key^   s<    !!),33G<'(4+B+B*C1VHMMr   r   c                 D    [        U5      u  p4nX2:w  a  [        SX25      eU$ )Nz"Mismatched master key identifiers:)r=   r!   )r   r   r   key_id_r<   s         r   r'   "MockVersioningKmsClient.unwrap_keyb   s0     +;73*A#< <
r   )rD   )r0   N)r)   r*   r+   r,   r-   r
   propertyr6   r>   bytesstrr   r'   r.   r/   r   r   r@   r@   I   sf    
3 <C < <N% N N N $' ,1r   r@   c                     [        U S5       nUR                  S5      nUS:X  d   e SSS5        g! , (       d  f       g= f)zVerify 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_encryptedr[   m   s7     
dD	TIIaL	G### 
		s   1
?c                     [         R                  " U 5      n[        5       nUR                  5        H"  nUR	                  U5      nXBUR
                  '   M$     U$ )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)rX   storekeysr9   key_materials        r   read_external_keys_to_dictre   w   sS     '//5E6D""$--b1+7\''( % Kr   )r   pyarrow.parquet.encryptionparquet
encryptionr   pyarrow._parquet_encryptionr   r2   r	   r   rR   tupler6   rQ   r=   MASTER_KEY_VERSIONr@   r[   re   r/   r   r   <module>rl      sk   "  ' ' B 	: :D
B3 
B5c5+A 
B * !bll !H$r   