Skip to content

Commit

Permalink
client support tls connections (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
famarting authored Jul 26, 2024
1 parent 6a14ce2 commit 822fd5c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/client/client-grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import * as grpc from "@grpc/grpc-js";

export class GrpcClient {
private readonly _hostAddress: string;
private readonly _tls: boolean;
private readonly _options: grpc.ChannelOptions;
private _stub: stubs.TaskHubSidecarServiceClient;

constructor(hostAddress: string = "localhost:4001", options: grpc.ChannelOptions = {}) {
constructor(hostAddress: string = "localhost:4001", options: grpc.ChannelOptions = {}, useTLS: boolean = false) {
this._hostAddress = hostAddress;
this._tls = useTLS;
this._options = this._generateChannelOptions(options);
this._stub = this._generateClient();
}
Expand All @@ -20,10 +22,17 @@ export class GrpcClient {
}

_generateClient(): stubs.TaskHubSidecarServiceClient {
const channelCreds = grpc.credentials.createInsecure();
const channelCreds = this._generateCredentials();
return new stubs.TaskHubSidecarServiceClient(this._hostAddress, channelCreds, this._options);
}

_generateCredentials(): grpc.ChannelCredentials {
if (this._tls) {
return grpc.ChannelCredentials.createSsl();
}
return grpc.ChannelCredentials.createInsecure();
}

_generateChannelOptions(options: grpc.ChannelOptions = {}): grpc.ChannelOptions {
const defaultOptions: Partial<grpc.ClientOptions> = {
"grpc.max_receive_message_length": -1,
Expand Down
4 changes: 2 additions & 2 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import * as grpc from "@grpc/grpc-js";
export class TaskHubGrpcClient {
private _stub: stubs.TaskHubSidecarServiceClient;

constructor(hostAddress?: string, option?: grpc.ChannelOptions) {
this._stub = new GrpcClient(hostAddress, option).stub;
constructor(hostAddress?: string, option?: grpc.ChannelOptions, useTLS?: boolean) {
this._stub = new GrpcClient(hostAddress, option, useTLS).stub;
}

async stop(): Promise<void> {
Expand Down
6 changes: 4 additions & 2 deletions src/worker/task-hub-grpc-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ export class TaskHubGrpcWorker {
private _responseStream: grpc.ClientReadableStream<pb.WorkItem> | null;
private _registry: Registry;
private _hostAddress?: string;
private _tls?: boolean;
private _grpcChannelOptions?: grpc.ChannelOptions;
private _isRunning: boolean;
private _stub: stubs.TaskHubSidecarServiceClient | null;

constructor(hostAddress?: string, options?: grpc.ChannelOptions) {
constructor(hostAddress?: string, options?: grpc.ChannelOptions, useTLS?: boolean) {
this._registry = new Registry();
this._hostAddress = hostAddress;
this._tls = useTLS;
this._grpcChannelOptions = options;
this._responseStream = null;
this._isRunning = false;
Expand Down Expand Up @@ -97,7 +99,7 @@ export class TaskHubGrpcWorker {
* Therefore, we open the stream and simply listen through the eventemitter behind the scenes
*/
async start(): Promise<void> {
const client = new GrpcClient(this._hostAddress, this._grpcChannelOptions);
const client = new GrpcClient(this._hostAddress, this._grpcChannelOptions, this._tls);

if (this._isRunning) {
throw new Error("The worker is already running.");
Expand Down

0 comments on commit 822fd5c

Please sign in to comment.