-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcerts.go
More file actions
147 lines (113 loc) · 2.69 KB
/
certs.go
File metadata and controls
147 lines (113 loc) · 2.69 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
package cmd
import (
"fmt"
"io/ioutil"
"strings"
"github.com/deis/pkg/prettyprint"
"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
}
certMap := make(map[string]string)
nameMax := 0
expiresMax := 0
for _, cert := range certList {
certMap[cert.Name] = cert.Expires
if len(cert.Name) > nameMax {
nameMax = len(cert.Name)
}
if len(cert.Expires) > nameMax {
expiresMax = len(cert.Expires)
}
}
nameHeader := "Common Name"
expiresHeader := "Expires"
tabSpaces := 5
bufferSpaces := tabSpaces
if nameMax < len(nameHeader) {
tabSpaces += len(nameHeader) - nameMax
nameMax = len(nameHeader)
} else {
bufferSpaces += nameMax - len(nameHeader)
}
if expiresMax < len(expiresHeader) {
expiresMax = len(expiresHeader)
}
fmt.Printf("%s%s%s\n", nameHeader, strings.Repeat(" ", bufferSpaces), expiresHeader)
fmt.Printf("%s%s%s\n", strings.Repeat("-", nameMax), strings.Repeat(" ", 5),
strings.Repeat("-", expiresMax))
fmt.Print(prettyprint.PrettyTabs(certMap, tabSpaces))
return nil
}
// CertAdd adds a cert to the controller.
func CertAdd(cert, key, commonName, sans string) error {
c, err := client.New()
if err != nil {
return err
}
fmt.Print("Adding SSL endpoint... ")
quit := progress()
err = processCertsAdd(c, cert, key, commonName, sans)
quit <- true
<-quit
if err != nil {
return err
}
fmt.Println("done")
return nil
}
func processCertsAdd(c *client.Client, cert, key, commonName, sans string) error {
if sans != "" {
for _, san := range strings.Split(sans, ",") {
if err := doCertAdd(c, cert, key, san); err != nil {
return err
}
}
return nil
}
return doCertAdd(c, cert, key, commonName)
}
func doCertAdd(c *client.Client, cert string, key string, commonName 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), commonName)
return err
}
// CertRemove deletes a cert from the controller.
func CertRemove(commonName string) error {
c, err := client.New()
if err != nil {
return err
}
fmt.Printf("Removing %s... ", commonName)
quit := progress()
certs.Delete(c, commonName)
quit <- true
<-quit
if err == nil {
fmt.Println("done")
}
return err
}