Skip to content

Commit bd8700d

Browse files
authored
Merge pull request #226013 from diberry/diberry/keyvault-passwordless-secret
Key Vault Secrets - Quickstart JS - passwordless
2 parents 01cbf1d + e934cf9 commit bd8700d

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

articles/key-vault/secrets/quick-create-node.md

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ title: Quickstart - Azure Key Vault secret client library for JavaScript (versi
33
description: Learn how to create, retrieve, and delete secrets from an Azure key vault using the JavaScript client library
44
author: msmbaldwin
55
ms.author: mbaldwin
6-
ms.date: 02/03/2022
6+
ms.date: 02/02/2023
77
ms.service: key-vault
88
ms.subservice: secrets
99
ms.topic: quickstart
1010
ms.devlang: javascript
11-
ms.custom: devx-track-js, mode-api
11+
ms.custom: devx-track-js, mode-api, passwordless-js
1212
---
1313

14-
# Quickstart: Azure Key Vault secret client library for JavaScript (version 4)
14+
# Quickstart: Azure Key Vault secret client library for JavaScript
1515

1616
Get started with the Azure Key Vault secret client library for JavaScript. [Azure Key Vault](../general/overview.md) is a cloud service that provides a secure store for secrets. You can securely store keys, passwords, certificates, and other secrets. Azure key vaults may be created and managed through the Azure portal. In this quickstart, you learn how to create, retrieve, and delete secrets from an Azure key vault using the JavaScript client library
1717

@@ -39,7 +39,7 @@ This quickstart assumes you are running [Azure CLI](/cli/azure/install-azure-cli
3939

4040
1. Run the `login` command.
4141

42-
```azurecli-interactive
42+
```azurecli
4343
az login
4444
```
4545
@@ -67,62 +67,90 @@ Create a Node.js application that uses your key vault.
6767
6868
## Install Key Vault packages
6969
70-
1. Using the terminal, install the Azure Key Vault secrets library, [@azure/keyvault-secrets](https://www.npmjs.com/package/@azure/keyvault-secrets) for Node.js.
70+
1. Using the terminal, install the Azure Key Vault secrets client library, [@azure/keyvault-secrets](https://www.npmjs.com/package/@azure/keyvault-secrets) for Node.js.
7171
7272
```terminal
7373
npm install @azure/keyvault-secrets
7474
```
7575
76-
1. Install the Azure Identity library, [@azure/identity](https://www.npmjs.com/package/@azure/identity) package to authenticate to a Key Vault.
76+
1. Install the Azure Identity client library, [@azure/identity](https://www.npmjs.com/package/@azure/identity) package to authenticate to a Key Vault.
7777
7878
```terminal
7979
npm install @azure/identity
8080
```
8181
8282
## Grant access to your key vault
8383
84-
Create an access policy for your key vault that grants secret permissions to your user account with the [az keyvault set-policy](/cli/azure/keyvault#az-keyvault-set-policy) command.
84+
Create a vault access policy for your key vault that grants secret permissions to your user account with the [az keyvault set-policy](/cli/azure/keyvault#az-keyvault-set-policy) command.
8585
8686
```azurecli
87-
az keyvault set-policy --name <your-key-vault-name> --upn [email protected] --secret-permissions delete get list set purge
87+
az keyvault set-policy --name <your-key-vault-name> --upn [email protected] --secret-permissions delete get list set purge update
8888
```
8989

9090
## Set environment variables
9191

9292
This application is using key vault name as an environment variable called `KEY_VAULT_NAME`.
9393

94-
Windows
94+
### [Windows](#tab/windows)
95+
9596
```cmd
9697
set KEY_VAULT_NAME=<your-key-vault-name>
9798
````
99+
100+
### [PowerShell](#tab/powershell)
101+
98102
Windows PowerShell
99103
```powershell
100104
$Env:KEY_VAULT_NAME="<your-key-vault-name>"
101105
```
102106

103-
macOS or Linux
107+
### [macOS or Linux](#tab/linux)
108+
104109
```cmd
105110
export KEY_VAULT_NAME=<your-key-vault-name>
106111
```
112+
---
113+
114+
115+
## Authenticate and create a client
116+
117+
Application requests to most Azure services must be authorized. Using the [DefaultAzureCredential](/javascript/api/@azure/identity/#@azure-identity-getdefaultazurecredential) method provided by the [Azure Identity client library](/javascript/api/@azure/identity) is the recommended approach for implementing passwordless connections to Azure services in your code. `DefaultAzureCredential` supports multiple authentication methods and determines which method should be used at runtime. This approach enables your app to use different authentication methods in different environments (local vs. production) without implementing environment-specific code.
118+
119+
In this quickstart, `DefaultAzureCredential` authenticates to key vault using the credentials of the local development user logged into the Azure CLI. When the application is deployed to Azure, the same `DefaultAzureCredential` code can automatically discover and use a managed identity that is assigned to an App Service, Virtual Machine, or other services. For more information, see [Managed Identity Overview](/azure/active-directory/managed-identities-azure-resources/overview).
120+
121+
In this code, the name of your key vault is used to create the key vault URI, in the format `https://<your-key-vault-name>.vault.azure.net`. For more information about authenticating to key vault, see [Developer's Guide](/azure/key-vault/general/developers-guide#authenticate-to-key-vault-in-code).
107122

108123
## Code example
109124

110125
The code samples below will show you how to create a client, set a secret, retrieve a secret, and delete a secret.
111126

127+
This code uses the following [Key Vault Secret classes and methods](/javascript/api/overview/azure/keyvault-secretss-readme):
128+
129+
* [DefaultAzureCredential](/javascript/api/@azure/identity/#@azure-identity-getdefaultazurecredential)
130+
* [SecretClient class](/javascript/api/@azure/keyvault-secrets/secretclient)
131+
* [setSecret](/javascript/api/@azure/keyvault-secrets/secretclient#@azure-keyvault-secrets-secretclient-setsecret)
132+
* [getSecret](/javascript/api/@azure/keyvault-secrets/secretclient#@azure-keyvault-secrets-secretclient-getsecret)
133+
* [updateSecretProperties](/javascript/api/@azure/keyvault-secrets/secretclient#@azure-keyvault-secrets-secretclient-updatesecretproperties)
134+
* [beginDeleteSecret](/javascript/api/@azure/keyvault-secrets/secretclient#@azure-keyvault-secrets-secretclient-begindeletesecret)
135+
136+
### Set up the app framework
137+
112138
1. Create new text file and paste the following code into the **index.js** file.
113139

114140
```javascript
115141
const { SecretClient } = require("@azure/keyvault-secrets");
116142
const { DefaultAzureCredential } = require("@azure/identity");
117143

118-
// Load the .env file if it exists
119-
const dotenv = require("dotenv");
120-
dotenv.config();
121-
122144
async function main() {
145+
// If you're using MSI, DefaultAzureCredential should "just work".
146+
// Otherwise, DefaultAzureCredential expects the following three environment variables:
147+
// - AZURE_TENANT_ID: The tenant ID in Azure Active Directory
148+
// - AZURE_CLIENT_ID: The application (client) ID registered in the AAD tenant
149+
// - AZURE_CLIENT_SECRET: The client secret for the registered application
123150
const credential = new DefaultAzureCredential();
124151

125152
const keyVaultName = process.env["KEY_VAULT_NAME"];
153+
if(!keyVaultName) throw new Error("KEY_VAULT_NAME is empty");
126154
const url = "https://" + keyVaultName + ".vault.azure.net";
127155

128156
const client = new SecretClient(url, credential);
@@ -146,8 +174,7 @@ The code samples below will show you how to create a client, set a secret, retri
146174
});
147175
console.log("updated secret: ", updatedSecret);
148176

149-
// Delete the secret
150-
// If we don't want to purge the secret later, we don't need to wait until this finishes
177+
// Delete the secret immediately without ability to restore or purge.
151178
await client.beginDeleteSecret(secretName);
152179
}
153180

0 commit comments

Comments
 (0)