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

Commit 02ce3f1

Browse files
tzhanlFredGao-new
authored andcommitted
Refactoring sample code to match README and provide output on errors for V4 (#9)
* Update v4 and README.md according to PR * Modified README.md * Change 'web app' to uppercase in README.md * Change secret name and secret value to custom name and value, update v4 * Update README.md * Update README.md Co-authored-by: Fred Gao (MSFT) <[email protected]>
1 parent ac5ca50 commit 02ce3f1

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ az keyvault create --name "<MyKeyVaultName>" --resource-group "<MyResourceGroupN
8282

8383
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.
8484

85-
In this tutorial, the password will be called **AppSecret** and will store the value of **MySecret** in it:
85+
In this tutorial, replace `"<MySecretName>"` and `"<MySecretValue>"` with your custom secret name and secret value:
8686

8787
```Bash
88-
az keyvault secret set --vault-name "<MyKeyVaultName>" --name AppSecret --value MySecret
88+
az keyvault secret set --vault-name "<MyKeyVaultName>" --name "<MySecretName>" --value "<MySecretValue>"
8989
```
9090

9191
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:
9292

9393
```Bash
94-
az keyvault secret show --name AppSecret --vault-name "<MyKeyVaultName>"
94+
az keyvault secret show --name "<MySecretName>" --vault-name "<MyKeyVaultName>"
9595
```
9696

9797
### Clone the repo
@@ -162,6 +162,8 @@ npm install
162162
az keyvault show --name "<MyKeyVaultName>"
163163
```
164164

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+
165167
### Enable Azure Managed Identities
166168

167169
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.
@@ -204,13 +206,9 @@ az appservice plan create --name "<MyAppServicePlan>" --resource-group "<MyResou
204206

205207
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:
206208

207-
208-
# Bash
209209
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:
210+
211+
After the Web App is created, the Azure CLI outputs something similar to the following:
214212

215213

216214
{
@@ -257,7 +255,7 @@ git push azure master
257255

258256
When the git push command has completed you can now navigate to `https://<AppName>.azurewebsites.net` to see the secret value.
259257

260-
Make sure that you replaced the name `<AppName>` with your vault name.
258+
Make sure that you replaced the name `<AppName>` with your Web App name.
261259

262260
## Next steps
263261

v4/index.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
var http = require('http');
2-
const {DefaultAzureCredential} = require('@azure/identity');
2+
const {DefaultAzureCredential, ManagedIdentityCredential} = require('@azure/identity');
33
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-
const vaultName = process.env["KEYVAULT_NAME"] || "<YourVaultName>";
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 = "<MyKeyVaultName>";
1115
const url = `https://${vaultName}.vault.azure.net`;
1216

1317
const client = new SecretClient(url, credential);
1418

15-
const secretName = "MySecretName";
19+
// Replace value with your secret name here
20+
const secretName = "<MySecretName>";
1621

1722
var server = http.createServer(function(request, response) {
1823
response.writeHead(200, {"Content-Type": "text/plain"});
19-
});
20-
21-
async function main(){
22-
// Create a secret
23-
const result = await client.setSecret(secretName, "MySecretValue");
24-
console.log("Secret name: ", result.name);
25-
// Read the secret we created
26-
const secret = await client.getSecret(secretName);
27-
console.log("Successfully retrieved 'MySecretName':", secret.value);
28-
}
29-
30-
main().catch((err) => {
31-
console.log("error code: ", err.code);
32-
console.log("error message: ", err.message);
33-
console.log("error stack: ", err.stack);
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+
});
3436
});
3537

3638
var port = process.env.PORT || 1337;

0 commit comments

Comments
 (0)