o
    i                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZ ddlmZ G d	d
 d
eZdedededefddZdedefddZdefddZdeddfddZG dd de	ZdS )    N)Dict)ClientAuthenticationError)HttpRequest)
HTTPPolicy)PipelineRequestPipelineResponse   )MsalManagedIdentityClientc                   @   s    e Zd ZddedefddZdS )AzureArcCredential descreturnc                 C   s
   d| S )NzCAzure Arc managed identity configuration not found in environment.  )selfr   r   r   \/home/app/Keep/.python/lib/python3.10/site-packages/azure/identity/_credentials/azure_arc.pyget_unavailable_message   s   
z*AzureArcCredential.get_unavailable_messageN)r   )__name__
__module____qualname__strr   r   r   r   r   r
      s    r
   urlscopeidentity_configr   c                 C   s.   |rt ddtd| td|dfi |dS )Na  User assigned managed identities are not supported by Azure Arc. To authenticate with the system assigned identity omit the client id when constructing the credential, and if authenticating with DefaultAzureCredential ensure the AZURE_CLIENT_ID environment variable is not set.messageGETz
2020-06-01)zapi-versionresource)params)r   r   dict)r   r   r   r   r   r   _get_request   s
    r   responsec                 C   s   | j jd}|stddz	|dd }W n ty, } z	td|d|d }~ww zt| W n tyH } z	td|d|d }~ww t	|dd	d
$}z|
 W W  d    S  tys } z
td||d|d }~ww 1 sww   Y  d S )NzWWW-Authenticatez4Did not receive a value from WWW-Authenticate headerr   =   z@Did not receive a correct value from WWW-Authenticate header: {}z The key file path is invalid: {}rzutf-8)encodingz#Could not read file {} contents: {})http_responseheadersgetr   split
IndexErrorformat_validate_key_file
ValueErroropenread	Exception)r    headerkey_fileexfileerrorr   r   r   _get_secret_key!   s@   

r5   c                  C   sV   t jdrdS t jdr#tjd} | stdtj|  ddS tdt j )	zReturns the expected path for the Azure Arc MSI key file based on the current platform.

    Only Linux and Windows are supported.

    :return: The expected path.
    :rtype: str
    :raises ValueError: If the current platform is not supported.
    linuxz/var/opt/azcmagent/tokenswinZPROGRAMDATAz8PROGRAMDATA environment variable is not set or is empty.ZAzureConnectedMachineAgentZTokensz0Azure Arc MSI is not supported on this platform )	sysplatform
startswithosenvironr'   r,   pathjoin)Zprogram_data_pathr   r   r   _get_key_file_path>   s   	r?   	file_pathc                 C   sx   | st dtj| st d|  t }tj| |ks%t d|  | ds.t dtj| dkr:t ddS )	aa  Validates that a given Azure Arc MSI file path is valid for use.

    A valid file will:
        1. Be in the expected path for the current platform.
        2. Have a `.key` extension.
        3. Be at most 4096 bytes in size.

    :param str file_path: The path to the key file.
    :raises ClientAuthenticationError: If the file path is invalid.
    z The file path must not be empty.zThe file path does not exist: z)Unexpected file path from HIMDS service: z.keyz+The file path must have a '.key' extension.i   z7The file size must be less than or equal to 4096 bytes.N)r,   r;   r=   existsr?   dirnameendswithgetsize)r@   Zexpected_directoryr   r   r   r+   Q   s   
r+   c                   @   s"   e Zd ZdZdedefddZdS )ArcChallengeAuthPolicyz8Policy for handling Azure Arc's challenge authenticationrequestr   c                 C   sN   d|j jd< | j|}|jjdkr%t|}d||j jd< | j|}|S )NtrueZMetadatai  zBasic {}Authorization)http_requestr&   nextsendr%   status_coder5   r*   )r   rF   r    Z
secret_keyr   r   r   rK   p   s   zArcChallengeAuthPolicy.sendN)r   r   r   __doc__r   r   rK   r   r   r   r   rE   m   s    rE   )r;   r8   typingr   Zazure.core.exceptionsr   Zazure.core.restr   Zazure.core.pipeline.policiesr   Zazure.core.pipeliner   r   Z&_internal.msal_managed_identity_clientr	   r
   r   r   r5   r?   r+   rE   r   r   r   r   <module>   s   