o
    PDi$&                     @   s   d dl Z d dlZd dl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mZmZ dd Zd	d
 Zdd ZdddZd ddZdedefddZdd Zdd Zdd Zede ede dS )!    N)urlparseparse_qs)AzureAppConfigurationClient)SecretClient)ResourceNotFoundError)ClientSecretCredentialCertificateCredentialManagedIdentityCredentialChainedTokenCredentialEnvironmentCredentialc                 C   s2   |  d|  d}|d ur| }|dkrd }|S )NauthenticationmethodAZURE_DEFAULT)getupper)
parametersauth_method r   _/var/www/Datamplify/venv/lib/python3.10/site-packages/oracledb/plugins/azure_config_provider.py_get_authentication_method2   s   r   c                 C   s   g }t | }|du s|dkr;d| v r%|tt| dt| dt| d nd| v r;|tt| dt| dt| d |du sC|dkrT| d}|durT|t|d	 t|d
kr`d}t|t|dkrj|d
 S |t	  t
| S )zi
    Returns the appropriate credential given the input supplied by the original
    connect string.
    NAZURE_SERVICE_PRINCIPALazure_client_secretazure_tenant_idazure_client_idazure_client_certificate_pathAZURE_MANAGED_IDENTITY azure_managed_identity_client_id)	client_idr   z>Authentication options were not available in Connection String   )r   appendr   _get_required_parameterr   r   r	   len	Exceptionr   r
   )r   tokensr   r   messager   r   r   _get_credential;   sB   	
r%   c                 C   s   zt | }W n t jy   d}t|w |d|d< d|d< i  |d< }t| |d< }|d u s7|dkrld	|v rQt|d
|d
< t|d|d< t|d	|d	< nd|v rlt|d
f|d
< t|df|d< t|d|d< |d u st|dkr{|d|d< |S )Nz?Password is expected to be JSON containing Azure Vault details.urivalue
azurevaulttyper   r   r   r   r   r   r   r   r   )jsonloadsJSONDecodeErrorr"   popr   r    r   )
pwd_stringr   pwdr$   r   r   r   r   r   _get_passwordg   sL   
r0   connection stringc                 C   s4   z| | W S  t y   d| d| }t|d w )NzParameter named "z" is missing from )KeyErrorr"   )r   namelocationr$   r   r   r   r       s   

r    Tc                 C   sh   z| dr| | }n| d| }| j||d}W |jS  ty3   |r0d| }t|Y dS w )zL
    Returns the configuration setting given the client, key and label.
    /)keylabelz$Missing required configuration key: N)endswithget_configuration_settingr   r"   r'   )clientr6   sub_keyr7   required
actual_keyobjr$   r   r   r   _get_setting   s   

r?   protocol_argreturnc                 C   sj   |  d}t| |d d }t|j}dd | D }| d| d}|ds/|d7 }||d< |S )	zA
    Parse the parameters from the protocol argument string.
    ?r   Nc                 S   s   i | ]\}}|  |d  qS )r   )lower).0r6   r'   r   r   r   
<dictcomp>   s    z%_parse_parameters.<locals>.<dictcomp>r5   z.azconfig.ioappconfigname)findr   r   pathitemsrstripr8   )r@   pos
parsed_urlparsed_valuesr   config_namer   r   r   _parse_parameters   s   


rO   c           	      C   s   t | dd}| d}|d u r | d}|d u rtdt|}td}||}|d u r2td|d}|d	}t||}|	|j
S )
Nr'   z"password" key section
credentialr   z5Azure Vault authentication details were not provided.zT(?P<vault_url>https://[A-Za-z0-9._-]+)/secrets/(?P<secretKey>[A-Za-z][A-Za-z0-9-]*)$zInvalid Azure Vault details	vault_url	secretKey)r    r   r"   r%   recompilematchgroupr   
get_secretr'   )	argsr&   rP   authpatternrU   rQ   
secret_keysecret_clientr   r   r   password_type_azure_vault_hook   s&   





r]   c           	      C   s   t | }tdt| d |}t| d}| d}i }t||d||d< t||d|dd|d< t||d	|dd}|d
urBt|| |d	< t||d|dd|d< t||d|dd|d< t||d|dd}|d
urlt||d< || d
S )zR
    Processes the configuration stored in the Azure App configuration store.
    zhttps://rF   r6   r7   connect_descriptoruserF)r<   passwordNconfig_time_to_live config_time_to_live_grace_periodpyo)	r%   r   r    r   r?   r0   r*   r+   set_from_config)	r   connect_paramsrP   r:   r6   r7   configr/   settingsr   r   r   _process_config   s2   





rh   c                 C   s   t |}t|| dS )zN
    Hook for handling parameters stored in an Azure configuration store.
    N)rO   rh   )protocolr@   re   r   r   r   r   config_azure_hook	  s   rj   r(   zconfig-azure)r1   )T)r*   rS   oracledburllib.parser   r   azure.appconfigurationr   azure.keyvault.secretsr   azure.core.exceptionsr   azure.identityr   r   r	   r
   r   r   r%   r0   r    r?   strdictrO   r]   rh   rj   register_password_typeregister_protocolr   r   r   r   <module>   s&   		,
4
'