|
| 1 | +# Troubleshooting Azure Identity Authentication Issues |
| 2 | + |
| 3 | +The Azure Identity SDK offers various `TokenCredential` implementations. These implementations typically throw `CredentialUnavailableError` and `ClientAuthenticationError` exceptions. |
| 4 | +The `CredentialUnavailableError` indicates that the credential cannot execute in the current environment setup due to lack of required configuration. |
| 5 | +The `ClientAuthenticationError` indicates that the credential was able to run/execute but ran into an authentication issue from the server's end. This can happen due to invalid configuration/details passed in to the credential at construction time. |
| 6 | +This troubleshooting guide covers mitigation steps to resolve these exceptions thrown by various `TokenCredential` implementations in the Azure Identity Python client library. |
| 7 | + |
| 8 | +## Table of contents |
| 9 | + |
| 10 | +- [Troubleshooting Default Azure Credential Authentication Issues](#troubleshooting-default-azure-credential-authentication-issues) |
| 11 | +- [Troubleshooting Environment Credential Authentication Issues](#troubleshooting-environment-credential-authentication-issues) |
| 12 | +- [Troubleshooting Service Principal Authentication Issues](#troubleshooting-service-principal-authentication-issues) |
| 13 | +- [Troubleshooting Username Password Authentication Issues](#troubleshooting-username-password-authentication-issues) |
| 14 | +- [Troubleshooting Managed Identity Authentication Issues](#troubleshooting-managed-identity-authentication-issues) |
| 15 | +- [Troubleshooting Visual Studio Code Authentication Issues](#troubleshooting-visual-studio-code-authentication-issues) |
| 16 | +- [Troubleshooting Azure CLI Authentication Issues](#troubleshooting-azure-cli-authentication-issues) |
| 17 | +- [Troubleshooting Azure Powershell Authentication Issues](#troubleshooting-azure-powershell-authentication-issues) |
| 18 | + |
| 19 | +## Troubleshooting Default Azure Credential Authentication Issues |
| 20 | + |
| 21 | +### Credential Unavailable Error |
| 22 | + |
| 23 | +The `DefaultAzureCredential` attempts to retrieve an access token by sequentially invoking a chain of credentials. The `ClientAuthenticationError` in this scenario signifies that all the credentials in the chain failed to retrieve the token in the current environment setup/configuration. You need to follow the configuration instructions for the respective credential you're looking to use via `DefaultAzureCredential` chain, so that the credential can work in your environment. |
| 24 | + |
| 25 | +Please follow the configuration instructions in the `Credential Unavailable Error` section of the troubleshooting guidelines below for the respective credential/authentication type you want to use via `DefaultAzureCredential`: |
| 26 | + |
| 27 | +| Credential Type | Troubleshoot Guide | |
| 28 | +| --- | --- | |
| 29 | +| Environment Credential | [Environment Credential Troubleshooting Guide](#troubleshooting-environment-credential-authentication-issues) | |
| 30 | +| Managed Identity Credential | [Managed Identity Troubleshooting Guide](#troubleshooting-managed-identity-authentication-issues) | |
| 31 | +| Visual Studio Code Credential | [Visual Studio Code Troubleshooting Guide](#troubleshooting-visual-studio-code-authentication-issues) | |
| 32 | +| Azure CLI Credential | [Azure CLI Troubleshooting Guide](#troubleshooting-azure-cli-authentication-issues) | |
| 33 | +| Azure Powershell Credential | [Azure Powershell Troubleshooting Guide](#troubleshooting-azure-powershell-authentication-issues) | |
| 34 | + |
| 35 | +## Troubleshooting Environment Credential Authentication Issues |
| 36 | + |
| 37 | +### Credential Unavailable Error |
| 38 | + |
| 39 | +#### Environment variables not configured |
| 40 | + |
| 41 | +The `EnvironmentCredential` supports Service Principal authentication and Username + Password authentication. To utilize the desired way of authentication via `EnvironmentCredential`, you need to ensure the environment variables below are configured properly and the application is able to read them. |
| 42 | + |
| 43 | +##### Service principal with secret |
| 44 | + |
| 45 | +| Variable Name | Value | |
| 46 | +| --- | --- | |
| 47 | +AZURE_CLIENT_ID | ID of an Azure Active Directory application. | |
| 48 | +AZURE_TENANT_ID |ID of the application's Azure Active Directory tenant. | |
| 49 | +AZURE_CLIENT_SECRET | One of the application's client secrets. | |
| 50 | + |
| 51 | +##### Service principal with certificate |
| 52 | + |
| 53 | +| Variable name | Value | |
| 54 | +| --- | --- | |
| 55 | +AZURE_CLIENT_ID |ID of an Azure Active Directory application. | |
| 56 | +AZURE_TENANT_ID | ID of the application's Azure Active Directory tenant. | |
| 57 | +AZURE_CLIENT_CERTIFICATE_PATH | Path to a PEM-encoded or PKCS12 certificate file including private key (without password protection). | |
| 58 | + |
| 59 | +##### Username and password |
| 60 | + |
| 61 | +| Variable name | Value | |
| 62 | +| --- | --- | |
| 63 | +AZURE_CLIENT_ID | ID of an Azure Active Directory application. | |
| 64 | +AZURE_USERNAME | A username (usually an email address). | |
| 65 | +AZURE_PASSWORD | The associated password for the given username. | |
| 66 | + |
| 67 | +### Client Authentication Error |
| 68 | + |
| 69 | +The `EnvironmentCredential` supports Service Principal authentication and Username + Password authentication. |
| 70 | +Please follow the troubleshooting guidelines below for the respective authentication which you tried and failed. |
| 71 | + |
| 72 | +| Authentication Type | Troubleshoot Guide | |
| 73 | +| --- | --- | |
| 74 | +| Service Principal | [Service Principal Auth Troubleshooting Guide](#troubleshooting-username-password-authentication-issues) | |
| 75 | +| Username Password | [Username Password Auth Troubleshooting Guide](#troubleshooting-username-password-authentication-issues) | |
| 76 | + |
| 77 | +## Troubleshooting Username Password Authentication Issues |
| 78 | + |
| 79 | +### Two Factor Authentication Required Error |
| 80 | + |
| 81 | +The `UsernamePassword` credential works only for users whose two factor authentication has been disabled in Azure Active Directory. You can change the Multi Factor Authentication in Azure Portal by following the steps [here](https://docs.microsoft.com/azure/active-directory/authentication/howto-mfa-userstates#change-the-status-for-a-user). |
| 82 | + |
| 83 | +## Troubleshooting Service Principal Authentication Issues |
| 84 | + |
| 85 | +### Illegal/Invalid Argument Issues |
| 86 | + |
| 87 | +#### Client Id |
| 88 | + |
| 89 | +The Client Id is the application Id of the registered application / service principal in Azure Active Directory. |
| 90 | +It is a required parameter for `ClientSecretCredential` and `ClientCertificateCredential`. If you have already created your service principal |
| 91 | +then you can retrieve the client/app id by following the instructions [here](https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal#get-tenant-and-app-id-values-for-signing-in). |
| 92 | + |
| 93 | +#### Tenant Id |
| 94 | + |
| 95 | +The tenant id is te Global Unique Identifier (GUID) that identifies your organization. It is a required parameter for |
| 96 | +`ClientSecretCredential` and `ClientCertificateCredential`. If you have already created your service principal |
| 97 | +then you can retrieve the client/app id by following the instructions [here](https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal#get-tenant-and-app-id-values-for-signing-in). |
| 98 | + |
| 99 | +### Client Secret Credential Issues |
| 100 | + |
| 101 | +#### Client Secret Argument |
| 102 | + |
| 103 | +The client secret is the secret string that the application uses to prove its identity when requesting a token; this can also can be referred to as an application password. |
| 104 | +If you have already created a service principal you can follow the instructions [here](https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal#option-2-create-a-new-application-secret) to create a client secret for your application. |
| 105 | + |
| 106 | +### Client Certificate Credential Issues |
| 107 | + |
| 108 | +#### Client Certificate Argument |
| 109 | + |
| 110 | +The `Client Certificate Credential` accepts `pfx` and `pem` certificates. The certificate needs to be associated with your registered application/service principal. To create and associate a certificate with your registered app, please follow the instructions [here](https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal#option-1-upload-a-certificate). |
| 111 | + |
| 112 | +### Create a new service principal |
| 113 | + |
| 114 | +Please follow the instructions [here](https://docs.microsoft.com/cli/azure/create-an-azure-service-principal-azure-cli) to create a new service principal. |
| 115 | + |
| 116 | +## Troubleshooting Managed Identity Authentication Issues |
| 117 | + |
| 118 | +### Credential Unavailable Error |
| 119 | + |
| 120 | +#### Connection Timed Out / Connection could not be established / Target Environment could not be determined |
| 121 | + |
| 122 | +Currently azure-identity supports [managed identity authentication](https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/overview) |
| 123 | +in the below listed Azure services; ensure you're running your application on one of these resources and have enabled the Managed Identity on |
| 124 | +them by following the instructions at their configuration links below. |
| 125 | + |
| 126 | +Azure Service | Managed Identity Configuration |
| 127 | +--- | --- | |
| 128 | +[Azure Virtual Machines](https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/how-to-use-vm-token) | [Configuration Instructions](https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vm) |
| 129 | +[Azure App Service](https://docs.microsoft.com/azure/app-service/overview-managed-identity?tabs=python) | [Configuration Instructions](https://docs.microsoft.com/azure/app-service/overview-managed-identity?tabs=python) |
| 130 | +[Azure Kubernetes Service](https://docs.microsoft.com/azure/aks/use-managed-identity) | [Configuration Instructions](https://docs.microsoft.com/azure/aks/use-managed-identity) |
| 131 | +[Azure Cloud Shell](https://docs.microsoft.com/azure/cloud-shell/msi-authorization) | | |
| 132 | +[Azure Arc](https://docs.microsoft.com/azure/azure-arc/servers/managed-identity-authentication) | [Configuration Instructions](https://docs.microsoft.com/azure/azure-arc/servers/security-overview#using-a-managed-identity-with-arc-enabled-servers) |
| 133 | +[Azure Service Fabric](https://docs.microsoft.com/azure/service-fabric/concepts-managed-identity) | [Configuration Instructions](https://docs.microsoft.com/azure/service-fabric/configure-existing-cluster-enable-managed-identity-token-service) |
| 134 | + |
| 135 | +## Troubleshooting Visual Studio Code Authentication Issues |
| 136 | + |
| 137 | +### Credential Unavailable Error |
| 138 | + |
| 139 | +#### Failed To Read VS Code Credentials / Authenticate via Azure Tools plugin in VS Code |
| 140 | + |
| 141 | +THe `VS Code Credential` failed to read the credential details from the cache. |
| 142 | + |
| 143 | +The Visual Studio Code authentication is handled by an integration with the Azure Account extension. |
| 144 | +To use this form of authentication, ensure that you have installed the Azure Account extension, |
| 145 | +then use View > Command Palette to execute the Azure: Sign In command. This command opens a browser window and displays a page that allows you |
| 146 | +to sign in to Azure. After you've completed the login process, you can close the browser as directed. Running your application |
| 147 | +(either in the debugger or anywhere on the development machine) will use the credential from your sign-in. |
| 148 | + |
| 149 | +If you already had the Azure Account extension installed and had logged in to your account. Then try logging out and logging in again, as |
| 150 | +that will re-populate the cache on the disk and potentially mitigate the error you're getting. |
| 151 | + |
| 152 | +#### Msal Interaction Required Error |
| 153 | + |
| 154 | +THe `VS Code Credential` was able to read the cached credentials from the cache but the cached token is likely expired. |
| 155 | +Log into the Azure Account extension by via View > Command Palette to execute the Azure: Sign In command in the VS Code IDE. |
| 156 | + |
| 157 | +#### ADFS Tenant Not Supported |
| 158 | + |
| 159 | +The ADFS Tenants are not supported via the Azure Account extension in VS Code currently. |
| 160 | +The supported clouds are: |
| 161 | + |
| 162 | +Azure Cloud | Cloud Authority Host |
| 163 | +--- | --- | |
| 164 | +AZURE PUBLIC CLOUD | https://login.microsoftonline.com/ |
| 165 | +AZURE GERMANY | https://login.microsoftonline.de/ |
| 166 | +AZURE CHINA | https://login.chinacloudapi.cn/ |
| 167 | +AZURE GOVERNMENT | https://login.microsoftonline.us/ |
| 168 | + |
| 169 | +## Troubleshooting Azure CLI Authentication Issues |
| 170 | + |
| 171 | +### Credential Unavailable Error |
| 172 | + |
| 173 | +#### Azure CLI Not Installed |
| 174 | + |
| 175 | +To use Azure CLI credential, the Azure CLI needs to be installed, please follow the instructions [here](https://docs.microsoft.com/cli/azure/install-azure-cli) |
| 176 | +to install it for your platform and then try running the credential again. |
| 177 | + |
| 178 | +#### Azure account not logged in |
| 179 | + |
| 180 | +`AzureCliCredential` authenticates as the identity currently logged in to Azure CLI. |
| 181 | +You need to login to your account in Azure CLI via `az login` command. You can further read instructions to [Sign in with Azure CLI](https://docs.microsoft.com/cli/azure/authenticate-azure-cli). |
| 182 | +Once logged in try running the credential again. |
| 183 | + |
| 184 | +### Illegal State |
| 185 | + |
| 186 | +#### Safe Working Directory Not Located |
| 187 | + |
| 188 | +The `Azure CLI Credential` was not able to locate a value for System Environment property `SystemRoot` to execute in. |
| 189 | +Please ensure the `SystemRoot` environment variable is configured to a safe working directory and then try running the credential again. |
| 190 | + |
| 191 | +## Troubleshooting Azure Powershell Authentication Issues |
| 192 | + |
| 193 | +### Credential Unavailable Error |
| 194 | + |
| 195 | +#### Powershell not installed |
| 196 | + |
| 197 | +Please ensure PowerShell is installed on your platform by following the instructions [here](https://docs.microsoft.com/powershell/scripting/install/installing-powershell?view=powershell-7.1). |
| 198 | + |
| 199 | +#### Azure Az Module Not Installed |
| 200 | + |
| 201 | +Please follow the instructions [here](https://docs.microsoft.com/powershell/azure/install-az-ps) |
| 202 | +to install the Azure Az PowerShell module. |
| 203 | + |
| 204 | +#### Azure account not logged in |
| 205 | + |
| 206 | +Log in via the `Connect-AzAccount` command. See [Sign in with Azure Powershell](https://docs.microsoft.com/powershell/azure/authenticate-azureps) for more information. |
| 207 | + |
| 208 | +#### Deserialization error |
| 209 | + |
| 210 | +The `Azure Powershell Credential` was able to retrieve a response from the Azure Powershell when attempting to get an access token but failed |
| 211 | +to parse that response. |
| 212 | +In your local powershell window, run the following command to ensure that Azure Powershell is returning an access token in correct format. |
| 213 | + |
| 214 | +```pwsh |
| 215 | +Get-AzAccessToken -ResourceUrl "<Scopes-Url>" |
| 216 | +``` |
| 217 | + |
| 218 | +In the event above command is not working properly, follow the instructions to resolve the Azure Powershell issue being faced and then try running the credential again. |
0 commit comments