Skip to content

Commit b037259

Browse files
Merge pull request #20 from kaleido-io/tls-reload
Reload TLS context on adding new cert
2 parents f04c507 + b3129c4 commit b037259

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

src/app.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
// limitations under the License.
1616

1717
import express from 'express';
18-
import https from 'https';
18+
import https, { Server } from 'https';
1919
import http from 'http';
2020
import WebSocket from 'ws';
2121
import { init as initConfig, config } from './lib/config';
22-
import { init as initCert, key, cert, ca } from './lib/cert';
22+
import { init as initCert, genTLSContext, loadCAs } from './lib/cert';
2323
import { createLogger, LogLevelString } from 'bunyan';
2424
import * as utils from './lib/utils';
25-
import { router as apiRouter } from './routers/api';
25+
import { router as apiRouter, setAddTLSContext } from './routers/api';
2626
import { router as p2pRouter, eventEmitter as p2pEventEmitter } from './routers/p2p';
2727
import RequestError, { errorHandler } from './lib/request-error';
2828
import * as eventsHandler from './handlers/events'
@@ -36,8 +36,17 @@ const log = createLogger({ name: 'app.ts', level: utils.constants.LOG_LEVEL as L
3636

3737
const swaggerDocument = YAML.load(path.join(__dirname, './swagger.yaml'));
3838

39+
let p2pServer : Server
40+
3941
let delegatedWebSocket: WebSocket | undefined = undefined;
4042

43+
export const addTLSContext = async (hostname: string) => {
44+
await loadCAs()
45+
// The most recent context wins (per the Node.js spec), so to get a reload we just add a wildcard context
46+
p2pServer.addContext(hostname, genTLSContext())
47+
};
48+
setAddTLSContext(addTLSContext)
49+
4150
export const start = async () => {
4251
await initConfig();
4352
await initCert();
@@ -46,13 +55,7 @@ export const start = async () => {
4655
const apiServer = http.createServer(apiApp);
4756

4857
const p2pApp = express();
49-
const p2pServer = https.createServer({
50-
key,
51-
cert,
52-
ca,
53-
rejectUnauthorized: true,
54-
requestCert: true,
55-
}, p2pApp);
58+
p2pServer = https.createServer(genTLSContext(), p2pApp);
5659

5760
const wss = new WebSocket.Server({
5861
server: apiServer, verifyClient: (info, cb) => {

src/lib/cert.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,13 @@ export const loadCAs = async () => {
4242
}
4343
log.debug(`Loaded ${ca.length} peer certificate(s)`);
4444
};
45+
46+
export const genTLSContext = () => {
47+
return {
48+
key,
49+
cert,
50+
ca,
51+
rejectUnauthorized: true,
52+
requestCert: true,
53+
}
54+
}

src/routers/api.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@ import RequestError from '../lib/request-error';
2222
import { config, persistConfig } from '../lib/config';
2323
import { IStatus } from '../lib/interfaces';
2424
import https from 'https';
25-
import { key, cert, ca, loadCAs, peerID } from '../lib/cert';
25+
import { key, cert, ca, peerID } from '../lib/cert';
2626
import * as eventsHandler from '../handlers/events';
2727
import { promises as fs } from 'fs';
2828
import path from 'path';
2929
import { v4 as uuidV4 } from 'uuid';
30+
import { URL } from 'url';
3031

3132
export const router = Router();
3233

34+
let addTLSContext: (hostname: string) => Promise<void>;
35+
36+
export const setAddTLSContext = (_addTLSContext: (hostname: string) => Promise<void>) => {
37+
addTLSContext = _addTLSContext;
38+
}
39+
3340
router.get('/id', async (_req, res, next) => {
3441
try {
3542
res.send({
@@ -93,7 +100,8 @@ router.put('/peers/:id', async (req, res, next) => {
93100
config.peers.push(peer);
94101
}
95102
await persistConfig();
96-
await loadCAs();
103+
let url = new URL(req.body.endpoint)
104+
await addTLSContext(url.hostname);
97105
res.send({ status: 'added' });
98106
} catch (err) {
99107
next(err);
@@ -114,7 +122,6 @@ router.delete('/peers/:id', async (req, res, next) => {
114122
}
115123
config.peers = config.peers.filter(peer => peer.id !== req.params.id);
116124
await persistConfig();
117-
await loadCAs();
118125
res.send({ status: 'removed' });
119126
} catch (err) {
120127
next(err);

src/routers/p2p.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ router.head('/ping', (_req, res) => {
3131
router.post('/messages', async (req, res, next) => {
3232
try {
3333
const cert = req.client.getPeerCertificate();
34-
const sender = cert.issuer.O + cert.issuer.OU;
34+
const sender = utils.getPeerID(cert.issuer.O, cert.issuer.OU);
3535
const message = await utils.extractMessageFromMultipartForm(req);
3636
eventEmitter.emit('event', {
3737
type: 'message-received',

0 commit comments

Comments
 (0)