services | platforms | author |
---|---|---|
app-service, key-vault |
node |
williexu |
For Service-to-Azure-Service authentication, the approach so far involved creating an Azure AD application and associated credential, and using that credential to get a token. While this approach works well, there are two shortcomings:
- The Azure AD application credentials are typically hard coded in source code. Developers tend to push the code to source repositories as-is, which leads to credentials in source.
- The Azure AD application credentials expire, and so need to be renewed, else can lead to application downtime.
With Managed Service Identity (MSI), both these problems are solved. This sample shows how a Web App can authenticate to Azure Key Vault without the need to explicitly create an Azure AD application or manage its credentials.
Here's another sample that how to use MSI from inside an Azure VM with a Managed Service Identity (MSI) - https://github.com/Azure-Samples/resource-manager-node-manage-resources-with-msi
To run and deploy this sample, you need the following:
- An Azure subscription to create an App Service and a Key Vault.
Use the "Deploy to Azure" button to deploy an ARM template to create the following resources:
- App Service with MSI.
- Key Vault with a secret, and an access policy that grants the App Service access to Get Secrets.
Note: When filling out the template you will see a textbox labelled 'Key Vault Secret'. Enter a secret value there. A secret with the name 'secret' and value from what you entered will be created in the Key Vault.
Review the resources created using the Azure portal. You should see an App Service and a Key Vault. View the access policies of the Key Vault to see that the App Service has access to it.
Using the Azure Portal, go to the Key Vault's access policies, and grant yourself Secret Management access to the Key Vault. This will allow you to run the application on your local development machine.
- Search for your Key Vault in “Search Resources dialog box” in Azure Portal.
- Select "Overview", and click on Access policies
- Click on "Add New", select "Secret Management" from the dropdown for "Configure from template"
- Click on "Select Principal", add your account
- Save the Access Policies
You can also create an Azure service principal either through Azure CLI, PowerShell or the portal and grant it the same access.
-
If you don't already have it, get node.js.
-
Clone the repository.
git clone https://github.com/Azure-Samples/app-service-msi-keyvault-node.git
-
Install the dependencies using pip.
cd app-service-msi-keyvault-node npm install
-
Set up the environment variable
KEY_VAULT_URL
with your KeyVault URL or replace the variable in the index.js file. -
Export these environment variables into your current shell or update the credentials in the index.js file.
export AZURE_TENANT_ID={your tenant id} export AZURE_CLIENT_ID={your client id} export AZURE_CLIENT_SECRET={your client secret}
[AZURE.NOTE] On Windows, use
set
instead ofexport
. -
Run the sample.
node index.js
-
This sample exposes two endpoints:
/ping
: This just answers "Hello World!!!" and is a good way to test if your packages are installed correctly without testing Azure itself./
: The MSI sample itself
-
Set the
KEY_VAULT_URI
environment variable using the "Application Settings" of your WebApp. You can also change the value of the variable fromnull
in the index.js file. -
This repo is ready to be deployed using local git. Read this tutorial to get more information on how to push using local git through portal
Using the loginWithAppServiceMSI()
method from ms-rest-azure will autodetect if you're on a WebApp and get the token from the MSI endpoint. Then, the code is simply:
function getKeyVaultCredentials(){
return msRestAzure.loginWithAppServiceMSI({resource: 'https://vault.azure.net'});
}
function getKeyVaultSecret(credentials) {
let keyVaultClient = new KeyVault.KeyVaultClient(credentials);
return keyVaultClient.getSecret(KEY_VAULT_URI, 'secret', "");
}
getKeyVaultCredentials().then(
getKeyVaultSecret
).then(function (secret){
console.log(`Your secret value is: ${secret.value}.`);
}).catch(function (err) {
throw (err);
});
If you want to execute this same code in your local environment machine, just use the appropriate login method.
If you need a fallback mechanism to allow this code to switch automatically from MSI to another approach, you can test for environment variables:
function getKeyVaultCredentials(){
if (process.env.APPSETTING_WEBSITE_SITE_NAME){
return msRestAzure.loginWithAppServiceMSI({resource: 'https://vault.azure.net'});
} else {
return msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain);
}
}
The web app was successfully able to get a secret at runtime from Azure Key Vault using your developer account during development, and using MSI when deployed to Azure, without any code change between local development environment and Azure. As a result, you did not have to explicitly handle a service principal credential to authenticate to Azure AD to get a token to call Key Vault. You do not have to worry about renewing the service principal credential either, since MSI takes care of that.
Azure Functions being powered by Azure WebApp, MSI is also available. You can copy the relevant code from the example into your Azure Functions with the right import.
- MSI is not setup on the App Service.
Check the environment variables MSI_ENDPOINT and MSI_SECRET exist using Kudu debug console. If these environment variables do not exist, MSI is not enabled on the App Service. Note that after enabling MSI, you need to restart your WebApp.
- Access denied
The principal used does not have access to the Key Vault. The principal used in show on the web page. Grant that user (in case of developer context) or application "Get secret" access to the Key Vault.