Skip to content

Commit 8d91e4b

Browse files
committed
First Pass at Supporting Self-Signed CAs
Signed-off-by: hfuss <[email protected]>
1 parent 30510e1 commit 8d91e4b

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import YAML from 'yamljs';
2424
import { eventEmitter as blobsEventEmitter } from './handlers/blobs';
2525
import * as eventsHandler from './handlers/events';
2626
import { eventEmitter as messagesEventEmitter } from './handlers/messages';
27-
import { genTLSContext, init as initCert, loadCAs } from './lib/cert';
27+
import { genTLSContext, init as initCert, loadPeerCAs } from './lib/cert';
2828
import { config, init as initConfig } from './lib/config';
2929
import { Logger } from './lib/logger';
3030
import RequestError, { errorHandler } from './lib/request-error';
@@ -43,7 +43,7 @@ let wss : WebSocket.Server
4343
let delegatedWebSocket: WebSocket | undefined = undefined;
4444

4545
export const refreshCACerts = async () => {
46-
await loadCAs()
46+
await loadPeerCAs()
4747
p2pServer.setSecureContext(genTLSContext())
4848
};
4949
setRefreshCACerts(refreshCACerts)

src/lib/cert.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,38 @@ const log = new Logger('lib/certs.ts')
2323

2424
export let key: string;
2525
export let cert: string;
26+
export let certBundle: string;
2627
export let ca: string[] = [];
2728
export let peerID: string;
2829

2930
export const init = async () => {
3031
log.debug("Reading key file");
3132
key = (await fs.readFile(path.join(utils.constants.DATA_DIRECTORY, utils.constants.KEY_FILE))).toString();
33+
log.debug("Loaded key");
3234
log.debug("Reading cert file");
3335
cert = (await fs.readFile(path.join(utils.constants.DATA_DIRECTORY, utils.constants.CERT_FILE))).toString();
36+
37+
log.debug("Loaded cert");
38+
log.debug(cert);
39+
40+
log.debug("Deriving peer ID from cert");
3441
const certData = utils.getCertData(cert);
3542
peerID = utils.getPeerID(certData.organization, certData.organizationUnit);
36-
await loadCAs();
43+
44+
let caCertPath = path.join(utils.constants.DATA_DIRECTORY, utils.constants.CA_FILE);
45+
if (await utils.fileExists(caCertPath)) {
46+
log.debug("Reading CA file");
47+
certBundle = (await fs.readFile(caCertPath)).toString() + cert;
48+
log.debug("Loaded CA + cert");
49+
log.debug(certBundle);
50+
} else {
51+
certBundle = cert;
52+
}
53+
54+
await loadPeerCAs();
3755
};
3856

39-
export const loadCAs = async () => {
57+
export const loadPeerCAs = async () => {
4058
const peerCertsPath = path.join(utils.constants.DATA_DIRECTORY, utils.constants.PEER_CERTS_SUBDIRECTORY);
4159
log.debug(`Reading peer CAs from ${peerCertsPath}`);
4260
const peerCerts = await fs.readdir(peerCertsPath);
@@ -49,6 +67,10 @@ export const loadCAs = async () => {
4967
}
5068
}
5169
log.debug(`Loaded ${ca.length} peer certificate(s)`);
70+
for (const caCert of ca) {
71+
log.debug("Outputting CA cert");
72+
log.debug(caCert);
73+
}
5274
};
5375

5476
export const genTLSContext = () => {

src/routers/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { v4 as uuidV4 } from 'uuid';
2222
import * as blobsHandler from '../handlers/blobs';
2323
import * as eventsHandler from '../handlers/events';
2424
import * as messagesHandler from '../handlers/messages';
25-
import { ca, cert, key, peerID } from '../lib/cert';
25+
import { ca, cert, certBundle, key, peerID } from '../lib/cert';
2626
import { config, persistPeers } from '../lib/config';
2727
import { IStatus } from '../lib/interfaces';
2828
import RequestError from '../lib/request-error';
@@ -41,7 +41,7 @@ router.get('/id', async (_req, res, next) => {
4141
res.send({
4242
id: peerID,
4343
endpoint: config.p2p.endpoint ?? `https://${config.p2p.hostname}:${config.p2p.port}`,
44-
cert
44+
cert: certBundle
4545
});
4646
} catch (err) {
4747
next(err);

0 commit comments

Comments
 (0)