-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
helloWorld.ts
74 lines (59 loc) · 2.62 KB
/
helloWorld.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* @summary Creates, reads, lists, and deletes keys.
*/
import { KeyClient } from "@azure/keyvault-keys";
import { DefaultAzureCredential } from "@azure/identity";
// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();
export async function main(): Promise<void> {
// This sample uses DefaultAzureCredential, which supports a number of authentication mechanisms.
// See https://docs.microsoft.com/javascript/api/overview/azure/identity-readme?view=azure-node-latest for more information
// about DefaultAzureCredential and the other credentials that are available for use.
const credential = new DefaultAzureCredential();
const url = process.env["KEYVAULT_URI"] || "<keyvault-url>";
const client = new KeyClient(url, credential);
// Create unique names for keys we will use in this sample
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");
console.log("key: ", result);
// Or using specialized key creation methods
const ecResult = await client.createEcKey(ecKeyName, { curve: "P-256" });
const rsaResult = await client.createRsaKey(rsaKeyName, { keySize: 2048 });
console.log("Elliptic curve key: ", ecResult);
console.log("RSA Key: ", rsaResult);
// Get a specific key
const key = await client.getKey(keyName);
console.log("key: ", key);
// Or list the keys we have
for await (const keyProperties of client.listPropertiesOfKeys()) {
const key = await client.getKey(keyProperties.name);
console.log("key: ", key);
}
// Update the key
const updatedKey = await client.updateKeyProperties(keyName, result.properties.version!, {
enabled: false,
});
console.log("updated key: ", updatedKey);
// Delete the key - the key is soft-deleted but not yet purged
const deletePoller = await client.beginDeleteKey(keyName);
await deletePoller.pollUntilDone();
// The `getDeletedKey` method can be used to retrieve any soft-deleted key
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) => {
console.error("An error occurred:", error);
process.exit(1);
});