├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── v3 ├── index.js └── package.json └── v4 ├── index.js └── package.json /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 4 | > Please provide us with the following information: 5 | > --------------------------------------------------------------- 6 | 7 | ### This issue is for a: (mark with an `x`) 8 | ``` 9 | - [ ] bug report -> please search issues before submitting 10 | - [ ] feature request 11 | - [ ] documentation issue or request 12 | - [ ] regression (a behavior that used to work and stopped in a new release) 13 | ``` 14 | 15 | ### Minimal steps to reproduce 16 | > 17 | 18 | ### Any log messages given by the failure 19 | > 20 | 21 | ### Expected/desired behavior 22 | > 23 | 24 | ### OS and Version? 25 | > Windows 7, 8 or 10. Linux (which distribution). macOS (Yosemite? El Capitan? Sierra?) 26 | 27 | ### Versions 28 | > 29 | 30 | ### Mention any other details that might be useful 31 | 32 | > --------------------------------------------------------------- 33 | > Thanks! We'll be in touch soon. 34 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Purpose 2 | 3 | * ... 4 | 5 | ## Does this introduce a breaking change? 6 | 7 | ``` 8 | [ ] Yes 9 | [ ] No 10 | ``` 11 | 12 | ## Pull Request Type 13 | What kind of change does this Pull Request introduce? 14 | 15 | 16 | ``` 17 | [ ] Bugfix 18 | [ ] Feature 19 | [ ] Code style update (formatting, local variables) 20 | [ ] Refactoring (no functional changes, no api changes) 21 | [ ] Documentation content changes 22 | [ ] Other... Please describe: 23 | ``` 24 | 25 | ## How to Test 26 | * Get the code 27 | 28 | ``` 29 | git clone [repo-address] 30 | cd [repo-name] 31 | git checkout [branch-name] 32 | npm install 33 | ``` 34 | 35 | * Test the code 36 | 37 | ``` 38 | ``` 39 | 40 | ## What to Check 41 | Verify that the following are valid 42 | * ... 43 | 44 | ## Other Information 45 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [project-title] Changelog 2 | 3 | 4 | # x.y.z (yyyy-mm-dd) 5 | 6 | *Features* 7 | * ... 8 | 9 | *Bug Fixes* 10 | * ... 11 | 12 | *Breaking Changes* 13 | * ... 14 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to [project-title] 2 | 3 | This project welcomes contributions and suggestions. Most contributions require you to agree to a 4 | Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us 5 | the rights to use your contribution. For details, visit https://cla.microsoft.com. 6 | 7 | When you submit a pull request, a CLA-bot will automatically determine whether you need to provide 8 | a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions 9 | provided by the bot. You will only need to do this once across all repos using our CLA. 10 | 11 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). 12 | For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or 13 | contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. 14 | 15 | - [Code of Conduct](#coc) 16 | - [Issues and Bugs](#issue) 17 | - [Feature Requests](#feature) 18 | - [Submission Guidelines](#submit) 19 | 20 | ## Code of Conduct 21 | Help us keep this project open and inclusive. Please read and follow our [Code of Conduct](https://opensource.microsoft.com/codeofconduct/). 22 | 23 | ## Found an Issue? 24 | If you find a bug in the source code or a mistake in the documentation, you can help us by 25 | [submitting an issue](#submit-issue) to the GitHub Repository. Even better, you can 26 | [submit a Pull Request](#submit-pr) with a fix. 27 | 28 | ## Want a Feature? 29 | You can *request* a new feature by [submitting an issue](#submit-issue) to the GitHub 30 | Repository. If you would like to *implement* a new feature, please submit an issue with 31 | a proposal for your work first, to be sure that we can use it. 32 | 33 | * **Small Features** can be crafted and directly [submitted as a Pull Request](#submit-pr). 34 | 35 | ## Submission Guidelines 36 | 37 | ### Submitting an Issue 38 | Before you submit an issue, search the archive, maybe your question was already answered. 39 | 40 | If your issue appears to be a bug, and hasn't been reported, open a new issue. 41 | Help us to maximize the effort we can spend fixing issues and adding new 42 | features, by not reporting duplicate issues. Providing the following information will increase the 43 | chances of your issue being dealt with quickly: 44 | 45 | * **Overview of the Issue** - if an error is being thrown a non-minified stack trace helps 46 | * **Version** - what version is affected (e.g. 0.1.2) 47 | * **Motivation for or Use Case** - explain what are you trying to do and why the current behavior is a bug for you 48 | * **Browsers and Operating System** - is this a problem with all browsers? 49 | * **Reproduce the Error** - provide a live example or a unambiguous set of steps 50 | * **Related Issues** - has a similar issue been reported before? 51 | * **Suggest a Fix** - if you can't fix the bug yourself, perhaps you can point to what might be 52 | causing the problem (line of code or commit) 53 | 54 | You can file new issues by providing the above information at the corresponding repository's issues link: https://github.com/[organization-name]/[repository-name]/issues/new]. 55 | 56 | ### Submitting a Pull Request (PR) 57 | Before you submit your Pull Request (PR) consider the following guidelines: 58 | 59 | * Search the repository (https://github.com/[organization-name]/[repository-name]/pulls) for an open or closed PR 60 | that relates to your submission. You don't want to duplicate effort. 61 | 62 | * Make your changes in a new git fork: 63 | 64 | * Commit your changes using a descriptive commit message 65 | * Push your fork to GitHub: 66 | * In GitHub, create a pull request 67 | * If we suggest changes then: 68 | * Make the required updates. 69 | * Rebase your fork and force push to your GitHub repository (this will update your Pull Request): 70 | 71 | ```shell 72 | git rebase master -i 73 | git push -f 74 | ``` 75 | 76 | That's it! Thank you for your contribution! 77 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. All rights reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | --- 2 | page_type: sample 3 | languages: 4 | - javascript 5 | - nodejs 6 | products: 7 | - azure-key-vault 8 | - azure 9 | description: "How to set and get secrets from Azure Key Vault using Node.js." 10 | urlFragment: get-set-keyvault-secrets-managed-id-nodejs 11 | --- 12 | 13 | # How to set and get secrets from Azure Key Vault using Node.js 14 | 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] 16 | 17 | > * Create a Key Vault. 18 | > * Store a secret in Key Vault. 19 | > * Retrieve a secret from Key Vault. 20 | > * Create an Azure Web Application. 21 | > * [Enable Azure Managed Identities]. 22 | > * Grant the required permissions for the web application to read data from Key vault. 23 | 24 | Before you proceed make sure that you are familiar with the [Key Vault Concepts]. 25 | 26 | ## SDK Versions 27 | 28 | In this sample, you will find the following folders: 29 | * **v3** - references Key Vault SDK v3 30 | * **v4** - references Key Vault SDK v4 31 | 32 | ## Prerequisites 33 | 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. 38 | 39 | ### Log in to Azure 40 | 41 | 1. Open a command prompt, i.e. cmd, terminal, etc 42 | 2. Execute the following command to log in to Azure 43 | 44 | ```Bash 45 | az login 46 | ``` 47 | 48 | ### Create Resource Group 49 | 50 | **1. What is a Resource Group** 51 | 52 | An Azure Resource Group is a logical container into which Azure resources are deployed and managed. 53 | 54 | **2. How to create a Resource Group** 55 | 56 | Create a Resource Group with the [az group create] command. 57 | 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 `""` with the custom name you created. 59 | 60 | The following example creates a Resource Group named ** in the *eastus* location. 61 | 62 | ```Bash 63 | # To list locations: az account list-locations --output table 64 | az group create --name "" --location eastus 65 | ``` 66 | 67 | The Resource Group you just created is used throughout this tutorial. 68 | 69 | ### Create an Azure Key Vault 70 | 71 | Next you will create a Key Vault using the Resource Group created in the previous step. Provide the following information: 72 | 73 | * Vault name - Create a custom name and replace `""` below. 74 | * Resource group name - Use the same Resource Group Name you used above. 75 | * The location - Use the same location that you created the Resource Group in above. 76 | 77 | ```Bash 78 | az keyvault create --name "" --resource-group "" --location eastus 79 | ``` 80 | 81 | ### Add a Secret to Key Vault 82 | 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. 84 | 85 | In this tutorial, replace `""` and `""` with your custom secret name and secret value: 86 | 87 | ```Bash 88 | az keyvault secret set --vault-name "" --name "" --value "" 89 | ``` 90 | 91 | To view the value contained in the Secret as plain text, please type the following command. This command shows the Secret Information including the URI. After completing these steps, you should have a URI to a Secret in an Azure Key Vault. Copy the output from the previous command to text editor. You will need it later: 92 | 93 | ```Bash 94 | az keyvault secret show --name "" --vault-name "" 95 | ``` 96 | 97 | ### Clone the repo 98 | 99 | Run the following command to clone this sample code to your local machine: 100 | 101 | ```Bash 102 | git clone https://github.com/Azure-Samples/azure-sdk-for-js-keyvault-secret-get-set-managedid.git 103 | ``` 104 | 105 | ### Install dependencies 106 | 107 | Run the following command to install dependencies for "SDK version 3" and "SDK version 4": 108 | 109 | - SDK version 4 110 | 111 | ```Bash 112 | cd v4 113 | ``` 114 | ```Bash 115 | npm install 116 | ``` 117 | 118 | - SDK version 3 119 | 120 | ```Bash 121 | cd v3 122 | ``` 123 | ```Bash 124 | npm install 125 | ``` 126 | 127 | ### Configuring your Key Vault 128 | 129 | - Create a service principal and configure its access to Azure resources: 130 | ```Bash 131 | az ad sp create-for-rbac -n "" --skip-assignment 132 | ``` 133 | Output: 134 | ```json 135 | { 136 | "appId": "generated-app-ID", 137 | "displayName": "", 138 | "name": "http://", 139 | "password": "random-password", 140 | "tenant": "tenant-ID" 141 | } 142 | ``` 143 | - Use the above returned credentials information to set **AZURE_CLIENT_ID**(appId), **AZURE_CLIENT_SECRET**(password) and **AZURE_TENANT_ID**(tenant) environment variables. The following example shows a way to do this in Bash: 144 | 145 | ```Bash 146 | export AZURE_CLIENT_ID="generated-app-ID" 147 | export AZURE_CLIENT_SECRET="random-password" 148 | export AZURE_TENANT_ID="tenant-ID" 149 | ``` 150 | 151 | - Grant the above mentioned application authorization to perform secret operations on the Key Vault: 152 | 153 | ```Bash 154 | az keyvault set-policy --name "" --spn $AZURE_CLIENT_ID --secret-permissions backup delete get list set 155 | ``` 156 | 157 | > --secret-permissions: 158 | > Accepted values: backup, delete, get, list, purge, recover, restore, set 159 | 160 | - Use the above mentioned Key Vault name to retrieve details of your Vault which also contains your Key Vault URL: 161 | ```Bash 162 | az keyvault show --name "" 163 | ``` 164 | 165 | - To use the above returned credentials information in your Web App, set **AZURE_CLIENT_ID**(appId), **AZURE_CLIENT_SECRET**(password) and **AZURE_TENANT_ID**(tenant) environment variables in the **Settings** > **Configuration** > **Application Settings** of your Web App. 166 | 167 | ### Enable Azure Managed Identities 168 | 169 | 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. 170 | 171 | Run the "identity assign" command to create an identity for this application, this command is the equivalent of going to the portal and switching **Azure Managed Identities** to **On** in the web application properties: 172 | 173 | ```Bash 174 | az webapp identity assign --name "" --resource-group "" 175 | ``` 176 | 177 | ### Assign permissions to your application to read secrets from Key Vault 178 | 179 | Copy the output to text editor for later use. It should be in the following format: 180 | 181 | { 182 | "principalId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", 183 | "tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", 184 | "type": "SystemAssigned" 185 | } 186 | 187 | Then, run this command using the name of your Key Vault and the value of PrincipalId copied from above: 188 | 189 | ```Bash 190 | az keyvault set-policy --name "" --object-id "" --secret-permissions get 191 | ``` 192 | 193 | ## Publish the web application to Azure 194 | 195 | To publish this web application to Azure, we need to create an Azure App Service, Azure Web App, and create a Deployment User. 196 | 197 | **1. Azure App Service** 198 | 199 | 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: 200 | 201 | ```Bash 202 | az appservice plan create --name "" --resource-group "" 203 | ``` 204 | 205 | **2. Azure Web App** 206 | 207 | Next we create a web app. In the following example, replace 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: 208 | 209 | az webapp create --resource-group "" --plan "" --name "" --runtime "NODE|6.9" --deployment-local-git 210 | 211 | After the Web App is created, the Azure CLI outputs something similar to the following: 212 | 213 | 214 | { 215 | "availabilityState": "Normal", 216 | "clientAffinityEnabled": true, 217 | "clientCertEnabled": false, 218 | "cloningInfo": null, 219 | "containerSize": 0, 220 | "dailyMemoryTimeQuota": 0, 221 | "defaultHostName": ".azurewebsites.net", 222 | "enabled": true, 223 | "deploymentLocalGitUrl": "https://@.scm.azurewebsites.net/.git" 224 | < JSON data removed for brevity. > 225 | } 226 | 227 | Browse to your newly created web app, and you should see a functioning web app. Replace `` with the unique app name that you chose previously. 228 | 229 | ```Bash 230 | http://.azurewebsites.net 231 | ``` 232 | 233 | The above command also creates a Git-enabled app which allows you to deploy to Azure from your local git. 234 | Local Git repository is configured with this url: 235 | 236 | ```Bash 237 | https://@.scm.azurewebsites.net/.git 238 | ``` 239 | 240 | **3. Deployment User** 241 | 242 | After running the previous command, you can add an Azure Remote to your local Git repository. Replace `` with the URL of the Git Remote that you got from [enabling Git for your app]. 243 | 244 | ```Bash 245 | git remote add azure 246 | ``` 247 | 248 | ## Deploy the Node App to Azure and retrieve the secret value 249 | 250 | 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: 251 | 252 | ```Bash 253 | git push azure master 254 | ``` 255 | 256 | When the git push command has completed you can now navigate to `https://.azurewebsites.net` to see the secret value. 257 | 258 | Make sure that you replaced the name `` with your Web App name. 259 | 260 | ## Next steps 261 | 262 | * [Azure Key Vault Home Page] 263 | * [Azure Key Vault Documentation] 264 | * [Azure SDK For JavaScript] 265 | * [Azure Key Vault REST API Reference] 266 | 267 | ## Contributing 268 | 269 | This project has adopted the [Microsoft Open Source Code of Conduct]. For more information see the [Code of Conduct FAQ] or contact [opencode@microsoft.com] with any additional questions or comments. 270 | 271 | 272 | 273 | [Azure Managed Identities]: https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/ 274 | [Enable Azure Managed Identities]: https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/ 275 | [Key Vault Concepts]: https://docs.microsoft.com/en-us/azure/key-vault/key-vault-overview 276 | [Node.js]: https://nodejs.org 277 | [Git]: https://www.git-scm.com/ 278 | [Azure CLI]: https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest 279 | [free account]: https://azure.microsoft.com/free/?WT.mc_id=A261C142F 280 | [az group create]: https://docs.microsoft.com/en-us/azure/azure-resource-manager/manage-resources-cli 281 | [Azure App Service]: https://azure.microsoft.com/services/app-service/ 282 | [enabling Git for your app]: https://docs.microsoft.com/en-us/azure/app-service/deploy-local-git 283 | [Azure Key Vault Home Page]: https://azure.microsoft.com/services/key-vault/ 284 | [Azure Key Vault Documentation]: https://docs.microsoft.com/azure/key-vault/ 285 | [Azure SDK For JavaScript]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/keyvault/keyvault-secrets 286 | [Azure Key Vault REST API Reference]: https://docs.microsoft.com/rest/api/keyvault/ 287 | [Microsoft Open Source Code of Conduct]: https://opensource.microsoft.com/codeofconduct/ 288 | [Code of Conduct FAQ]: https://opensource.microsoft.com/codeofconduct/faq/ 289 | [opencode@microsoft.com]: mailto:opencode@microsoft.com 290 | -------------------------------------------------------------------------------- /v3/index.js: -------------------------------------------------------------------------------- 1 | var http = require('http'); 2 | const KeyVault = require('azure-keyvault'); 3 | const msRestAzure = require('ms-rest-azure'); 4 | 5 | 6 | var server = http.createServer(function(request, response) { 7 | response.writeHead(200, {"Content-Type": "text/plain"}); 8 | }); 9 | 10 | // The ms-rest-azure library allows us to login with MSI by providing the resource name. In this case the resource is Key Vault. 11 | // For public regions the resource name is Key Vault 12 | msRestAzure.loginWithAppServiceMSI({resource: 'https://vault.azure.net'}).then( (credentials) => { 13 | const keyVaultClient = new KeyVault.KeyVaultClient(credentials); 14 | 15 | var vaultUri = "https://" + "" + ".vault.azure.net/"; 16 | 17 | // We're setting the Secret value here and retrieving the secret value 18 | keyVaultClient.setSecret(vaultUri, 'my-secret', 'test-secret-value', {}) 19 | .then( (kvSecretBundle, httpReq, httpResponse) => { 20 | console.log("Secret id: '" + kvSecretBundle.id + "'."); 21 | return keyVaultClient.getSecret(kvSecretBundle.id, {}); 22 | }) 23 | .then( (bundle) => { 24 | console.log("Successfully retrieved 'test-secret'"); 25 | console.log(bundle); 26 | }) 27 | .catch( (err) => { 28 | console.log(err); 29 | }); 30 | 31 | // Below code demonstrates how to retrieve a secret value 32 | 33 | // keyVaultClient.getSecret(vaultUri, "AppSecret", "").then(function(response){ 34 | // console.log(response); 35 | // }) 36 | }); 37 | 38 | var port = process.env.PORT || 1337; 39 | server.listen(port); 40 | 41 | console.log("Server running at http://localhost:%d", port); 42 | -------------------------------------------------------------------------------- /v3/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "key-vault-node-quickstart", 3 | "description": "Simple Hello World Node.js sample for Azure Key Vault using Managed Service Identity", 4 | "version": "0.0.1", 5 | "private": true, 6 | "license": "MIT", 7 | "author": "Microsoft", 8 | "engines": { 9 | "node": ">=6.9.1" 10 | }, 11 | "scripts": { 12 | "start": "node index.js" 13 | }, 14 | "dependencies": { 15 | "azure-keyvault": "^3.0.4", 16 | "ms-rest-azure": "^2.5.7" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /v4/index.js: -------------------------------------------------------------------------------- 1 | var http = require('http'); 2 | const {DefaultAzureCredential, ManagedIdentityCredential} = require('@azure/identity'); 3 | const {SecretClient} = require('@azure/keyvault-secrets'); 4 | // // DefaultAzureCredential expects the following three environment variables: 5 | // // - AZURE_TENANT_ID: The tenant ID in Azure Active Directory 6 | // // - AZURE_CLIENT_ID: The application (client) ID registered in the AAD tenant 7 | // // - AZURE_CLIENT_SECRET: The client secret for the registered application 8 | // const credential = new DefaultAzureCredential(); 9 | 10 | // ManagedIdentityCredential created by "identity assign" command 11 | const credential = new ManagedIdentityCredential(); 12 | 13 | // Replace value with your Key Vault name here 14 | const vaultName = ""; 15 | const url = `https://${vaultName}.vault.azure.net`; 16 | 17 | const client = new SecretClient(url, credential); 18 | 19 | // Replace value with your secret name here 20 | const secretName = ""; 21 | 22 | var server = http.createServer(function(request, response) { 23 | response.writeHead(200, {"Content-Type": "text/plain"}); 24 | async function main(){ 25 | // Get the secret we created 26 | const secret = await client.getSecret(secretName); 27 | response.write(`Your secret value is: ${secret.value}`); 28 | response.end(); 29 | } 30 | main().catch((err) => { 31 | response.write(`error code: ${err.code}`); 32 | response.write(`error message: ${err.message}`); 33 | response.write(`error stack: ${err.stack}`); 34 | response.end(); 35 | }); 36 | }); 37 | 38 | var port = process.env.PORT || 1337; 39 | server.listen(port); 40 | 41 | console.log("Server running at http://localhost:%d", port); 42 | -------------------------------------------------------------------------------- /v4/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "key-vault-node-quickstart", 3 | "description": "Simple Hello World Node.js sample for Azure Key Vault using Managed Service Identity", 4 | "version": "0.0.1", 5 | "private": true, 6 | "license": "MIT", 7 | "author": "Microsoft", 8 | "engines": { 9 | "node": ">=6.9.1" 10 | }, 11 | "scripts": { 12 | "start": "node index.js" 13 | }, 14 | "dependencies": { 15 | "@azure/keyvault-secrets": "4.0.0", 16 | "@azure/identity": "1.0.0" 17 | } 18 | } 19 | --------------------------------------------------------------------------------