Skip to content

Commit 1b053ea

Browse files
committed
feat: NCM, NM and Sigchain handle the certs changed event
[ci skip]
1 parent 0c8b29f commit 1b053ea

File tree

7 files changed

+148
-65
lines changed

7 files changed

+148
-65
lines changed

src/PolykeyAgent.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { DeepPartial, FileSystem, PromiseDeconstructed } from './types';
22
import type { PolykeyWorkerManagerInterface } from './workers/types';
33
import type { TLSConfig } from './network/types';
44
import type { SeedNodes } from './nodes/types';
5-
import type { CertManagerChangeData, Key } from './keys/types';
5+
import type { Key } from './keys/types';
66
import type { RecoveryCode, PrivateKey } from './keys/types';
77
import type { PasswordMemLimit, PasswordOpsLimit } from './keys/types';
88
import path from 'path';
@@ -99,7 +99,6 @@ interface PolykeyAgent extends CreateDestroyStartStop {}
9999
new errors.ErrorPolykeyAgentDestroyed(),
100100
)
101101
class PolykeyAgent {
102-
103102
/**
104103
* Create the Polykey Agent.
105104
*
@@ -321,8 +320,9 @@ class PolykeyAgent {
321320
sigchain =
322321
sigchain ??
323322
(await Sigchain.createSigchain({
324-
keyRing,
325323
db,
324+
keyRing,
325+
certManager,
326326
logger: logger.getChild(Sigchain.name),
327327
fresh,
328328
}));
@@ -370,6 +370,7 @@ class PolykeyAgent {
370370
new NodeConnectionManager({
371371
keyRing,
372372
nodeGraph,
373+
certManager,
373374
tlsConfig,
374375
seedNodes: optionsDefaulted.seedNodes,
375376
connectionFindConcurrencyLimit:
@@ -396,6 +397,7 @@ class PolykeyAgent {
396397
nodeConnectionManager,
397398
taskManager,
398399
gestaltGraph,
400+
certManager,
399401
logger: logger.getChild(NodeManager.name),
400402
});
401403
await nodeManager.start();
@@ -611,6 +613,22 @@ class PolykeyAgent {
611613
this.rpcServerAgent.handleStream(stream);
612614
};
613615

616+
protected handleEventsCertManagerCertChange = async (
617+
evt: keysEvents.EventsCertManagerCertChange,
618+
) => {
619+
const data = evt.detail;
620+
this.logger.info(`${KeyRing.name} change propagating`);
621+
await this.status.updateStatusLive({
622+
nodeId: data.nodeId,
623+
});
624+
const tlsConfig: TLSConfig = {
625+
keyPrivatePem: keysUtils.privateKeyToPEM(data.keyPair.privateKey),
626+
certChainPem: await this.certManager.getCertPEMsChainPEM(),
627+
};
628+
this.webSocketServerClient.setTlsConfig(tlsConfig);
629+
this.logger.info(`${KeyRing.name} change propagated`);
630+
};
631+
614632
constructor({
615633
nodePath,
616634
status,
@@ -709,25 +727,10 @@ class PolykeyAgent {
709727
try {
710728
this.logger.info(`Starting ${this.constructor.name}`);
711729
// Register event handlers
712-
// FIXME: we need to handle the EventCertManagerCertChanged event to update the status
713-
const handleCertChange = async (evt: keysEvents.EventsCertManagerCertChange) => {
714-
const data = evt.detail
715-
this.logger.info(`${KeyRing.name} change propagating`);
716-
await this.status.updateStatusLive({
717-
nodeId: data.nodeId,
718-
});
719-
await this.nodeManager.resetBuckets();
720-
// Update the sigchain
721-
await this.sigchain.onKeyRingChange();
722-
const tlsConfig: TLSConfig = {
723-
keyPrivatePem: keysUtils.privateKeyToPEM(data.keyPair.privateKey),
724-
certChainPem: await this.certManager.getCertPEMsChainPEM(),
725-
};
726-
this.webSocketServerClient.setTlsConfig(tlsConfig);
727-
this.nodeConnectionManager.updateTlsConfig(tlsConfig);
728-
this.logger.info(`${KeyRing.name} change propagated`);
729-
}
730-
this.certManager.addEventListener(keysEvents.EventsCertManagerCertChange.name, handleCertChange);
730+
this.certManager.addEventListener(
731+
keysEvents.EventsCertManagerCertChange.name,
732+
this.handleEventsCertManagerCertChange,
733+
);
731734
await this.status.start({ pid: process.pid });
732735
await this.schema.start({ fresh });
733736
// Starting modules
@@ -818,7 +821,10 @@ class PolykeyAgent {
818821
this.logger.warn(
819822
`Failed Starting ${this.constructor.name} with ${e.message}`,
820823
);
821-
this.certManager.removeEventListener(keysEvents.EventsCertManagerCertChange.name, handleCertChange);
824+
this.certManager.removeEventListener(
825+
keysEvents.EventsCertManagerCertChange.name,
826+
this.handleEventsCertManagerCertChange,
827+
);
822828
await this.status?.beginStop({ pid: process.pid });
823829
await this.taskManager?.stopProcessing();
824830
await this.taskManager?.stopTasks();
@@ -856,7 +862,10 @@ class PolykeyAgent {
856862
*/
857863
public async stop() {
858864
this.logger.info(`Stopping ${this.constructor.name}`);
859-
this.certManager.removeEventListener(keysEvents.EventsCertManagerCertChange.name, handleCertChange);
865+
this.certManager.removeEventListener(
866+
keysEvents.EventsCertManagerCertChange.name,
867+
this.handleEventsCertManagerCertChange,
868+
);
860869
await this.status.beginStop({ pid: process.pid });
861870
await this.taskManager.stopProcessing();
862871
await this.taskManager.stopTasks();

src/keys/CertManager.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,16 @@ class CertManager {
454454
if (this.tasksRunning) {
455455
await this.setupRenewCurrentCertTask(now);
456456
}
457-
this.dispatchEvent(new events.EventsCertManagerCertChange({
458-
detail: {
459-
nodeId: this.keyRing.getNodeId(),
460-
keyPair: this.keyRing.keyPair,
461-
cert: certNew,
462-
recoveryCode: recoveryCodeNew!,
463-
},
464-
}));
457+
this.dispatchEvent(
458+
new events.EventsCertManagerCertChange({
459+
detail: {
460+
nodeId: this.keyRing.getNodeId(),
461+
keyPair: this.keyRing.keyPair,
462+
cert: certNew,
463+
recoveryCode: recoveryCodeNew!,
464+
},
465+
}),
466+
);
465467
this.logger.info('Renewed certificate chain with new key pair');
466468
});
467469
return certNew!;
@@ -518,14 +520,16 @@ class CertManager {
518520
if (this.tasksRunning) {
519521
await this.setupRenewCurrentCertTask(now);
520522
}
521-
this.dispatchEvent(new events.EventsCertManagerCertChange({
522-
detail: {
523-
nodeId: this.keyRing.getNodeId(),
524-
keyPair: this.keyRing.keyPair,
525-
cert: certNew,
526-
recoveryCode: undefined,
527-
},
528-
}));
523+
this.dispatchEvent(
524+
new events.EventsCertManagerCertChange({
525+
detail: {
526+
nodeId: this.keyRing.getNodeId(),
527+
keyPair: this.keyRing.keyPair,
528+
cert: certNew,
529+
recoveryCode: undefined,
530+
},
531+
}),
532+
);
529533
this.logger.info('Renewed certificate chain with current key pair');
530534
});
531535
return certNew!;
@@ -589,14 +593,16 @@ class CertManager {
589593
if (this.tasksRunning) {
590594
await this.setupRenewCurrentCertTask(now);
591595
}
592-
this.dispatchEvent(new events.EventsCertManagerCertChange({
593-
detail: {
594-
nodeId: this.keyRing.getNodeId(),
595-
keyPair: this.keyRing.keyPair,
596-
cert: certNew!,
597-
recoveryCode: recoveryCodeNew!,
598-
},
599-
}));
596+
this.dispatchEvent(
597+
new events.EventsCertManagerCertChange({
598+
detail: {
599+
nodeId: this.keyRing.getNodeId(),
600+
keyPair: this.keyRing.keyPair,
601+
cert: certNew!,
602+
recoveryCode: recoveryCodeNew!,
603+
},
604+
}),
605+
);
600606
this.logger.info('Resetted certificate chain with new key pair');
601607
});
602608
return certNew!;
@@ -648,14 +654,16 @@ class CertManager {
648654
if (this.tasksRunning) {
649655
await this.setupRenewCurrentCertTask(now);
650656
}
651-
this.dispatchEvent(new events.EventsCertManagerCertChange({
652-
detail: {
653-
nodeId: this.keyRing.getNodeId(),
654-
keyPair: this.keyRing.keyPair,
655-
cert: certNew,
656-
recoveryCode: undefined,
657-
},
658-
}));
657+
this.dispatchEvent(
658+
new events.EventsCertManagerCertChange({
659+
detail: {
660+
nodeId: this.keyRing.getNodeId(),
661+
keyPair: this.keyRing.keyPair,
662+
cert: certNew,
663+
recoveryCode: undefined,
664+
},
665+
}),
666+
);
659667
this.logger.info('Resetted certificate chain with current key pair');
660668
});
661669
return certNew!;

src/keys/events.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import type { CertManagerChangeData } from './types';
12
import { AbstractEvent } from '@matrixai/events';
2-
import { CertManagerChangeData } from "./types";
33

44
abstract class EventsKeys<T = null> extends AbstractEvent<T> {}
55

@@ -29,4 +29,4 @@ export {
2929
EventsCertManagerDestroy,
3030
EventsCertManagerDestroyed,
3131
EventsCertManagerCertChange,
32-
}
32+
};

src/nodes/NodeConnectionManager.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
SeedNodes,
1313
} from './types';
1414
import type KeyRing from '../keys/KeyRing';
15+
import type CertManager from '../keys/CertManager';
1516
import type { Key, CertificatePEM } from '../keys/types';
1617
import type { ConnectionData, Host, Hostname, Port } from '../network/types';
1718
import type { TLSConfig } from '../network/types';
@@ -36,6 +37,7 @@ import * as networkUtils from '../network/utils';
3637
import { clientManifest as agentClientManifest } from '../agent/handlers/clientManifest';
3738
import * as utils from '../utils';
3839
import config from '../config';
40+
import * as keysEvents from '../keys/events';
3941

4042
type AgentClientManifest = typeof agentClientManifest;
4143

@@ -123,6 +125,7 @@ class NodeConnectionManager {
123125
protected logger: Logger;
124126
protected keyRing: KeyRing;
125127
protected nodeGraph: NodeGraph;
128+
protected certManager?: CertManager;
126129
protected tlsConfig: TLSConfig;
127130
protected seedNodes: SeedNodes;
128131

@@ -174,9 +177,21 @@ class NodeConnectionManager {
174177
this.dispatchEvent(event.clone());
175178
};
176179

180+
protected handleEventsCertManagerCertChange = async (
181+
evt: keysEvents.EventsCertManagerCertChange,
182+
) => {
183+
const data = evt.detail;
184+
const tlsConfig: TLSConfig = {
185+
keyPrivatePem: keysUtils.privateKeyToPEM(data.keyPair.privateKey),
186+
certChainPem: await this.certManager!.getCertPEMsChainPEM(),
187+
};
188+
this.updateTlsConfig(tlsConfig);
189+
};
190+
177191
public constructor({
178192
keyRing,
179193
nodeGraph,
194+
certManager,
180195
tlsConfig,
181196
seedNodes = {},
182197
connectionFindConcurrencyLimit = config.defaultsSystem
@@ -195,6 +210,7 @@ class NodeConnectionManager {
195210
}: {
196211
keyRing: KeyRing;
197212
nodeGraph: NodeGraph;
213+
certManager?: CertManager;
198214
tlsConfig: TLSConfig;
199215
seedNodes?: SeedNodes;
200216
connectionFindConcurrencyLimit?: number;
@@ -208,6 +224,7 @@ class NodeConnectionManager {
208224
this.logger = logger ?? new Logger(this.constructor.name);
209225
this.keyRing = keyRing;
210226
this.nodeGraph = nodeGraph;
227+
this.certManager = certManager;
211228
this.tlsConfig = tlsConfig;
212229
// Filter out own node ID
213230
const nodeIdEncodedOwn = nodesUtils.encodeNodeId(keyRing.getNodeId());
@@ -339,13 +356,21 @@ class NodeConnectionManager {
339356
EventDefault.name,
340357
this.handleQUICServerEvents,
341358
);
359+
this.certManager?.addEventListener(
360+
keysEvents.EventsCertManagerCertChange.name,
361+
this.handleEventsCertManagerCertChange,
362+
);
342363

343364
this.logger.info(`Started ${this.constructor.name}`);
344365
}
345366

346367
public async stop() {
347368
this.logger.info(`Stop ${this.constructor.name}`);
348369

370+
this.certManager?.removeEventListener(
371+
keysEvents.EventsCertManagerCertChange.name,
372+
this.handleEventsCertManagerCertChange,
373+
);
349374
this.quicServer.removeEventListener(
350375
EventDefault.name,
351376
this.handleQUICServerEvents,

0 commit comments

Comments
 (0)