From 848da2ad12721899b7eca0c51f1b645491d090e5 Mon Sep 17 00:00:00 2001 From: Rushil Date: Thu, 27 Jan 2022 17:05:09 +0530 Subject: [PATCH 1/2] Updated Readme with key alias extension use cases --- README.md | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/README.md b/README.md index b45cc23..1216cb6 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,98 @@ async function keyProtectSdkExample() { keyProtectSdkExample(); ``` +### Example of wrap/unwrap/delete key using key alias-extensions in SDK + +```js +const KeyProtectV2 = require('@ibm-cloud/ibm-key-protect/ibm-key-protect-api/v2'); +const { IamAuthenticator } = require('@ibm-cloud/ibm-key-protect/auth'); + +// env vars, using external configuration in this example +const envConfigs = { + apiKey: process.env.IBMCLOUD_API_KEY, + iamAuthUrl: process.env.IAM_AUTH_URL, + serviceUrl: process.env.KP_SERVICE_URL, + bluemixInstance: process.env.KP_INSTANCE_ID, + prefer:"", + id:"", + body:{}, + alias: "" +}; + +async function keyProtectSdkExample() { + let response; + + // Create an IAM authenticator. + const authenticator = new IamAuthenticator({ + apikey: envConfigs.apiKey, + url: envConfigs.iamAuthUrl, + }); + + // Construct the service client. + const keyProtectClient = new KeyProtectV2({ + authenticator, + serviceUrl: envConfigs.serviceUrl, + }); + + // Create a key + const body = { + metadata: { + collectionType: 'application/vnd.ibm.kms.key+json', + collectionTotal: 1, + }, + resources: [ + { + type: 'application/vnd.ibm.kms.key+json', + name: 'nodejsKey', + extractable: false, + }, + ], + }; + const createParams = Object.assign({}, envConfigs); + createParams.body = body; + response = await keyProtectClient.createKey(createParams); + const keyId = response.result.resources[0].id; + console.log('Key created, id is: ' + keyId); + + //Create Key alias + const keyAliasParam = Object.assign({}, envConfigs); + keyAliasParam.id = keyId + keyAliasParam.alias = "alias1" + keyAliasParam.body = body + response = await keyProtectClient.createKeyAlias(keyAliasParam); + console.log('Key alias created, id is: ' + response.status); + + // Wrap and unwrap key + const samplePlaintext = 'dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmcK'; // base64 encoded plaintext + + const wrapKeyParams = Object.assign({}, envConfigs); + wrapKeyParams.id = keyAliasParam.alias; // use key alias instead of key id to wrap a key + wrapKeyParams.keyActionWrapBody = { + plaintext: samplePlaintext, + }; + response = await keyProtectClient.wrapKey(wrapKeyParams); + console.log('Wrap key response status: ' + response.status); + const ciphertextResult = response.result.ciphertext; + + const unwrapKeyParams = Object.assign({}, envConfigs); + unwrapKeyParams.id = keyAliasParam.alias; // use key alias to unwrap a key + unwrapKeyParams.keyActionUnwrapBody = { + ciphertext: ciphertextResult, // from wrap key response + }; + response = await keyProtectClient.unwrapKey(unwrapKeyParams); + console.log('Key plain text is: ' + response.result.plaintext); //should be the same as 'samplePlaintext' above + + // Delete key + const deleteKeyParams = Object.assign({}, envConfigs); + deleteKeyParams.id = keyAliasParam.alias; // use key alias to delete a key + deleteKeyParams.prefer = 'return=representation'; + response = await keyProtectClient.deleteKey(deleteKeyParams); + console.log('Delete key response status: ' + response.status); +} + +keyProtectSdkExample(); +``` + For more information and IBM Cloud SDK usage examples for Node.js, see the [IBM Cloud SDK Common documentation](https://github.com/IBM/ibm-cloud-sdk-common/blob/master/README.md) From bf1d8f606bb8e888398154589b2304d14ec0650d Mon Sep 17 00:00:00 2001 From: Rushil Date: Mon, 31 Jan 2022 11:03:58 +0530 Subject: [PATCH 2/2] Added key alias extension for integration test case --- README.md | 92 ------------------------- test/integration/key-protect.v2.test.js | 50 ++++++++++++++ 2 files changed, 50 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index 1216cb6..b45cc23 100644 --- a/README.md +++ b/README.md @@ -163,98 +163,6 @@ async function keyProtectSdkExample() { keyProtectSdkExample(); ``` -### Example of wrap/unwrap/delete key using key alias-extensions in SDK - -```js -const KeyProtectV2 = require('@ibm-cloud/ibm-key-protect/ibm-key-protect-api/v2'); -const { IamAuthenticator } = require('@ibm-cloud/ibm-key-protect/auth'); - -// env vars, using external configuration in this example -const envConfigs = { - apiKey: process.env.IBMCLOUD_API_KEY, - iamAuthUrl: process.env.IAM_AUTH_URL, - serviceUrl: process.env.KP_SERVICE_URL, - bluemixInstance: process.env.KP_INSTANCE_ID, - prefer:"", - id:"", - body:{}, - alias: "" -}; - -async function keyProtectSdkExample() { - let response; - - // Create an IAM authenticator. - const authenticator = new IamAuthenticator({ - apikey: envConfigs.apiKey, - url: envConfigs.iamAuthUrl, - }); - - // Construct the service client. - const keyProtectClient = new KeyProtectV2({ - authenticator, - serviceUrl: envConfigs.serviceUrl, - }); - - // Create a key - const body = { - metadata: { - collectionType: 'application/vnd.ibm.kms.key+json', - collectionTotal: 1, - }, - resources: [ - { - type: 'application/vnd.ibm.kms.key+json', - name: 'nodejsKey', - extractable: false, - }, - ], - }; - const createParams = Object.assign({}, envConfigs); - createParams.body = body; - response = await keyProtectClient.createKey(createParams); - const keyId = response.result.resources[0].id; - console.log('Key created, id is: ' + keyId); - - //Create Key alias - const keyAliasParam = Object.assign({}, envConfigs); - keyAliasParam.id = keyId - keyAliasParam.alias = "alias1" - keyAliasParam.body = body - response = await keyProtectClient.createKeyAlias(keyAliasParam); - console.log('Key alias created, id is: ' + response.status); - - // Wrap and unwrap key - const samplePlaintext = 'dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmcK'; // base64 encoded plaintext - - const wrapKeyParams = Object.assign({}, envConfigs); - wrapKeyParams.id = keyAliasParam.alias; // use key alias instead of key id to wrap a key - wrapKeyParams.keyActionWrapBody = { - plaintext: samplePlaintext, - }; - response = await keyProtectClient.wrapKey(wrapKeyParams); - console.log('Wrap key response status: ' + response.status); - const ciphertextResult = response.result.ciphertext; - - const unwrapKeyParams = Object.assign({}, envConfigs); - unwrapKeyParams.id = keyAliasParam.alias; // use key alias to unwrap a key - unwrapKeyParams.keyActionUnwrapBody = { - ciphertext: ciphertextResult, // from wrap key response - }; - response = await keyProtectClient.unwrapKey(unwrapKeyParams); - console.log('Key plain text is: ' + response.result.plaintext); //should be the same as 'samplePlaintext' above - - // Delete key - const deleteKeyParams = Object.assign({}, envConfigs); - deleteKeyParams.id = keyAliasParam.alias; // use key alias to delete a key - deleteKeyParams.prefer = 'return=representation'; - response = await keyProtectClient.deleteKey(deleteKeyParams); - console.log('Delete key response status: ' + response.status); -} - -keyProtectSdkExample(); -``` - For more information and IBM Cloud SDK usage examples for Node.js, see the [IBM Cloud SDK Common documentation](https://github.com/IBM/ibm-cloud-sdk-common/blob/master/README.md) diff --git a/test/integration/key-protect.v2.test.js b/test/integration/key-protect.v2.test.js index a85087b..0867a40 100644 --- a/test/integration/key-protect.v2.test.js +++ b/test/integration/key-protect.v2.test.js @@ -763,4 +763,54 @@ describe('key protect v2 integration', () => { done(); }); }); + describe('key alias extensions', () => { + it('checkKeyaliasExtension', async done => { + let response; + const samplePlaintext = 'dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmcK'; + try { + // create a key alias + const keyAlias = 'nodejsAlias'; + const createKeyAliasParams = Object.assign({}, options); + createKeyAliasParams.id = keyId; + createKeyAliasParams.alias = keyAlias; + response = await keyProtectClient.createKeyAlias(createKeyAliasParams); + expect(response).toBeDefined(); + expect(response.status).toEqual(201); + + // wrap using key alias + const wrapKeyParams = Object.assign({}, options); + wrapKeyParams.id = createKeyAliasParams.alias; + wrapKeyParams.keyActionWrapBody = { + plaintext: samplePlaintext, + }; + response = await keyProtectClient.wrapKey(wrapKeyParams); + const ciphertextResult = response.result.ciphertext; + expect(response).toBeDefined(); + expect(response.status).toEqual(200); + + // un-wrap using key alias + const unwrapKeyParams = Object.assign({}, options); + unwrapKeyParams.id = createKeyAliasParams.alias; + unwrapKeyParams.keyActionUnwrapBody = { + ciphertext: ciphertextResult, + }; + response = await keyProtectClient.unwrapKey(unwrapKeyParams); + const plaintextResult = response.result.plaintext; + expect(response).toBeDefined(); + expect(plaintextResult).toEqual(samplePlaintext); + expect(response.status).toEqual(200); + + // delete a key using key alias + const deleteKeyParams = Object.assign({}, options); + deleteKeyParams.id = createKeyAliasParams.alias; + deleteKeyParams.prefer = 'return=representation'; + response = await keyProtectClient.deleteKey(deleteKeyParams); + expect(response).toBeDefined(); + expect(response.status).toEqual(200); + } catch (err) { + done(err); + } + done(); + }); + }); });