o
    ix)                  	   @   s   d dl Z d dlZd dlmZmZmZmZmZ d dlm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZ e eZdee d	eeeef  d
eeeef  fddZG dd dZdS )    N)OptionalAnyMappingcastTuple)AccessTokenAccessTokenInfoTokenRequestOptionsTokenCredentialSupportsTokenInfo   )CredentialUnavailableError)EnvironmentVariables)log_get_token	client_ididentity_configreturnc                    s    r@h d}| r!t  fdd|D rtdd| d| fS d }|D ]}| v r=|r7tdd| | | f}q%|S | rFd| fS d S )N>   Z	object_idr   Zresource_idc                 3   s    | ]}| v V  qd S N ).0keyr   r   c/home/app/Keep/.python/lib/python3.10/site-packages/azure/identity/_credentials/managed_identity.py	<genexpr>   s    z+validate_identity_config.<locals>.<genexpr>zrWhen 'client_id' is provided as a keyword argument, 'identity_config' must not contain any of the following keys: z, r   zFidentity_config must not contain more than one of the following keys: )any
ValueErrorjoin)r   r   Z
valid_keysresultr   r   r   r   validate_identity_config   s2   r   c                   @   s   e Zd ZdZddddee deeeef  deddfdd	Zdd
dZ	deddfddZ
dddZeddddedee dee dedef
ddZedddedee defddZdS )ManagedIdentityCredentialaK  Authenticates with an Azure managed identity in any hosting environment which supports managed identities.

    This credential defaults to using a system-assigned identity. To configure a user-assigned identity, use one of
    the keyword arguments. See `Microsoft Entra ID documentation
    <https://learn.microsoft.com/entra/identity/managed-identities-azure-resources/overview>`__ for more
    information about configuring managed identity for applications.

    :keyword str client_id: a user-assigned identity's client ID or, when using Pod Identity, the client ID of a
       Microsoft Entra app registration. This argument is supported in all hosting environments.
    :keyword identity_config: a mapping ``{parameter_name: value}`` specifying a user-assigned identity by its object
        or resource ID, for example ``{"object_id": "..."}``. Check the documentation for your hosting environment to
        learn what values it expects.
    :paramtype identity_config: Mapping[str, str]

    .. admonition:: Example:

        .. literalinclude:: ../samples/credential_creation_code_snippets.py
            :start-after: [START create_managed_identity_credential]
            :end-before: [END create_managed_identity_credential]
            :language: python
            :dedent: 4
            :caption: Create a ManagedIdentityCredential.
    Nr   r   r   r   kwargsr   c                K   s8  t ||}d | _|dd}|dd | _d }tjtjrotjtj	rTtjtj
r@d}ddlm} |d||d|| _nd}dd	lm} |d||d|| _ntjtjrnd
}ddlm}	 |	d||d|| _ntjtjrtjtjrd}ddlm}
 |
d||d|| _ned}ddlm} |d||d|| _nQtdd tjD r|sddlm} |ptjtj}|stdd}d|f}|dtjtj |tjtj d|| _nd}ddl m!} |d||| jd|| _|r| j"j# d| }|r|d|d  d|d  7 }t$%| d S d S )NZ%_exclude_workload_identity_credentialF_enable_imds_probezService Fabric managed identity   )ServiceFabricCredentialr    zApp Service managed identity)AppServiceCredentialzAzure Arc managed identity)AzureArcCredentialzAzure ML managed identity)AzureMLCredentialzCloud Shell managed identity)CloudShellCredentialc                 s   s    | ]	}t j|V  qd S r   )osenvironget)r   varr   r   r   r   s   s    z5ManagedIdentityCredential.__init__.<locals>.<genexpr>)WorkloadIdentityCredentialzWorkload identity was selected but no client ID was provided. Configure the environment with a client ID or pass a value for "client_id" argumentzworkload identityr   )	tenant_idr   Ztoken_file_pathZIMDS)ImdsCredential)r   r   r"   z
 will use z with r   z: r   )&r   _credentialpopr"   r)   r*   r+   r   ZIDENTITY_ENDPOINTZIDENTITY_HEADERZIDENTITY_SERVER_THUMBPRINTZservice_fabricr$   Zapp_servicer%   ZIMDS_ENDPOINTZ	azure_arcr&   ZMSI_ENDPOINTZ
MSI_SECRETZazure_mlr'   Zcloud_shellr(   allZWORKLOAD_IDENTITY_VARSZworkload_identityr-   ZAZURE_CLIENT_IDr   ZAZURE_TENANT_IDZAZURE_FEDERATED_TOKEN_FILEZimdsr/   	__class____name___LOGGERinfo)selfr   r   r!   Zuser_identity_infoZexclude_workload_identityZmanaged_identity_typer$   r%   r&   r'   r(   r-   Zworkload_client_idr/   Zlog_msgr   r   r   __init__I   s   





z"ManagedIdentityCredential.__init__c                 C   s   | j r| j   | S r   )r0   	__enter__r7   r   r   r   r9      s   
z#ManagedIdentityCredential.__enter__argsc                 G   s   | j r| j j|  d S d S r   )r0   __exit__)r7   r;   r   r   r   r<      s   z"ManagedIdentityCredential.__exit__c                 C   s   |    dS )z)Close the credential's transport session.N)r<   r:   r   r   r   close   s   zManagedIdentityCredential.closeclaimsr.   scopesr?   r.   c                O   s.   | j stddtt| j j|||d|S )ac  Request an access token for `scopes`.

        This method is called automatically by Azure SDK clients.

        :param str scopes: desired scope for the access token. This credential allows only one scope per request.
            For more information about scopes, see
            https://learn.microsoft.com/entra/identity-platform/scopes-oidc.
        :keyword str claims: additional claims required in the token, such as those returned in a resource provider's
            claims challenge following an authorization failure.
        :keyword str tenant_id: not used by this credential; any value provided will be ignored.

        :return: An access token with the desired scopes.
        :rtype: ~azure.core.credentials.AccessToken
        :raises ~azure.identity.CredentialUnavailableError: managed identity isn't available in the hosting environment
        No managed identity endpoint found. 
The Target Azure platform could not be determined from environment variables. 
Visit https://aka.ms/azsdk/python/identity/managedidentitycredential/troubleshoot to troubleshoot this issue.messager>   )r0   r   r   r
   	get_token)r7   r?   r.   r@   r!   r   r   r   rD      s
   z#ManagedIdentityCredential.get_token)optionsrE   c                G   s(   | j stddtt| j j|d|iS )a  Request an access token for `scopes`.

        This is an alternative to `get_token` to enable certain scenarios that require additional properties
        on the token. This method is called automatically by Azure SDK clients.

        :param str scopes: desired scope for the access token. This credential allows only one scope per request.
            For more information about scopes, see https://learn.microsoft.com/entra/identity-platform/scopes-oidc.
        :keyword options: A dictionary of options for the token request. Unknown options will be ignored. Optional.
        :paramtype options: ~azure.core.credentials.TokenRequestOptions

        :rtype: ~azure.core.credentials.AccessTokenInfo
        :return: An AccessTokenInfo instance containing information about the token.
        :raises ~azure.identity.CredentialUnavailableError: managed identity isn't available in the hosting environment.
        rA   rB   rE   )r0   r   r   r   get_token_info)r7   rE   r@   r   r   r   rF      s
   z(ManagedIdentityCredential.get_token_info)r   r   )r   N)r4   
__module____qualname____doc__r   strr   r   r8   r9   r<   r=   r   r   rD   r	   r   rF   r   r   r   r   r   0   s<    

P
&r   )loggingr)   typingr   r   r   r   r   Zazure.core.credentialsr   r   r	   r
   r    r   
_constantsr   Z_internal.decoratorsr   	getLoggerr4   r5   rJ   r   r   r   r   r   r   <module>   s    

