Skip to content

Commit

Permalink
[KeyVault] - Clean up sample key deletion (#18574)
Browse files Browse the repository at this point in the history
## What

- Skip deleting keys in every sample
- Delete the `purgeAllKeys` sample
- Add an example on how to delete and purge a single key

## Why

Deleting and purging keys is a costly operation in terms of time. There's no
reason to wait for deleting and purging every single key when we run the
samples now that we deploy and tear down key vaults per test run.
  • Loading branch information
maorleger authored Nov 9, 2021
1 parent e86eefe commit fab3ed3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 63 deletions.
5 changes: 1 addition & 4 deletions sdk/keyvault/keyvault-keys/samples-dev/cryptography.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export async function main(): Promise<void> {
// Connection to Azure Key Vault
const client = new KeyClient(url, credential);

const uniqueString = new Date().getTime();
const keyName = `key${uniqueString}`;
const keyName = `crypto-sample-key${Date.now()}`;

// Connection to Azure Key Vault Cryptography functionality
const myWorkKey = await client.createKey(keyName, "RSA");
Expand Down Expand Up @@ -67,8 +66,6 @@ export async function main(): Promise<void> {

const unwrapped = await cryptoClient.unwrapKey("RSA-OAEP", wrapped.result);
console.log("unwrap result: ", unwrapped);

await client.beginDeleteKey(keyName);
}

main().catch((error) => {
Expand Down
23 changes: 16 additions & 7 deletions sdk/keyvault/keyvault-keys/samples-dev/helloWorld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export async function main(): Promise<void> {
const url = process.env["KEYVAULT_URI"] || "<keyvault-url>";
const client = new KeyClient(url, credential);

const uniqueString = new Date().getTime();
const keyName = `KeyName${uniqueString}`;
const ecKeyName = `ECKeyName${uniqueString}`;
const rsaKeyName = `RSAKeyName${uniqueString}`;
const uniqueString = Date.now();
const keyName = `sample-key-${uniqueString}`;
const ecKeyName = `sample-ec-key-${uniqueString}`;
const rsaKeyName = `sample-rsa-key-${uniqueString}`;

// You can create keys using the general method
const result = await client.createKey(keyName, "EC");
Expand Down Expand Up @@ -53,9 +53,18 @@ export async function main(): Promise<void> {
});
console.log("updated key: ", updatedKey);

await client.beginDeleteKey(keyName);
await client.beginDeleteKey(ecKeyName);
await client.beginDeleteKey(rsaKeyName);
// Delete the key - the key is soft-deleted but not yet purged
const deletePoller = await client.beginDeleteKey(keyName);
await deletePoller.pollUntilDone();

const deletedKey = await client.getDeletedKey(keyName);
console.log("deleted key: ", deletedKey);

// Purge the key - the key is permanently deleted
// This operation could take some time to complete
console.time("purge a single key");
await client.purgeDeletedKey(keyName);
console.timeEnd("purge a single key");
}

main().catch((error) => {
Expand Down
7 changes: 1 addition & 6 deletions sdk/keyvault/keyvault-keys/samples-dev/keyRotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export async function main(): Promise<void> {
const url = process.env["KEYVAULT_URI"] || "<keyvault-url>";
const client = new KeyClient(url, credential);

const uniqueString = `KeyRotationSample${Date.now()}`;
const keyName = `KeyName${uniqueString}`;
const keyName = `key-rotation-sample-key-${Date.now()}`;
const key = await client.createKey(keyName, "EC");
console.log("created key", key);

Expand Down Expand Up @@ -64,10 +63,6 @@ export async function main(): Promise<void> {
// Rotate the key on-demand, generating a new version of the key.
const newKeyVersion = await client.rotateKey(key.name);
console.log("rotated key", newKeyVersion);

// Delete the key. Deleting a key is a long running operation; however, for this sample we will
// fire-and-forget the process and assume the key was successfully deleted.
await client.beginDeleteKey(key.name);
}

main().catch((error) => {
Expand Down
46 changes: 0 additions & 46 deletions sdk/keyvault/keyvault-keys/samples-dev/purgeAllKeys.ts

This file was deleted.

0 comments on commit fab3ed3

Please sign in to comment.