/
config_trust.go
227 lines (183 loc) · 4.83 KB
/
config_trust.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
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
package main
import (
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"os"
"sort"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
cli "github.com/lxc/lxd/shared/cmd"
"github.com/lxc/lxd/shared/i18n"
)
type cmdConfigTrust struct {
global *cmdGlobal
config *cmdConfig
profile *cmdProfile
}
func (c *cmdConfigTrust) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = i18n.G("trust")
cmd.Short = i18n.G("Manage trusted clients")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Manage trusted clients`))
// Add
configTrustAddCmd := cmdConfigTrustAdd{global: c.global, config: c.config, configTrust: c}
cmd.AddCommand(configTrustAddCmd.Command())
// List
configTrustListCmd := cmdConfigTrustList{global: c.global, config: c.config, configTrust: c}
cmd.AddCommand(configTrustListCmd.Command())
// Remove
configTrustRemoveCmd := cmdConfigTrustRemove{global: c.global, config: c.config, configTrust: c}
cmd.AddCommand(configTrustRemoveCmd.Command())
return cmd
}
// Add
type cmdConfigTrustAdd struct {
global *cmdGlobal
config *cmdConfig
configTrust *cmdConfigTrust
}
func (c *cmdConfigTrustAdd) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = i18n.G("add [<remote>:] <cert>")
cmd.Short = i18n.G("Add new trusted clients")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Add new trusted clients`))
cmd.RunE = c.Run
return cmd
}
func (c *cmdConfigTrustAdd) Run(cmd *cobra.Command, args []string) error {
// Sanity checks
exit, err := c.global.CheckArgs(cmd, args, 1, 2)
if exit {
return err
}
// Parse remote
remote := ""
if len(args) > 0 {
remote = args[0]
}
resources, err := c.global.ParseServers(remote)
if err != nil {
return err
}
resource := resources[0]
// Add trust relationship
fname := args[len(args)-1]
x509Cert, err := shared.ReadCert(shared.HostPath(fname))
if err != nil {
return err
}
name, _ := shared.SplitExt(fname)
cert := api.CertificatesPost{}
cert.Certificate = base64.StdEncoding.EncodeToString(x509Cert.Raw)
cert.Name = name
cert.Type = "client"
return resource.server.CreateCertificate(cert)
}
// List
type cmdConfigTrustList struct {
global *cmdGlobal
config *cmdConfig
configTrust *cmdConfigTrust
}
func (c *cmdConfigTrustList) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = i18n.G("list [<remote>:]")
cmd.Aliases = []string{"ls"}
cmd.Short = i18n.G("List trusted clients")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`List trusted clients`))
cmd.RunE = c.Run
return cmd
}
func (c *cmdConfigTrustList) Run(cmd *cobra.Command, args []string) error {
// Sanity checks
exit, err := c.global.CheckArgs(cmd, args, 0, 1)
if exit {
return err
}
// Parse remote
remote := ""
if len(args) > 0 {
remote = args[0]
}
resources, err := c.global.ParseServers(remote)
if err != nil {
return err
}
resource := resources[0]
// List trust relationships
trust, err := resource.server.GetCertificates()
if err != nil {
return err
}
data := [][]string{}
for _, cert := range trust {
fp := cert.Fingerprint[0:12]
certBlock, _ := pem.Decode([]byte(cert.Certificate))
if certBlock == nil {
return fmt.Errorf(i18n.G("Invalid certificate"))
}
cert, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
return err
}
const layout = "Jan 2, 2006 at 3:04pm (MST)"
issue := cert.NotBefore.Format(layout)
expiry := cert.NotAfter.Format(layout)
data = append(data, []string{fp, cert.Subject.CommonName, issue, expiry})
}
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetRowLine(true)
table.SetHeader([]string{
i18n.G("FINGERPRINT"),
i18n.G("COMMON NAME"),
i18n.G("ISSUE DATE"),
i18n.G("EXPIRY DATE")})
sort.Sort(stringList(data))
table.AppendBulk(data)
table.Render()
return nil
}
// Remove
type cmdConfigTrustRemove struct {
global *cmdGlobal
config *cmdConfig
configTrust *cmdConfigTrust
}
func (c *cmdConfigTrustRemove) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = i18n.G("remove [<remote>:] <hostname|fingerprint>")
cmd.Aliases = []string{"rm"}
cmd.Short = i18n.G("Remove trusted clients")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Remove trusted clients`))
cmd.RunE = c.Run
return cmd
}
func (c *cmdConfigTrustRemove) Run(cmd *cobra.Command, args []string) error {
// Sanity checks
exit, err := c.global.CheckArgs(cmd, args, 1, 2)
if exit {
return err
}
// Parse remote
remote := ""
if len(args) > 0 {
remote = args[0]
}
resources, err := c.global.ParseServers(remote)
if err != nil {
return err
}
resource := resources[0]
// Remove trust relationship
return resource.server.DeleteCertificate(args[len(args)-1])
}