Skip to content

Commit

Permalink
feat: support defaultRequestMeta on Cryption + OSS client (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Jul 13, 2023
1 parent 29a5468 commit 0cec838
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
15 changes: 11 additions & 4 deletions src/client/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ import { Metadata } from '@grpc/grpc-js';
import { KV, RequestWithMeta, Map } from '../types/common';

export class API {
createMetadata(request: RequestWithMeta<{}>): Metadata {
createMetadata(request: RequestWithMeta<{}>, defaultRequestMeta?: Record<string, string>): Metadata {
const metadata = new Metadata();
if (!request.requestMeta) return metadata;
for (const key of Object.keys(request.requestMeta)) {
metadata.add(key, request.requestMeta[key]);
if (defaultRequestMeta) {
for (const key in defaultRequestMeta) {
metadata.add(key, defaultRequestMeta[key]);
}
}

if (request.requestMeta) {
for (const key of Object.keys(request.requestMeta)) {
metadata.add(key, request.requestMeta[key]);
}
}
return metadata;
}
Expand Down
9 changes: 6 additions & 3 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ import Configuration from './Configuration';
import PubSub from './PubSub';
import File from './File';
import Binding from './Binding';
import Oss from './Oss';
import Oss, { OssOptions } from './Oss';
import Cryption, { CryptionOptions } from './Cryption';

const debug = debuglog('layotto:client:main');

export interface ClientOptions {
ossEnable?: boolean;
oss?: OssOptions;
cryption?: CryptionOptions;
}

Expand All @@ -41,6 +42,7 @@ export default class Client {
readonly port: string;
private readonly _runtime: RuntimeClient;
private readonly _ossClient: ObjectStorageServiceClient;
private readonly _ossOptions: OssOptions;
private readonly _cryptionClient: CryptionServiceClient;
private readonly _cryptionOptions: CryptionOptions;
private _hello: Hello;
Expand All @@ -63,7 +65,8 @@ export default class Client {
const address = `${this.host}:${this.port}`;
this._runtime = new RuntimeClient(address, clientCredentials);
debug('Start connection to %o', address);
if (options?.ossEnable) {
if (options?.ossEnable || options?.oss) {
this._ossOptions = options?.oss || {};
this._ossClient = new ObjectStorageServiceClient(address, clientCredentials);
}
if (options?.cryption?.componentName) {
Expand Down Expand Up @@ -122,7 +125,7 @@ export default class Client {
if (!this._ossClient) {
throw new Error('client not enable oss');
}
this._oss = new Oss(this._ossClient);
this._oss = new Oss(this._ossClient, this._ossOptions);
}
return this._oss;
}
Expand Down
8 changes: 6 additions & 2 deletions src/client/Cryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export type DecryptResponse = {

export type CryptionOptions = {
componentName: string;
// set default metadata on every request
defaultRequestMeta?: Record<string, string>;
};

export default class Cryption extends API {
Expand All @@ -50,8 +52,9 @@ export default class Cryption extends API {
req.setKeyId(request.keyId);
}

const metadata = this.createMetadata(request, this.options.defaultRequestMeta);
return new Promise((resolve, reject) => {
this.cryptionClient.encrypt(req, this.createMetadata(request), (err, res: EncryptResponse) => {
this.cryptionClient.encrypt(req, metadata, (err, res: EncryptResponse) => {
if (err) return reject(err);
resolve(res.toObject());
});
Expand All @@ -63,8 +66,9 @@ export default class Cryption extends API {
req.setComponentName(this.options.componentName);
req.setCipherText(request.cipherText);

const metadata = this.createMetadata(request, this.options.defaultRequestMeta);
return new Promise((resolve, reject) => {
this.cryptionClient.decrypt(req, this.createMetadata(request), (err, res: DecryptResponsePB) => {
this.cryptionClient.decrypt(req, metadata, (err, res: DecryptResponsePB) => {
if (err) return reject(err);
const plainText = Buffer.from(res.getPlainText_asB64(), 'base64');
resolve({
Expand Down
32 changes: 24 additions & 8 deletions src/client/Oss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ import {
import { ObjectStorageServiceClient } from '../../proto/extension/v1/s3/oss_grpc_pb';
import { API } from './API';

export type OssOptions = {
// set default metadata on every request
defaultRequestMeta?: Record<string, string>;
};

export default class Oss extends API {
private readonly ossClient: ObjectStorageServiceClient;
private readonly options: OssOptions;

constructor(ossClient: ObjectStorageServiceClient) {
constructor(ossClient: ObjectStorageServiceClient, options: OssOptions) {
super();
this.ossClient = ossClient;
this.options = options;
}

private async* putObjectIterator(request: PutObjectRequest): AsyncGenerator<PutObjectInput> {
Expand Down Expand Up @@ -82,7 +89,8 @@ export default class Oss extends API {
resolve = res;
reject = rej;
});
const writeStream = this.ossClient.putObject(this.createMetadata(request), (err, res) => {
const metadata = this.createMetadata(request, this.options.defaultRequestMeta);
const writeStream = this.ossClient.putObject(metadata, (err, res) => {
if (err) {
return reject(err);
}
Expand Down Expand Up @@ -175,7 +183,8 @@ export default class Oss extends API {
if (request.signedUrl) {
req.setSignedUrl(request.signedUrl);
}
const callStream = this.ossClient.getObject(req, this.createMetadata(request));
const metadata = this.createMetadata(request, this.options.defaultRequestMeta);
const callStream = this.ossClient.getObject(req, metadata);
const getObjectIterator = this.getObjectIterator(callStream);
const firstChunk = (await getObjectIterator.next()).value;
const getObjectBufIterator = this.getObjectBufferIterator(firstChunk, getObjectIterator);
Expand Down Expand Up @@ -204,8 +213,9 @@ export default class Oss extends API {
if (request.metadataDirective) {
req.setMetadataDirective(request.metadataDirective);
}
const metadata = this.createMetadata(request, this.options.defaultRequestMeta);
return new Promise((resolve, reject) => {
this.ossClient.copyObject(req, this.createMetadata(request), (err, response) => {
this.ossClient.copyObject(req, metadata, (err, response) => {
if (err) {
return reject(err);
}
Expand Down Expand Up @@ -258,8 +268,9 @@ export default class Oss extends API {
if (request.withDetails) {
req.setWithDetails(request.withDetails);
}
const metadata = this.createMetadata(request, this.options.defaultRequestMeta);
return new Promise((resolve, reject) => {
this.ossClient.headObject(req, this.createMetadata(request), (err, response) => {
this.ossClient.headObject(req, metadata, (err, response) => {
if (err) {
return reject(err);
}
Expand All @@ -280,8 +291,9 @@ export default class Oss extends API {
req.setVersionId(request.versionId);
}

const metadata = this.createMetadata(request, this.options.defaultRequestMeta);
return new Promise((resolve, reject) => {
this.ossClient.deleteObject(req, this.createMetadata(request), (err, response) => {
this.ossClient.deleteObject(req, metadata, (err, response) => {
if (err) {
return reject(err);
}
Expand Down Expand Up @@ -315,8 +327,10 @@ export default class Oss extends API {
if (request.requestPayer) {
req.setRequestPayer(request.requestPayer);
}

const metadata = this.createMetadata(request, this.options.defaultRequestMeta);
return new Promise((resolve, reject) => {
this.ossClient.listObjects(req, this.createMetadata(request), (err, response) => {
this.ossClient.listObjects(req, metadata, (err, response) => {
if (err) {
return reject(err);
}
Expand All @@ -332,8 +346,10 @@ export default class Oss extends API {
req.setKey(request.key);
req.setMethod(request.method);
req.setExpiredInSec(request.expiredInSec);

const metadata = this.createMetadata(request, this.options.defaultRequestMeta);
return new Promise((resolve, reject) => {
this.ossClient.signURL(req, this.createMetadata(request), (err, response) => {
this.ossClient.signURL(req, metadata, (err, response) => {
if (err) {
return reject(err);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/client/Cryption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe.skip('Cryption.test.ts', () => {
const componentName = 'cryption_demo';

beforeAll(() => {
client = new Client('34904', '127.0.0.1', { cryption: { componentName } });
client = new Client('34904', '127.0.0.1', { cryption: { componentName, defaultRequestMeta: {} } });
});

it('should encrypt and decrypt success', async () => {
Expand Down

0 comments on commit 0cec838

Please sign in to comment.