Accessing Managed Identity in Docker Compose App Service

Umer Rashid 110 Reputation points
2024-01-30T10:41:45.35+00:00

I am running Docker Compose in an Azure App Service that has system-assigned managed identity enabled. I deployed this docker compose on Azure Container Registry that also had system-assigned managed identity enabled. I also granted this App Service's managed identity the access to a key vault and assigned it the role of 'Key Vault Administrator'. When I run this app, I get this error: "const err = new AggregateAuthenticationError(errors, "ChainedTokenCredential authentication failed."); AggregateAuthenticationError: ChainedTokenCredential authentication failed. CredentialUnavailableError: EnvironmentCredential is unavailable. No underlying credential could be used. To troubleshoot, visit https://aka.ms/azsdk/js/identity/environmentcredential/troubleshoot. CredentialUnavailableError: WorkloadIdentityCredential: is unavailable. tenantId, clientId, and federatedTokenFilePath are required parameters." Can anyone please tell me if I have to make some other configurations to make this work? Regards, Umar

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,500 questions
0 comments No comments
{count} votes

Accepted answer
  1. brtrach-MSFT 17,476 Reputation points Microsoft Employee
    2024-02-08T02:41:05.11+00:00

    @Umer Rashid It seems like you are trying to access a key vault from your Docker Compose app running in an Azure App Service with system-assigned managed identity enabled. The error message you are seeing indicates that the app is unable to authenticate using the available credentials. To troubleshoot this issue, you can try the following steps:

    1. Make sure that the managed identity of your App Service has been granted access to the key vault and has the appropriate permissions. You can check this by going to the Access policies tab in the Azure portal and verifying that the managed identity is listed with the correct role.
    2. Make sure that your Docker Compose app is running with the correct environment variables. You can set the environment variables for your app in the Docker Compose file or in the Azure portal. The environment variables should include the following:
      • AZURE_CLIENT_ID: The client ID of the managed identity.
      • AZURE_CLIENT_SECRET: The client secret of the managed identity. This is not required for system-assigned managed identities.
      • AZURE_TENANT_ID: The tenant ID of the Azure AD directory that the managed identity belongs to.
      • AZURE_MANAGED_IDENTITY_ENDPOINT: The endpoint for the managed identity service. This is not required for system-assigned managed identities.
    3. Make sure that your Docker Compose app is using the correct Azure SDK version and that the SDK is configured to use the managed identity. You can use the DefaultAzureCredential class from the Azure SDK to authenticate with the managed identity. Here's an example of how you can use the DefaultAzureCredential class to authenticate with the managed identity:
    const { DefaultAzureCredential } = require("@azure/identity");
    const { SecretClient } = require("@azure/keyvault-secrets");
    
    const credential = new DefaultAzureCredential();
    const vaultName = "<your-key-vault-name>";
    const url = `https://${vaultName}.vault.azure.net`;
    
    const client = new SecretClient(url, credential);
    
    const secretName = "<your-secret-name>";
    const secret = await client.getSecret(secretName);
    
    console.log(`The value of ${secretName} is ${secret.value}.`);
    
    
    

    You are correct that system-assigned managed identities are not supported with multi-container web apps in Azure App Service. In this case, using a service principal is a recommended approach to access Key Vault from a Docker Compose web app. To use a service principal to access Key Vault from a Docker Compose web app, you can follow the steps outlined in the article you mentioned. Here's a summary of the steps: Create a service principal in Azure AD and grant it access to the Key Vault. Store the service principal's credentials in the environment file of your Docker Compose app. Use the Azure SDK to authenticate with the service principal and access the Key Vault secrets.

    Here's an example of how you can use the Azure SDK to authenticate with the service principal and access the Key Vault secrets:

    const { ClientSecretCredential } = require("@azure/identity");
    const { SecretClient } = require("@azure/keyvault-secrets");
    
    const clientId = "<your-service-principal-client-id>";
    const clientSecret = "<your-service-principal-client-secret>";
    const tenantId = "<your-tenant-id>";
    const vaultName = "<your-key-vault-name>";
    const url = `https://${vaultName}.vault.azure.net`;
    
    const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
    const client = new SecretClient(url, credential);
    
    const secretName = "<your-secret-name>";
    const secret = await client.getSecret(secretName);
    
    console.log(`The value of ${secretName} is ${secret.value}.`);
    
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Deepanshu katara 14,810 Reputation points MVP
    2024-01-30T10:55:21.12+00:00

    Hi Umer, As of now managed identities is not supported with Multi-container webapps . Web App for Multi Containers is still in preview. You can keep up to date by checking this documentation or on the App Service Team blog site. Please let us know if you have further questions. Thanks Deepanshu


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.