Skip to content

Commit

Permalink
feat: support Cryption API (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 11, 2023
1 parent 21d5ec2 commit bc0d340
Show file tree
Hide file tree
Showing 39 changed files with 217 additions and 90 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "eslint-config-egg/typescript"
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
2 changes: 1 addition & 1 deletion demo/configuration/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import { Client, utils } from 'layotto';

const client = new Client();
Expand Down
2 changes: 1 addition & 1 deletion demo/helloworld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import { Client } from 'layotto';

const client = new Client();
Expand Down
2 changes: 1 addition & 1 deletion demo/pubsub/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import { Client } from 'layotto';

const client = new Client();
Expand Down
2 changes: 1 addition & 1 deletion demo/rpc/http/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const http = require('http');
const http = require('node:http');

const hostname = '127.0.0.1';
const port = 8889;
Expand Down
2 changes: 1 addition & 1 deletion demo/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import { Client } from 'layotto';

const client = new Client();
Expand Down
2 changes: 1 addition & 1 deletion layotto
Submodule layotto updated 66 files
+7 −5 .github/dead_link_check_config.json
+1 −1 README.md
+7 −0 cmd/layotto/main.go
+2 −0 cmd/layotto_multiple_api/main.go
+2 −0 cmd/layotto_without_xds/main.go
+8 −28 components/configstores/apollo/default_logger.go
+13 −4 components/configstores/apollo/default_logger_test.go
+65 −0 components/configstores/nacos/client.go
+105 −0 components/configstores/nacos/config.go
+39 −0 components/configstores/nacos/config_test.go
+508 −0 components/configstores/nacos/configstore.go
+731 −0 components/configstores/nacos/configstore_test.go
+25 −0 components/configstores/nacos/const.go
+70 −0 components/configstores/nacos/default_logger.go
+49 −0 components/configstores/nacos/default_logger_test.go
+34 −0 components/configstores/nacos/errors.go
+30 −0 components/configstores/nacos/errors_test.go
+1 −0 components/configstores/types.go
+93 −0 components/cryption/aliyun/kms.go
+23 −0 components/cryption/aliyun/meta.go
+4 −0 components/cryption/struct_generated.go
+31 −16 components/go.mod
+90 −23 components/go.sum
+84 −67 components/oss/oss.go
+148 −0 components/pkg/mock/nacos_mock.go
+83 −0 configs/config_cryption.json
+106 −0 configs/config_nacos.json
+6 −0 configs/config_standalone.json
+84 −0 demo/cryption/client.go
+104 −0 demo/file/local/client.go
+3 −1 demo/go.mod
+4 −1,616 demo/go.sum
+7 −0 demo/hello/common/README.md
+52 −0 demo/hello/common/client.go
+51 −0 docker/layotto-nacos/config_nacos.json
+44 −0 docker/layotto-nacos/docker-compose.yml
+5 −0 docker/layotto-nacos/mysql.env
+13 −0 docker/layotto-nacos/nacos-standlone-mysql.env
+2 −0 docs/_sidebar.md
+1 −1 docs/en/component_specs/configuration/apollo.md
+36 −0 docs/en/component_specs/configuration/nacos.md
+349 −1 docs/en/sdk_reference/go/start.md
+2 −1 docs/en/start/configuration/overview.md
+1 −1 docs/en/start/configuration/start-apollo.md
+74 −0 docs/en/start/configuration/start-nacos.md
+ docs/img/configuration/nacos/img.png
+ docs/img/configuration/nacos/layotto-nacos-configstore-component.png
+2 −0 docs/zh/_sidebar.md
+1 −1 docs/zh/component_specs/configuration/apollo.md
+36 −0 docs/zh/component_specs/configuration/nacos.md
+350 −1 docs/zh/sdk_reference/go/start.md
+2 −1 docs/zh/start/configuration/overview.md
+1 −1 docs/zh/start/configuration/start-apollo.md
+75 −0 docs/zh/start/configuration/start-nacos.md
+1 −2 docs/zh/start/file/minio.md
+31 −15 go.mod
+85 −28 go.sum
+20 −9 make/golang.mk
+1 −0 pkg/runtime/runtime.go
+40 −19 spec/proto/extension/v1/cryption/cryption.pb.go
+5 −0 spec/proto/extension/v1/cryption/cryption.proto
+0 −1 spec/proto/extension/v1/cryption/cryption_grpc.pb.go
+1,598 −1,226 spec/proto/extension/v1/s3/oss.pb.go
+36 −0 spec/proto/extension/v1/s3/oss.proto
+1 −1 spec/proto/runtime/v1/appcallback.pb.go
+1 −1 spec/proto/runtime/v1/lifecycle.pb.go
6 changes: 0 additions & 6 deletions src/client/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@
* limitations under the License.
*/
import { Metadata } from '@grpc/grpc-js';
import { RuntimeClient } from '../../proto/runtime/v1/runtime_grpc_pb';
import { KV, RequestWithMeta, Map } from '../types/common';

export class API {
readonly runtime: RuntimeClient;
constructor(runtime: RuntimeClient) {
this.runtime = runtime;
}

createMetadata(request: RequestWithMeta<{}>): Metadata {
const metadata = new Metadata();
if (!request.requestMeta) return metadata;
Expand Down
4 changes: 2 additions & 2 deletions src/client/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import {
InvokeBindingRequest as InvokeBindingRequestPB,
InvokeBindingResponse as InvokeBindingResponsePB,
} from '../../proto/runtime/v1/runtime_pb';
import { API } from './API';
import { RuntimeAPI } from './RuntimeAPI';
import {
InvokeBindingRequest,
InvokeBindingResponse,
} from '../types/Binding';
import { convertMapToKVString } from '../utils';

export default class Binding extends API {
export default class Binding extends RuntimeAPI {
async invoke(request: InvokeBindingRequest): Promise<InvokeBindingResponse> {
const req = new InvokeBindingRequestPB();
req.setName(request.name);
Expand Down
31 changes: 25 additions & 6 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import { debuglog } from 'node:util';
import { ChannelCredentials } from '@grpc/grpc-js';
import { RuntimeClient } from '../../proto/runtime/v1/runtime_grpc_pb';
import { ObjectStorageServiceClient } from '../../proto/extension/v1/s3/oss_grpc_pb';
import { CryptionServiceClient } from '../../proto/extension/v1/cryption/cryption_grpc_pb';
import State from './State';
import Hello from './Hello';
import Invoker from './Invoker';
Expand All @@ -24,20 +26,22 @@ import Configuration from './Configuration';
import PubSub from './PubSub';
import File from './File';
import Binding from './Binding';
import { ObjectStorageServiceClient } from '../../proto/extension/v1/s3/oss_grpc_pb';
import Oss from './Oss';
import Cryption from './Cryption';

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

export interface ClientOptions {
ossEnable?: boolean;
cryptionEnable?: boolean;
}

export default class Client {
readonly host: string;
readonly port: string;
private _runtime: RuntimeClient;
private _ossClient: ObjectStorageServiceClient;
private readonly _runtime: RuntimeClient;
private readonly _ossClient: ObjectStorageServiceClient;
private readonly _cryptionClient: CryptionServiceClient;
private _hello: Hello;
private _state: State;
private _invoker: Invoker;
Expand All @@ -48,16 +52,21 @@ export default class Client {
private _file: File;
private _binding: Binding;
private _oss: Oss;
private _cryption: Cryption;

constructor(port: string = process.env.runtime_GRPC_PORT ?? '34904',
host: string = process.env.runtime_GRPC_HOST ?? '127.0.0.1', options?: ClientOptions) {
this.host = host;
this.port = port;
const clientCredentials = ChannelCredentials.createInsecure();
this._runtime = new RuntimeClient(`${this.host}:${this.port}`, clientCredentials);
debug('Start connection to %s:%s', this.host, this.port);
const address = `${this.host}:${this.port}`;
this._runtime = new RuntimeClient(address, clientCredentials);
debug('Start connection to %o', address);
if (options?.ossEnable) {
this._ossClient = new ObjectStorageServiceClient(`${this.host}:${this.port}`, clientCredentials);
this._ossClient = new ObjectStorageServiceClient(address, clientCredentials);
}
if (options?.cryptionEnable) {
this._cryptionClient = new CryptionServiceClient(address, clientCredentials);
}
}

Expand Down Expand Up @@ -115,4 +124,14 @@ export default class Client {
}
return this._oss;
}

get cryption() {
if (!this._cryption) {
if (!this._cryptionClient) {
throw new Error('client not enable cryption');
}
this._cryption = new Cryption(this._cryptionClient);
}
return this._cryption;
}
}
4 changes: 2 additions & 2 deletions src/client/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
SubscribeConfigurationRequest as SubscribeConfigurationRequestPB,
SubscribeConfigurationResponse as SubscribeConfigurationResponsePB,
} from '../../proto/runtime/v1/runtime_pb';
import { API } from './API';
import { RuntimeAPI } from './RuntimeAPI';
import {
GetConfigurationRequest,
GetConfigurationItem,
Expand All @@ -34,7 +34,7 @@ import { convertArrayToKVString } from '../types/common';

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

export default class Configuration extends API {
export default class Configuration extends RuntimeAPI {
// GetConfiguration gets configuration from configuration store.
async get(request: GetConfigurationRequest): Promise<GetConfigurationItem[]> {
const req = new GetConfigurationRequestPB();
Expand Down
70 changes: 70 additions & 0 deletions src/client/Cryption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
EncryptRequest as EncryptRequestPB,
EncryptResponse,
DecryptRequest as DecryptRequestPB,
DecryptResponse as DecryptResponsePB,
} from '../../proto/extension/v1/cryption/cryption_pb';
import { CryptionServiceClient } from '../../proto/extension/v1/cryption/cryption_grpc_pb';
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 DecryptResponse = {
plainText: Uint8Array;
keyId: string,
keyVersionId: string,
requestId: string,
};

export default class Cryption extends API {
private readonly cryptionClient: CryptionServiceClient;

constructor(cryptionClient: CryptionServiceClient) {
super();
this.cryptionClient = cryptionClient;
}

async encrypt(request: EncryptRequest): Promise<EncryptResponse.AsObject> {
const req = new EncryptRequestPB();
req.setComponentName(request.componentName);
let plainText = request.plainText;
if (typeof plainText === 'string') {
plainText = Buffer.from(plainText);
}
req.setPlainText(plainText);
if (request.keyId) {
req.setKeyId(request.keyId);
}

return new Promise((resolve, reject) => {
this.cryptionClient.encrypt(req, this.createMetadata(request), (err, res: EncryptResponse) => {
if (err) return reject(err);
resolve(res.toObject());
});
});
}

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

return new Promise((resolve, reject) => {
this.cryptionClient.decrypt(req, this.createMetadata(request), (err, res: DecryptResponsePB) => {
if (err) return reject(err);
const plainText = Buffer.from(res.getPlainText_asB64(), 'base64');
resolve({
...res.toObject(),
plainText,
});
});
});
}
}
8 changes: 4 additions & 4 deletions src/client/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
* limitations under the License.
*/
import { debuglog } from 'node:util';
import { Transform, Readable } from 'stream';
import { pipeline as pipelinePromise } from 'stream/promises';
import { Transform, Readable } from 'node:stream';
import { pipeline as pipelinePromise } from 'node:stream/promises';
import {
GetFileRequest as GetFileRequestPB,
GetFileResponse as GetFileResponsePB,
PutFileRequest as PutFileRequestPB,
ListFileRequest as ListFileRequestPB,
DelFileRequest as DelFileRequestPB,
} from '../../proto/runtime/v1/runtime_pb';
import { API } from './API';
import { RuntimeAPI } from './RuntimeAPI';
import { GetFileRequest, ListFileResponse, PutFileRequest } from '../types/File';

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

export default class File extends API {
export default class File extends RuntimeAPI {
// Get a file stream
async get(request: GetFileRequest): Promise<Readable> {
const req = new GetFileRequestPB();
Expand Down
4 changes: 2 additions & 2 deletions src/client/Hello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import {
SayHelloRequest as SayHelloRequestPB,
SayHelloResponse as SayHelloResponsePB,
} from '../../proto/runtime/v1/runtime_pb';
import { API } from './API';
import { RuntimeAPI } from './RuntimeAPI';
import { SayHelloRequest } from '../types/Hello';

export default class Hello extends API {
export default class Hello extends RuntimeAPI {
async sayHello(request?: SayHelloRequest): Promise<string> {
const req = new SayHelloRequestPB();
if (!request) request = {};
Expand Down
4 changes: 2 additions & 2 deletions src/client/Invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import {
HTTPExtension,
InvokeResponse as InvokeResponsePB,
} from '../../proto/runtime/v1/runtime_pb';
import { API } from './API';
import { RuntimeAPI } from './RuntimeAPI';
import { InvokeServiceRequest, InvokeResponse } from '../types/Invoker';

export default class Invoker extends API {
export default class Invoker extends RuntimeAPI {
async invoke(request: InvokeServiceRequest): Promise<InvokeResponse> {
const message = new CommonInvokeRequestPB();
message.setMethod(request.method);
Expand Down
8 changes: 4 additions & 4 deletions src/client/Lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as crypto from 'crypto';
import { randomUUID } from 'node:crypto';
import {
TryLockRequest as TryLockRequestPB,
TryLockResponse as TryLockResponsePB,
UnlockRequest as UnlockRequestPB,
UnlockResponse as UnlockResponsePB,
} from '../../proto/runtime/v1/runtime_pb';
import { API } from './API';
import { RuntimeAPI } from './RuntimeAPI';
import { TryLockRequest, UnlockRequest } from '../types/Lock';

export default class Lock extends API {
export default class Lock extends RuntimeAPI {
// A non-blocking method trying to get a lock with ttl
// expire is the time before expire. The time unit is second.
async tryLock(request: TryLockRequest): Promise<boolean> {
Expand Down Expand Up @@ -55,6 +55,6 @@ export default class Lock extends API {
}

uuid() {
return crypto.randomUUID();
return randomUUID();
}
}
19 changes: 5 additions & 14 deletions src/client/Oss.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Readable, PassThrough, Writable } from 'node:stream';
import { pipeline as pipelinePromise } from 'node:stream/promises';
import {
CopyObjectRequest,
DeleteObjectRequest,
Expand Down Expand Up @@ -27,27 +29,16 @@ import {
SignURLOutput,
} from '../../proto/extension/v1/s3/oss_pb';
import { ObjectStorageServiceClient } from '../../proto/extension/v1/s3/oss_grpc_pb';
import { RequestWithMeta } from '../types/common';
import { Metadata } from '@grpc/grpc-js';
import { Readable, PassThrough, Writable } from 'stream';
import { pipeline as pipelinePromise } from 'stream/promises';
import { API } from './API';

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

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

createMetadata(request: RequestWithMeta<{}>): Metadata {
const metadata = new Metadata();
if (!request.requestMeta) return metadata;
for (const key of Object.keys(request.requestMeta)) {
metadata.add(key, request.requestMeta[key]);
}
return metadata;
}

private async* putObjectIterator(request: PutObjectRequest): AsyncGenerator<PutObjectInput> {
for await (const chunk of request.body) {
const req = new PutObjectInput();
Expand Down
4 changes: 2 additions & 2 deletions src/client/PubSub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import {
PublishEventRequest as PublishEventRequestPB,
} from '../../proto/runtime/v1/runtime_pb';
import { API } from './API';
import { RuntimeAPI } from './RuntimeAPI';
import { PublishEventRequest } from '../types/PubSub';

export default class PubSub extends API {
export default class PubSub extends RuntimeAPI {
async publish(request: PublishEventRequest): Promise<void> {
const req = new PublishEventRequestPB();
req.setPubsubName(request.pubsubName);
Expand Down
10 changes: 10 additions & 0 deletions src/client/RuntimeAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { RuntimeClient } from '../../proto/runtime/v1/runtime_grpc_pb';
import { API } from './API';

export class RuntimeAPI extends API {
readonly runtime: RuntimeClient;
constructor(runtime: RuntimeClient) {
super();
this.runtime = runtime;
}
}
Loading

0 comments on commit bc0d340

Please sign in to comment.