Automation in Azure web apps to import key vault certificate - azure-web-app-service

Is there a way to automate the process of importing a key vault certificate within Azure app services? I'm referring specifically to the option in the portal circled below. I can't find anything in PowerShell or ARM Templates.
I found a way to do this as a one-time deployment via ARM templates, but that option does not rotate the certificate if it rotates in KV. To my understanding the "import" functionality in circled below maintains the connection to KV and handles the rotation automatically.
If there's a way to automate this, it would be great, rather than having to go through the portal and click these buttons for each region I deploy each service to.

Looks like there is a web job that does this when a new version of certificate is created in KeyVault. According to the blog, the web job that applies the new certificate runs once a day so you have to time your creation of new version of the certificate in KeyVault at least a day before the expiry so certificate gets renewed before the expiry.

Related

Can I move a certificate purchased in one Azure subscription to another Azure subscription later?

I am thinking of buying a wildcard certificate for my domain. In the near future, we plan to move the app and app service and databases to another Azure subscription. I would like to know if it is possible to move the wildcard certificate to the new subscription?
According to the documentation, it should be able to meet your requirements, but there are some things that need attention. Move guidance for App Service resources
You need to try,if can't. You will need to delete the SSL bindings, and move a certificate bound to the new website.
To add to this you can refer to the following Azure documentation: Purchase, Store and Assign an SSL Certificate for your custom domain for details on this topic. If you plan to move App Service resources to a new resource group or subscription, there are a few limitations to be aware of, see App Service limitations.
Also, you can migrate your App Service Certificate within the Azure portal. You can also export it as a PFX file for use in another subscription. See this blog post for more information.

Azure deployment deletes configuration

I have configured a new .Net core application with CI/CD trough azure devops. I use the configuration tab in the application in azure to set settings - like token secrets, for example. However, every time the CI/CD deploys a new version, those settings are erased and I have to set them again.
Does anyone know how to stop this from happening?
I do not think that you can "stop" this from happening. The deployment of an application includes appication settings.
What you can do is to get your CI/CD pipeline to set the settings correctly. See: https://devblogs.microsoft.com/devops/managing-configuration-app-settings-for-multiple-environments-in-your-cd-pipeline/
Just want to through out another alternative to this is to have the secrets stored in a Key Vault and if deploying your App Service via an ARM template the secret values can be referenced to those stored in a key vault. Refer to this answer as a starting point.
Best practice would be the App Service is configured for Managed Service Identity and granted an access policy to a Key Vault where the secrets or connection strings are stored. This would lead to a more decoupled and reusable architecture. On App Service deployment the secrets will be grabbed from Key Vault and inserted into the App Settings. This type of deployment would work ideally with static keys.
If your keys could potentially changes there are methods to check Key Vault for a new password.
I will call out that this type of implementation will shift the secrets being wiped out from every time the app service is being deployed to every time the Key Vault is deployed.

Azure key vault. How to set which web app uses what key vault?

I have several versions of the same web app running on the same subscription service.
I have 2 logical environments. Dev and UAT.
I have WebAppDev and WebAppUAT.
I have two key vaults KVDev and KVUAT.
How can I configure the correct web app to use the correct key vault?
What process assigns the web apps to key vaults?
Edit:
I had assumed that the key vault would act like the secrets do when developing.
This
Tutorial
seems to suggest that the key vault can be used as a configuration provider. However, the web app is not accessing the key vault values.
Get the Object ID from the identity blade of the web app.
Find your azure key vault and create a new access policy using the Object Id of web app.
Alternatively use the following in the powershell cli.
Set-AzKeyVaultAccessPolicy –VaultName -ObjectId "" -PermissionsToKeys backup,create,delete,get,import,list,restore -PermissionsToSecrets get,list,backup,restore,recover
Follow this tutorial and copy the context from the Program.cs in the sample code.
Tutorial
Taking a different angle, the current questions and comments tackle the authentication to KeyVault.
However, it sounds like a more fundamental problem and that you need to vary your configuration per environment.
i.e. WebAppDev needs to be configured to use a KVDev URL and WebAppUAT needs to use KVUAT URL.
Assuming you are using App Service Plans; this documentation provides a mechanism to store environment specific configuration along with guidance on how to use it for your programming language of choice, you will need to refer to the Application Specific configuration section.
https://learn.microsoft.com/en-us/azure/app-service/configure-common
Configuring in the portal will get you so far, but over time you will likely wish to contain the configuration in a release management pipeline so you don't need to configure things by hand. Azure DevOps Pipeline is one such tool for this:
https://learn.microsoft.com/en-us/azure/devops/pipelines/get-started/?toc=%2Fazure%2Fdevops%2Fpipelines%2Ftoc.json&bc=%2Fazure%2Fdevops%2Fboards%2Fpipelines%2Fbreadcrumb%2Ftoc.json&view=azure-devops
To get you started there is a specific deployment task which can aid in setting configuration for App Service Plans: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-rm-web-app-deployment?view=azure-devops
Over time I'd suggest rather than splitting configuration between a Release Pipeline and source control, instead having configuration which doesn't require deployment time modifications instead to stay source controlled in ARM templates, but that is an answer in it's own right so I won't confuse matters with too much detail.
If you want to access Azure key Vault by programming with SDK or REST API, then it would not be a problem. Because different key vaults have different DNS names. When you try to retrieve a secret or key in your code, you need to use its identify URL which contains its key vault DNS name. It will finally find the target key vault.
If you want to use key vault in web app with managed identity, you may refer to the tutorial: Use Azure Key Vault with an Azure web app in .NET
In that tutorial, you will enable the identity of a web app. And then you can assign access policy to that identity. In this way, the web app will be able to access the key vault with managed identity.

Azure website not able to communicate to the third party API

Team,
I have recently migrated my azure classic portal resources to CSP subscription. I have successfully converted my azure cloud service to azure app website in the CSP subscription. But there is one thing i am not able connect. Its the third party API When we had the cloud service we had a .pfx uploaded to azure and in the code we use to create a uri which consists of the certifcate key + certificate secret key.
The certificate key is got directly from web.config. But the certificate secret key is got from EncryptedSettings.Appsettings("SecretKeyName").
This is basically got from the encrypted app setting done earlier by
https://eren.ws/2014/02/04/encrypting-the-web-config-file-of-an-azure-cloud-service/
But i am not sure what way should we implement on Azure website.
I have tried implementing the same but unfortunately it seems the secret key retrieval technique for cloud service is not the same as in the azure web app service.
When i debug the azure web site i can see that it gives the error as.
Failed to decrypt using provider ‘CustomProvider’. Error message from the provider: Value cannot be null.
Parameter name: keyObject
Can anyone please guide me ?
Rather than storing secrets in your config, you may wish instead to store them Azure KeyVault (which also gives you secret management capabilities etc) and then load the secrets at runtime.
KeyVault documentation:
https://learn.microsoft.com/en-us/azure/key-vault/
Specifically how to use keyvault with azure websites:
https://learn.microsoft.com/en-us/azure/key-vault/key-vault-use-from-web-application
And these days, don't bother with manual authentication to use keyvault, instead use "Managed Service Identity", here's a tutorial:
https://azure.microsoft.com/en-gb/resources/samples/app-service-msi-keyvault-dotnet/
Interestingly the second tutorial does mention specifically using certificates for the purposes of authentication (against keyvault), you may wish to use this technique for yourself as a simplified way to get direct access to your certificate.

Azure Automation Credentials using Azure Key Vault secrets

Is there a way to create an Azure Automation Credential asset which links to an Azure Key Vault secret? Similar question for a Certificate in Azure Automation.
I want to be able to store my passwords and such all in one place, the Key Vault, so that when I change it I don't have to change it in a bunch of places. I cannot find any documentation that indicates this is possible though. Am I missing it?
Thank you for any suggestions....
You can't link a Credential asset directly to Key Vault, however it should be possible to write a script that connects to Key Vault and updates the appropriate Automation Credentials from there.
This could either be fired on a schedule, a webhook, or by picking up Key Vault events from the new Event Grid (presuming they are currently wired up)

Resources