You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 17, 2024. It is now read-only.
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."
# 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
14
14
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]
*[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.
46
38
47
-
## Log in to Azure
39
+
###Log in to Azure
48
40
49
41
1. Open a command prompt, i.e. cmd, terminal, etc
50
42
2. Execute the following command to log in to Azure
@@ -53,15 +45,15 @@ In this sample, you will find the following folders:
53
45
az login
54
46
```
55
47
56
-
## Create Resource Group
48
+
###Create Resource Group
57
49
58
50
**1. What is a Resource Group**
59
51
60
52
An Azure Resource Group is a logical container into which Azure resources are deployed and managed.
61
53
62
54
**2. How to create a Resource Group**
63
55
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.
65
57
66
58
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.
67
59
@@ -74,7 +66,7 @@ az group create --name "<MyResourceGroupName>" --location eastus
74
66
75
67
The Resource Group you just created is used throughout this tutorial.
76
68
77
-
## Create an Azure Key Vault
69
+
###Create an Azure Key Vault
78
70
79
71
Next you will create a Key Vault using the Resource Group created in the previous step. Provide the following information:
80
72
@@ -86,7 +78,7 @@ Next you will create a Key Vault using the Resource Group created in the previou
86
78
az keyvault create --name "<MyKeyVaultName>" --resource-group "<MyResourceGroupName>" --location eastus
87
79
```
88
80
89
-
## Add a Secret to Key Vault
81
+
###Add a Secret to Key Vault
90
82
91
83
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.
92
84
@@ -102,95 +94,36 @@ To view the value contained in the Secret as plain text, please type the followi
102
94
az keyvault secret show --name AppSecret --vault-name "<MyKeyVaultName>"
103
95
```
104
96
105
-
## Clone the repo
97
+
###Clone the repo
106
98
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:
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":
116
108
117
-
- SDK version 4.0
109
+
- SDK version 4
118
110
119
111
```Bash
120
-
cdkey-vault-node-quickstart-v4
112
+
cd v4
121
113
```
122
114
```Bash
123
115
npm install
124
116
```
125
117
126
-
- SDK version 3.0
118
+
- SDK version 3
127
119
128
120
```Bash
129
-
cdkey-vault-node-quickstart-v3
121
+
cd v3
130
122
```
131
123
```Bash
132
124
npm install
133
125
```
134
126
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:
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.
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
-
194
127
### Configuring your Key Vault
195
128
196
129
- Create a service principal and configure its access to Azure resources:
@@ -229,7 +162,7 @@ git remote add azure <url>
229
162
az keyvault show --name "<MyKeyVaultName>"
230
163
```
231
164
232
-
## Enable Azure Managed Identities
165
+
###Enable Azure Managed Identities
233
166
234
167
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.
235
168
@@ -255,6 +188,65 @@ Then, run this command using the name of your Key Vault and the value of Princip
255
188
az keyvault set-policy --name "<MyKeyVaultName>" --object-id "<PrincipalId>" --secret-permissions get
256
189
```
257
190
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:
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.
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
+
258
250
## Deploy the Node App to Azure and retrieve the secret value
259
251
260
252
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.
269
261
270
262
## Next steps
271
263
272
-
*[Azure Key Vault Home Page](https://azure.microsoft.com/services/key-vault/)
*[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]
276
268
277
269
## Contributing
278
270
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.
0 commit comments