1 | 'use strict'
|
2 |
|
3 | let co = require('co')
|
4 | let cli = require('heroku-cli-util')
|
5 |
|
6 | let flags = require('../../lib/flags.js')
|
7 | let readFile = require('../../lib/read_file.js')
|
8 | let sslDoctor = require('../../lib/ssl_doctor.js')
|
9 | let displayWarnings = require('../../lib/display_warnings.js')
|
10 | let formatEndpoint = require('../../lib/format_endpoint.js')
|
11 | let certificateDetails = require('../../lib/certificate_details.js')
|
12 |
|
13 | function * run (context, heroku) {
|
14 | let endpoint = yield flags(context, heroku)
|
15 |
|
16 | let files = yield {
|
17 | crt: readFile(context.args.CRT, 'utf-8'),
|
18 | key: readFile(context.args.KEY, 'utf-8')
|
19 | }
|
20 |
|
21 | let crt, key
|
22 | if (context.flags.bypass) {
|
23 | crt = files.crt
|
24 | key = files.key
|
25 | } else {
|
26 | let res = JSON.parse(yield sslDoctor('resolve-chain-and-key', [files.crt, files.key]))
|
27 | crt = res.pem
|
28 | key = res.key
|
29 | }
|
30 |
|
31 | let formattedEndpoint = formatEndpoint(endpoint)
|
32 |
|
33 | yield cli.confirmApp(context.app, context.flags.confirm, `Potentially Destructive Action\nThis command will change the certificate of endpoint ${formattedEndpoint} from ${cli.color.app(context.app)}.`)
|
34 |
|
35 | let cert = yield cli.action(`Updating SSL certificate ${formattedEndpoint} for ${cli.color.app(context.app)}`, {}, heroku.request({
|
36 | path: endpoint._meta.path,
|
37 | method: 'PATCH',
|
38 | headers: {'Accept': `application/vnd.heroku+json; version=3.${endpoint._meta.variant}`},
|
39 | body: {certificate_chain: crt, private_key: key}
|
40 | }))
|
41 |
|
42 | certificateDetails(cert, 'Updated certificate details:')
|
43 | displayWarnings(cert)
|
44 | }
|
45 |
|
46 | module.exports = {
|
47 | topic: 'certs',
|
48 | command: 'update',
|
49 | args: [
|
50 | {name: 'CRT', optional: false},
|
51 | {name: 'KEY', optional: false}
|
52 | ],
|
53 | flags: [
|
54 | {name: 'bypass', description: 'bypass the trust chain completion step', hasValue: false},
|
55 | {name: 'confirm', hasValue: true, hidden: true},
|
56 | {name: 'name', hasValue: true, description: 'name to update'},
|
57 | {name: 'endpoint', hasValue: true, description: 'endpoint to update'}
|
58 | ],
|
59 | description: 'update an SSL certificate on an app',
|
60 | help: `Example:
|
61 |
|
62 | $ heroku certs:update example.com.crt example.com.key
|
63 | `,
|
64 | needsApp: true,
|
65 | needsAuth: true,
|
66 | run: cli.command({preauth: true}, co.wrap(run))
|
67 | }
|