Skip to content

Commit

Permalink
add gRPC tls server
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkav committed Sep 24, 2024
1 parent 602c4d4 commit a39177b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
47 changes: 44 additions & 3 deletions packages/insomnia-smoke-test/server/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
/* eslint-disable camelcase */
// inspiration: https://github.com/grpc/grpc/blob/master/examples/node/dynamic_codegen/route_guide/route_guide_server.js
import * as grpc from '@grpc/grpc-js';
import { ServerCredentials } from '@grpc/grpc-js';
import { HandleCall } from '@grpc/grpc-js/build/src/server-call';
import * as protoLoader from '@grpc/proto-loader';
import { addReflection } from '@ravanallc/grpc-server-reflection';
import fs from 'fs';
import fs, { readFileSync } from 'fs';
import path from 'path';

const PROTO_PATH = path.resolve('../../packages/insomnia/src/network/grpc/__fixtures__/library/route_guide.proto');
Expand Down Expand Up @@ -192,8 +193,7 @@ export const startGRPCServer = (port: number) => {
const server = new grpc.Server();

// Enable reflection
const descriptorSet = '../../packages/insomnia-smoke-test/fixtures/route_guide.bin';
addReflection(server, descriptorSet);
addReflection(server, '../../packages/insomnia-smoke-test/fixtures/route_guide.bin');

// @ts-expect-error generated from proto file
server.addService(routeguide.RouteGuide.service, {
Expand All @@ -218,5 +218,46 @@ export const startGRPCServer = (port: number) => {
resolve();
});
});
const serverWithTLS = new grpc.Server();

// Enable reflection
addReflection(serverWithTLS, '../../packages/insomnia-smoke-test/fixtures/route_guide.bin');

// @ts-expect-error generated from proto file
serverWithTLS.addService(routeguide.RouteGuide.service, {
getFeature: getFeature,
listFeatures: listFeatures,
recordRoute: recordRoute,
routeChat: routeChat,
});
const rootCert = readFileSync(path.join(__dirname, '../fixtures/certificates/rootCA.pem'));
const serverCert = readFileSync(path.join(__dirname, '../fixtures/certificates/localhost.pem'));
const serverKey = readFileSync(path.join(__dirname, '../fixtures/certificates/localhost-key.pem'));
const serverCredentials = ServerCredentials.createSsl(
rootCert,
[
{
cert_chain: serverCert,
private_key: serverKey,
},
],
true
);
serverWithTLS.bindAsync('localhost:50052', serverCredentials, error => {
if (error) {
return reject(error);
}

const dbPath = '../../packages/insomnia/src/network/grpc/__fixtures__/library/route_guide_db.json';
fs.readFile(path.resolve(dbPath), (err, data) => {
if (err) {
throw err;
}
featureList = JSON.parse(data.toString());
console.log('Listening at grpcs://localhost:50052 for route_guide.proto');
serverWithTLS.start();
resolve();
});
});
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,14 @@ export const GrpcRequestPane: FunctionComponent<Props> = ({
},
});
const workspaceClientCertificates = await models.clientCertificate.findByParentId(workspaceId);
const clientCertificate = workspaceClientCertificates.find(c => !c.disabled && urlMatchesCertHost(setDefaultProtocol(c.host, 'grpc:'), request.url, false));
const clientCertificate = workspaceClientCertificates.find(c => !c.disabled);
const caCertificatePath = (await models.caCertificate.findByParentId(workspaceId))?.path;
const clientCert = await readFile(clientCertificate?.cert || '', 'utf8');
const clientKey = await readFile(clientCertificate?.key || '', 'utf8');
rendered = {
...rendered,
clientCert: clientCertificate?.cert || undefined,
clientKey: clientCertificate?.key || undefined,
clientCert,
clientKey,
caCertificate: caCertificatePath ? await readFile(caCertificatePath, 'utf8') : undefined,
};
const methods = await window.main.grpc.loadMethodsFromReflection(rendered);
Expand Down

0 comments on commit a39177b

Please sign in to comment.