o
    PDi#                     @   s   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mZ ejj	Z
ejjZejjZejjZ	 edZdd Zdd Zdd	d
ZdedefddZdd Zdd Zdd ZdededejfddZ e!de e"de  dS )    N)urlparseparse_qszs(?P<objservername>[^/]+)/n/(?P<namespace>[^/]+)/b/(?P<bucketname>[^/]+)/o/(?P<filename>[^/]+)(/c/(?P<alias>[^/]+))?c                 C   s\  i }t | \}}| d}|d ur| }|d u s|dkr"t|}n|dks*|dkr0t||d}t| dt| dt| dd	}|jdi |}t|j}	t	|	}
| d
}|r\|
| }
t|
d|d< |j
d u r|
d|d< d|
v r|
d  |d< }|d dkr|di }|d| ||d< |
d|d< |
d|d< |
dd |d< || d S )NauthenticationOCI_DEFAULTOCI_INSTANCE_PRINCIPALOCI_RESOURCE_PRINCIPALconfigsignerfilename
bucketname	namespace)object_namebucket_namenamespace_namealiasconnect_descriptoruserpasswordtypeocivaultmethod
credentialconfig_time_to_live config_time_to_live_grace_periodpyo )_get_credentialgetupperoci_object_storage_client_get_required_parameter
get_object_stream_to_stringdatajsonloadsr   
setdefaultset_from_config)
parametersconnect_paramsr	   r   r
   auth_method
client_ociget_object_requestget_object_responserespsettings
user_aliaspwdr   r   r   ]/var/www/Datamplify/venv/lib/python3.10/site-packages/oracledb/plugins/oci_config_provider.py_get_config6   sN   





r4   c                 C   s0  |  d|  d}|dur| }|  dt|  d}z|du s%|dkr+t dfW S W nF tjjyr   d| v rpd| v rpt| d	 d
}| }W d   n1 sSw   Y  t	| d | d | d | d	 ||d}|df Y S Y nw |dkrtj
j }t	|d|fS |dkrtj
j }i |fS d}t|)zi
    Returns the appropriate credential given the input supplied by the original
    connect string.
    r   r   N
oci_regionobjservernamer   oci_tenancyoci_useroci_key_fileroci_fingerprint)tenancyr   fingerprintkey_fileprivate_key_contentregionr   )r@   r   z>Authentication options were not available in Connection String)r   r   _retrieve_regionoci_from_fileoci
exceptionsClientErroropenreaddictauthsigners%InstancePrincipalsSecurityTokenSignerget_resource_principals_signer	Exception)r)   r+   r@   file_content
public_keyproviderr
   msgr   r   r3   r   p   sH   
r   connection stringc                 C   s4   z| | W S  t y   d| d| }t|d w )NzParameter named "z" is missing from )KeyErrorrM   )r)   namelocationmessager   r   r3   r!      s   

r!   protocol_argreturnc                 C   s   |  d}t| |d d }t|j}dd | D }t| d| }|rP|d|d< |d|d< |d|d< |d	|d	< |d
rP|d
|d
< |S )zA
    Parse the parameters from the protocol argument string.
    ?   Nc                 S   s   i | ]\}}|  |d  qS )r   )lower).0keyvaluer   r   r3   
<dictcomp>   s    z%_parse_parameters.<locals>.<dictcomp>r6   r   r   r   r   )findr   r   pathitemscloud_net_naming_pattern_ocimatchgroup)rW   pos
parsed_urlparsed_valuesr)   rd   r   r   r3   _parse_parameters   s   


ri   c           	      C   s   t | dd}| d}|d u rtd|d}|d u r"t|\}}|d}|d ur/| }|d u s7|dkr<t|}n!|dkrMtjj	 }t||d	}n|d
kr]tjj
 }t||d	}|j|d}|jjj}t| S )Nr^   z"password" key sectionr   z7OCI Key Vault authentication details were not provided.r   r   r   r   r   r   )	secret_id)r!   r   rM   r   r   oci_secrets_clientrC   rI   rJ   rK   rL   get_secret_bundler$   secret_bundle_contentcontentbase64	b64decodedecode)	argsrj   r   r   r
   r+   secret_client_ociget_secret_bundle_response
b64contentr   r   r3   password_type_oci_vault_hook   s>   




rv   c                 C   s*   | d ur|  d}|d  ddS d S )N.rZ   _-)splitr[   replace)r6   arrr   r   r3   rA      s   
rA   c                 C   s   d |  S )N    )joinrq   )streamr   r   r3   r#      s   r#   protocolr*   c                 C   s   t |}t|| dS )zE
    Hook for handling parameters stored in an OCI Object store.
    N)ri   r4   )r   rW   r*   r)   r   r   r3   config_oci_hook   s   r   r   zconfig-ociobject)rR   )#ro   r%   rC   oracledbreurllib.parser   r   r	   	from_filerB   rD   rE   oci_client_errorobject_storageObjectStorageClientr    secretsSecretsClientrk   compilerc   r4   r   r!   strrH   ri   rv   rA   r#   ConnectParamsr   register_password_typeregister_protocolr   r   r   r3   <module>   s<   :
2(

