This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
ssl.go
195 lines (158 loc) · 4.42 KB
/
ssl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/heroku/hk/Godeps/_workspace/src/github.com/bgentry/heroku-go"
)
var cmdSSL = &Command{
Run: runSSL,
Usage: "ssl",
NeedsApp: true,
Category: "ssl",
Short: "show ssl endpoint info",
Long: `Show SSL endpoint and certificate information.`,
}
func runSSL(cmd *Command, args []string) {
if len(args) != 0 {
cmd.PrintUsage()
os.Exit(2)
}
endpoints, err := client.SSLEndpointList(mustApp(), nil)
must(err)
if len(endpoints) == 0 {
return
}
chain, err := decodeCertChain(endpoints[0].CertificateChain)
must(err)
fmt.Println("Hostname: ", endpoints[0].Cname)
fmt.Println("Common Name(s): ", strings.Join(chain.CommonNames(), ", "))
fmt.Println("Expires: ", chain.Expires().UTC().Format(time.RFC3339))
}
var cmdSSLCertAdd = &Command{
Run: runSSLCertAdd,
Usage: "ssl-cert-add [-s] <certfile> <keyfile>",
NeedsApp: true,
Category: "ssl",
Short: "add a new ssl cert",
Long: `
Add a new SSL certificate to an app. An SSL endpoint will be
created if the app doesn't yet have one. Otherwise, its cert will
be updated.
Options:
-s skip SSL cert optimization and pre-processing
Examples:
$ hk ssl-cert-add cert.pem key.pem
hobby-dev $0/mo
`,
}
var (
skipCertPreprocess bool
)
func init() {
cmdSSLCertAdd.Flag.BoolVarP(&skipCertPreprocess, "skip-preprocess", "s", false, "skip SSL cert preprocessing")
}
func runSSLCertAdd(cmd *Command, args []string) {
if len(args) != 2 {
cmd.PrintUsage()
os.Exit(2)
}
appname := mustApp()
certb, err := ioutil.ReadFile(args[0])
if err != nil {
printFatal("reading certfile: %s", err.Error())
}
keyb, err := ioutil.ReadFile(args[1])
if err != nil {
printFatal("reading keyfile: %s", err.Error())
}
endpoints, err := client.SSLEndpointList(appname, nil)
must(err)
cert := string(certb)
key := string(keyb)
preprocess := !skipCertPreprocess
if len(endpoints) == 0 {
opts := heroku.SSLEndpointCreateOpts{Preprocess: &preprocess}
ep, err := client.SSLEndpointCreate(appname, cert, key, &opts)
must(err)
fmt.Printf("Added cert for %s at %s.\n", appname, ep.Cname)
return
}
opts := heroku.SSLEndpointUpdateOpts{
CertificateChain: &cert,
Preprocess: &preprocess,
PrivateKey: &key,
}
_, err = client.SSLEndpointUpdate(appname, endpoints[0].Id, &opts)
must(err)
fmt.Printf("Updated cert for %s at %s.\n", appname, endpoints[0].Cname)
}
var cmdSSLDestroy = &Command{
Run: runSSLDestroy,
Usage: "ssl-destroy",
NeedsApp: true,
Category: "ssl",
Short: "destroy ssl endpoint",
Long: `
Removes the SSL endpoints from an app along with all SSL
certificates. If your app's DNS is still configured to point at
the SSL endpoint, this may take your app offline. The command
will prompt for confirmation, or accept confirmation via stdin.
Examples:
$ hk ssl-destroy
warning: This will destroy the SSL endpoint on myapp. Please type "myapp" to continue:
> myapp
Destroyed SSL endpoint on myapp.
$ echo myapp | hk ssl-destroy
Destroyed SSL endpoint on myapp.
`,
}
func runSSLDestroy(cmd *Command, args []string) {
if len(args) != 0 {
cmd.PrintUsage()
os.Exit(2)
}
appname := mustApp()
endpoints, err := client.SSLEndpointList(appname, nil)
must(err)
if len(endpoints) == 0 {
printFatal("App %s has no SSL endpoint to destroy.", appname)
}
warning := "This will destroy the SSL endpoint on %s. Please type %q to continue:"
mustConfirm(fmt.Sprintf(warning, appname, appname), appname)
err = client.SSLEndpointDelete(appname, endpoints[0].Id)
must(err)
fmt.Printf("Destroyed SSL endpoint on %s.\n", appname)
}
var cmdSSLCertRollback = &Command{
Run: runSSLCertRollback,
Usage: "ssl-cert-rollback",
NeedsApp: true,
Category: "ssl",
Short: "add a new ssl cert",
Long: `
Rolls back an SSL endpoint's certificate to the previous version.
Examples:
$ hk ssl-cert-rollback
Rolled back cert for myapp.
`,
}
func runSSLCertRollback(cmd *Command, args []string) {
if len(args) != 0 {
cmd.PrintUsage()
os.Exit(2)
}
appname := mustApp()
endpoints, err := client.SSLEndpointList(appname, nil)
must(err)
if len(endpoints) == 0 {
printFatal("App %s has no SSL endpoint to rollback.", appname)
}
t := true
opts := heroku.SSLEndpointUpdateOpts{Rollback: &t}
_, err = client.SSLEndpointUpdate(appname, endpoints[0].Id, &opts)
must(err)
fmt.Printf("Rolled back cert for %s.\n", appname)
}