Add TLS/SSL support (#2827)

This commit is contained in:
Opender Singh 2020-11-12 12:42:52 +13:00 committed by GitHub
parent f858b6625d
commit 6f3c41659f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 7 deletions

View File

@ -0,0 +1,43 @@
import parseGrpcUrl from '../parse-grpc-url';
describe('parseGrpcUrl', () => {
it.each([
['grpcb.in:9000', 'grpcb.in:9000'],
['GRPCB.IN:9000', 'grpcb.in:9000'],
['custom.co', 'custom.co'],
])('should not enable tls with no protocol: %s', (input, expected) => {
expect(parseGrpcUrl(input)).toStrictEqual({
url: expected,
enableTls: false,
});
});
it.each([
['grpcs://grpcb.in:9000', 'grpcb.in:9000'],
['GRPCS://GRPCB.IN:9000', 'grpcb.in:9000'],
['grpcs://custom.co', 'custom.co'],
])('should enable tls with grpcs:// protocol: %s', (input, expected) => {
expect(parseGrpcUrl(input)).toStrictEqual({
url: expected,
enableTls: true,
});
});
it.each([
['grpc://grpcb.in:9000', 'grpcb.in:9000'],
['GRPC://GRPCB.IN:9000', 'grpcb.in:9000'],
['grpc://custom.co', 'custom.co'],
])('should not enable tls with no grpc:// protocol: %s', (input, expected) => {
expect(parseGrpcUrl(input)).toStrictEqual({
url: expected,
enableTls: false,
});
});
it.each([null, undefined, ''])('can handle falsey urls', input => {
expect(parseGrpcUrl(input)).toStrictEqual({
url: '',
enableTls: false,
});
});
});

View File

@ -8,15 +8,21 @@ import callCache from './call-cache';
import type { ServiceError } from './service-error';
import { GrpcStatusEnum } from './service-error';
import type { Call } from './call-cache';
import parseGrpcUrl from './parse-grpc-url';
const createClient = (req: GrpcRequest, respond: ResponseCallbacks): Object | undefined => {
if (!req.url) {
respond.sendError(req._id, new Error('gRPC url not specified')); // TODO: update wording
const _createClient = (req: GrpcRequest, respond: ResponseCallbacks): Object | undefined => {
const { url, enableTls } = parseGrpcUrl(req.url);
if (!url) {
respond.sendError(req._id, new Error('gRPC url not specified'));
return undefined;
}
console.log(`[gRPC] connecting to url=${req.url}`);
const credentials = enableTls ? grpc.credentials.createSsl() : grpc.credentials.createInsecure();
console.log(`[gRPC] connecting to url=${url} ${enableTls ? 'with' : 'without'} TLS`);
const Client = grpc.makeGenericClientConstructor({});
return new Client(req.url, grpc.credentials.createInsecure());
return new Client(url, credentials);
};
export const sendUnary = async (requestId: string, respond: ResponseCallbacks): Promise<void> => {
@ -39,7 +45,7 @@ export const sendUnary = async (requestId: string, respond: ResponseCallbacks):
}
// Create client
const client = createClient(req, respond);
const client = _createClient(req, respond);
if (!client) {
return;
@ -81,7 +87,7 @@ export const startClientStreaming = async (
}
// Create client
const client = createClient(req, respond);
const client = _createClient(req, respond);
if (!client) {
return;

View File

@ -0,0 +1,17 @@
// @flow
import url from 'url';
const parseGrpcUrl = (grpcUrl?: string): { url: string, enableTls: boolean } => {
const { protocol, host, href } = url.parse(grpcUrl?.toLowerCase() || '');
switch (protocol) {
case 'grpcs:':
return { url: host, enableTls: true };
case 'grpc:':
return { url: host, enableTls: false };
default:
return { url: href, enableTls: false };
}
};
export default parseGrpcUrl;