From 129372d1618d0b14cdb77c2f279e480f3920559d Mon Sep 17 00:00:00 2001 From: Olli Vanhoja Date: Tue, 1 Nov 2016 00:07:09 +0200 Subject: [PATCH] Certs improvements (#114) * Exit cleanly if a cert entry is not found with getCertIdCn() * Support creating a new cert entry with user-provide certificate files --- bin/now-certs.js | 32 ++++++++++++++++++++++++++++---- lib/certs.js | 2 +- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/bin/now-certs.js b/bin/now-certs.js index c3425c9..4a7e69c 100755 --- a/bin/now-certs.js +++ b/bin/now-certs.js @@ -156,9 +156,24 @@ async function run(token) { return exit(1) } const cn = args[0] - const cert = await certs.create(cn) + let cert + + if (argv.crt || argv.key || argv.ca) { // Issue a custom certificate + if (!argv.crt || !argv.key) { + error(`Missing required arguments for a custom certificate entry. Usage: ${chalk.cyan('`now certs create --crt DOMAIN.CRT --key DOMAIN.KEY [--ca CA.CRT] `')}`) + return exit(1) + } + + const crt = readX509File(argv.crt) + const key = readX509File(argv.key) + const ca = argv.ca ? readX509File(argv.ca) : '' + + cert = await certs.put(cn, crt, key, ca) + } else { // Issue a standard certificate + cert = await certs.create(cn) + } const elapsed = ms(new Date() - start) - console.log(`${chalk.cyan('> Success!')} Certificate ${chalk.bold(cn)} ${chalk.gray(`(${cert.uid})`)} issued ${chalk.gray(`[${elapsed}]`)}`) + console.log(`${chalk.cyan('> Success!')} Certificate entry ${chalk.bold(cn)} ${chalk.gray(`(${cert.uid})`)} created ${chalk.gray(`[${elapsed}]`)}`) } else if (subcommand === 'renew') { if (args.length !== 1) { error(`Invalid number of arguments. Usage: ${chalk.cyan('`now certs renew `')}`) @@ -166,6 +181,9 @@ async function run(token) { } const cert = await getCertIdCn(certs, args[0]) + if (!cert) { + return exit(1) + } const yes = await readConfirmation(cert, 'The following certificate will be renewed\n') if (!yes) { @@ -187,13 +205,16 @@ async function run(token) { const ca = argv.ca ? readX509File(argv.ca) : '' const cert = await getCertIdCn(certs, args[0]) + if (!cert) { + return exit(1) + } const yes = await readConfirmation(cert, 'The following certificate will be replaced permanently\n') if (!yes) { error('User abort') return exit(0) } - await certs.replace(cert.cn, crt, key, ca) + await certs.put(cert.cn, crt, key, ca) const elapsed = ms(new Date() - start) console.log(`${chalk.cyan('> Success!')} Certificate ${chalk.bold(cert.cn)} ${chalk.gray(`(${cert.uid})`)} replaced ${chalk.gray(`[${elapsed}]`)}`) } else if (subcommand === 'rm' || subcommand === 'remove') { @@ -203,6 +224,9 @@ async function run(token) { } const cert = await getCertIdCn(certs, args[0]) + if (!cert) { + return exit(1) + } const yes = await readConfirmation(cert, 'The following certificate will be removed permanently\n') if (!yes) { error('User abort') @@ -257,7 +281,7 @@ async function getCertIdCn(certs, idOrCn) { if (!thecert) { error(`No certificate found by id or cn "${idOrCn}"`) - return exit(1) + return null } return thecert diff --git a/lib/certs.js b/lib/certs.js index c479680..2646fa9 100644 --- a/lib/certs.js +++ b/lib/certs.js @@ -28,7 +28,7 @@ export default class Certs extends Now { return this.createCert(cn, {renew: true}) } - replace(cn, crt, key, ca) { + put(cn, crt, key, ca) { return this.retry(async (bail, attempt) => { if (this._debug) { console.time(`> [debug] #${attempt} PUT now/certs`)