Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KeyVault-Keys] The CryptographyClient indeed works with a KeyVaultKey #9647

Merged
merged 16 commits into from
Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions sdk/keyvault/keyvault-keys/review/keyvault-keys.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ export interface CreateRsaKeyOptions extends CreateKeyOptions {

// @public
export class CryptographyClient {
constructor(key: string | KeyVaultKey, // keyUrl or KeyVaultKey
credential: TokenCredential, pipelineOptions?: CryptographyClientOptions);
constructor(key: string | KeyVaultKey, credential: TokenCredential, pipelineOptions?: CryptographyClientOptions);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks weird, but it's just an alignment after the comment was moved from this parameter to the proper place in the constructor documentation.

decrypt(algorithm: EncryptionAlgorithm, ciphertext: Uint8Array, options?: DecryptOptions): Promise<DecryptResult>;
encrypt(algorithm: EncryptionAlgorithm, plaintext: Uint8Array, options?: EncryptOptions): Promise<EncryptResult>;
sign(algorithm: SignatureAlgorithm, digest: Uint8Array, options?: SignOptions): Promise<SignResult>;
Expand Down
4 changes: 2 additions & 2 deletions sdk/keyvault/keyvault-keys/samples/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ urlFragment: keyvault-keys-javascript

These sample programs show how to use the JavaScript client libraries for Azure Key Vault Keys in some common scenarios.

| **File Name** | **Description** |
| **FileName** | **Description** |
| ------------------------------- | ---------------------------------------------------------------- |
| [cryptography.js][cryptography] | uses a key to sign/verify, encrypt/decrypt, and wrap/unwrap data |
| [helloWorld.js][helloworld] | creates, reads, lists, and deletes keys |
| [purgeAllKeys.js][purgeAllKeys] | purges all the keys of a Key Vault (useful for repeated tests) |
| [purgeAllKeys.js][purgeAllKeys] | purges all the keys of a Key Vault (useful for repeated tests) |

## Prerequisites

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ async function main() {
// Connection to Azure Key Vault Cryptography functionality
let myWorkKey = await client.createKey(keyName, "RSA");

const cryptoClient = new CryptographyClient(myWorkKey.id, credential);
const cryptoClient = new CryptographyClient(
myWorkKey.id // Or just `myWorkKey`. You can use either the key or the key Id to create a CryptographyClient.
sadasant marked this conversation as resolved.
Show resolved Hide resolved
, credential);

// Sign and Verify
const signatureValue = "MySignature";
Expand Down
4 changes: 2 additions & 2 deletions sdk/keyvault/keyvault-keys/samples/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ urlFragment: keyvault-keys-typescript
These sample programs show how to use the TypeScript client libraries for Azure Key Vault Keys in some common scenarios.

| **File Name** | **Description** |
| ------------------------------- | ---------------------------------------------------------------- |
| ------------------------------- | ------------------------------------------------------------- |
| [cryptography.ts][cryptography] | uses a key to sign/verify, encrypt/decrypt, and wrap/unwrap data |
| [helloWorld.ts][helloworld] | creates, reads, lists, and deletes keys |
| [purgeAllKeys.ts][purgeAllKeys] | purges all the keys of a Key Vault (useful for repeated tests) |
| [purgeAllKeys.ts][purgeAllKeys] | purges all the keys of a Key Vault (useful for repeated tests) |

## Prerequisites

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export async function main(): Promise<void> {
// Connection to Azure Key Vault Cryptography functionality
const myWorkKey = await client.createKey(keyName, "RSA");

const cryptoClient = new CryptographyClient(myWorkKey.id!, credential);
const cryptoClient = new CryptographyClient(
myWorkKey.id! // Or just `myWorkKey`. You can use either the key or the key Id to create a CryptographyClient.
sadasant marked this conversation as resolved.
Show resolved Hide resolved
, credential);
ramya-rao-a marked this conversation as resolved.
Show resolved Hide resolved

// Sign and Verify
const signatureValue = "MySignature";
Expand Down
4 changes: 2 additions & 2 deletions sdk/keyvault/keyvault-keys/src/cryptographyClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,14 +665,14 @@ export class CryptographyClient {
* // or
* let client = new CryptographyClient(keyVaultKey, credentials);
* ```
* @param key The key to use during cryptography tasks.
* @param key The key to use during cryptography tasks. It can be either a Key Vault Key identifier (or URL) or a full KeyVaultKey.
sadasant marked this conversation as resolved.
Show resolved Hide resolved
* @param {TokenCredential} credential An object that implements the `TokenCredential` interface used to authenticate requests to the service. Use the @azure/identity package to create a credential that suits your needs.
* @param {PipelineOptions} [pipelineOptions={}] Optional. Pipeline options used to configure Key Vault API requests.
* Omit this parameter to use the default pipeline configuration.
* @memberof CryptographyClient
*/
constructor(
key: string | KeyVaultKey, // keyUrl or KeyVaultKey
key: string | KeyVaultKey,
credential: TokenCredential,
pipelineOptions: CryptographyClientOptions = {}
) {
Expand Down
15 changes: 14 additions & 1 deletion sdk/keyvault/keyvault-keys/test/public/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as assert from "assert";
import { createHash, publicEncrypt } from "crypto";
import * as constants from "constants";
import { isRecordMode, Recorder } from "@azure/test-utils-recorder";
import { isRecordMode, Recorder, env } from "@azure/test-utils-recorder";
import { ClientSecretCredential } from "@azure/identity";
import { isNode } from "@azure/core-http";

Expand All @@ -15,6 +15,7 @@ import TestClient from "../utils/testClient";
import { stringToUint8Array, uint8ArrayToString } from "../utils/crypto";

describe("CryptographyClient (all decrypts happen remotely)", () => {
const keyPrefix = `crypto${env.KEY_NAME || "KeyName"}`;
let client: KeyClient;
let testClient: TestClient;
let cryptoClient: CryptographyClient;
Expand Down Expand Up @@ -83,6 +84,18 @@ describe("CryptographyClient (all decrypts happen remotely)", () => {
const decryptedText = uint8ArrayToString(decryptResult.result);
assert.equal(text, decryptedText);
});

it("the CryptographyClient can be created from a full KeyVaultKey object", async function() {
const keyName = testClient.formatName(`${keyPrefix}-${this!.test!.title}-${keySuffix}`);
const keyVaultKey = await client.createKey(keyName, "RSA");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other tests in this block use the key created in the beforeEach() block. Any reason why this test deviates from that pattern?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to show that a new key with nothing special can be used to create this client.

const cryptoClientFromKey = new CryptographyClient(keyVaultKey, credential);

const text = this.test!.title;
const encryptResult = await cryptoClientFromKey.encrypt("RSA1_5", stringToUint8Array(text));
const decryptResult = await cryptoClientFromKey.decrypt("RSA1_5", encryptResult.result);
const decryptedText = uint8ArrayToString(decryptResult.result);
assert.equal(text, decryptedText);
});
}

// Local encryption is only supported in NodeJS.
Expand Down