-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcerts.go
More file actions
240 lines (189 loc) · 4.8 KB
/
certs.go
File metadata and controls
240 lines (189 loc) · 4.8 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package cmd
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/olekukonko/tablewriter"
"github.com/deis/workflow/client/controller/client"
"github.com/deis/workflow/client/controller/models/certs"
)
// CertsList lists certs registered with the controller.
func CertsList(results int) error {
c, err := client.New()
if err != nil {
return err
}
if results == defaultLimit {
results = c.ResponseLimit
}
certList, _, err := certs.List(c, results)
if err != nil {
return err
}
if len(certList) == 0 {
fmt.Println("No certs")
return nil
}
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetBorder(false)
table.SetAutoFormatHeaders(false)
table.SetHeaderLine(true)
table.SetHeader([]string{"Name", "Common Name", "SubjectAltName", "Expires", "Fingerprint", "Domains", "Updated", "Created"})
for _, cert := range certList {
domains := strings.Join(cert.Domains[:], ",")
san := strings.Join(cert.SubjectAltName[:], ",")
// Make dates more readable
now := time.Now()
expires := cert.Expires.Time.Format("2 Jan 2006")
created := cert.Created.Time.Format("2 Jan 2006")
updated := cert.Updated.Time.Format("2 Jan 2006")
if cert.Expires.Time.Before(now) {
expires += " (expired)"
} else {
// Ghetto solution
expires += " (in"
year := cert.Expires.Time.Year() - now.Year()
month := cert.Expires.Time.Month() - now.Month()
day := cert.Expires.Time.Day() - now.Day()
if year > 0 {
expires += fmt.Sprintf(" %d year", year)
if year > 1 {
expires += "s"
}
} else if month > 0 {
expires += fmt.Sprintf(" %d month", month)
if month > 1 {
expires += "s"
}
} else if day != 0 {
// special handling on negative days
if day < 0 {
day *= -1
}
expires += fmt.Sprintf(" %d day", day)
if day > 1 {
expires += "s"
}
}
expires += ")"
}
// show a shorter version of the fingerprint
fingerprint := cert.Fingerprint[:5] + "[...]" + cert.Fingerprint[len(cert.Fingerprint)-5:]
table.Append([]string{cert.Name, cert.CommonName, san, expires, fingerprint, domains, updated, created})
}
table.Render()
return nil
}
// CertAdd adds a cert to the controller.
func CertAdd(cert string, key string, name string) error {
c, err := client.New()
if err != nil {
return err
}
fmt.Print("Adding SSL endpoint... ")
quit := progress()
err = doCertAdd(c, cert, key, name)
quit <- true
<-quit
if err != nil {
return err
}
fmt.Println("done")
return nil
}
func doCertAdd(c *client.Client, cert string, key string, name string) error {
certFile, err := ioutil.ReadFile(cert)
if err != nil {
return err
}
keyFile, err := ioutil.ReadFile(key)
if err != nil {
return err
}
_, err = certs.New(c, string(certFile), string(keyFile), name)
return err
}
// CertRemove deletes a cert from the controller.
func CertRemove(name string) error {
c, err := client.New()
if err != nil {
return err
}
fmt.Printf("Removing %s... ", name)
quit := progress()
certs.Delete(c, name)
quit <- true
<-quit
if err == nil {
fmt.Println("done")
}
return err
}
// CertInfo gets info about certficiate
func CertInfo(name string) error {
c, err := client.New()
if err != nil {
return err
}
cert, err := certs.Get(c, name)
if err != nil {
return err
}
domains := strings.Join(cert.Domains[:], ",")
if domains == "" {
domains = "No connected domains"
}
san := strings.Join(cert.SubjectAltName[:], ",")
if san == "" {
san = "N/A"
}
fmt.Printf("=== %s Certificate\n", cert.Name)
fmt.Println("Common Name(s): ", cert.CommonName)
fmt.Println("Expires At: ", cert.Expires)
fmt.Println("Starts At: ", cert.Starts)
fmt.Println("Fingerprint: ", cert.Fingerprint)
fmt.Println("Subject Alt Name: ", san)
fmt.Println("Issuer: ", cert.Issuer)
fmt.Println("Subject: ", cert.Subject)
fmt.Println()
fmt.Println("Connected Domains: ", domains)
fmt.Println("Owner: ", cert.Owner)
fmt.Println("Created: ", cert.Created)
fmt.Println("Updated: ", cert.Updated)
return nil
}
// CertAttach attaches a certificate to a domain
func CertAttach(name string, domain string) error {
c, err := client.New()
if err != nil {
return err
}
fmt.Printf("Attaching certificate %s to domain %s... ", name, domain)
quit := progress()
certs.Attach(c, name, domain)
quit <- true
<-quit
if err == nil {
fmt.Println("done")
}
return err
}
// CertDetach detaches a certificate from a domain
func CertDetach(name string, domain string) error {
c, err := client.New()
if err != nil {
return err
}
fmt.Printf("Detaching certificate %s from domain %s... ", name, domain)
quit := progress()
certs.Detach(c, name, domain)
quit <- true
<-quit
if err == nil {
fmt.Println("done")
}
return err
}