Skip to content

Commit 643a962

Browse files
authored
[KeyVault] - Migrate all packages to Samples V2 (Azure#15023)
This PR migrates all 4 KeyVault packages to the new Samples V2 implementation. I left most samples untouched (because otherwise it would be difficult to tell what is migrated vs. what actually changed), but did minor clean up where it made sense. Resolves Azure#14475
1 parent a91042b commit 643a962

File tree

117 files changed

+3611
-891
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+3611
-891
lines changed

sdk/keyvault/keyvault-admin/README.md

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,8 @@ The methods that begin long running operations return a poller that allows you t
205205

206206
We have samples both in JavaScript and TypeScript that show the access control and backup/restore features in this package. Please follow the corresponding readmes for detailed steps to run the samples.
207207

208-
- [Readme for JavaScript samples](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/keyvault/keyvault-admin/samples/javascript/README.md)
209-
- [Readme for TypeScript samples](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/keyvault/keyvault-admin/samples/typescript/README.md)
210-
211-
Direct links to the specific JavaScript samples follow:
212-
213-
- Access control (RBAC):
214-
- [Listing All Role Definitions](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/keyvault/keyvault-admin/samples/javascript/accessControlHelloWorld.js)
215-
- [Listing All Role Assignments](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/keyvault/keyvault-admin/samples/javascript/accessControlHelloWorld.js)
216-
- [Creating a Role Assignment](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/keyvault/keyvault-admin/samples/javascript/accessControlHelloWorld.js)
217-
- [Getting a Role Assignment](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/keyvault/keyvault-admin/samples/javascript/accessControlHelloWorld.js)
218-
- [Deleting a Role Assignment](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/keyvault/keyvault-admin/samples/javascript/accessControlHelloWorld.js)
219-
- Backup and restore:
220-
- [Performing a full key backup](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/keyvault/keyvault-admin/samples/javascript/backupRestoreHelloWorld.js)
221-
- [Performing a full key restore](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/keyvault/keyvault-admin/samples/javascript/backupRestoreHelloWorld.js)
222-
- [Performing a selective key backup](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/keyvault/keyvault-admin/samples/javascript/backupSelectiveRestore.js)
223-
- [Performing a selective key restore](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/keyvault/keyvault-admin/samples/javascript/backupSelectiveRestore.js)
208+
- [Readme for JavaScript samples](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/keyvault/keyvault-admin/samples/v4/javascript/README.md)
209+
- [Readme for TypeScript samples](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/keyvault/keyvault-admin/samples/v4/typescript/README.md)
224210

225211
## Troubleshooting
226212

sdk/keyvault/keyvault-admin/package.json

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@
4646
},
4747
"scripts": {
4848
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
49-
"build:samples": "echo skipped",
49+
"build:samples": "echo Obsolete.",
5050
"build:node": "tsc -p . && cross-env ONLY_NODE=true rollup -c 2>&1",
5151
"build:browser": "tsc -p . && cross-env ONLY_BROWSER=true rollup -c 2>&1",
5252
"build:nodebrowser": "rollup -c 2>&1",
5353
"build:test": "tsc -p . && rollup -c rollup.test.config.js 2>&1",
5454
"build": "tsc -p . && npm run build:nodebrowser && api-extractor run --local",
55-
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
55+
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
5656
"clean": "rimraf dist dist-* types *.tgz *.log statistics.html coverage && rimraf src/**/*.js && rimraf test/**/*.js",
57-
"execute:samples": "npm run build:samples && echo skipped",
57+
"execute:samples": "dev-tools samples run samples-dev",
5858
"extract-api": "tsc -p . && api-extractor run --local",
59-
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
59+
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
6060
"integration-test:browser": "echo skipped",
6161
"integration-test:node": "nyc mocha -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 180000 --full-trace \"dist-esm/**/*.spec.js\"",
6262
"integration-test:node:no-timeout": "nyc mocha -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --no-timeouts --full-trace \"dist-esm/**/*.spec.js\"",
@@ -74,6 +74,20 @@
7474
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
7575
"docs": "typedoc --excludePrivate --excludeNotExported --excludeExternals --stripInternal --mode file --out ./dist/docs ./src"
7676
},
77+
"//sampleConfiguration": {
78+
"productName": "Azure Key Vault Administration",
79+
"productSlugs": [
80+
"azure",
81+
"azure-key-vault"
82+
],
83+
"requiredResources": {
84+
"Azure Key Vault": "https://docs.microsoft.com/azure/key-vault/quick-create-portal"
85+
},
86+
"customSnippets": {
87+
"prerequisites": "samples-dev/snippets/_prerequisites.md"
88+
},
89+
"skipFolder": true
90+
},
7791
"sideEffects": false,
7892
"dependencies": {
7993
"@azure/abort-controller": "^1.0.0",

sdk/keyvault/keyvault-admin/sample.env

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
# The name of the key vault to use in the samples.
2-
# Create a Key Vault in the Azure Portal and enter its URI (e.g. https://mytest.vault.azure.net/) here.
3-
KEYVAULT_URI=<key-vault-uri>
4-
5-
# The name of the Managed HSM Key Vault to use in the tests.
1+
# The name of the Managed HSM Key Vault to use in the samples.
62
# At the moment only Azure Managed HSM supports administration operations.
73
AZURE_MANAGEDHSM_URI=<managed-hsm-uri>
84

@@ -25,7 +21,3 @@ CLIENT_OBJECT_ID=<client-object-id>
2521
AZURE_TENANT_ID=<AD tenant id or name>
2622
AZURE_CLIENT_ID=<ID of the user/service principal to authenticate as>
2723
AZURE_CLIENT_SECRET=<client secret used to authenticate to Azure AD>
28-
29-
# Our tests assume that TEST_MODE is "playback" by default. You can
30-
# change it to "record" to generate new recordings, or "live" to bypass the recorder entirely.
31-
# TEST_MODE=playback

sdk/keyvault/keyvault-admin/samples/typescript/src/accessControlHelloWorld.ts renamed to sdk/keyvault/keyvault-admin/samples-dev/accessControlHelloWorld.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4+
/**
5+
* @summary Uses an AccessControlClient to list, create, and assign roles to users.
6+
*/
7+
48
import { KeyVaultAccessControlClient, KeyVaultPermission } from "@azure/keyvault-admin";
59
import { DefaultAzureCredential } from "@azure/identity";
6-
import { v4 as uuidv4 } from "uuid";
10+
import * as uuid from "uuid";
711

812
// Load the .env file if it exists
913
import * as dotenv from "dotenv";
@@ -14,9 +18,11 @@ export async function main(): Promise<void> {
1418
// - AZURE_TENANT_ID: The tenant ID in Azure Active Directory
1519
// - AZURE_CLIENT_ID: The application (client) ID registered in the AAD tenant
1620
// - AZURE_CLIENT_SECRET: The client secret for the registered application
17-
// - CLIENT_OBJECT_ID: Object ID of the application, tenant or principal to whom the role will be assigned to
1821
const credential = new DefaultAzureCredential();
19-
const url = process.env["KEYVAULT_URI"] || "<keyvault-url>";
22+
const url = process.env["AZURE_MANAGEDHSM_URI"];
23+
if (!url) {
24+
throw new Error("Missing environment variable AZURE_MANAGEDHSM_URI.");
25+
}
2026
const client = new KeyVaultAccessControlClient(url, credential);
2127

2228
for await (const roleAssignment of client.listRoleAssignments("/")) {
@@ -25,7 +31,7 @@ export async function main(): Promise<void> {
2531

2632
const globalScope = "/";
2733

28-
const roleDefinitionName = uuidv4();
34+
const roleDefinitionName = uuid.v4();
2935
const permissions: KeyVaultPermission[] = [
3036
{
3137
dataActions: [
@@ -44,12 +50,16 @@ export async function main(): Promise<void> {
4450

4551
// This sample uses a custom role but you may assign one of the many built-in roles.
4652
// Please refer to https://docs.microsoft.com/azure/key-vault/managed-hsm/built-in-roles for more information.
47-
const roleAssignmentName = uuidv4();
53+
const roleAssignmentName = uuid.v4();
54+
const clientObjectId = process.env["CLIENT_OBJECT_ID"];
55+
if (!clientObjectId) {
56+
throw new Error("Missing environment variable CLIENT_OBJECT_ID.");
57+
}
4858
let assignment = await client.createRoleAssignment(
4959
globalScope,
5060
roleAssignmentName,
5161
roleDefinition.id,
52-
process.env["CLIENT_OBJECT_ID"]
62+
clientObjectId
5363
);
5464
console.log(assignment);
5565

sdk/keyvault/keyvault-admin/samples/typescript/src/backupRestoreHelloWorld.ts renamed to sdk/keyvault/keyvault-admin/samples-dev/backupRestoreHelloWorld.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4+
/**
5+
* @summary Uses a BackupClient to backup and fully restore an Azure Key Vault using Azure Storage Blob.
6+
*/
7+
48
import { KeyVaultBackupClient } from "@azure/keyvault-admin";
59
import { DefaultAzureCredential } from "@azure/identity";
610

@@ -17,18 +21,27 @@ export async function main(): Promise<void> {
1721
// - BLOB_STORAGE_SAS_TOKEN: URI of the Blob Storage instance, with the name of the container where the Key Vault backups will be generated
1822
// - CLIENT_OBJECT_ID: Object ID of the application, tenant or principal to whom the role will be assigned to
1923
const credential = new DefaultAzureCredential();
20-
const url = process.env["KEYVAULT_URI"] || "<keyvault-url>";
24+
const url = process.env["KEYVAULT_URI"];
25+
if (!url) {
26+
throw new Error("Missing environment variable KEYVAULT_URI.");
27+
}
2128
const client = new KeyVaultBackupClient(url, credential);
2229

2330
const blobStorageUri = process.env["BLOB_STORAGE_URI"];
31+
if (!blobStorageUri) {
32+
throw new Error("Missing environment variable BLOB_STORAGE_URI.");
33+
}
2434
const sasToken = process.env["BLOB_STORAGE_SAS_TOKEN"];
25-
const backupPoller = await client.beginBackup(blobStorageUri, sasToken);
35+
if (!sasToken) {
36+
throw new Error("Missing environment variable BLOB_STORAGE_SAS_TOKEN.");
37+
}
38+
const backupPoller = await client.beginBackup(blobStorageUri!, sasToken);
2639
const backupResult = await backupPoller.pollUntilDone();
2740

2841
// The folder name should be at the end of the backupFolderUri, as in: https://<blob-storage-endpoint>/<folder-name>
29-
const folderName = backupResult.backupFolderUri.split("/").pop();
42+
const folderName = backupResult.backupFolderUri!.split("/").pop();
3043

31-
const restorePoller = await client.beginRestore(blobStorageUri, sasToken, folderName);
44+
const restorePoller = await client.beginRestore(blobStorageUri, sasToken, folderName!);
3245
await restorePoller.pollUntilDone();
3346
}
3447

sdk/keyvault/keyvault-admin/samples/typescript/src/backupSelectiveRestore.ts renamed to sdk/keyvault/keyvault-admin/samples-dev/backupSelectiveRestore.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4+
/**
5+
* @summary Uses a BackupClient to backup and restore a specific key in Azure Key Vault using Azure Storage Blob.
6+
*/
7+
48
import { KeyVaultBackupClient } from "@azure/keyvault-admin";
59
import { KeyClient } from "@azure/keyvault-keys";
610
import { DefaultAzureCredential } from "@azure/identity";
@@ -18,25 +22,34 @@ export async function main(): Promise<void> {
1822
// - BLOB_STORAGE_SAS_TOKEN: URI of the Blob Storage instance, with the name of the container where the Key Vault backups will be generated
1923
// - CLIENT_OBJECT_ID: Object ID of the application, tenant or principal to whom the role will be assigned to
2024
const credential = new DefaultAzureCredential();
21-
const url = process.env["KEYVAULT_URI"] || "<keyvault-url>";
25+
const url = process.env["AZURE_MANAGEDHSM_URI"];
26+
if (!url) {
27+
throw new Error("Missing environment variable AZURE_MANAGEDHSM_URI.");
28+
}
2229
const client = new KeyVaultBackupClient(url, credential);
2330

2431
const keyClient = new KeyClient(url, credential);
2532
const keyName = "key-name";
2633
const key = await keyClient.createRsaKey(keyName);
2734

2835
const blobStorageUri = process.env["BLOB_STORAGE_URI"];
36+
if (!blobStorageUri) {
37+
throw new Error("Missing environment variable BLOB_STORAGE_URI.");
38+
}
2939
const sasToken = process.env["BLOB_STORAGE_SAS_TOKEN"];
40+
if (!sasToken) {
41+
throw new Error("Missing environment variable BLOB_STORAGE_SAS_TOKEN.");
42+
}
3043
const backupPoller = await client.beginBackup(blobStorageUri, sasToken);
3144
const backupResult = await backupPoller.pollUntilDone();
3245

3346
// The folder name should be at the end of the backupFolderUri, as in: https://<blob-storage-endpoint>/<folder-name>
34-
const folderName = backupResult.backupFolderUri.split("/").pop();
47+
const folderName = backupResult.backupFolderUri!.split("/").pop();
3548

3649
const selectiveRestorePoller = await client.beginSelectiveRestore(
3750
blobStorageUri,
3851
sasToken,
39-
folderName,
52+
folderName!,
4053
key.name
4154
);
4255
await selectiveRestorePoller.pollUntilDone();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
To quickly create the needed Key Vault resources in Azure and to receive a connection string for them, you can deploy our sample template by clicking:
2+
3+
[![](http://azuredeploy.net/deploybutton.png)](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-sdk-for-js%2Fmaster%2Fsdk%2Fkeyvault%2Ftest-resources.json)
4+
5+
If creating the Key Vault manually using the Azure Portal, be aware that the samples require that the soft-delete feature be enabled. Our template above will enable this feature automatically, but it is possible to enable it manually using the Azure CLI. See [the documentation for enabling soft-delete in Key Vault](https://docs.microsoft.com/azure/key-vault/key-vault-soft-delete-cli) for more information.

sdk/keyvault/keyvault-admin/samples/javascript/README.md

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)