o
    RDi)                     @  s  d dl mZ d dlZd dlZd dlZd dlm  mZ d dl	m	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZmZmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ ddlmZ ddl m!Z! eroddl"m#Z#m$Z$ ee%Z&G dd deZ'dZ(dZ)dZ*dZ+G dd deZ,dS )    )annotationsN)datetimetimezone)	getLogger)choice)	hexdigits)TYPE_CHECKINGAny
NamedTuple   )quote)
FileHeaderResultStatus)EncryptionMetadata)SnowflakeStorageClient)get_md5_for_integrity)requests)SnowflakeFileMetaStorageCredentialc                   @  s   e Zd ZU ded< ded< dS )AzureLocationstrcontainer_namepathN)__name__
__module____qualname____annotations__ r   r   a/var/www/Datamplify/venv/lib/python3.10/site-packages/snowflake/connector/azure_storage_client.pyr      s   
 r   )z/Signature not valid in the specified time framez*Server failed to authenticate the request.zx-ms-meta-sfcdigestzx-ms-meta-encryptiondatazx-ms-meta-matdescc                      s   e Zd Z	d4d5 fddZed6ddZd7ddZ		d8d9d!d"Zd:d%d&Zd;d(d)Z	d<d*d+Z
d=d.d/Zd<d0d1Zd>d2d3Z  ZS )?SnowflakeAzureRestClientFmetar   credentialsStorageCredential | None
chunk_sizeint
stage_infodict[str, Any]unsafe_file_writeboolreturnNonec                   sb   t  j|||||d |d }|dr|tdd  }|| _|d | _| |d | _g | _d S )N)r!   r'   endPointzblob.storageAccountlocation)	super__init__
startswithlenendpointstorage_accountextract_container_name_and_pathazure_location	block_ids)selfr    r!   r#   r%   r'   	end_point	__class__r   r   r/   )   s    


z!SnowflakeAzureRestClient.__init__stage_locationr   r   c                 C  sN   t j| } | }d}d| v r!| d\}}}|r!|ds!|d7 }t||dS )N /)r   r   )osr   
expanduser	partitionendswithr   )r;   r   r   _r   r   r   r4   B   s   z8SnowflakeAzureRestClient.extract_container_name_and_pathresponserequests.Responsec                   s     j dkot fddtD S )Ni  c                 3  s    | ]}| j v V  qd S N)reason).0messagerC   r   r   	<genexpr>Q   s    

z>SnowflakeAzureRestClient._has_expired_token.<locals>.<genexpr>)status_codeanyTOKEN_EXPIRATION_ERR_MESSAGE)r7   rC   r   rI   r   _has_expired_tokenP   s   z+SnowflakeAzureRestClient._has_expired_tokenNverburlretry_id	int | strheadersdatabytesc                   s*   si d fdd} |||S )Nr)   tuple[bytes, dict[str, Any]]c                    s   t tjjd d} | d}jjd }|r"|dr"|dd  }dv r-d | }nd | }|d< di} rA |d	< ||fS )
N)tzinfoz
YYYY-MM-DDAZURE_SAS_TOKEN?r   &DaterS   rT   )	r   nowr   utcreplacestrftimer!   credsr0   )curtime	timestamp	sas_token_url	rest_argsrT   rS   r7   rP   r   r   (generate_authenticated_url_and_rest_args`   s   
zvSnowflakeAzureRestClient._send_request_with_authentication_and_retry.<locals>.generate_authenticated_url_and_rest_args)r)   rV   )_send_request_with_retry)r7   rO   rP   rQ   rS   rT   rg   r   rf   r   +_send_request_with_authentication_and_retryU   s   zDSnowflakeAzureRestClient._send_request_with_authentication_and_retryfilenameFileHeader | Nonec                 C  s  t | jj}t | jjt | }| j}d| j d| j d| d| }d}d| j|< | d||}|j	dkrqt
j|_|jt}|du rFdnt|}	|	sOdnt|	d d	 |	d
 |jtd}
t|jtt|jd|
dS |j	dkrt
j|_tddddS |  dS )zGets Azure file properties.https://.blob.r=   HEADr      NWrappedContentKeyEncryptedKeyContentEncryptionIV)keyivmatdescContent-Length)digestcontent_lengthencryption_metadatai  )r   r5   r   r   r    r3   r2   retry_countri   rK   r   UPLOADEDresult_statusrS   getENCRYPTION_DATAjsonloadsr   MATDESCr   	SFCDIGESTr$   NOT_FOUND_FILEraise_for_status)r7   rj   r   r   r    rP   rQ   renc_data_strencryption_datary   r   r   r   get_file_headert   s<    



	

z(SnowflakeAzureRestClient.get_file_headerdict[str, str | None]c                 C  sV   t | jji}| j}|r)|ttdd|jddddd|j	dd	id
t
|ji |S )NFullBlobsymmKey1AES_CBC_256)KeyIdrq   	Algorithmz1.0AES_CBC_128)ProtocolEncryptionAlgorithmEncryptionLibraryz
Java 5.3.0)EncryptionModerp   EncryptionAgentrr   KeyWrappingMetadata)r   r    sha256_digestry   updater~   r   dumpsrs   rt   r   ru   )r7   azure_metadatary   r   r   r   _prepare_file_metadata   s,   z/SnowflakeAzureRestClient._prepare_file_metadatac                 C  s   dd t | jD | _d S )Nc                 S  s$   g | ]}d  dd tdD qS )r<   c                 s  s    | ]}t tV  qd S rE   )r   r   rG   rB   r   r   r   rJ      s    zQSnowflakeAzureRestClient._initiate_multipart_upload.<locals>.<listcomp>.<genexpr>   )joinranger   r   r   r   
<listcomp>   s    zGSnowflakeAzureRestClient._initiate_multipart_upload.<locals>.<listcomp>)r   num_of_chunksr6   )r7   r   r   r   _initiate_multipart_upload   s   z3SnowflakeAzureRestClient._initiate_multipart_uploadchunk_idchunkc           
   
   C  s   t | jj}t | jj| jjd }| jdkrC| j| }d| j	 d| j
 d| d| d| 
}dtt|i}| jd||||d}n(|  }	d| j	 d| j
 d| d| }d	d
d}||	 | jd||||d}|  d S )Nr=   r   rl   rm   z?comp=block&blockid=rv   PUTrS   rT   	BlockBlobutf-8)zx-ms-blob-typezContent-Encoding)r   r5   r   r   r    dst_file_namelstripr   r6   r3   r2   r   r1   ri   r   r   r   )
r7   r   r   r   r   block_idrP   rS   r   r   r   r   r   _upload_chunk   s,   


 

z&SnowflakeAzureRestClient._upload_chunkc              	   C  sD  t | jj}t | jj| jjd }d| j d| j d| d| d	}t	
d}| jD ]}t	
d}||_|| q,t| jjpD| jj }| jjpU| jjpUt| jjd}z|s^|d | }	W |ri|  n|rq|  w w d	tt|	d	d
}
|  }|
| d}d| j|< | jd|d|
t	|d}|  d S )Nr=   rl   rm   z?comp=blocklist	BlockListLatestrbr   r   )zx-ms-blob-content-encodingzx-ms-blob-content-md5COMPLETEr   r   ) r   r5   r   r   r    r   r   r3   r2   ETElementr6   textappendr(   
src_streamintermediate_streamopenreal_src_file_nameseekreadclosebase64	b64encoder   decoder   r   rz   ri   tostringr   )r7   r   r   rP   rootr   part
new_streamfdfile_contentrS   r   rQ   r   r   r   r   _complete_multipart_upload   sN    







z3SnowflakeAzureRestClient._complete_multipart_uploadc           	      C  s   t | jj}t | jj| jjd }d| j d| j d| d| }| j	dkrZ| j
}|| j	d k rB||  d|d | d  }n||  d}dd| i}| jd|||d	}n| d||}|jd
v rm| ||j |  d S )Nr=   rl   rm   r   -Rangezbytes=GET)rS   )ro      )r   r5   r   r   r    src_file_namer   r3   r2   r   r#   ri   rK   write_downloaded_chunkcontentr   )	r7   r   r   r   rP   r#   _rangerS   r   r   r   r   download_chunk  s     
 
z'SnowflakeAzureRestClient.download_chunk)F)r    r   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   )r;   r   r)   r   )rC   rD   r)   r(   )NN)rO   r   rP   r   rQ   rR   rS   r&   rT   rU   r)   rD   )rj   r   r)   rk   )r)   r   )r)   r*   )r   r$   r   rU   r)   r*   )r   r$   r)   r*   )r   r   r   r/   staticmethodr4   rN   ri   r   r   r   r   r   r   __classcell__r   r   r9   r   r   (   s    



$


*r   )-
__future__r   r   r   r>   xml.etree.ElementTreeetreeElementTreer   r   r   loggingr   randomr   stringr   typingr   r	   r
   compatr   	constantsr   r   encryption_utilr   storage_clientr   	util_textr   vendoredr   file_transfer_agentr   r   r   loggerr   rM   r   r~   r   r   r   r   r   r   <module>   s2    