-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcerts.go
More file actions
193 lines (159 loc) · 4.78 KB
/
certs.go
File metadata and controls
193 lines (159 loc) · 4.78 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
package cmd
import (
"fmt"
"os"
"strings"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/certs"
dtime "github.com/drycc/controller-sdk-go/pkg/time"
)
const dateFormat = "2 Jan 2006"
// CertsList lists certs registered with the controller.
func (d *DryccCmd) CertsList(appID string, results int) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
if results == defaultLimit {
results = s.Limit
}
certList, _, err := certs.List(s.Client, appID, results)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
if len(certList) == 0 {
d.Println("No certs")
} else {
table := d.getDefaultFormatTable([]string{"NAME", "COMMON-NAME", "EXPIRES", "SAN", "DOMAINS"})
for _, cert := range certList {
expires := safeGetString("")
if cert.Expires.Time != nil {
expires = cert.Expires.Format(dateFormat)
}
san := safeGetString(strings.Join(cert.SubjectAltName[:], ","))
if len(san) > 32 {
san = fmt.Sprintf("%s[...]", san[:32])
}
domains := safeGetString(strings.Join(cert.Domains[:], ","))
if len(domains) > 32 {
domains = fmt.Sprintf("%s[...]", domains[:32])
}
table.Append([]string{cert.Name, cert.CommonName, expires, san, domains})
}
table.Render()
}
return nil
}
// CertAdd adds a cert to the controller.
func (d *DryccCmd) CertAdd(appID string, cert string, key string, name string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
d.Print("Adding SSL endpoint... ")
quit := progress(d.WOut)
err = d.doCertAdd(s.Client, appID, cert, key, name)
quit <- true
<-quit
if err != nil {
return err
}
d.Println("done")
return nil
}
func (d *DryccCmd) doCertAdd(c *drycc.Client, appID string, cert string, key string, name string) error {
certFile, err := os.ReadFile(cert)
if err != nil {
return err
}
keyFile, err := os.ReadFile(key)
if err != nil {
return err
}
_, err = certs.New(c, appID, string(certFile), string(keyFile), name)
return d.checkAPICompatibility(c, err)
}
// CertRemove deletes a cert from the controller.
func (d *DryccCmd) CertRemove(appID string, name string) error {
s, appID, err := load(d.ConfigFile, appID)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Printf("Removing %s... ", name)
quit := progress(d.WOut)
err = certs.Delete(s.Client, appID, name)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Println("done")
return nil
}
// CertInfo gets info about certficiate
func (d *DryccCmd) CertInfo(appID string, name string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
cert, err := certs.Get(s.Client, appID, name)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
table := d.getDefaultFormatTable([]string{})
table.Append([]string{"Name:", safeGetString(cert.Name)})
table.Append([]string{"Common Name(s):", safeGetString(cert.CommonName)})
table.Append([]string{"Expires At:", safeGetTime(cert.Expires, dateFormat)})
table.Append([]string{"Starts At:", safeGetTime(cert.Starts, dateFormat)})
table.Append([]string{"Fingerprint:", safeGetString(cert.Fingerprint)})
table.Append([]string{"Subject Alt Name:", safeGetString(strings.Join(cert.SubjectAltName[:], ","))})
table.Append([]string{"Issuer:", safeGetString(cert.Issuer)})
table.Append([]string{"Subject:", safeGetString(cert.Subject)})
table.Append([]string{""})
table.Append([]string{"Connected Domains:", safeGetString(strings.Join(cert.Domains[:], ","))})
table.Append([]string{"Owner:", safeGetString(cert.Owner)})
table.Append([]string{"Created:", d.formatTime(cert.Created)})
table.Append([]string{"Updated:", d.formatTime(cert.Updated)})
table.Render()
return nil
}
// CertAttach attaches a certificate to a domain
func (d *DryccCmd) CertAttach(appID string, name, domain string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
d.Printf("Attaching certificate %s to domain %s... ", name, domain)
quit := progress(d.WOut)
err = certs.Attach(s.Client, appID, name, domain)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) == nil {
d.Println("done")
}
return err
}
// CertDetach detaches a certificate from a domain
func (d *DryccCmd) CertDetach(appID, name, domain string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
d.Printf("Detaching certificate %s from domain %s... ", name, domain)
quit := progress(d.WOut)
err = certs.Detach(s.Client, appID, name, domain)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Println("done")
return nil
}
func safeGetTime(t dtime.Time, format string) string {
out := ""
if t.Time != nil {
out = t.Format(format)
}
return safeGetString(out)
}