Skip to content

Commit

Permalink
feat(ifc-cli): allow consumer to specify their own ssl cert and key
Browse files Browse the repository at this point in the history
Instead of solely relying on devCertAuthority for certificates when using the --ssl option, allow
the consumer to specify their own ssl cert and key. If either of the two do not exist, then
devCertAuthority will be used.

COMUI-1082
  • Loading branch information
Umer Farooq committed Jun 22, 2022
1 parent de759fc commit 700e963
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ Options:
-f, --config-file <file> iframe client configuration file (default: "/home/mcheely/projects/iframe-coordinator/ifc-cli.config.js")
-p, --port <port_num> port number to host on (default: 3000)
-s, --ssl serve over https
--ssl-cert <cert_path> certificate file to use for https
--ssl-key <key_path> key file to use for https
-h, --help output usage information
This program will start a server for a basic iframe-coordinator host app. In
Expand Down
25 changes: 22 additions & 3 deletions cli/ifc-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function main() {
app.use(express.static(appPath));

if (opts.ssl) {
const options = devCertAuthority('localhost');
const options = getSslOpts(opts.sslCert, opts.sslKey);
https.createServer(options, app).listen(opts.port);
} else {
app.listen(opts.port);
Expand All @@ -63,15 +63,19 @@ function parseProgramOptions() {
defaultJsConfig
)
.option('-p, --port <port_num>', 'port number to host on', 3000)
.option('-s, --ssl', 'serve over https');
.option('-s, --ssl', 'serve over https')
.option('--ssl-cert <cert_path>', 'certificate file to use for https')
.option('--ssl-key <key_path>', 'key file to use for https');
program.on('--help', showHelpText);

program.parse(process.argv);

return {
clientConfigFile: findConfigFile(program.configFile),
port: program.port,
ssl: program.ssl
ssl: program.ssl,
sslCert: program.sslCert,
sslKey: program.sslKey
};
}

Expand Down Expand Up @@ -136,6 +140,21 @@ function findConfigFile(cliPath) {
}
}

function getSslOpts(certPath, keyPath) {
if (!certPath || !keyPath) {
return devCertAuthority('localhost');
}
if (fs.existsSync(certPath) && fs.existsSync(keyPath)) {
return {
cert: fs.readFileSync(certPath),
key: fs.readFileSync(keyPath)
};
} else {
console.log(`Certificate files not found @ ${certPath}, and ${keyPath}`);
process.exit(1);
}
}

// Make sure a path isn't interpreted as a module when required.
function relativizePath(inPath) {
let outPath = path.relative(process.cwd(), inPath);
Expand Down

0 comments on commit 700e963

Please sign in to comment.