From 3dfcea4198eeda91bcde0d2c55ca9cc0f13008a0 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 12 Jul 2023 00:10:33 +0800 Subject: [PATCH 1/2] fix: init Cryption with componentName --- layotto | 2 +- src/client/Client.ts | 10 +++++---- src/client/Cryption.ts | 17 ++++++++++----- test/unit/client/Cryption.test.ts | 35 ++++++++++++++++++++----------- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/layotto b/layotto index c8c8e51..6602df6 160000 --- a/layotto +++ b/layotto @@ -1 +1 @@ -Subproject commit c8c8e5176971ac16dd64cfcf3a4898fc918d4870 +Subproject commit 6602df6b80af1abb3fb50d99a72777853917936a diff --git a/src/client/Client.ts b/src/client/Client.ts index ca47640..5afb6bc 100644 --- a/src/client/Client.ts +++ b/src/client/Client.ts @@ -27,13 +27,13 @@ import PubSub from './PubSub'; import File from './File'; import Binding from './Binding'; import Oss from './Oss'; -import Cryption from './Cryption'; +import Cryption, { CryptionOptions } from './Cryption'; const debug = debuglog('layotto:client:main'); export interface ClientOptions { ossEnable?: boolean; - cryptionEnable?: boolean; + cryption?: CryptionOptions; } export default class Client { @@ -42,6 +42,7 @@ export default class Client { private readonly _runtime: RuntimeClient; private readonly _ossClient: ObjectStorageServiceClient; private readonly _cryptionClient: CryptionServiceClient; + private readonly _cryptionOptions: CryptionOptions; private _hello: Hello; private _state: State; private _invoker: Invoker; @@ -65,7 +66,8 @@ export default class Client { if (options?.ossEnable) { this._ossClient = new ObjectStorageServiceClient(address, clientCredentials); } - if (options?.cryptionEnable) { + if (options?.cryption?.componentName) { + this._cryptionOptions = options.cryption; this._cryptionClient = new CryptionServiceClient(address, clientCredentials); } } @@ -130,7 +132,7 @@ export default class Client { if (!this._cryptionClient) { throw new Error('client not enable cryption'); } - this._cryption = new Cryption(this._cryptionClient); + this._cryption = new Cryption(this._cryptionClient, this._cryptionOptions); } return this._cryption; } diff --git a/src/client/Cryption.ts b/src/client/Cryption.ts index 6f70e2c..f9c3d7d 100644 --- a/src/client/Cryption.ts +++ b/src/client/Cryption.ts @@ -9,12 +9,13 @@ import { API } from './API'; import { RequestWithMeta } from '../types/common'; export type EncryptRequest = RequestWithMeta<{ - componentName: string; plainText: Uint8Array | string; keyId?: string; }>; -export type DecryptRequest = RequestWithMeta; +export type DecryptRequest = RequestWithMeta<{ + cipherText: Uint8Array | string; +}>; export type DecryptResponse = { plainText: Uint8Array; @@ -23,17 +24,23 @@ export type DecryptResponse = { requestId: string, }; +export type CryptionOptions = { + componentName: string; +}; + export default class Cryption extends API { private readonly cryptionClient: CryptionServiceClient; + private readonly options: CryptionOptions; - constructor(cryptionClient: CryptionServiceClient) { + constructor(cryptionClient: CryptionServiceClient, options: CryptionOptions) { super(); + this.options = options; this.cryptionClient = cryptionClient; } async encrypt(request: EncryptRequest): Promise { const req = new EncryptRequestPB(); - req.setComponentName(request.componentName); + req.setComponentName(this.options.componentName); let plainText = request.plainText; if (typeof plainText === 'string') { plainText = Buffer.from(plainText); @@ -53,7 +60,7 @@ export default class Cryption extends API { async decrypt(request: DecryptRequest): Promise { const req = new DecryptRequestPB(); - req.setComponentName(request.componentName); + req.setComponentName(this.options.componentName); req.setCipherText(request.cipherText); return new Promise((resolve, reject) => { diff --git a/test/unit/client/Cryption.test.ts b/test/unit/client/Cryption.test.ts index a29466f..112f931 100644 --- a/test/unit/client/Cryption.test.ts +++ b/test/unit/client/Cryption.test.ts @@ -1,17 +1,16 @@ import { strict as assert } from 'node:assert'; import { Client } from '../../../src'; -describe.skip('Cryption.test.ts', () => { +describe('Cryption.test.ts', () => { let client: Client; const componentName = 'cryption_demo'; beforeAll(() => { - client = new Client('34904', '127.0.0.1', { cryptionEnable: true }); + client = new Client('34904', '127.0.0.1', { cryption: { componentName } }); }); it('should encrypt and decrypt success', async () => { - const encryptResult = await client.cryption.encrypt({ - componentName, + const encryptResult1 = await client.cryption.encrypt({ plainText: 'hello layotto 😄 你好', }); // { @@ -20,12 +19,18 @@ describe.skip('Cryption.test.ts', () => { // keyVersionId: 'xxx-6950-407a-91d9-xxx', // requestId: '63969176-4659-4059-8593-cd2f7ea2282b' // } - // console.log(encryptResult); - assert(encryptResult.cipherText); - assert.equal(typeof encryptResult.cipherText, 'string'); - const decryptResult = await client.cryption.decrypt({ - componentName, - cipherText: encryptResult.cipherText, + console.log(encryptResult1); + assert(encryptResult1.cipherText); + assert.equal(typeof encryptResult1.cipherText, 'string'); + const encryptResult2 = await client.cryption.encrypt({ + plainText: 'hello layotto 😄 你好', + }); + console.log(encryptResult2); + assert.notEqual(encryptResult2.cipherText, encryptResult1.cipherText); + assert.equal(encryptResult2.keyId, encryptResult1.keyId); + + const decryptResult1 = await client.cryption.decrypt({ + cipherText: encryptResult1.cipherText, }); // { // plainText: , @@ -33,7 +38,13 @@ describe.skip('Cryption.test.ts', () => { // keyVersionId: 'xxx-6950-407a-91d9-xxx', // requestId: 'ecf966e9-027e-4f1f-993f-1685f1bcd7e7' // } - assert(Buffer.isBuffer(decryptResult.plainText)); - assert.equal(decryptResult.plainText.toString(), 'hello layotto 😄 你好'); + console.log(decryptResult1); + assert(Buffer.isBuffer(decryptResult1.plainText)); + assert.equal(decryptResult1.plainText.toString(), 'hello layotto 😄 你好'); + + const decryptResult2 = await client.cryption.decrypt({ + cipherText: encryptResult2.cipherText, + }); + assert.equal(decryptResult2.plainText.toString(), decryptResult1.plainText.toString()); }); }); From 171aad9f5a6858a07be60a6d057124ea656fcb56 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 12 Jul 2023 09:22:28 +0800 Subject: [PATCH 2/2] f --- test/unit/client/Cryption.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/client/Cryption.test.ts b/test/unit/client/Cryption.test.ts index 112f931..4e5d4a0 100644 --- a/test/unit/client/Cryption.test.ts +++ b/test/unit/client/Cryption.test.ts @@ -1,7 +1,7 @@ import { strict as assert } from 'node:assert'; import { Client } from '../../../src'; -describe('Cryption.test.ts', () => { +describe.skip('Cryption.test.ts', () => { let client: Client; const componentName = 'cryption_demo';