Skip to content

Commit

Permalink
fix: init Cryption with componentName (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Jul 12, 2023
1 parent 2016e73 commit e178a88
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
10 changes: 6 additions & 4 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
17 changes: 12 additions & 5 deletions src/client/Cryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DecryptRequestPB.AsObject>;
export type DecryptRequest = RequestWithMeta<{
cipherText: Uint8Array | string;
}>;

export type DecryptResponse = {
plainText: Uint8Array;
Expand All @@ -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<EncryptResponse.AsObject> {
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);
Expand All @@ -53,7 +60,7 @@ export default class Cryption extends API {

async decrypt(request: DecryptRequest): Promise<DecryptResponse> {
const req = new DecryptRequestPB();
req.setComponentName(request.componentName);
req.setComponentName(this.options.componentName);
req.setCipherText(request.cipherText);

return new Promise((resolve, reject) => {
Expand Down
33 changes: 22 additions & 11 deletions test/unit/client/Cryption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ describe.skip('Cryption.test.ts', () => {
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 😄 你好',
});
// {
Expand All @@ -20,20 +19,32 @@ 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: <Buffer 68 65 6c 6c 6f 20 6c 61 79 6f 74 74 6f 20 f0 9f 98 84 20 e4 bd a0 e5 a5 bd>,
// keyId: 'xxx-665f-486a-9529-xxx',
// 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());
});
});

0 comments on commit e178a88

Please sign in to comment.