|
1 | | -# Project Name |
| 1 | +--- |
| 2 | +services: app-service, key-vault |
| 3 | +platforms: node |
| 4 | +author: williexu |
| 5 | +--- |
2 | 6 |
|
3 | | -(short, 1-3 sentenced, description of the project) |
| 7 | +# Use Key Vault from App Service with Managed Service Identity and Nodejs |
4 | 8 |
|
5 | | -## Features |
| 9 | +## Background |
| 10 | +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: |
| 11 | +1. 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. |
| 12 | +2. The Azure AD application credentials expire, and so need to be renewed, else can lead to application downtime. |
6 | 13 |
|
7 | | -This project framework provides the following features: |
| 14 | +With [Managed Service Identity (MSI)](https://docs.microsoft.com/en-us/azure/app-service/app-service-managed-service-identity), 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. |
8 | 15 |
|
9 | | -* Feature 1 |
10 | | -* Feature 2 |
11 | | -* ... |
| 16 | +>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](https://github.com/Azure-Samples/resource-manager-node-manage-resources-with-msi) |
12 | 17 |
|
13 | | -## Getting Started |
| 18 | +## Prerequisites |
| 19 | +To run and deploy this sample, you need the following: |
| 20 | +1. An Azure subscription to create an App Service and a Key Vault. |
| 21 | +2. [Azure CLI 2.0](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest) to run the application on your local development machine. |
14 | 22 |
|
15 | | -### Prerequisites |
| 23 | +### Step 1: Create an App Service with a Managed Service Identity (MSI) |
| 24 | +<a href="https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure-Samples%2Fapp-service-msi-keyvault-node%2Fmaster%2Fazuredeploy.json" target="_blank"> |
| 25 | + <img src="http://azuredeploy.net/deploybutton.png"/> |
| 26 | +</a> |
16 | 27 |
|
17 | | -(ideally very short, if any) |
| 28 | +Use the "Deploy to Azure" button to deploy an ARM template to create the following resources: |
| 29 | +1. App Service with MSI. |
| 30 | +2. Key Vault with a secret, and an access policy that grants the App Service access to **Get Secrets**. |
| 31 | +>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. |
18 | 32 |
|
19 | | -- OS |
20 | | -- Library version |
21 | | -- ... |
| 33 | +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. |
22 | 34 |
|
23 | | -### Installation |
| 35 | +### Step 2: Grant yourself data plane access to the Key Vault |
| 36 | +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. |
24 | 37 |
|
25 | | -(ideally very short) |
| 38 | +1. Search for your Key Vault in “Search Resources dialog box” in Azure Portal. |
| 39 | +2. Select "Overview", and click on Access policies |
| 40 | +3. Click on "Add New", select "Secret Management" from the dropdown for "Configure from template" |
| 41 | +4. Click on "Select Principal", add your account |
| 42 | +5. Save the Access Policies |
26 | 43 |
|
27 | | -- npm install [package name] |
28 | | -- mvn install |
29 | | -- ... |
| 44 | +You can also create an Azure service principal either through |
| 45 | +[Azure CLI](https://azure.microsoft.com/documentation/articles/resource-group-authenticate-service-principal-cli/), |
| 46 | +[PowerShell](https://azure.microsoft.com/documentation/articles/resource-group-authenticate-service-principal/) |
| 47 | +or [the portal](https://azure.microsoft.com/documentation/articles/resource-group-create-service-principal-portal/) |
| 48 | +and grant it the same access. |
30 | 49 |
|
31 | | -### Quickstart |
32 | | -(Add steps to get up and running quickly) |
33 | 50 |
|
34 | | -1. git clone [repository clone url] |
35 | | -2. cd [respository name] |
36 | | -3. ... |
| 51 | +## Local dev installation |
37 | 52 |
|
| 53 | +1. If you don't already have it, [get node.js](https://nodejs.org). |
38 | 54 |
|
39 | | -## Demo |
40 | 55 |
|
41 | | -A demo app is included to show how to use the project. |
| 56 | +1. Clone the repository. |
42 | 57 |
|
43 | | -To run the demo, follow these steps: |
| 58 | + ``` |
| 59 | + git clone https://github.com/Azure-Samples/app-service-msi-keyvault-node.git |
| 60 | + ``` |
44 | 61 |
|
45 | | -(Add steps to start up the demo) |
| 62 | +2. Install the dependencies using pip. |
46 | 63 |
|
47 | | -1. |
48 | | -2. |
49 | | -3. |
| 64 | + ``` |
| 65 | + cd app-service-msi-keyvault-node |
| 66 | + npm install |
| 67 | + ``` |
50 | 68 |
|
51 | | -## Resources |
| 69 | +3. Set up the environment variable `KEY_VAULT_URL` with your KeyVault URL of replace the variable in the index.js file. |
52 | 70 |
|
53 | | -(Any additional resources or related projects) |
| 71 | +1. Export these environment variables into your current shell or update the credentials in the index.js file. |
54 | 72 |
|
55 | | -- Link to supporting information |
56 | | -- Link to similar sample |
57 | | -- ... |
| 73 | + ``` |
| 74 | + export AZURE_TENANT_ID={your tenant id} |
| 75 | + export AZURE_CLIENT_ID={your client id} |
| 76 | + export AZURE_CLIENT_SECRET={your client secret} |
| 77 | + ``` |
| 78 | +
|
| 79 | +1. Run the sample. |
| 80 | +
|
| 81 | + ``` |
| 82 | + node index.js |
| 83 | + ``` |
| 84 | +
|
| 85 | +1. This sample exposes two endpoints: |
| 86 | + |
| 87 | + - `/ping` : This just answers "Hello World!!!" and is a good way to test if your packages are installed correctly without testing Azure itself. |
| 88 | + - `/` : The MSI sample itself |
| 89 | +
|
| 90 | +## Installation on Azure |
| 91 | +
|
| 92 | +1. Set the `KEY_VAULT_URI` environment variable using the "Application Settings" of your WebApp. You can also change the value of the variable from `null` in the index.js file. |
| 93 | +
|
| 94 | +1. 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](https://docs.microsoft.com/en-us/azure/app-service/app-service-deploy-local-git) |
| 95 | +
|
| 96 | +## At a glance |
| 97 | +
|
| 98 | +Using the `loginWithAppServiceMSI()` method from [ms-rest-azure](https://www.npmjs.com/package/ms-rest-azure) will autodetect if you're on a WebApp and get the token from the MSI endpoint. Then, the code is simply: |
| 99 | +
|
| 100 | +```javascript |
| 101 | + function get_key_vault_credentials(){ |
| 102 | + return msRestAzure.loginWithAppServiceMSI({resource: 'https://vault.azure.net'}); |
| 103 | + } |
| 104 | +
|
| 105 | + function get_key_vault_secret(credentials) { |
| 106 | + let keyVaultClient = new KeyVault.KeyVaultClient(credentials); |
| 107 | + return keyVaultClient.getSecret(KEY_VAULT_URI, 'secret', ""); |
| 108 | + } |
| 109 | +
|
| 110 | + get_key_vault_credentials().then( |
| 111 | + get_key_vault_secret |
| 112 | + ).then(function (secret){ |
| 113 | + console.log(`Your secret value is: ${secret.value}.`); |
| 114 | + }).catch(function (err) { |
| 115 | + throw (err); |
| 116 | + }); |
| 117 | +``` |
| 118 | + |
| 119 | +If you want to execute this same code in your local environment machine, just use the appropriate login method. |
| 120 | + |
| 121 | +If you need a fallback mechanism to allow this code to switch automatically from MSI to another approach, you can test for environment variables: |
| 122 | + |
| 123 | +```javascript |
| 124 | +function get_key_vault_credentials(){ |
| 125 | + if (process.env.APPSETTING_WEBSITE_SITE_NAME){ |
| 126 | + return msRestAzure.loginWithAppServiceMSI({resource: 'https://vault.azure.net'}); |
| 127 | + } else { |
| 128 | + return msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain); |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +## Summary |
| 134 | + |
| 135 | +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. |
| 136 | +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. |
| 137 | + |
| 138 | +## Azure Functions |
| 139 | + |
| 140 | +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. |
| 141 | + |
| 142 | +## Troubleshooting |
| 143 | + |
| 144 | +### Common issues when deployed to Azure App Service: |
| 145 | + |
| 146 | +1. MSI is not setup on the App Service. |
| 147 | + |
| 148 | +Check the environment variables MSI_ENDPOINT and MSI_SECRET exist using [Kudu debug console](https://azure.microsoft.com/en-us/resources/videos/super-secret-kudu-debug-console-for-azure-web-sites/). 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. |
| 149 | + |
| 150 | +### Common issues across environments: |
| 151 | + |
| 152 | +1. Access denied |
| 153 | + |
| 154 | +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. |
0 commit comments