Managed Identity fails when running WebApp from Azure VM - azure

I have created a asp.net API that accesses KeyVault to get secrets.
When I run locally on my laptop, I use Managed Identity to access KeyVault with a dedicated Service Princiapl. (with AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_TENANT_ID set in my system
environment variables).
This is working just fine.
Now I have created a VM in Azure to be used as a developer machine. All the tools are installed and configured like they are on my laptop.
But when I run the web API I get a 403 telling me the object ID #### does not have Get/List access to KeyVault.
I've checked and the object ID matches the one of the VM. Indeed the VM does not have this access to KeyVault, however I'd expect the Web API to run with the configured AZURE_CLIENT_ID in my environment variables.
Am I missing something ?

Ensure get/list operations are selected/enabled in the access policy created for your managed identity/service principal:

Related

Azure: System assigned identity cannot be used

One of our app services has a system assigned identity:
We wanted to start using Azure keyvault and use this identity to give this app service access.
Unfortuntatly the identity cannot be found:
Did anybody had a similar problem before?
I tried to find the system assigned identity in other places like Azure Ad but it could not be found there either.
I tried using systen assigned identities for other app services we have and it worked for those. So the problem is only present for that specific one.
I tried to reproduce the same in my environment and got the results successfully as below:
I created an Azure App Service and turned-on System Managed Identity:
Copy the Object ID and you will find the application in Azure Enterprise Applications as below:
Now, I created an Azure Key Vault and created access policy:
Search with the ObjectID of the Application as below:
The access policy created successfully using Service Principal as below:

Provision Secrets from Azure Key Vault to Azure App Service as environment variables

I have:
Azure App Service with a Docker container running in it. Inside the Docker container, there is a Python FastAPI Web App.
Azure Key Vault with some App specific secrets in it (e.g. Public API Client Secret for a side service to send requests to)
I'm trying to set secrets to the App.
There are several examples when an app directly reads the secrets from Key Vault (e.g. official MS documentation), but I believe the provisioning should not be the responsibility of the App.
Probably, secrets should be set as environment variables with the CD pipeline (correct me if it's a bad idea). Otherwise, the App knows details about the infrastructure where it's running, which, I guess, is not the best practice.
Is there a proper way to provision Key Vault secrets to the App environment?
I tried to reproduce the same issue in my environment and got the below results
I have created the app service
The azure key vaults supplies a way to store keys & secrets outside of an application, Using access polices we can allow the application to access the keys with in keyvault
To access the keyvault as a environment variables in the azure app service we have to setup the polices mentioned as below
In the app services => identity => click on status is ON
Go to the keyvault and the polices settings click on add polices
And the access polices page set any permissions for keys and secrets
In the select principal page search for the app service name and select then the app service can access as a environment variables
After that we have to add specific secrets in the app service and copy its in secret identifier as below format
#microsoft.keyvault(secretUri={secret url from the keyvault})
After adding the secret it will look like as below
By following the above steps we can access the secrets as a environment variables

How to use Managed Identity to fetch credentials on AKS environment?

I have node(nestjs) application deploy in AKS, clusters were created using kubenet network. The node application needs to access the sql DB, we have configured managed identities, so it'll be user managed identity.
To create connection I'm using token based approached, where I get the credentials using https://www.npmjs.com/package/#azure/identity package ( using DefaultAzureCredential on local and ManagedIdentityCredential on server), and with those credentials, I get the token, and then token is used in creating the DB connection. For local it worked fine, using DefaultAzureCredential and az cli with user logged in.
But on AKS it throws error as no managed identity found( when ManagedIdentityCredential used), why is that, please help to get to the solution, it has become a huge blocker?
Have you checked if aad identity is created?
kubectl get azureidentity -n $POD_IDENTITY_NAMESPACE
kubectl get azureidentitybinding -n $POD_IDENTITY_NAMESPACE
If not add it on the cluster. There should be pods in a namespace which control the traffic flow to microsoft AAD, so Node Managed Identity(nmi), and describe node reveals pods on the node which intercept the calls.
Check out the docs

Azure - assign managed identity access to VM before setup

I am writing a program in Java that creates a VM instance in Azure, uploads a script to a container, and downloads and executes the script in the VM. However, I am currently facing a difficulty in granting the machine access to the container. When the machine is already up, I can manually go to Azure and assign a role with access, however I want to do that in my program (when creating the VM) before the machine is up, so that the program can run uninterrupted. Is there a way to do that? From the Documentation
Currently, the Azure portal does not support assigning a user-assigned managed identity during the creation of a VM. Instead, refer to one of the following VM creation Quickstart articles to first create a VM, and then proceed to the next section for details on assigning a user-assigned managed identity to the VM
Am I understanding correctly that it is not possible? Is there a workaround?
It is possible, the doc just means you can't do that in the portal, not mean in the code.
In your case, actually I am not sure you want to use system-assigned identity or user-assigned identity.
Here is a sample which creates a Linux VM with system-assigned identity enabled via withSystemAssignedManagedServiceIdentity, if you want to use user-assigned identity, you could change the code to use WithUserAssignedManagedServiceIdentity, you can specific an existing or not-yet-created user assigned identity, it depends on your requirement.
VirtualMachine virtualMachine = azureResourceManager.virtualMachines()
.define(linuxVMName)
.withRegion(region)
.withNewResourceGroup(rgName)
.withNewPrimaryNetwork("10.0.0.0/28")
.withPrimaryPrivateIPAddressDynamic()
.withNewPrimaryPublicIPAddress(pipName)
.withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
.withRootUsername(userName)
.withRootPassword(password)
.withSize(VirtualMachineSizeTypes.STANDARD_DS2_V2)
.withOSDiskCaching(CachingTypes.READ_WRITE)
.withSystemAssignedManagedServiceIdentity()
.create();

Unable to Import Key Vault Certificate in Azure Government Cloud

I am trying to import a certificate from a Key Vault to an App Service to configure SSL in the Azure Government Cloud. When I do this I get an error stating:
Failed to get App Service Service principal details.
I am getting a similar error when I try to do this through an ARM template which it what caused me to try this manually. I have tried giving my App Service a managed identity and giving that identity access to the key vault. I have tried a technique that worked in the regular Azure Cloud of giving "Microsoft Azure App Service" account permission to the key vault but that doesn't seem to exist in the Government Cloud.
I would have expected this to simply work and allow me to configure my SSL correctly on the app service so I don't need to manage the certificates individually on every app service.
You have to enable in the Identity of your app services that can be assigned through the azure permissions, then you have to go to the KeyVault and grant the permissions to the App Services.
https://learn.microsoft.com/en-us/azure/app-service/media/app-service-managed-service-identity/msi-blade-system.png
More information:
https://learn.microsoft.com/en-us/azure/app-service/overview-managed-identity
https://learn.microsoft.com/es-es/azure/key-vault/tutorial-net-create-vault-azure-web-app
https://azure.microsoft.com/en-us/resources/samples/app-service-msi-keyvault-dotnet/
I eventually found the solution to the issue.
Following the directions found here:
https://github.com/Azure/azure-quickstart-templates/tree/master/201-web-app-certificate-from-key-vault
I tried to authorize the 'Microsoft.Azure.WebSites' Resource Provider as described in the link, but that GUID doesn't exist in the Government Cloud.
This link however does give you the equivalent GUID for the Government Cloud:
https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/azure-government/documentation-government-services-webandmobile.md#app-services
After using the script from the first link with the GUID value from the second link I was able to get both deployments and manual SSL added.

Resources