Skip to content

Commit 17be622

Browse files
tegefaulkesCMCDragonkai
authored andcommitted
fix: WebSocketServer can now update TLS certs while running
* Related #540 [ci skip]
1 parent e5ad709 commit 17be622

4 files changed

Lines changed: 71 additions & 58 deletions

File tree

src/PolykeyAgent.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,10 +739,7 @@ class PolykeyAgent {
739739
keyPrivatePem: keysUtils.privateKeyToPEM(data.keyPair.privateKey),
740740
certChainPem: await this.certManager.getCertPEMsChainPEM(),
741741
};
742-
// FIXME: Can we even support updating TLS config anymore?
743-
// We would need to shut down the Websocket server and re-create it with the new config.
744-
// Right now graceful shutdown is not supported.
745-
// this.grpcServerClient.setTLSConfig(tlsConfig);
742+
this.webSocketServerClient.setTlsConfig(tlsConfig);
746743
this.nodeConnectionManager.updateTlsConfig(tlsConfig);
747744
this.logger.info(`${KeyRing.name} change propagated`);
748745
},

src/websockets/WebSocketServer.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { TLSConfig } from '../network/types';
22
import type { IncomingMessage, ServerResponse } from 'http';
3+
import type tls from 'tls';
34
import https from 'https';
45
import { startStop, status } from '@matrixai/async-init';
56
import Logger from '@matrixai/logger';
@@ -24,7 +25,6 @@ class WebSocketServer extends EventTarget {
2425
* @param obj
2526
* @param obj.connectionCallback -
2627
* @param obj.tlsConfig - TLSConfig containing the private key and cert chain used for TLS.
27-
* @param obj.basePath - Directory path used for storing temp cert files for starting the `uWebsocket` server.
2828
* @param obj.host - Listen address to bind to.
2929
* @param obj.port - Listen port to bind to.
3030
* @param obj.maxIdleTimeout - Timeout time for when the connection is cleaned up after no activity.
@@ -38,7 +38,6 @@ class WebSocketServer extends EventTarget {
3838
static async createWebSocketServer({
3939
connectionCallback,
4040
tlsConfig,
41-
basePath,
4241
host,
4342
port,
4443
maxIdleTimeout = 120,
@@ -48,7 +47,6 @@ class WebSocketServer extends EventTarget {
4847
}: {
4948
connectionCallback: ConnectionCallback;
5049
tlsConfig: TLSConfig;
51-
basePath?: string;
5250
host?: string;
5351
port?: number;
5452
maxIdleTimeout?: number;
@@ -66,7 +64,6 @@ class WebSocketServer extends EventTarget {
6664
await wsServer.start({
6765
connectionCallback,
6866
tlsConfig,
69-
basePath,
7067
host,
7168
port,
7269
});
@@ -106,7 +103,6 @@ class WebSocketServer extends EventTarget {
106103
connectionCallback,
107104
}: {
108105
tlsConfig: TLSConfig;
109-
basePath?: string;
110106
host?: string;
111107
port?: number;
112108
connectionCallback?: ConnectionCallback;
@@ -212,6 +208,15 @@ class WebSocketServer extends EventTarget {
212208
return this._host;
213209
}
214210

211+
@startStop.ready(new webSocketErrors.ErrorWebSocketServerNotRunning())
212+
public setTlsConfig(tlsConfig: TLSConfig): void {
213+
const tlsServer = this.server as tls.Server;
214+
tlsServer.setSecureContext({
215+
key: tlsConfig.keyPrivatePem,
216+
cert: tlsConfig.certChainPem,
217+
});
218+
}
219+
215220
/**
216221
* Handles the creation of the `ReadableWritablePair` and provides it to the
217222
* StreamPair handler.

src/websockets/WebSocketStream.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type {
66
import type * as ws from 'ws';
77
import type Logger from '@matrixai/logger';
88
import type { NodeIdEncoded } from '../ids/types';
9-
import type { JSONValue } from '../types';
109
import { WritableStream, ReadableStream } from 'stream/web';
1110
import * as webSocketErrors from './errors';
1211
import * as utilsErrors from '../utils/errors';
@@ -297,7 +296,7 @@ class WebSocketStream implements ReadableWritablePair<Uint8Array, Uint8Array> {
297296
return this._endedProm;
298297
}
299298

300-
get meta(): Record<string, JSONValue> {
299+
get meta() {
301300
// Spreading to avoid modifying the data
302301
return {
303302
...this.metadata,

0 commit comments

Comments
 (0)