DownLoad Complete Project: WebApiWithAzureKeyVault
Why Azure Key Vault?
Almost every Azure app has some kind of cryptographic key, storage account key, sensitive setting, password, or connection string.
For example, consider a web app that requires a connection string to an Azure SQL Database.Storing this sensitive information in an App.config file could result in it being checked in to a source-code control system and unintentionally exposed to many developers.
Compare this to using Azure Key Vault, where the App.config file only contains a reference to this sensitive data, and is controlled by the access policy of Azure Key Vault.
Below is insecure way which is commonly used in azure based solutions:
you can see here all secret information is clearly mentioned in webconfig.cs file in plain text from and think if some one got access on server and stolen all sensitive information easily. Usually these configuration files also checked in on repository systems like TFS,GitHub etc along with other project files.Any team who have access to these repositories can also see these secret information.
By using Key Vault you can securely store data and avoid having these sensitive pieces of information stored in source code which may then be compromised.
The Microsoft Azure cloud platform provides a secure secrets management service, Azure Key Vault, to store sensitive information. It is a multi-tenant service for developers to store and use sensitive data for their application in Azure.
The Azure Key Vault service can store three types of items: secrets, keys, and certificates.
- Secrets are any sequence of bytes under 10KB like connection strings, account keys, or the passwords for PFX (private key files). An authorized application can retrieve a secret for use in its operation.
- Keys involve cryptographic material imported into Key Vault, or generated when a service requests the Key Vault to do so. An authorized cloud service can request the Key Vault perform one or more cryptographic operations with a key on its behalf.
- An Azure Key Vault certificate is simply a managed X.509 certificate. What’s different is Azure Key Vault offers life-cycle management capabilities. Like Azure Keys, a service can request Azure Key Vault to create a certificate. When Azure Key Vault creates the certificate, it creates a related private key and password. The password is stored as an Azure Secret while the private key is stored as an Azure Key. Expired certificates can roll over with notifications before these operations happen.
Application flow with key vault
- Create A Key Vault
- Create a Secret
- Register an App in Azure Active Directory
- Create an API Key for the App
- Give App-Specific Permissions to Access Key Vault
- Configure your Dot Net Application
1. Create a key vault
Login on azure portal and add new service “key vault”. If ‘Key vaults’ is not already in your list, click on ‘More services’ and use the filter to find it. Select ‘Key vaults’.Fill all the mandatory information and press create button.
2. Create Secrets:
To do this, click on ‘Secrets’ under ‘Settings’ on the left, or under ‘Assets’ in the Overview panel. Once the ‘Secrets’ panel opens up, click the ‘Add’ button at the top so you can create a new one.
Activation and expiry dates can be used if you only want the secret to be accessed for a specific period of time. When you are finished, click ‘Create’.
Key vault DNS name will be used as Key Vault url in application from where key request will initiate.
3.Register An App In Azure Active Directory
Now You have data protected by Key Vault and You need to give our application (secure) access to this data, first.
Again go to azure portal and search “Azure Active Directory”. inside AD, select ‘App registrations’ from under the ‘Manage’ panel on the left. This is where You will configure the access and permissions.
our application will have when accessing Key Vault programmatically.
In my case i have already created a webapi app named as “DubaiProperties-Api” which is running under azure app service and i have register the same application in azure active directory to read secure keys/secrets from key vault by this application.
4. Create An Api Key For Registered App
From the ‘App registrations’ menu, you should see your newly created app listed.
Click on registered application and Copy the ‘Application ID’ that you should be able to see under ‘Essentials’.
select ‘Keys’ from the ‘API Access’ section on the right.Give the Key a meaningful description that will explain its purpose, then set an expiration setting. Click ‘Save’ and your API Key ‘Value’ will be presented to you. Copy this key value now as when you navigate away it will never be presented again.
You can always create a new one, if you forgot to copy it.
5.Give App-Specific Permissions to Access Key Vault
Return to key vault and select ‘Access Policies’ under the ‘Settings’ panel on the right. Click the ‘Add New’ button. Click the ‘Select Principal’ option to be presented with a new blade. Enter the Application ID of the app in Azure AD into this field, and select the app when it is presented to you. Click the ‘Select’ button at the bottom to confirm. You can now configure the permissions that you wish to grant the application.
Only assign the necessary permissions. As it is only Secrets that your app needs access to (and read-only access at that), I would suggest picking ‘Get’ and ‘List’ under the ‘Secret permissions’ option. This is all you need to do, so click ‘OK’ to complete this step.
Now key vault configurations are ready to store secret keys and refer by any application.
6.Configure your Dot Net Application
Now all key vault and active directory administration task has completed and now you need to set up dotnet application to use key vault for consuming secret keys instead of define those keys in app.config or some where in application.
Let’s start with Webapi project that needs to use some secrets that is stored in key vault.
Initially there are few things that are required to configure in you application like application Id,Key Vault Url and App Registration keys. All these information already described above at the time of application registration in AD and key Vault Creation.
Below are the settings for webconfig.cs:
Now add some nuget packages for azure key vault to the application
Create a helper class to interact with azure key vault by using Azure SDK and fetch all required secret keys and use in application.
Now Use this helper class in our webapi controller.
Now Publish webapi project on azure app service. webapi application should work after deployment.
Now Check complete swagger url for deployed api and see all api’s controller with all http verbs.
expend Get method of keyvault controller and try to make request to read key vault secret keys value from azure key vault.
Below is response of webapi with key vault values.
if you analysis whole code you will not find any secret keys configured in application configuration files or application settings section of azure app service.Keys value directly comes from key vault that is different location.
If you can add new version of same key in keyvault again then no need to make any changes in your application and application always pick latest version of key.
let’s create new version of same key with differ value.Go back to secret keys section under key vault settings pane,here you will found all defined keys.
click on key for which you want to create new version with new value. choose “DemoSecretKey” to update the value.Once you click on that,you will found all versions of selected key.Currently single version is created.
you never see values of secret keys,its hide to every one.
Click on “New Version” and select “manual” from the drop down.enter new secret value for key and save it.
now you can see new version added with updated value,and previous version also maintain by the Azure key vault.
Let’s test our webapi and it should read new updated value from the key vault.Important thing this is, i have not make any changes in deployed webapi.
That’s it! This configuration should enable to you to protect your sensitive information in Key Vault and then provide a Dot Net with secure access to that data
The Azure Key Vault is an excellent service and a welcome addition to the overall Azure services family. It promotes the secure management of cryptographic keys without the associated overhead, which is an important step to adopting and implementing better security within our applications. In the next article, we’ll see how you can set up a Key Vault for our application and use the .NET SDK to create, manage and retrieve keys.