-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcert.go
143 lines (124 loc) · 2.97 KB
/
cert.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
package commands
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"github.com/spf13/cobra"
"github.com/nanopack/portal/core"
)
// add-cert
// remove-cert
// show-certs
// set-certs
var (
certAddCmd = &cobra.Command{
Use: "add-cert",
Short: "Add cert",
Long: ``,
Run: certAdd,
}
certRemoveCmd = &cobra.Command{
Use: "remove-cert",
Short: "Remove cert",
Long: ``,
Run: certRemove,
}
certsShowCmd = &cobra.Command{
Use: "show-certs",
Short: "Show all certs",
Long: ``,
Run: certsShow,
}
certsSetCmd = &cobra.Command{
Use: "set-certs",
Short: "Set cert list",
Long: ``,
Run: certsSet,
}
certJsonString string
cert core.CertBundle
)
func init() {
certFlags(certAddCmd) // must use json string because Targets is a []string
certFlags(certsSetCmd)
certFlags(certRemoveCmd)
}
func certFlags(ccmd *cobra.Command) {
ccmd.Flags().StringVarP(&certJsonString, "json", "j", "", "Json encoded data for cert(s)")
ccmd.Flags().StringVarP(&cert.Key, "key", "k", "", "PEM style key")
ccmd.Flags().StringVarP(&cert.Cert, "cert", "C", "", "PEM style certificate")
}
func certAdd(ccmd *cobra.Command, args []string) {
if certJsonString != "" {
err := json.Unmarshal([]byte(certJsonString), &cert)
if err != nil {
fail("Bad JSON syntax")
}
}
jsonBytes, err := json.Marshal(cert)
if err != nil {
fail("Bad values for cert")
}
res, err := rest("certs", "POST", bytes.NewBuffer(jsonBytes))
if err != nil {
fail("Could not contact portal - %s", err)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
fail("Could not read portal's response - %s", err)
}
fmt.Print(string(b))
}
func certRemove(ccmd *cobra.Command, args []string) {
if certJsonString != "" {
err := json.Unmarshal([]byte(certJsonString), &cert)
if err != nil {
fail("Bad JSON syntax")
}
}
jsonBytes, err := json.Marshal(cert)
if err != nil {
fail("Bad values for cert")
}
res, err := rest("certs", "DELETE", bytes.NewBuffer(jsonBytes))
if err != nil {
fail("Could not contact portal - %s", err)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
fail("Could not read portal's response - %s", err)
}
fmt.Print(string(b))
}
func certsShow(ccmd *cobra.Command, args []string) {
res, err := rest("certs", "GET", nil)
if err != nil {
fail("Could not contact portal - %s", err)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
fail("Could not read portal's response - %s", err)
}
fmt.Print(string(b))
}
func certsSet(ccmd *cobra.Command, args []string) {
certs := []core.CertBundle{}
err := json.Unmarshal([]byte(certJsonString), &certs)
if err != nil {
fail("Bad JSON syntax")
}
jsonBytes, err := json.Marshal(certs)
if err != nil {
fail("Bad values for cert")
}
res, err := rest("certs", "PUT", bytes.NewBuffer(jsonBytes))
if err != nil {
fail("Could not contact portal - %s", err)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
fail("Could not read portal's response - %s", err)
}
fmt.Print(string(b))
}