=removing support for loading certs via file paths, adding host factory module, removing config section
This commit is contained in:
@@ -13,13 +13,6 @@ backend:
|
||||
database:
|
||||
client: sqlite3
|
||||
connection: ':memory:'
|
||||
# https:
|
||||
# certificate:
|
||||
# size: 2048
|
||||
# algorithm: sha256
|
||||
# days: 30
|
||||
# attributes:
|
||||
# commonName: 'dfds.com'
|
||||
|
||||
proxy:
|
||||
'/circleci/api':
|
||||
|
||||
@@ -21,7 +21,6 @@ import express, { Router } from 'express';
|
||||
import helmet from 'helmet';
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
import * as fs from 'fs';
|
||||
import stoppable from 'stoppable';
|
||||
import { Logger } from 'winston';
|
||||
import { useHotCleanup } from '../../hot';
|
||||
@@ -38,10 +37,7 @@ import {
|
||||
readHttpsSettings,
|
||||
HttpsSettings,
|
||||
} from './config';
|
||||
import {
|
||||
createHttpServer,
|
||||
createHttpsServer,
|
||||
} from './hostFactory';
|
||||
import { createHttpServer, createHttpsServer } from './hostFactory';
|
||||
|
||||
const DEFAULT_PORT = 7000;
|
||||
// '' is express default, which listens to all interfaces
|
||||
@@ -149,83 +145,16 @@ export class ServiceBuilderImpl implements ServiceBuilder {
|
||||
reject(e);
|
||||
});
|
||||
|
||||
let server: http.Server;
|
||||
|
||||
if (httpsSettings) {
|
||||
logger.info('Initializing https server');
|
||||
|
||||
const credentials: { key: string; cert: string } = {
|
||||
key: '',
|
||||
cert: '',
|
||||
};
|
||||
const signingOptions: any = httpsSettings?.certificate;
|
||||
|
||||
if (signingOptions?.algorithm !== undefined) {
|
||||
logger.info('Generating self-signed certificate with attributes');
|
||||
|
||||
const certificateAttributes: Array<any> = Object.entries(
|
||||
signingOptions.attributes,
|
||||
).map(([name, value]) => ({ name, value }));
|
||||
|
||||
// TODO: Create a type def for selfsigned.
|
||||
const signatures = require('selfsigned').generate(
|
||||
certificateAttributes,
|
||||
{
|
||||
algorithm: signingOptions?.algorithm,
|
||||
keySize: signingOptions?.size || 2048,
|
||||
days: signingOptions?.days || 30,
|
||||
},
|
||||
);
|
||||
|
||||
logger.info(
|
||||
'Bootstrapping key and cert from self-signed certificate',
|
||||
);
|
||||
|
||||
credentials.key = signatures.private;
|
||||
credentials.cert = signatures.cert;
|
||||
} else {
|
||||
if (fs.existsSync(signingOptions?.key)) {
|
||||
if (fs.lstatSync(signingOptions?.key).isFile()) {
|
||||
logger.info('Bootstrapping key from file');
|
||||
|
||||
credentials.key = fs.readFileSync(signingOptions?.key).toString();
|
||||
}
|
||||
} else {
|
||||
logger.info('Bootstrapping key from config');
|
||||
|
||||
credentials.key = signingOptions?.key;
|
||||
}
|
||||
|
||||
if (fs.existsSync(signingOptions?.cert)) {
|
||||
if (fs.lstatSync(signingOptions?.cert).isFile()) {
|
||||
logger.info('Bootstrapping cert from file');
|
||||
|
||||
credentials.cert = fs
|
||||
.readFileSync(signingOptions?.cert)
|
||||
.toString();
|
||||
}
|
||||
} else {
|
||||
logger.info('Bootstrapping cert from config');
|
||||
|
||||
credentials.cert = signingOptions?.cert;
|
||||
}
|
||||
}
|
||||
|
||||
if (credentials.key === '' || credentials.cert === '') {
|
||||
throw new Error('Invalid credentials');
|
||||
}
|
||||
|
||||
server = https.createServer(credentials, app) as http.Server;
|
||||
} else {
|
||||
logger.info('Initializing http server');
|
||||
|
||||
server = createHttpServer(app);
|
||||
}
|
||||
const server: http.Server = httpsSettings
|
||||
? createHttpsServer(app, httpsSettings, logger)
|
||||
: createHttpServer(app, logger);
|
||||
|
||||
const stoppableServer = stoppable(
|
||||
server.listen(port, host, () => {
|
||||
logger.info(`Listening on ${host}:${port}`);
|
||||
}), 0);
|
||||
logger.info(`Listening on ${host}:${port}`);
|
||||
}),
|
||||
0,
|
||||
);
|
||||
|
||||
useHotCleanup(this.module, () =>
|
||||
stoppableServer.stop((e: any) => {
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
import express from 'express';
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
import { Logger } from 'winston';
|
||||
import { HttpsSettings } from './config';
|
||||
|
||||
/**
|
||||
* Reads some base options out of a config object.
|
||||
@@ -31,11 +33,57 @@ import * as https from 'https';
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function createHttpServer(app: express.Express): http.Server {
|
||||
export function createHttpServer(
|
||||
app: express.Express,
|
||||
logger: Logger,
|
||||
): http.Server {
|
||||
logger.info('Initializing http server');
|
||||
|
||||
return http.createServer(app);
|
||||
}
|
||||
|
||||
export function createHttpsServer(
|
||||
app: express.Express,
|
||||
httpsSettings: HttpsSettings,
|
||||
logger: Logger,
|
||||
): http.Server {
|
||||
logger.info('Initializing https server');
|
||||
|
||||
export function createHttpsServer(app: express.Express): http.Server {
|
||||
return https.createServer(app);
|
||||
}
|
||||
const credentials: { key: string; cert: string } = {
|
||||
key: '',
|
||||
cert: '',
|
||||
};
|
||||
|
||||
const signingOptions: any = httpsSettings?.certificate;
|
||||
|
||||
if (signingOptions?.algorithm !== undefined) {
|
||||
logger.info('Generating self-signed certificate with attributes');
|
||||
|
||||
const certificateAttributes: Array<any> = Object.entries(
|
||||
signingOptions.attributes,
|
||||
).map(([name, value]) => ({ name, value }));
|
||||
|
||||
// TODO: Create a type def for selfsigned.
|
||||
const signatures = require('selfsigned').generate(certificateAttributes, {
|
||||
algorithm: signingOptions?.algorithm,
|
||||
keySize: signingOptions?.size || 2048,
|
||||
days: signingOptions?.days || 30,
|
||||
});
|
||||
|
||||
logger.info('Bootstrapping self-signed certificate');
|
||||
|
||||
credentials.key = signatures.private;
|
||||
credentials.cert = signatures.cert;
|
||||
} else {
|
||||
logger.info('Bootstrapping cert from config');
|
||||
|
||||
credentials.key = signingOptions?.key;
|
||||
credentials.cert = signingOptions?.cert;
|
||||
}
|
||||
|
||||
if (credentials.key === '' || credentials.cert === '') {
|
||||
throw new Error('Invalid credentials');
|
||||
}
|
||||
|
||||
return https.createServer(credentials, app) as http.Server;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user