|
| 1 | +/** |
| 2 | + * @prettier |
| 3 | + */ |
| 4 | +import * as express from 'express'; |
| 5 | +import * as path from 'path'; |
| 6 | +import debug from 'debug'; |
| 7 | +import * as https from 'https'; |
| 8 | +import * as http from 'http'; |
| 9 | +import * as morgan from 'morgan'; |
| 10 | +import * as fs from 'fs'; |
| 11 | +import * as timeout from 'connect-timeout'; |
| 12 | +import * as bodyParser from 'body-parser'; |
| 13 | +import * as _ from 'lodash'; |
| 14 | +import { SSL_OP_NO_TLSv1 } from 'constants'; |
| 15 | + |
| 16 | +import { Config, config, TlsMode } from './config'; |
| 17 | +import * as routes from './routes'; |
| 18 | + |
| 19 | +const debugLogger = debug('enclaved:express'); |
| 20 | +const pjson = require('../package.json'); |
| 21 | + |
| 22 | +/** |
| 23 | + * Set up the logging middleware provided by morgan |
| 24 | + * |
| 25 | + * @param app |
| 26 | + * @param config |
| 27 | + */ |
| 28 | +function setupLogging(app: express.Application, config: Config): void { |
| 29 | + // Set up morgan for logging, with optional logging into a file |
| 30 | + let middleware; |
| 31 | + if (config.logFile) { |
| 32 | + // create a write stream (in append mode) |
| 33 | + const accessLogPath = path.resolve(config.logFile); |
| 34 | + const accessLogStream = fs.createWriteStream(accessLogPath, { flags: 'a' }); |
| 35 | + /* eslint-disable-next-line no-console */ |
| 36 | + console.log('Log location: ' + accessLogPath); |
| 37 | + // setup the logger |
| 38 | + middleware = morgan('combined', { stream: accessLogStream }); |
| 39 | + } else { |
| 40 | + middleware = morgan('combined'); |
| 41 | + } |
| 42 | + |
| 43 | + app.use(middleware); |
| 44 | + morgan.token('remote-user', function (req: express.Request) { |
| 45 | + return (req as any).clientCert ? (req as any).clientCert.subject.CN : 'unknown'; |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Create a startup function which will be run upon server initialization |
| 51 | + * |
| 52 | + * @param config |
| 53 | + * @param baseUri |
| 54 | + * @return {Function} |
| 55 | + */ |
| 56 | +export function startup(config: Config, baseUri: string): () => void { |
| 57 | + return function () { |
| 58 | + /* eslint-disable no-console */ |
| 59 | + console.log('BitGo-Enclaved-Express running'); |
| 60 | + console.log(`Base URI: ${baseUri}`); |
| 61 | + console.log(`TLS Mode: ${config.tlsMode}`); |
| 62 | + console.log(`mTLS Enabled: ${config.tlsMode === TlsMode.MTLS}`); |
| 63 | + console.log(`Request Client Cert: ${config.mtlsRequestCert}`); |
| 64 | + console.log(`Reject Unauthorized: ${config.mtlsRejectUnauthorized}`); |
| 65 | + /* eslint-enable no-console */ |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +function isTLS(config: Config): boolean { |
| 70 | + const { keyPath, crtPath, tlsKey, tlsCert, tlsMode } = config; |
| 71 | + console.log('TLS Configuration:', { |
| 72 | + tlsMode, |
| 73 | + hasKeyPath: Boolean(keyPath), |
| 74 | + hasCrtPath: Boolean(crtPath), |
| 75 | + hasTlsKey: Boolean(tlsKey), |
| 76 | + hasTlsCert: Boolean(tlsCert), |
| 77 | + }); |
| 78 | + if (tlsMode === TlsMode.DISABLED) return false; |
| 79 | + return Boolean((keyPath && crtPath) || (tlsKey && tlsCert)); |
| 80 | +} |
| 81 | + |
| 82 | +async function createHttpsServer(app: express.Application, config: Config): Promise<https.Server> { |
| 83 | + const { keyPath, crtPath, tlsKey, tlsCert, tlsMode, mtlsRequestCert, mtlsRejectUnauthorized } = config; |
| 84 | + let key: string; |
| 85 | + let cert: string; |
| 86 | + if (tlsKey && tlsCert) { |
| 87 | + key = tlsKey; |
| 88 | + cert = tlsCert; |
| 89 | + console.log('Using TLS key and cert from environment variables'); |
| 90 | + } else if (keyPath && crtPath) { |
| 91 | + const privateKeyPromise = require('fs').promises.readFile(keyPath, 'utf8'); |
| 92 | + const certificatePromise = require('fs').promises.readFile(crtPath, 'utf8'); |
| 93 | + [key, cert] = await Promise.all([privateKeyPromise, certificatePromise]); |
| 94 | + console.log(`Using TLS key and cert from files: ${keyPath}, ${crtPath}`); |
| 95 | + } else { |
| 96 | + throw new Error('Failed to get TLS key and certificate'); |
| 97 | + } |
| 98 | + |
| 99 | + const httpsOptions: https.ServerOptions = { |
| 100 | + secureOptions: SSL_OP_NO_TLSv1, |
| 101 | + key, |
| 102 | + cert, |
| 103 | + // Add mTLS options if in mTLS mode |
| 104 | + requestCert: tlsMode === TlsMode.MTLS && mtlsRequestCert, |
| 105 | + rejectUnauthorized: tlsMode === TlsMode.MTLS && mtlsRejectUnauthorized, |
| 106 | + }; |
| 107 | + |
| 108 | + const server = https.createServer(httpsOptions, app); |
| 109 | + |
| 110 | + // Add middleware to validate client certificate fingerprints if in mTLS mode |
| 111 | + if (tlsMode === TlsMode.MTLS && config.mtlsAllowedClientFingerprints?.length) { |
| 112 | + app.use((req: express.Request, res: express.Response, next: express.NextFunction) => { |
| 113 | + const clientCert = (req as any).socket?.getPeerCertificate(); |
| 114 | + if (!clientCert) { |
| 115 | + return res.status(403).json({ error: 'Client certificate required' }); |
| 116 | + } |
| 117 | + |
| 118 | + const fingerprint = clientCert.fingerprint256?.replace(/:/g, '').toUpperCase(); |
| 119 | + if (!fingerprint || !config.mtlsAllowedClientFingerprints?.includes(fingerprint)) { |
| 120 | + return res.status(403).json({ error: 'Invalid client certificate fingerprint' }); |
| 121 | + } |
| 122 | + |
| 123 | + // Store client certificate info for logging |
| 124 | + (req as any).clientCert = clientCert; |
| 125 | + next(); |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + return server; |
| 130 | +} |
| 131 | + |
| 132 | +function createHttpServer(app: express.Application): http.Server { |
| 133 | + return http.createServer(app); |
| 134 | +} |
| 135 | + |
| 136 | +export async function createServer(config: Config, app: express.Application): Promise<https.Server | http.Server> { |
| 137 | + const server = isTLS(config) ? await createHttpsServer(app, config) : createHttpServer(app); |
| 138 | + if (config.keepAliveTimeout !== undefined) { |
| 139 | + server.keepAliveTimeout = config.keepAliveTimeout; |
| 140 | + } |
| 141 | + if (config.headersTimeout !== undefined) { |
| 142 | + server.headersTimeout = config.headersTimeout; |
| 143 | + } |
| 144 | + return server; |
| 145 | +} |
| 146 | + |
| 147 | +export function createBaseUri(config: Config): string { |
| 148 | + const { bind, port } = config; |
| 149 | + const tls = isTLS(config); |
| 150 | + const isStandardPort = (port === 80 && !tls) || (port === 443 && tls); |
| 151 | + return `http${tls ? 's' : ''}://${bind}${!isStandardPort ? ':' + port : ''}`; |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Create error handling middleware |
| 156 | + */ |
| 157 | +function errorHandler() { |
| 158 | + return function (err: any, req: express.Request, res: express.Response, _next: express.NextFunction) { |
| 159 | + debugLogger('Error: ' + (err && err.message ? err.message : String(err))); |
| 160 | + const statusCode = err && err.status ? err.status : 500; |
| 161 | + const result = { |
| 162 | + error: err && err.message ? err.message : String(err), |
| 163 | + name: err && err.name ? err.name : 'Error', |
| 164 | + code: err && err.code ? err.code : undefined, |
| 165 | + version: pjson.version, |
| 166 | + }; |
| 167 | + return res.status(statusCode).json(result); |
| 168 | + }; |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * Create and configure the express application |
| 173 | + */ |
| 174 | +export function app(cfg: Config): express.Application { |
| 175 | + debugLogger('app is initializing'); |
| 176 | + |
| 177 | + const app = express(); |
| 178 | + |
| 179 | + setupLogging(app, cfg); |
| 180 | + debugLogger('logging setup'); |
| 181 | + |
| 182 | + const { debugNamespace } = cfg; |
| 183 | + |
| 184 | + // enable specified debug namespaces |
| 185 | + if (_.isArray(debugNamespace)) { |
| 186 | + for (const ns of debugNamespace) { |
| 187 | + if (ns && !debug.enabled(ns)) { |
| 188 | + debug.enable(ns); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + // Be more robust about accepting URLs with double slashes |
| 194 | + app.use(function replaceUrlSlashes(req: express.Request, res: express.Response, next: express.NextFunction) { |
| 195 | + req.url = req.url.replace(/\/{2,}/g, '/'); |
| 196 | + next(); |
| 197 | + }); |
| 198 | + |
| 199 | + // Set timeout |
| 200 | + app.use(timeout(cfg.timeout)); |
| 201 | + |
| 202 | + // Add body parser |
| 203 | + app.use(bodyParser.json({ limit: '20mb' })); |
| 204 | + |
| 205 | + // Setup routes |
| 206 | + routes.setupRoutes(app); |
| 207 | + |
| 208 | + // Add error handler |
| 209 | + app.use(errorHandler()); |
| 210 | + |
| 211 | + return app; |
| 212 | +} |
| 213 | + |
| 214 | +// Add prepareIpc function |
| 215 | +async function prepareIpc(ipcSocketFilePath: string) { |
| 216 | + if (process.platform === 'win32') { |
| 217 | + throw new Error(`IPC option is not supported on platform ${process.platform}`); |
| 218 | + } |
| 219 | + try { |
| 220 | + const stat = fs.statSync(ipcSocketFilePath); |
| 221 | + if (!stat.isSocket()) { |
| 222 | + throw new Error('IPC socket is not actually a socket'); |
| 223 | + } |
| 224 | + fs.unlinkSync(ipcSocketFilePath); |
| 225 | + } catch (e: any) { |
| 226 | + if (e.code !== 'ENOENT') { |
| 227 | + throw e; |
| 228 | + } |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +export async function init(): Promise<void> { |
| 233 | + const cfg = config(); |
| 234 | + const expressApp = app(cfg); |
| 235 | + const server = await createServer(cfg, expressApp); |
| 236 | + const { port, bind, ipc } = cfg; |
| 237 | + const baseUri = createBaseUri(cfg); |
| 238 | + |
| 239 | + if (ipc) { |
| 240 | + await prepareIpc(ipc); |
| 241 | + server.listen(ipc, startup(cfg, baseUri)); |
| 242 | + } else { |
| 243 | + server.listen(port, bind, startup(cfg, baseUri)); |
| 244 | + } |
| 245 | +} |
0 commit comments