|
3 | 3 |
|
4 | 4 | import chai, { assert } from "chai"; |
5 | 5 | import chaiExclude from "chai-exclude"; |
| 6 | +import chaiAsPromised from "chai-as-promised"; |
6 | 7 | chai.use(chaiExclude); |
| 8 | +chai.use(chaiAsPromised); |
7 | 9 | import { Context } from "mocha"; |
8 | 10 | import { RestError } from "@azure/core-http"; |
9 | 11 | import { AbortController } from "@azure/abort-controller"; |
10 | | -import { env, Recorder } from "@azure-tools/test-recorder"; |
| 12 | +import { env, isPlaybackMode, isRecordMode, Recorder } from "@azure-tools/test-recorder"; |
11 | 13 |
|
12 | 14 | import { |
13 | 15 | KeyClient, |
14 | 16 | CreateEcKeyOptions, |
15 | 17 | UpdateKeyPropertiesOptions, |
16 | 18 | GetKeyOptions |
17 | 19 | } from "../../src"; |
18 | | -import { assertThrowsAbortError, getServiceVersion, onVersions } from "../utils/utils.common"; |
| 20 | +import { |
| 21 | + assertThrowsAbortError, |
| 22 | + getServiceVersion, |
| 23 | + isPublicCloud, |
| 24 | + onVersions |
| 25 | +} from "../utils/utils.common"; |
19 | 26 | import { testPollerProperties } from "../utils/recorderUtils"; |
20 | 27 | import { authenticate } from "../utils/testAuthentication"; |
21 | 28 | import TestClient from "../utils/testClient"; |
@@ -401,132 +408,136 @@ describe("Keys client - create, read, update and delete operations", () => { |
401 | 408 | }); |
402 | 409 |
|
403 | 410 | onVersions({ minVer: "7.3-preview" }).describe("key rotation", () => { |
404 | | - it("rotateKey supports rotating a key", async () => { |
405 | | - const keyName = recorder.getUniqueName("keyrotate"); |
406 | | - const key = await client.createKey(keyName, "RSA"); |
407 | | - const rotatedKey = await client.rotateKey(keyName); |
408 | | - |
409 | | - // The rotated key should have mostly the same data, excluding properties that are rotated. |
410 | | - assert.deepEqualExcludingEvery(rotatedKey, key, ["id", "kid", "version", "n", "e"] as any); |
411 | | - |
412 | | - // A new version is created, and the key material is rotated (RSA key, check n and e). |
413 | | - assert.notEqual(rotatedKey.id, key.id); |
414 | | - assert.notEqual(rotatedKey.properties.version, key.properties.version); |
415 | | - assert.notDeepEqual(rotatedKey.key?.n, key.key?.n); |
416 | | - }); |
417 | | - |
418 | | - it("rotateKey supports tracing", async () => { |
419 | | - const keyName = recorder.getUniqueName("keyrotatetracing"); |
420 | | - const key = await client.createKey(keyName, "RSA"); |
| 411 | + if (isPublicCloud() || isRecordMode() || isPlaybackMode()) { |
| 412 | + // Key Rotation is a preview feature that is not supported in all clouds yet. |
| 413 | + // Once 7.3 GAs we should be able to run this unconditionally. |
| 414 | + it("rotateKey supports rotating a key", async () => { |
| 415 | + const keyName = recorder.getUniqueName("keyrotate"); |
| 416 | + const key = await client.createKey(keyName, "RSA"); |
| 417 | + const rotatedKey = await client.rotateKey(keyName); |
| 418 | + |
| 419 | + // The rotated key should have mostly the same data, excluding properties that are rotated. |
| 420 | + assert.deepEqualExcludingEvery(rotatedKey, key, ["id", "kid", "version", "n", "e"] as any); |
| 421 | + |
| 422 | + // A new version is created, and the key material is rotated (RSA key, check n and e). |
| 423 | + assert.notEqual(rotatedKey.id, key.id); |
| 424 | + assert.notEqual(rotatedKey.properties.version, key.properties.version); |
| 425 | + assert.notDeepEqual(rotatedKey.key?.n, key.key?.n); |
| 426 | + }); |
421 | 427 |
|
422 | | - await supportsTracing((tracingOptions) => client.rotateKey(key.name, { tracingOptions }), [ |
423 | | - "Azure.KeyVault.Keys.KeyClient.rotateKey" |
424 | | - ]); |
425 | | - }); |
| 428 | + it("rotateKey supports tracing", async () => { |
| 429 | + const keyName = recorder.getUniqueName("keyrotatetracing"); |
| 430 | + const key = await client.createKey(keyName, "RSA"); |
426 | 431 |
|
427 | | - it("updateKeyRotationPolicy supports creating a new rotation policy and fetching it", async () => { |
428 | | - const keyName = recorder.getUniqueName("keyrotationpolicy"); |
429 | | - const key = await client.createKey(keyName, "RSA"); |
430 | | - |
431 | | - const rotationPolicy = await client.updateKeyRotationPolicy(key.name, { |
432 | | - expiresIn: "P90D", |
433 | | - lifetimeActions: [ |
434 | | - { |
435 | | - action: "Rotate", |
436 | | - timeBeforeExpiry: "P30D" |
437 | | - } |
438 | | - ] |
| 432 | + await supportsTracing((tracingOptions) => client.rotateKey(key.name, { tracingOptions }), [ |
| 433 | + "Azure.KeyVault.Keys.KeyClient.rotateKey" |
| 434 | + ]); |
439 | 435 | }); |
440 | 436 |
|
441 | | - const fetchedPolicy = await client.getKeyRotationPolicy(keyName); |
| 437 | + it("updateKeyRotationPolicy supports creating a new rotation policy and fetching it", async () => { |
| 438 | + const keyName = recorder.getUniqueName("keyrotationpolicy"); |
| 439 | + const key = await client.createKey(keyName, "RSA"); |
442 | 440 |
|
443 | | - assert.deepEqual(fetchedPolicy, rotationPolicy); |
444 | | - }); |
| 441 | + const rotationPolicy = await client.updateKeyRotationPolicy(key.name, { |
| 442 | + expiresIn: "P90D", |
| 443 | + lifetimeActions: [ |
| 444 | + { |
| 445 | + action: "Rotate", |
| 446 | + timeBeforeExpiry: "P30D" |
| 447 | + } |
| 448 | + ] |
| 449 | + }); |
445 | 450 |
|
446 | | - it("updateKeyRotationPolicy supports updating an existing policy", async () => { |
447 | | - const keyName = recorder.getUniqueName("keyrotationpolicy"); |
448 | | - const key = await client.createKey(keyName, "RSA"); |
449 | | - |
450 | | - // Create a policy which we will override later. |
451 | | - await client.updateKeyRotationPolicy(key.name, { |
452 | | - lifetimeActions: [ |
453 | | - { |
454 | | - action: "Rotate", |
455 | | - timeAfterCreate: "P2M" |
456 | | - } |
457 | | - ] |
458 | | - }); |
| 451 | + const fetchedPolicy = await client.getKeyRotationPolicy(keyName); |
459 | 452 |
|
460 | | - const updatedPolicy = await client.updateKeyRotationPolicy(key.name, { |
461 | | - expiresIn: "P90D", |
462 | | - lifetimeActions: [ |
463 | | - { |
464 | | - action: "Notify", |
465 | | - timeBeforeExpiry: "P30D" |
466 | | - } |
467 | | - ] |
| 453 | + assert.deepEqual(fetchedPolicy, rotationPolicy); |
468 | 454 | }); |
469 | 455 |
|
470 | | - assert.deepEqual(updatedPolicy, { |
471 | | - id: updatedPolicy.id, |
472 | | - createdOn: updatedPolicy.createdOn, |
473 | | - updatedOn: updatedPolicy.updatedOn, |
474 | | - expiresIn: "P90D", |
475 | | - lifetimeActions: [ |
476 | | - { |
477 | | - timeAfterCreate: undefined, |
478 | | - action: "Notify", |
479 | | - timeBeforeExpiry: "P30D" |
480 | | - } |
481 | | - ] |
482 | | - }); |
483 | | - }); |
484 | | - |
485 | | - it("updateKeyRotationPolicy supports tracing", async () => { |
486 | | - const keyName = recorder.getUniqueName("updaterotationpolicy"); |
487 | | - const key = await client.createKey(keyName, "EC"); |
| 456 | + it("updateKeyRotationPolicy supports updating an existing policy", async () => { |
| 457 | + const keyName = recorder.getUniqueName("keyrotationpolicy"); |
| 458 | + const key = await client.createKey(keyName, "RSA"); |
488 | 459 |
|
489 | | - await supportsTracing( |
490 | | - (tracingOptions) => |
491 | | - client.updateKeyRotationPolicy( |
492 | | - key.name, |
| 460 | + // Create a policy which we will override later. |
| 461 | + await client.updateKeyRotationPolicy(key.name, { |
| 462 | + lifetimeActions: [ |
493 | 463 | { |
494 | | - lifetimeActions: [ |
495 | | - { |
496 | | - action: "Notify", |
497 | | - timeBeforeExpiry: "P30D" |
498 | | - } |
499 | | - ], |
500 | | - expiresIn: "P90D" |
501 | | - }, |
502 | | - { tracingOptions } |
503 | | - ), |
504 | | - ["Azure.KeyVault.Keys.KeyClient.updateKeyRotationPolicy"] |
505 | | - ); |
506 | | - }); |
| 464 | + action: "Rotate", |
| 465 | + timeAfterCreate: "P2M" |
| 466 | + } |
| 467 | + ] |
| 468 | + }); |
| 469 | + |
| 470 | + const updatedPolicy = await client.updateKeyRotationPolicy(key.name, { |
| 471 | + expiresIn: "P90D", |
| 472 | + lifetimeActions: [ |
| 473 | + { |
| 474 | + action: "Notify", |
| 475 | + timeBeforeExpiry: "P30D" |
| 476 | + } |
| 477 | + ] |
| 478 | + }); |
| 479 | + |
| 480 | + assert.deepEqual(updatedPolicy, { |
| 481 | + id: updatedPolicy.id, |
| 482 | + createdOn: updatedPolicy.createdOn, |
| 483 | + updatedOn: updatedPolicy.updatedOn, |
| 484 | + expiresIn: "P90D", |
| 485 | + lifetimeActions: [ |
| 486 | + { |
| 487 | + timeAfterCreate: undefined, |
| 488 | + action: "Notify", |
| 489 | + timeBeforeExpiry: "P30D" |
| 490 | + } |
| 491 | + ] |
| 492 | + }); |
| 493 | + }); |
507 | 494 |
|
508 | | - it("throws when attempting to fetch a policy of a non-existent key", async () => { |
509 | | - const keyName = recorder.getUniqueName("nonexistentkey"); |
510 | | - await assert.isRejected(client.getKeyRotationPolicy(keyName)); |
511 | | - }); |
| 495 | + it("updateKeyRotationPolicy supports tracing", async () => { |
| 496 | + const keyName = recorder.getUniqueName("updaterotationpolicy"); |
| 497 | + const key = await client.createKey(keyName, "EC"); |
| 498 | + |
| 499 | + await supportsTracing( |
| 500 | + (tracingOptions) => |
| 501 | + client.updateKeyRotationPolicy( |
| 502 | + key.name, |
| 503 | + { |
| 504 | + lifetimeActions: [ |
| 505 | + { |
| 506 | + action: "Notify", |
| 507 | + timeBeforeExpiry: "P30D" |
| 508 | + } |
| 509 | + ], |
| 510 | + expiresIn: "P90D" |
| 511 | + }, |
| 512 | + { tracingOptions } |
| 513 | + ), |
| 514 | + ["Azure.KeyVault.Keys.KeyClient.updateKeyRotationPolicy"] |
| 515 | + ); |
| 516 | + }); |
512 | 517 |
|
513 | | - it("getKeyRotationPolicy supports tracing", async () => { |
514 | | - const keyName = recorder.getUniqueName("rotationpolicytracing"); |
515 | | - const key = await client.createKey(keyName, "RSA"); |
516 | | - |
517 | | - await client.updateKeyRotationPolicy(key.name, { |
518 | | - lifetimeActions: [ |
519 | | - { |
520 | | - action: "Rotate", |
521 | | - timeAfterCreate: "P2M" |
522 | | - } |
523 | | - ] |
| 518 | + it("throws when attempting to fetch a policy of a non-existent key", async () => { |
| 519 | + const keyName = recorder.getUniqueName("nonexistentkey"); |
| 520 | + await assert.isRejected(client.getKeyRotationPolicy(keyName)); |
524 | 521 | }); |
525 | 522 |
|
526 | | - await supportsTracing( |
527 | | - (tracingOptions) => client.getKeyRotationPolicy(key.name, { tracingOptions }), |
528 | | - ["Azure.KeyVault.Keys.KeyClient.getKeyRotationPolicy"] |
529 | | - ); |
530 | | - }); |
| 523 | + it("getKeyRotationPolicy supports tracing", async () => { |
| 524 | + const keyName = recorder.getUniqueName("rotationpolicytracing"); |
| 525 | + const key = await client.createKey(keyName, "RSA"); |
| 526 | + |
| 527 | + await client.updateKeyRotationPolicy(key.name, { |
| 528 | + lifetimeActions: [ |
| 529 | + { |
| 530 | + action: "Rotate", |
| 531 | + timeAfterCreate: "P2M" |
| 532 | + } |
| 533 | + ] |
| 534 | + }); |
| 535 | + |
| 536 | + await supportsTracing( |
| 537 | + (tracingOptions) => client.getKeyRotationPolicy(key.name, { tracingOptions }), |
| 538 | + ["Azure.KeyVault.Keys.KeyClient.getKeyRotationPolicy"] |
| 539 | + ); |
| 540 | + }); |
| 541 | + } |
531 | 542 | }); |
532 | 543 | }); |
0 commit comments