Skip to content

Commit

Permalink
search for dev certs from module in devDependencies (#1612)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip authored Apr 22, 2020
1 parent fbdf021 commit 268ce95
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
46 changes: 44 additions & 2 deletions packages/xarc-app-dev/config/dev-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,50 @@ const envWebpack = require("./env-webpack");
const envApp = require("./env-app");
const envProxy = require("./env-proxy");

/*
* Look in app's devDependencies for a module with a name that starts with ssl-certs,
* load it, and try to get dev SSL key/cert file from the functions devKeyFile and devCertFile.
*
* If they exist and return strings, then use them as the path to the SSL key/cert file
* for the dev proxy.
*/
function searchSSLCertsModule() {
let sslCertsMod;

try {
const appPkg = JSON.parse(Fs.readFileSync("package.json"));
sslCertsMod = Object.keys(appPkg.devDependencies).find(n => n.match(/(@[^\/]+\/|)ssl-certs.*/));
if (sslCertsMod) {
const sslCerts = require(sslCertsMod);
const key = sslCerts.devKeyFile();
const cert = sslCerts.devCertFile();
Fs.accessSync(key);
Fs.accessSync(cert);
console.log(`dev proxy found SSL certs module ${sslCertsMod}, using:
KEY: ${key}
CERT: ${cert}
`);
return { key, cert };
}
} catch (err) {
if (sslCertsMod && err.code !== "MODULE_NOT_FOUND") {
console.error(
`dev proxy trying to load Key/Cert from module ${sslCertsMod} failed, error:`,
err
);
}
}

return undefined;
}

function searchSSLCerts() {
const fromModule = searchSSLCertsModule();

if (fromModule) {
return fromModule;
}

const searchDirs = ["", "config", "test", "src"];
for (const f of searchDirs) {
const key = Path.resolve(f, "dev-proxy.key");
Expand Down Expand Up @@ -86,6 +129,5 @@ module.exports = {
// If using dev proxy in HTTPS, then it's also listening on a HTTP port also:
httpDevServer: { protocol: "http", host, port: httpPort, https: false },
controlPaths,
searchSSLCerts,
certs: searchSSLCerts()
searchSSLCerts
};
5 changes: 3 additions & 2 deletions packages/xarc-app-dev/lib/dev-admin/redbird-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const _ = require("lodash");
const redbird = require("@jchip/redbird");
const ck = require("chalker");
const optionalRequire = require("optional-require")(require);
const { settings, certs: proxyCerts, controlPaths } = require("../../config/dev-proxy");
const { settings, searchSSLCerts, controlPaths } = require("../../config/dev-proxy");

const { formUrl } = require("../utils");

Expand Down Expand Up @@ -206,7 +206,8 @@ const startProxy = inOptions => {
const ssl = Boolean(options.httpsPort);

if (ssl) {
assert(proxyCerts.key, "Dev Proxy can't find SSL key and certs");
const proxyCerts = searchSSLCerts();
assert(proxyCerts.key && proxyCerts.cert, "Dev Proxy can't find SSL key and certs");
Object.assign(proxyOptions, {
// We still setup a regular http rules even if HTTPS is enabled
port: options.httpPort,
Expand Down

0 comments on commit 268ce95

Please sign in to comment.