Browse Source

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
master
Olli Vanhoja 8 years ago
committed by Tony Kovanen
parent
commit
129372d161
  1. 32
      bin/now-certs.js
  2. 2
      lib/certs.js

32
bin/now-certs.js

@ -156,9 +156,24 @@ async function run(token) {
return exit(1) return exit(1)
} }
const cn = args[0] 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] <id | cn>`')}`)
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) 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') { } else if (subcommand === 'renew') {
if (args.length !== 1) { if (args.length !== 1) {
error(`Invalid number of arguments. Usage: ${chalk.cyan('`now certs renew <id | cn>`')}`) error(`Invalid number of arguments. Usage: ${chalk.cyan('`now certs renew <id | cn>`')}`)
@ -166,6 +181,9 @@ async function run(token) {
} }
const cert = await getCertIdCn(certs, args[0]) const cert = await getCertIdCn(certs, args[0])
if (!cert) {
return exit(1)
}
const yes = await readConfirmation(cert, 'The following certificate will be renewed\n') const yes = await readConfirmation(cert, 'The following certificate will be renewed\n')
if (!yes) { if (!yes) {
@ -187,13 +205,16 @@ async function run(token) {
const ca = argv.ca ? readX509File(argv.ca) : '' const ca = argv.ca ? readX509File(argv.ca) : ''
const cert = await getCertIdCn(certs, args[0]) 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') const yes = await readConfirmation(cert, 'The following certificate will be replaced permanently\n')
if (!yes) { if (!yes) {
error('User abort') error('User abort')
return exit(0) 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) const elapsed = ms(new Date() - start)
console.log(`${chalk.cyan('> Success!')} Certificate ${chalk.bold(cert.cn)} ${chalk.gray(`(${cert.uid})`)} replaced ${chalk.gray(`[${elapsed}]`)}`) console.log(`${chalk.cyan('> Success!')} Certificate ${chalk.bold(cert.cn)} ${chalk.gray(`(${cert.uid})`)} replaced ${chalk.gray(`[${elapsed}]`)}`)
} else if (subcommand === 'rm' || subcommand === 'remove') { } else if (subcommand === 'rm' || subcommand === 'remove') {
@ -203,6 +224,9 @@ async function run(token) {
} }
const cert = await getCertIdCn(certs, args[0]) 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') const yes = await readConfirmation(cert, 'The following certificate will be removed permanently\n')
if (!yes) { if (!yes) {
error('User abort') error('User abort')
@ -257,7 +281,7 @@ async function getCertIdCn(certs, idOrCn) {
if (!thecert) { if (!thecert) {
error(`No certificate found by id or cn "${idOrCn}"`) error(`No certificate found by id or cn "${idOrCn}"`)
return exit(1) return null
} }
return thecert return thecert

2
lib/certs.js

@ -28,7 +28,7 @@ export default class Certs extends Now {
return this.createCert(cn, {renew: true}) return this.createCert(cn, {renew: true})
} }
replace(cn, crt, key, ca) { put(cn, crt, key, ca) {
return this.retry(async (bail, attempt) => { return this.retry(async (bail, attempt) => {
if (this._debug) { if (this._debug) {
console.time(`> [debug] #${attempt} PUT now/certs`) console.time(`> [debug] #${attempt} PUT now/certs`)

Loading…
Cancel
Save