Skip to content
This repository was archived by the owner on Jun 17, 2024. It is now read-only.

Commit 8f7261a

Browse files
committed
updated dependencies based on remote public repo
readme
1 parent 21e073e commit 8f7261a

File tree

2,840 files changed

+138
-424985
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,840 files changed

+138
-424985
lines changed

CHANGELOG.md

Lines changed: 0 additions & 13 deletions
This file was deleted.

README.md

Lines changed: 132 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,154 @@
1-
# Project Name
1+
---
2+
services: app-service, key-vault
3+
platforms: node
4+
author: williexu
5+
---
26

3-
(short, 1-3 sentenced, description of the project)
7+
# Use Key Vault from App Service with Managed Service Identity and Nodejs
48

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.
613

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.
815

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)
1217
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.
1422

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>
1627

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.
1832
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.
2234

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.
2437

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
2643

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.
3049

31-
### Quickstart
32-
(Add steps to get up and running quickly)
3350

34-
1. git clone [repository clone url]
35-
2. cd [respository name]
36-
3. ...
51+
## Local dev installation
3752

53+
1. If you don't already have it, [get node.js](https://nodejs.org).
3854

39-
## Demo
4055

41-
A demo app is included to show how to use the project.
56+
1. Clone the repository.
4257

43-
To run the demo, follow these steps:
58+
```
59+
git clone https://github.com/Azure-Samples/app-service-msi-keyvault-node.git
60+
```
4461
45-
(Add steps to start up the demo)
62+
2. Install the dependencies using pip.
4663
47-
1.
48-
2.
49-
3.
64+
```
65+
cd app-service-msi-keyvault-node
66+
npm install
67+
```
5068
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.
5270
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.
5472
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.

index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
const express = require('express');
2-
const async = require('async');
32
const msRestAzure = require('ms-rest-azure');
43
const KeyVault = require('azure-keyvault');
5-
const KeyVaultManagementClient = require('azure-arm-keyvault')
6-
const KEY_VAULT_URI = "https://wilxkv.vault.azure.net/";
4+
const KEY_VAULT_URI = null || process.env['KEY_VAULT_URI'];
75

86
let app = express();
97
let clientId = process.env['CLIENT_ID']; // service principal
@@ -16,7 +14,7 @@ function get_key_vault_credentials(){
1614
} else {
1715
return msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain);
1816
}
19-
}
17+
}
2018

2119
function get_key_vault_secret(credentials) {
2220
let keyVaultClient = new KeyVault.KeyVaultClient(credentials);

node_modules/.bin/mime

Lines changed: 0 additions & 15 deletions
This file was deleted.

node_modules/.bin/mime.cmd

Lines changed: 0 additions & 7 deletions
This file was deleted.

node_modules/.bin/sshpk-conv

Lines changed: 0 additions & 15 deletions
This file was deleted.

node_modules/.bin/sshpk-conv.cmd

Lines changed: 0 additions & 7 deletions
This file was deleted.

node_modules/.bin/sshpk-sign

Lines changed: 0 additions & 15 deletions
This file was deleted.

node_modules/.bin/sshpk-sign.cmd

Lines changed: 0 additions & 7 deletions
This file was deleted.

node_modules/.bin/sshpk-verify

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)