I would like to retrive the keyvault secrets using managed identity in web Api.
- Able to retrive the data using aboe code.
- Built image and pushed into Azure container registry
- Created azure container instace using above image
- Provided Key Vault Reader and Key Vault Secrets User by slected the instance using managed identity
- When I try to access the API that reads the data from AzureKeyVault, it is giving 404 erro.
ManagedIdentityCredential authentication failed: DefaultIdentityNotDefined rnStatus: 404 (Not Found)rnErrorCode: DefaultIdentityNotDefinedrnrnContent:rn{rn "error": {rn "code": "DefaultIdentityNotDefined
How can I access the keyvault data inside container instace web api?
2
Answers
It seems you’re using Azure Container Apps and you have 2 options in there:
For any of them, the configuration must be enabled and done like this:
And the process to do so it’s defined in the oficial documentation in here
Also, in order to have access to the KeyVault Secrets, you must add an Access Policy to that Managed Identity with at least Get and List secrets. It might be worth to take a look at the section to add the KV Secret references to your Container App in here
As per the error, it seems you haven’t enable the Managed Identity yet; therefore, there would be no Identity defined to connect to the KV.
UPDATE
Since what you’re using is Azure Container Instance, then you should follow the official guide for enabling the Managed Identity
If you look into the docs of
DefaultAzureCredential
, it combines several credential types:…
…
If your instance can’t connect to the Azure key vault, it may not have the correct permissions for managed identity.
You need to enable managed identity on your App Service.
https://learn.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=portal%2Chttp#add-a-system-assigned-identity
You need to assign permissions to access key vault for this managed identity:
https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/tutorial-windows-vm-access-nonaad