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

Commit ac5ca50

Browse files
tzhanljongio
authored andcommitted
Move to Track2 Update 2 (#8)
1 parent e72f6bd commit ac5ca50

File tree

5 files changed

+112
-100
lines changed

5 files changed

+112
-100
lines changed

README.md

Lines changed: 112 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,37 @@ languages:
66
products:
77
- azure-key-vault
88
- azure
9-
description: "This QuickStart shows how to store a secret in Key Vault and how to retrieve it using a Web app. This web app may be run locally or in Azure."
10-
urlFragment: key-vault-node-quickstart
9+
description: "How to set and get secrets from Azure Key Vault using Node.js."
10+
urlFragment: get-set-keyvault-secrets-managed-id-nodejs
1111
---
1212

13-
# Quickstart: Set and retrieve a secret from Azure Key Vault using a Node Web App
13+
# How to set and get secrets from Azure Key Vault using Node.js
1414

15-
This Quickstart shows how to store a secret in Key Vault and how to retrieve it using a Web app. This web app may be run locally or in Azure. The Quickstart uses Node.js and [Azure Managed Identities](https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/)
15+
This sample shows how to store a secret in Key Vault and how to retrieve it using a Web app. This web app may be run locally or in Azure. The sample uses Node.js and [Azure Managed Identities]
1616

1717
> * Create a Key Vault.
1818
> * Store a secret in Key Vault.
1919
> * Retrieve a secret from Key Vault.
2020
> * Create an Azure Web Application.
21-
> * [Enable Azure Managed Identities](https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/).
21+
> * [Enable Azure Managed Identities].
2222
> * Grant the required permissions for the web application to read data from Key vault.
2323
24-
Before you proceed make sure that you are familiar with the [Key Vault Concepts](https://docs.microsoft.com/en-us/azure/key-vault/key-vault-overview).
24+
Before you proceed make sure that you are familiar with the [Key Vault Concepts].
2525

26-
# SDK Versions
26+
## SDK Versions
2727

2828
In this sample, you will find the following folders:
29-
* **key-vault-node-quickstart-v3** - references Key Vault SDK v3.0
30-
* **key-vault-node-quickstart-v4** - references Key Vault SDK v4.0
31-
32-
33-
* To use the latest Azure SDK version [key-vault-node-quickstart-v4](./key-vault-node-quickstart-v4) please add the following dependency:
34-
* [@azure/identity](https://www.npmjs.com/package/@azure/identity)
35-
* [@azure/keyvault-secrets](https://www.npmjs.com/package/@azure/keyvault-secrets)
36-
* For the previous Azure SDK version [key-vault-node-quickstart-v3](./key-vault-node-quickstart-v3) please add the following dependency:
37-
* [ms-rest-azure](https://www.npmjs.com/package/ms-rest-azure)
38-
* [azure-keyvault](https://www.npmjs.com/package/azure-keyvault)
29+
* **v3** - references Key Vault SDK v3
30+
* **v4** - references Key Vault SDK v4
3931

4032
## Prerequisites
4133

42-
* [Node.js](https://nodejs.org)
43-
* [Git](https://www.git-scm.com/)
44-
* [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest) 2.0.4 or later
45-
* An Azure subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
34+
* [Node.js]
35+
* [Git]
36+
* [Azure CLI] 2.0.4 or later
37+
* An Azure subscription. If you don't have an Azure subscription, create a [free account] before you begin.
4638

47-
## Log in to Azure
39+
### Log in to Azure
4840

4941
1. Open a command prompt, i.e. cmd, terminal, etc
5042
2. Execute the following command to log in to Azure
@@ -53,15 +45,15 @@ In this sample, you will find the following folders:
5345
az login
5446
```
5547

56-
## Create Resource Group
48+
### Create Resource Group
5749

5850
**1. What is a Resource Group**
5951

6052
An Azure Resource Group is a logical container into which Azure resources are deployed and managed.
6153

6254
**2. How to create a Resource Group**
6355

64-
Create a Resource Group with the [az group create](https://docs.microsoft.com/en-us/azure/azure-resource-manager/manage-resources-cli) command.
56+
Create a Resource Group with the [az group create] command.
6557

6658
When you create a Resource Group you have give it a unique custom name. Please think of a custom name for your Resource Group and replace the text below `"<MyResourceGroupName>"` with the custom name you created.
6759

@@ -74,7 +66,7 @@ az group create --name "<MyResourceGroupName>" --location eastus
7466

7567
The Resource Group you just created is used throughout this tutorial.
7668

77-
## Create an Azure Key Vault
69+
### Create an Azure Key Vault
7870

7971
Next you will create a Key Vault using the Resource Group created in the previous step. Provide the following information:
8072

@@ -86,7 +78,7 @@ Next you will create a Key Vault using the Resource Group created in the previou
8678
az keyvault create --name "<MyKeyVaultName>" --resource-group "<MyResourceGroupName>" --location eastus
8779
```
8880

89-
## Add a Secret to Key Vault
81+
### Add a Secret to Key Vault
9082

9183
Next, we'll add a secret to Key Vault to help illustrate how Secret Value works. You could store an SQL connection string or any other information that you need to keep secure and make it available to your application.
9284

@@ -102,95 +94,36 @@ To view the value contained in the Secret as plain text, please type the followi
10294
az keyvault secret show --name AppSecret --vault-name "<MyKeyVaultName>"
10395
```
10496

105-
## Clone the repo
97+
### Clone the repo
10698

107-
Run the following command to clone this Quickstart code to your local machine:
99+
Run the following command to clone this sample code to your local machine:
108100

109101
```Bash
110-
git clone https://github.com/Azure-Samples/key-vault-node-quickstart.git
102+
git clone https://github.com/Azure-Samples/azure-sdk-for-js-keyvault-secret-get-set-managedid.git
111103
```
112104

113-
## Install dependencies
105+
### Install dependencies
114106

115-
Run the following command to install dependencies for "SDK version 3.0" and "SDK version 4.0":
107+
Run the following command to install dependencies for "SDK version 3" and "SDK version 4":
116108

117-
- SDK version 4.0
109+
- SDK version 4
118110

119111
```Bash
120-
cd key-vault-node-quickstart-v4
112+
cd v4
121113
```
122114
```Bash
123115
npm install
124116
```
125117

126-
- SDK version 3.0
118+
- SDK version 3
127119

128120
```Bash
129-
cd key-vault-node-quickstart-v3
121+
cd v3
130122
```
131123
```Bash
132124
npm install
133125
```
134126

135-
## Publish the web application to Azure
136-
137-
To publish this web application to Azure, we need to create an Azure App Service, Azure Web App, and create a Deployment User.
138-
139-
**1. Azure App Service**
140-
141-
The first step is to create an [Azure App Service](https://azure.microsoft.com/services/app-service/) Plan. You can store multiple web apps in this plan. Use the Resource Group that you created earlier in the following command:
142-
143-
```Bash
144-
az appservice plan create --name "<MyAppServicePlan>" --resource-group "<MyResourceGroup>"
145-
```
146-
147-
**2. Azure Web App**
148-
149-
Next we create a web app. In the following example, replace <AppName> with a globally unique app name (valid characters are a-z, 0-9, and -). The runtime is set to NODE|6.9. To see all supported runtimes, run az webapp list-runtimes:
150-
151-
152-
# Bash
153-
az webapp create --resource-group "<MyResourceGroup>" --plan "<MyAppServicePlan>" --name "<AppName>" --runtime "NODE|6.9" --deployment-local-git
154-
# PowerShell
155-
az webapp create --resource-group "<MyResourceGroup>" --plan "<MyAppServicePlan>" --name "<AppName>" --runtime "NODE|6.9"
156-
157-
After the web app is created, the Azure CLI outputs something similar to the following:
158-
159-
160-
{
161-
"availabilityState": "Normal",
162-
"clientAffinityEnabled": true,
163-
"clientCertEnabled": false,
164-
"cloningInfo": null,
165-
"containerSize": 0,
166-
"dailyMemoryTimeQuota": 0,
167-
"defaultHostName": "<AppName>.azurewebsites.net",
168-
"enabled": true,
169-
"deploymentLocalGitUrl": "https://<UserName>@<AppName>.scm.azurewebsites.net/<AppName>.git"
170-
< JSON data removed for brevity. >
171-
}
172-
173-
Browse to your newly created web app, and you should see a functioning web app. Replace `<AppName>` with the unique app name that you chose previously.
174-
175-
```Bash
176-
http://<AppName>.azurewebsites.net
177-
```
178-
179-
The above command also creates a Git-enabled app which allows you to deploy to Azure from your local git.
180-
Local Git repository is configured with this url:
181-
182-
```Bash
183-
https://<UserName>@<AppName>.scm.azurewebsites.net/<AppName>.git
184-
```
185-
186-
**3. Deployment User**
187-
188-
After running the previous command, you can add an Azure Remote to your local Git repository. Replace `<url>` with the URL of the Git Remote that you got from [enabling Git for your app](https://docs.microsoft.com/en-us/azure/app-service/deploy-local-git).
189-
190-
```Bash
191-
git remote add azure <url>
192-
```
193-
194127
### Configuring your Key Vault
195128

196129
- Create a service principal and configure its access to Azure resources:
@@ -229,7 +162,7 @@ git remote add azure <url>
229162
az keyvault show --name "<MyKeyVaultName>"
230163
```
231164

232-
## Enable Azure Managed Identities
165+
### Enable Azure Managed Identities
233166

234167
Azure Key Vault provides a way to securely store credentials and other keys and secrets, but your code needs to be authenticated to Key Vault before retrieving them. Azure Managed Identities simplify this need by giving Azure services an automatically managed identity in Azure Active Directory (Azure AD). You can use this identity to authenticate to any service that supports Azure AD authentication, including Key Vault, without having to store any credentials in your code.
235168

@@ -255,6 +188,65 @@ Then, run this command using the name of your Key Vault and the value of Princip
255188
az keyvault set-policy --name "<MyKeyVaultName>" --object-id "<PrincipalId>" --secret-permissions get
256189
```
257190

191+
## Publish the web application to Azure
192+
193+
To publish this web application to Azure, we need to create an Azure App Service, Azure Web App, and create a Deployment User.
194+
195+
**1. Azure App Service**
196+
197+
The first step is to create an [Azure App Service] Plan. You can store multiple web apps in this plan. Use the Resource Group that you created earlier in the following command:
198+
199+
```Bash
200+
az appservice plan create --name "<MyAppServicePlan>" --resource-group "<MyResourceGroup>"
201+
```
202+
203+
**2. Azure Web App**
204+
205+
Next we create a web app. In the following example, replace <AppName> with a globally unique app name (valid characters are a-z, 0-9, and -). The runtime is set to NODE|6.9. To see all supported runtimes, run az webapp list-runtimes:
206+
207+
208+
# Bash
209+
az webapp create --resource-group "<MyResourceGroup>" --plan "<MyAppServicePlan>" --name "<AppName>" --runtime "NODE|6.9" --deployment-local-git
210+
# PowerShell
211+
az webapp create --resource-group "<MyResourceGroup>" --plan "<MyAppServicePlan>" --name "<AppName>" --runtime "NODE|6.9"
212+
213+
After the web app is created, the Azure CLI outputs something similar to the following:
214+
215+
216+
{
217+
"availabilityState": "Normal",
218+
"clientAffinityEnabled": true,
219+
"clientCertEnabled": false,
220+
"cloningInfo": null,
221+
"containerSize": 0,
222+
"dailyMemoryTimeQuota": 0,
223+
"defaultHostName": "<AppName>.azurewebsites.net",
224+
"enabled": true,
225+
"deploymentLocalGitUrl": "https://<UserName>@<AppName>.scm.azurewebsites.net/<AppName>.git"
226+
< JSON data removed for brevity. >
227+
}
228+
229+
Browse to your newly created web app, and you should see a functioning web app. Replace `<AppName>` with the unique app name that you chose previously.
230+
231+
```Bash
232+
http://<AppName>.azurewebsites.net
233+
```
234+
235+
The above command also creates a Git-enabled app which allows you to deploy to Azure from your local git.
236+
Local Git repository is configured with this url:
237+
238+
```Bash
239+
https://<UserName>@<AppName>.scm.azurewebsites.net/<AppName>.git
240+
```
241+
242+
**3. Deployment User**
243+
244+
After running the previous command, you can add an Azure Remote to your local Git repository. Replace `<url>` with the URL of the Git Remote that you got from [enabling Git for your app].
245+
246+
```Bash
247+
git remote add azure <url>
248+
```
249+
258250
## Deploy the Node App to Azure and retrieve the secret value
259251

260252
Now that everything is deployed and configured, run the following command to deploy the app to Azure. This will push your local master branch to the git remote called 'azure' that you created earlier:
@@ -269,11 +261,31 @@ Make sure that you replaced the name `<AppName>` with your vault name.
269261

270262
## Next steps
271263

272-
* [Azure Key Vault Home Page](https://azure.microsoft.com/services/key-vault/)
273-
* [Azure Key Vault Documentation](https://docs.microsoft.com/azure/key-vault/)
274-
* [Azure SDK For Node.js](https://docs.microsoft.com/javascript/api/overview/azure/key-vault)
275-
* [Azure REST API Reference](https://docs.microsoft.com/rest/api/keyvault/)
264+
* [Azure Key Vault Home Page]
265+
* [Azure Key Vault Documentation]
266+
* [Azure SDK For JavaScript]
267+
* [Azure Key Vault REST API Reference]
276268

277269
## Contributing
278270

279-
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
271+
This project has adopted the [Microsoft Open Source Code of Conduct]. For more information see the [Code of Conduct FAQ] or contact [[email protected]] with any additional questions or comments.
272+
273+
274+
<!-- LINKS -->
275+
[Azure Managed Identities]: https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/
276+
[Enable Azure Managed Identities]: https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/
277+
[Key Vault Concepts]: https://docs.microsoft.com/en-us/azure/key-vault/key-vault-overview
278+
[Node.js]: https://nodejs.org
279+
[Git]: https://www.git-scm.com/
280+
[Azure CLI]: https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest
281+
[free account]: https://azure.microsoft.com/free/?WT.mc_id=A261C142F
282+
[az group create]: https://docs.microsoft.com/en-us/azure/azure-resource-manager/manage-resources-cli
283+
[Azure App Service]: https://azure.microsoft.com/services/app-service/
284+
[enabling Git for your app]: https://docs.microsoft.com/en-us/azure/app-service/deploy-local-git
285+
[Azure Key Vault Home Page]: https://azure.microsoft.com/services/key-vault/
286+
[Azure Key Vault Documentation]: https://docs.microsoft.com/azure/key-vault/
287+
[Azure SDK For JavaScript]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/keyvault/keyvault-secrets
288+
[Azure Key Vault REST API Reference]: https://docs.microsoft.com/rest/api/keyvault/
289+
[Microsoft Open Source Code of Conduct]: https://opensource.microsoft.com/codeofconduct/
290+
[Code of Conduct FAQ]: https://opensource.microsoft.com/codeofconduct/faq/
291+
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)