Skip to content

Commit

Permalink
[KeyVault] - Update duration sample and doc comments (#18508)
Browse files Browse the repository at this point in the history
## What

- Use Day.js in the Key Rotation sample
- Update the doc comments for Key Rotation

## Why

We discussed adding updating the API of Key Rotation to take an object instead
of a raw string for ISO8601 durations; however, with Temporal adding support for
Durations we want to avoid duplicating the type for now. Instead, we decided to
demonstrate interop with an existing 3rd party library. I chose Day.js for this
sample as it has a nice clean API.
  • Loading branch information
maorleger authored Nov 3, 2021
1 parent 5db226a commit 416b79b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
8 changes: 7 additions & 1 deletion common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sdk/keyvault/keyvault-keys/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"chai-as-promised": "^7.1.1",
"chai-exclude": "^2.0.2",
"cross-env": "^7.0.2",
"dayjs": "^1.10.7",
"dotenv": "^8.2.0",
"eslint": "^7.15.0",
"esm": "^3.2.18",
Expand Down
9 changes: 8 additions & 1 deletion sdk/keyvault/keyvault-keys/samples-dev/keyRotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import { KeyClient } from "@azure/keyvault-keys";
import { DefaultAzureCredential } from "@azure/identity";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
dayjs.extend(duration);

// Load the .env file if it exists
import * as dotenv from "dotenv";
Expand Down Expand Up @@ -43,11 +46,15 @@ export async function main(): Promise<void> {
console.log("fetched policy", currentPolicy);

// Update the key's automated rotation policy to notify 30 days before the key expires.
// By using the ISO8601 duration standard, interoperability with any 3rd party library that supports Durations is supported.
// In this example, we'll use Day.js (documented in https://day.js.org) to create the duration.
// For more information on the ISO 8601 Duration standard, please refer to the Wikipedia page on Durations:
// https://wikipedia.org/wiki/ISO_8601#Durations
const updatedPolicy = await client.updateKeyRotationPolicy(key.name, {
lifetimeActions: [
{
action: "Notify",
timeBeforeExpiry: "P30D"
timeBeforeExpiry: dayjs.duration({ days: 30 }).toISOString()
}
],
expiresIn: "P90D"
Expand Down
5 changes: 4 additions & 1 deletion sdk/keyvault/keyvault-keys/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ export class KeyClient {

/**
* Gets the requested number of bytes containing random values from a managed HSM.
* This operation requires the managedHsm/rng permission.
*
* Example usage:
* ```ts
Expand Down Expand Up @@ -848,11 +849,12 @@ export class KeyClient {

/**
* Gets the rotation policy of a Key Vault Key.
* By default, all keys have a policy that will notify 30 days before expiry.
*
* This operation requires the keys/get permission.
* Example usage:
* ```ts
* let client = new KeyClient(vaultUrl, credentials);
* await client.updateKeyRotationPolicy("MyKey", myPolicy);
* let result = await client.getKeyRotationPolicy("myKey");
* ```
*
Expand All @@ -871,6 +873,7 @@ export class KeyClient {

/**
* Updates the rotation policy of a Key Vault Key.
* This operation requires the keys/update permission.
*
* Example usage:
* ```ts
Expand Down

0 comments on commit 416b79b

Please sign in to comment.