
    ɯei                    *   d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZmZmZ ddlmZmZmZ dd	lmZmZmZmZ dd
lmZ ddlmZ  eej>                  j@                  dz        Z e
rddl!m"Z"  ee#      Z$ddZ% G d d      Z&y)    )annotationsN)	getLogger)IOTYPE_CHECKING)default_backend)Cipher
algorithmsmodes   )PKCS5_OFFSET	PKCS5_PADPKCS5_UNPAD)UTF8EncryptionMetadataMaterialDescriptorkilobyte)owner_rw_opener)random_string   )SnowflakeFileEncryptionMaterialc           	         t        t        j                  | j                  t        | j                        t        | j
                        dd            S )z.Convert Material Descriptor to Unicode String.)queryIdsmkIdkeySize),:)
separators)strjsondumpsquery_idsmk_idkey_size)matdescs    e/var/www/html/glpi_dashboard/venv/lib/python3.12/site-packages/snowflake/connector/encryption_util.pymatdesc_to_unicoder&      sH    

"++W^^,w//0
 "	
	 	    c                      e Zd Zed	d       Zedez  f	 	 	 	 	 	 	 	 	 d
d       Zedez  df	 	 	 	 	 	 	 	 	 dd       Zedez  f	 	 	 	 	 	 	 	 	 	 	 dd       Zedez  ddf	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z	y)SnowflakeEncryptionUtilc                ,    t        j                  |       S )N)osurandom)byte_lengths    r%   get_secure_randomz)SnowflakeEncryptionUtil.get_secure_random*   s    zz+&&r'   @   c                    t        t              }t        j                  | j                        }t        |      }|j                  d|       t        j                  t              }t        j                  |      }t               }	t        t        j                  |      t        j                  |      |	      }
|
j!                         }d}	 |j#                  |      }t        |      dk(  rnHt        |      t        z  dk7  rt%        |t              }d}|j'                  |j)                  |             h|sG|j'                  |j)                  t        t+        t              j-                  t.              z               |j'                  |j1                                t        t        j                  |      t        j2                         |	      }
|
j!                         }|j)                  t%        |t                    |j1                         z   }t5        | j6                  | j8                  |dz        }t;        t        j<                  |      j?                  d      t        j<                  |      j?                  d      tA        |      	      }|S )
a  Reads content from src and write the encrypted content into out.

        This function is sensitive to current position of src and out.
        It does not seek to position 0 in neither stream objects before or after the encryption.

        Args:
            encryption_material: The encryption material for file.
            src: The input stream.
            out: The output stream.
            chunk_size: The size of read chunks (Default value = block_size * 4 * 1024

        Returns:
            The encryption metadata.
        zkey_size = %sbackendFTr   r   )r"   r!   r#   zutf-8)keyivr$   )!r   __name__base64standard_b64decodequery_stage_master_keylendebugr)   r.   
block_sizer   r   r	   AESr
   CBC	encryptorreadr   writeupdatechrencoder   finalizeECBr   r"   r!   r   	b64encodedecoder&   )encryption_materialsrcout
chunk_sizeloggerdecoded_keyr#   iv_datafile_keyr2   cipherr>   paddedchunkenc_kekmat_descmetadatas                    r%   encrypt_streamz&SnowflakeEncryptionUtil.encrypt_stream.   s   * 8$//66
 {#_h/ *;;JG*<<XF!#
x0%))G2DgV$$&	HHZ(E5zQUj(A-!%4IIi&&u-.  IIi&&zC
O4J4J44P'PQR		)$$&' 
{3UYY['R$$&	Yx<=	@R@R@TT 	 &&--(11\

 &  )009(//8&x0

 r'   Nc                   t        t              }t        j                  d|t        j
                  j                  |      dz         \  }}|j                  d|||       t        |d      5 }t	        j                  |d      5 }t        j                  | |||      }	ddd       ddd       	|fS # 1 sw Y   xY w# 1 sw Y   	|fS xY w)a  Encrypts a file in a temporary directory.

        Args:
            encryption_material: The encryption material for file.
            in_filename: The input file's name.
            chunk_size: The size of read chunks (Default value = block_size * 4 * 1024).
            tmp_dir: Temporary directory to use, optional (Default value = None).

        Returns:
            The encryption metadata and the encrypted file's location.
        F#)textdirprefixz0unencrypted file: %s, temp file: %s, tmp_dir: %srbwbN)r   r5   tempfilemkstempr+   pathbasenamer:   openfdopenr)   rV   )
rH   in_filenamerK   tmp_dirrL   temp_output_fdtemp_output_fileinfileoutfilerU   s
             r%   encrypt_filez$SnowflakeEncryptionUtil.encrypt_fileq   s    $ 8$+3+;+;GBGG,<,<[,IC,O,
(( 	>		
 +t$ 	>40 G2AA'*	
 )))	 	
 )))s$   +B;B/B;/B8	4B;;Cc                   | j                   }| j                  }t        j                  |j                        }t        j                  |      }t        j                  |      }	t               }
t        t        j                  |      t        j                         |
      }|j                         }t        |j                  |      |j                         z         }t        t        j                  |      t        j                  |	      |
      }|j                         }d}|j!                  |      }t#        |      dk7  rF||j%                  |       |j                  |      }|}|j!                  |      }t#        |      dk7  rF| t'        |      }|j%                  |d|         |j%                  |j                                y)z7To read from `src` stream then decrypt to `out` stream.r1   Nr   )r3   r4   r6   r7   r8   r   r   r	   r<   r
   rE   	decryptorr   rA   rD   r=   r?   r9   r@   r   )rU   rH   rI   rJ   rK   
key_base64	iv_base64rM   	key_bytesiv_bytesr2   rP   rl   rO   last_decrypted_chunkrR   doffsets                     r%   decrypt_streamz&SnowflakeEncryptionUtil.decrypt_stream   sv    \\
KK	//66
 --j9	,,Y7!#
{3UYY['R$$&	y//	:Y=O=O=QQR
x0%))H2EwW$$&	#$%jAo#/		./  'A#$ HHZ(E %jAo  +!"67FII*8VG45		)$$&'r'   Fc           	        t         j                  j                  |       dt                }|r t         j                  j	                  ||      }t
        j                  d||       |rdnt        }t        |d      5 }t        |d|      5 }	t        j                  | |||	|       ddd       ddd       |S # 1 sw Y   xY w# 1 sw Y   |S xY w)a  Decrypts a file and stores the output in the temporary directory.

        Args:
            metadata: The file's metadata input.
            encryption_material: The file's encryption material.
            in_filename: The name of the input file.
            chunk_size: The size of read chunks (Default value = block_size * 4 * 1024).
            tmp_dir: Temporary directory to use, optional (Default value = None).

        Returns:
            The decrypted file's location.
        rX   z encrypted file: %s, tmp file: %sNr\   r]   )opener)r+   r`   ra   r   joinrL   r:   r   rb   r)   rt   )
rU   rH   rd   rK   re   unsafe_file_writerg   file_openerrh   ri   s
             r%   decrypt_filez$SnowflakeEncryptionUtil.decrypt_file   s    * !gg..{;<Amo=NO!ww||G5EF7FVW/d_+t$ 	&[A W'66167J	
  	 	
  s$   <CB7%C7C 	<CC)r-   intreturnbytes)
rH   r   rI   	IO[bytes]rJ   r~   rK   r{   r|   r   )
rH   r   rd   r   rK   r{   re   
str | Noner|   ztuple[EncryptionMetadata, str])rU   r   rH   r   rI   r~   rJ   r~   rK   r{   r|   None)rU   r   rH   r   rd   r   rK   r{   re   r   rx   boolr|   r   )
r5   
__module____qualname__staticmethodr.   r   rV   rj   rt   rz    r'   r%   r)   r)   )   sq   ' ' 
 x-	@<@@ @ 	@
 
@ @D  x-"	 *< * *  * 	 *
 
( *  *D  x-$($$(<$( $( 	$(
 $( 
$( $(L 
 x-""'  $  <      	  
        
     r'   r)   )r$   r   r|   r   )'
__future__r   r6   r   r+   r^   loggingr   typingr   r   cryptography.hazmat.backendsr   &cryptography.hazmat.primitives.ciphersr   r	   r
   compatr   r   r   	constantsr   r   r   r   	file_utilr   	util_textr   r{   r<   r;   storage_clientr   r5   rL   r&   r)   r   r'   r%   <module>r      so    "   	   $ 8 L L 8 8 M M & $**Q./
?	8	s  s r'   