-
Notifications
You must be signed in to change notification settings - Fork 355
Closed
Labels
Description
When opening an https connection I noticed that express-gateway/lib/gateway/server.js is having issues properly capturing the file path in the .yml.
I made some minor changes to createTlsServer() which seem to have resolved the issue that I would like to propose here:
function createTlsServer(httpsConfig, app) {
let defaultCert = null;
let sniCerts = [];
let domainCount = httpsConfig.tls.length;
let domains = [];
httpsConfig.tls.forEach(domainObj => {
domains = [...domains, Object.getOwnPropertyNames(domainObj)];
});
for (let i = 0; i < domainCount; i++) {
let domain = domains[i].toString();
let certPaths = httpsConfig.tls[i][domain];
let cert;
if (domain === 'default') {
cert = defaultCert = {};
} else {
cert = {};
sniCerts.push([domain, cert]);
}
cert.key = fs.readFileSync(path.resolve(certPaths.key), 'utf-8');
cert.cert = fs.readFileSync(path.resolve(certPaths.cert), 'utf-8');
if (certPaths.ca && certPaths.ca.length) {
cert.ca = certPaths.ca.map(ca => fs.readFileSync(path.resolve(ca), 'utf-8'));
}
}
// see possible options https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
let options = Object.assign({}, httpsConfig.options);
if (defaultCert) {
options.key = defaultCert.key;
options.cert = defaultCert.cert;
options.ca = defaultCert.ca;
}
if (sniCerts.length > 0) {
options.SNICallback = (servername, cb) => {
for (let [domain, cert] of sniCerts) {
if (minimatch(servername, domain)) {
logger.debug(`sni: using cert for ${domain}`);
cb(null, tls.createSecureContext(cert));
return;
}
}
if (defaultCert) {
logger.debug('sni: using default cert');
cb(null, tls.createSecureContext(defaultCert));
} else {
logger.error('sni: no cert!');
cb(new Error('cannot start TLS SNI - no cert configured'));
}
};
}
return https.createServer(options, app);
}