Skip to content

Commit 2f41ca0

Browse files
author
Joshua Anderson
committed
feat(client-go): add certs endpoint
1 parent d332f73 commit 2f41ca0

6 files changed

Lines changed: 530 additions & 0 deletions

File tree

client-go/cmd/certs.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"strings"
7+
8+
"github.com/deis/deis/pkg/prettyprint"
9+
10+
"github.com/deis/deis/client-go/controller/client"
11+
"github.com/deis/deis/client-go/controller/models/certs"
12+
)
13+
14+
// CertsList lists certs registered with the controller.
15+
func CertsList() error {
16+
c, err := client.New()
17+
18+
if err != nil {
19+
return err
20+
}
21+
22+
certList, err := certs.List(c)
23+
24+
if err != nil {
25+
return err
26+
}
27+
28+
if len(certList) == 0 {
29+
fmt.Println("No certs")
30+
return nil
31+
}
32+
33+
certMap := make(map[string]string)
34+
nameMax := 0
35+
expiresMax := 0
36+
for _, cert := range certList {
37+
certMap[cert.Name] = cert.Expires
38+
39+
if len(cert.Name) > nameMax {
40+
nameMax = len(cert.Name)
41+
}
42+
if len(cert.Expires) > nameMax {
43+
expiresMax = len(cert.Expires)
44+
}
45+
}
46+
47+
nameHeader := "Common Name"
48+
expiresHeader := "Expires"
49+
tabSpaces := 5
50+
bufferSpaces := tabSpaces
51+
52+
if nameMax < len(nameHeader) {
53+
tabSpaces += len(nameHeader) - nameMax
54+
nameMax = len(nameHeader)
55+
} else {
56+
bufferSpaces += nameMax - len(nameHeader)
57+
}
58+
59+
if expiresMax < len(expiresHeader) {
60+
expiresMax = len(expiresHeader)
61+
}
62+
63+
fmt.Printf("%s%s%s\n", nameHeader, strings.Repeat(" ", bufferSpaces), expiresHeader)
64+
fmt.Printf("%s%s%s\n", strings.Repeat("-", nameMax), strings.Repeat(" ", 5),
65+
strings.Repeat("-", expiresMax))
66+
fmt.Print(prettyprint.PrettyTabs(certMap, tabSpaces))
67+
return nil
68+
}
69+
70+
// CertAdd adds a cert to the controller.
71+
func CertAdd(cert, key, commonName, sans string) error {
72+
c, err := client.New()
73+
74+
if err != nil {
75+
return err
76+
}
77+
78+
fmt.Print("Adding SSL endpoint... ")
79+
quit := progress()
80+
err = processCertsAdd(c, cert, key, commonName, sans)
81+
quit <- true
82+
<-quit
83+
84+
if err != nil {
85+
return err
86+
}
87+
88+
fmt.Println("done")
89+
return nil
90+
}
91+
92+
func processCertsAdd(c *client.Client, cert, key, commonName, sans string) error {
93+
if sans != "" {
94+
for _, san := range strings.Split(sans, ",") {
95+
if err := doCertAdd(c, cert, key, san); err != nil {
96+
return err
97+
}
98+
}
99+
return nil
100+
}
101+
102+
return doCertAdd(c, cert, key, commonName)
103+
}
104+
105+
func doCertAdd(c *client.Client, cert string, key string, commonName string) error {
106+
certFile, err := ioutil.ReadFile(cert)
107+
108+
if err != nil {
109+
return err
110+
}
111+
112+
keyFile, err := ioutil.ReadFile(key)
113+
114+
if err != nil {
115+
return err
116+
}
117+
118+
_, err = certs.New(c, string(certFile), string(keyFile), commonName)
119+
return err
120+
}
121+
122+
// CertRemove deletes a cert from the controller.
123+
func CertRemove(commonName string) error {
124+
c, err := client.New()
125+
126+
if err != nil {
127+
return err
128+
}
129+
130+
fmt.Printf("Removing %s... ", commonName)
131+
quit := progress()
132+
133+
certs.Delete(c, commonName)
134+
135+
quit <- true
136+
<-quit
137+
138+
if err == nil {
139+
fmt.Println("done")
140+
}
141+
142+
return err
143+
}

client-go/controller/api/certs.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package api
2+
3+
// Cert is the definition of the cert object.
4+
// Some fields are omtempty because they are only
5+
// returned when creating or getting a cert.
6+
type Cert struct {
7+
Updated string `json:"updated,omitempty"`
8+
Created string `json:"created,omitempty"`
9+
Name string `json:"common_name"`
10+
Expires string `json:"expires"`
11+
Owner string `json:"owner,omitempty"`
12+
ID int `json:"id,omitempty"`
13+
}
14+
15+
// Certs is the definition of GET /v1/certs/.
16+
type Certs struct {
17+
Count int `json:"count"`
18+
Next int `json:"next"`
19+
Previous int `json:"previous"`
20+
Certs []Cert `json:"results"`
21+
}
22+
23+
// CertCreateRequest is the definition of POST /v1/certs/.
24+
type CertCreateRequest struct {
25+
Certificate string `json:"certificate"`
26+
Key string `json:"key"`
27+
Name string `json:"common_name,omitempty"`
28+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package certs
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
8+
"github.com/deis/deis/client-go/controller/api"
9+
"github.com/deis/deis/client-go/controller/client"
10+
)
11+
12+
// List certs registered with the controller.
13+
func List(c *client.Client) ([]api.Cert, error) {
14+
body, status, err := c.BasicRequest("GET", "/v1/certs/", nil)
15+
16+
if err != nil {
17+
return []api.Cert{}, err
18+
}
19+
20+
if status != 200 {
21+
return []api.Cert{}, errors.New(body)
22+
}
23+
24+
res := api.Certs{}
25+
if err = json.Unmarshal([]byte(body), &res); err != nil {
26+
return []api.Cert{}, err
27+
}
28+
29+
return res.Certs, nil
30+
}
31+
32+
// New creates a new cert.
33+
func New(c *client.Client, cert string, key string, commonName string) (api.Cert, error) {
34+
req := api.CertCreateRequest{Certificate: cert, Key: key, Name: commonName}
35+
reqBody, err := json.Marshal(req)
36+
37+
if err != nil {
38+
return api.Cert{}, err
39+
}
40+
41+
resBody, status, err := c.BasicRequest("POST", "/v1/certs/", reqBody)
42+
43+
if err != nil {
44+
return api.Cert{}, err
45+
}
46+
47+
if status != 201 {
48+
return api.Cert{}, errors.New(resBody)
49+
}
50+
51+
resCert := api.Cert{}
52+
if err = json.Unmarshal([]byte(resBody), &resCert); err != nil {
53+
return api.Cert{}, err
54+
}
55+
56+
return resCert, nil
57+
}
58+
59+
// Delete removes a cert.
60+
func Delete(c *client.Client, commonName string) error {
61+
u := fmt.Sprintf("/v1/certs/%s", commonName)
62+
63+
resBody, status, err := c.BasicRequest("DELETE", u, nil)
64+
65+
if err != nil {
66+
return err
67+
}
68+
69+
if status != 204 {
70+
return errors.New(resBody)
71+
}
72+
73+
return nil
74+
}

0 commit comments

Comments
 (0)