Skip to content

Commit

Permalink
fix: allow { cert: null, key: null } for self-signed https config, fix
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Feb 19, 2024
1 parent bb29d24 commit 586668c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/server/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isPlainObject, idGenerator, getTime } from '@ircam/sc-utils';
import chalk from 'chalk';
import compression from 'compression';
import express from 'express';
import equal from 'fast-deep-equal';
import Keyv from 'keyv';
import { KeyvFile } from 'keyv-file';
import merge from 'lodash/merge.js';
Expand Down Expand Up @@ -214,11 +215,11 @@ class Server {
throw new Error(`[soundworks:Server] Invalid "env.httpsInfos" config, should contain both "cert" and "key" entries`);
}
// @todo - move that to constructor
if (!fs.existsSync(httpsInfos.cert)) {
if (httpsInfos.cert !== null && !fs.existsSync(httpsInfos.cert)) {
throw new Error(`[soundworks:Server] Invalid "env.httpsInfos" config, "cert" file not found`);
}

if (!fs.existsSync(httpsInfos.key)) {
if (httpsInfos.key !== null && !fs.existsSync(httpsInfos.key)) {
throw new Error(`[soundworks:Server] Invalid "env.httpsInfos" config, "key" file not found`);
}
}
Expand Down Expand Up @@ -444,19 +445,22 @@ class Server {
this.router.use(soundworksAuth);
}

// start http server
const useHttps = this.config.env.useHttps || false;

// ------------------------------------------------------------
// create HTTP(S) SERVER
// ------------------------------------------------------------
const useHttps = this.config.env.useHttps || false;

if (!useHttps) {
this.httpServer = http.createServer(this.router);
} else {
const httpsInfos = this.config.env.httpsInfos;
let useSelfSigned = false;

if (!httpsInfos || equal(httpsInfos, { cert: null, key: null })) {
useSelfSigned = true;
}

// if certs have been given in config
if (httpsInfos !== null) {
if (!useSelfSigned) {
try {
// existance of file is checked in contructor
let cert = fs.readFileSync(httpsInfos.cert);
Expand Down
13 changes: 13 additions & 0 deletions tests/essentials/Server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ describe('# server::Server', () => {
if (!errored) { assert.fail('should have thrown'); }
});

it(`should use self-signed certificates if both cert and key file are null`, async () => {
const selfSignedConfig = merge({}, config);
selfSignedConfig.env.useHttps = true;
selfSignedConfig.env.httpsInfos = {
cert: null,
key: null, // this is invalid
};

const server = new Server(selfSignedConfig);
await server.init();
});

it(`should store self-signed certificated in db`, async () => {
// these test crash the CI for some reason,just ignore them in CI too
const envPathname = path.join(__dirname, '.env');
Expand Down Expand Up @@ -421,6 +433,7 @@ describe('# server::Server', () => {
assert.isOk('server and process should stop');
});

// this will stop after a timeout
it('should stop the server even if a client is connected', async() => {
const server = new Server(config);

Expand Down

0 comments on commit 586668c

Please sign in to comment.