o
    NDi                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ dZzd dl	m
Z
mZmZ W n   dZY d dlmZ zd dlmZ W n
   d dlmZ Y d d	lmZ eeZd
gZdZG dd deZddd
Zdd Zdd Zdd Zdd Zdd Zdd Z dS )    N)urlopenT)
SSLContextPROTOCOL_TLSCERT_REQUIREDF)ZipFile)
BadZipFile)
BadZipfile)DriverExceptionget_cloud_configDATASTAX_APOLLOc                   @   s@   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZedd ZdS )CloudConfigNc                 C   sz   |  }| dd |_z	t|d |_W n   Y | dd |_| dd |_| dd |_| dd |_| dd |_|S )NportusernamepasswordhostkeyspacelocalDC)getr   intr   r   r   r   local_dc)clsdc r   Z/var/www/Datamplify/venv/lib/python3.10/site-packages/cassandra/datastax/cloud/__init__.py	from_dict<   s   zCloudConfig.from_dict)__name__
__module____qualname__r   r   r   r   r   r   ssl_contextsni_hostsni_porthost_idsclassmethodr   r   r   r   r   r   .   s    r   c                 C   s\   t stdd| vrtdzt| |}W n ty    tdw t|| }|r,|j|_|S )NzIA Python installation with SSL is required to connect to a cloud cluster.secure_connect_bundlez@The cloud config doesn't have a secure_connect_bundle specified.zSUnable to open the zip file for the cloud config. Check your secure connect bundle.)_HAS_SSLr	   
ValueErrorread_cloud_config_from_zipr   read_metadata_infopyopenssl_contextr   )cloud_configcreate_pyopenssl_contextconfigr   r   r   r
   O   s   
c              
   C   s   | d }|  dd }t|8}|rt ntj|}tj|d}z|j|d t	tj
|d| |W t| W  d    S t| w 1 sIw   Y  d S )Nr$   use_default_tempdir)dir)pathzconfig.json)r   r   tempfile
gettempdirosr/   dirnamemkdtemp
extractallparse_cloud_configjoinshutilrmtree)r*   r+   secure_bundler-   zipfilebase_dirtmp_dirr   r   r   r'   a   s   

r'   c           
      C   s   t | d}t|}W d    n1 sw   Y  t|}tj| }d|v r0|d |_|S tj	|d}tj	|d}tj	|d}	t
|||	|_|rUt|||	|_|S )Nrr   zca.crtcertkey)openjsonloadr   r   r2   r/   r3   r   r7   _ssl_context_from_cert_pyopenssl_context_from_certr)   )
r/   r*   r+   streamdatar,   
config_dirca_cert_locationcert_locationkey_locationr   r   r   r6   n   s   

r6   c              
   C   s   d | j| j}d|v r|d nd}z
t|| j|d}W n ty3 } zt| td| d }~ww |j	dkrBtd||j	f t
| | dS )	Nzhttps://{}:{}/metadataconnect_timeout   )contexttimeoutz`Unable to connect to the metadata service at %s. Check the cluster status in the cloud console.    zMError while fetching the metadata at: %s. The service returned error code %d.zutf-8)formatr   r   r   r   	Exceptionlog	exceptionr	   codeparse_metadata_inforeaddecode)r,   r*   urlrO   responseer   r   r   r(      s    

r(   c                 C   s   zt |}W n	   d}t||d }|d | _|d d}|d | _z	t|d | _W n   d| _Y d	d
 |d D | _| S )NzFailed to load cluster metadatacontact_infor   sni_proxy_address:r      iR#  c                 S   s   g | ]}|qS r   r   ).0host_idr   r   r   
<listcomp>   s    z'parse_metadata_info.<locals>.<listcomp>contact_points)	rB   loadsr	   r   splitr    r   r!   r"   )r,   	http_datarG   msgr\   
proxy_infor   r   r   rV      s   

rV   c                 C   s*   t t}||  t|_|j||d |S )N)certfilekeyfile)r   r   load_verify_locationsr   verify_modeload_cert_chain)rI   rJ   rK   r   r   r   r   rD      s
   
rD   c              
   C   s|   zddl m} W n ty } ztd|jd }~ww ||j}|j|jdd d |	| |
| ||  |S )Nr   )SSLzXPyOpenSSL must be installed to connect to Astra with the Eventlet or Twisted event loopsc                 S   s   |S )Nr   )_1_2_3_4okr   r   r   <lambda>   s    z._pyopenssl_context_from_cert.<locals>.<lambda>)callback)OpenSSLrn   ImportErrorwith_traceback__traceback__ContextTLSv1_METHOD
set_verifyVERIFY_PEERuse_certificate_fileuse_privatekey_filerk   )rI   rJ   rK   rn   r[   r   r   r   r   rE      s    


rE   )F)!r2   loggingrB   sysr0   r8   urllib.requestr   r%   sslr   r   r   r;   r   r   r   	cassandrar	   	getLoggerr   rS   __all__DATASTAX_CLOUD_PRODUCT_TYPEobjectr   r
   r'   r6   r(   rV   rD   rE   r   r   r   r   <module>   s:   

!	