-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.go
435 lines (342 loc) · 8.85 KB
/
cli.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package main
import (
"bytes"
"fmt"
"os"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
type App struct {
cfg struct {
configPath string
flagKind string
mountTimeout int
}
// entry point
Execute func() error
}
func NewApp() *App {
a := &App{}
// root
rootCmd := &cobra.Command{
Use: "scum",
Short: "Secret Credentials Utility/Manager",
}
rootCmd.PersistentFlags().StringVarP(&a.cfg.configPath, "config", "c", os.ExpandEnv("$HOME/.config/scum/config.yml"), "Configuration file for scum")
a.Execute = rootCmd.Execute
// types
typesCmd := &cobra.Command{
Use: "types",
Short: "Show information about the supported credential types",
Run: a.typesCmd,
}
rootCmd.AddCommand(typesCmd)
// list
listCmd := &cobra.Command{
Use: "list",
Short: "List credential",
Run: a.listCmd,
}
rootCmd.AddCommand(listCmd)
// add
addCmd := &cobra.Command{
Use: "add",
Short: "Add a new set of credential",
Run: a.addCmd,
}
addCmd.PersistentFlags().StringVarP(&a.cfg.flagKind, "type", "t", "aws", "Profile type")
rootCmd.AddCommand(addCmd)
// edit
editCmd := &cobra.Command{
Use: "edit",
Short: "Edit an existing set of credential in $EDITOR",
Run: a.editCmd,
Args: cobra.MinimumNArgs(1),
}
rootCmd.AddCommand(editCmd)
// show
showCmd := &cobra.Command{
Use: "show",
Short: "Show set of credential",
Args: cobra.MinimumNArgs(1),
Run: a.showCmd,
}
rootCmd.AddCommand(showCmd)
// mount
mountCmd := &cobra.Command{
Use: "mount",
Short: "Mount a set of credential",
Args: cobra.MinimumNArgs(1),
Run: a.mountCmd,
}
mountCmd.PersistentFlags().IntVar(&a.cfg.mountTimeout, "timeout", a.cfg.mountTimeout, "Timeout of the mount in seconds")
rootCmd.AddCommand(mountCmd)
// rotate
rotateCmd := &cobra.Command{
Use: "rotate",
Short: "Rotate credential",
Args: cobra.MinimumNArgs(1),
Run: a.rotateCmd,
}
rootCmd.AddCommand(rotateCmd)
// verify
verifyCmd := &cobra.Command{
Use: "verify",
Short: "Verify a set of credential",
Args: cobra.MinimumNArgs(1),
Run: a.verifyCmd,
}
rootCmd.AddCommand(verifyCmd)
// config
configCmd := &cobra.Command{
Use: "config",
Short: "Print configuration",
Run: a.configCmd,
}
rootCmd.AddCommand(configCmd)
// version
versionCmd := &cobra.Command{
Use: "version",
Short: "Print version info",
Run: a.versionCmd,
}
rootCmd.AddCommand(versionCmd)
return a
}
func (a *App) typesCmd(cmd *cobra.Command, args []string) {
for _, name := range ptr.List() {
d, err := ptr.Describe(name)
exitOnErr(err)
fmt.Printf("Profile Type \033[1m'%s'\033[0m\n\n", name)
fmt.Printf("%s\n\n", d)
}
}
func (a *App) configCmd(cmd *cobra.Command, args []string) {
cfg, err := NewConfig(a.cfg.configPath)
exitOnErr(err)
d, err := yaml.Marshal(&cfg)
exitOnErr(err)
fmt.Printf("---\n%s\n", string(d))
}
func (a *App) addCmd(cmd *cobra.Command, args []string) {
cfg, err := NewConfig(a.cfg.configPath)
exitOnErr(err)
c, err := NewCrypt(cfg.PublicRSAKey, cfg.PrivateRSAKey)
exitOnErr(err)
p, err := NewProfile(a.cfg.flagKind)
exitOnErr(err)
err = p.Prompt()
exitOnErr(err)
serialized, err := p.Serialize()
exitOnErr(err)
encrypted, err := c.Encrypt(serialized)
exitOnErr(err)
b, err := NewBag(cfg.BagPath)
exitOnErr(err)
err = b.Write(p.Name(), p.Type(), encrypted)
exitOnErr(err)
}
func (a *App) listCmd(cmd *cobra.Command, args []string) {
cfg, err := NewConfig(a.cfg.configPath)
exitOnErr(err)
b, err := NewBag(cfg.BagPath)
exitOnErr(err)
list, err := b.List(args)
exitOnErr(err)
if len(list) == 0 {
fmt.Println("No matches found")
}
for name, kind := range list {
fmt.Printf("%s (type %s)\n", name, kind)
}
}
func (a *App) showCmd(cmd *cobra.Command, args []string) {
cfg, err := NewConfig(a.cfg.configPath)
exitOnErr(err)
c, err := NewCrypt(cfg.PublicRSAKey, cfg.PrivateRSAKey)
exitOnErr(err)
b, err := NewBag(cfg.BagPath)
exitOnErr(err)
list, err := b.List(args)
exitOnErr(err)
var pw []byte
if len(list) > 0 {
pw, err = promptPassword(cfg.PrivateRSAKey, os.Stderr)
exitOnErr(err)
} else {
fmt.Println("No matches found")
return
}
for name, kind := range list {
p, err := NewProfile(kind)
exitOnErr(err)
encrypted, err := b.Read(name, kind)
exitOnErr(err)
data, err := c.Decrypt(encrypted, pw)
exitOnErr(err)
err = p.Deserialize(data)
exitOnErr(err)
fmt.Println(p)
}
}
func (a *App) editCmd(cmd *cobra.Command, args []string) {
cfg, err := NewConfig(a.cfg.configPath)
exitOnErr(err)
c, err := NewCrypt(cfg.PublicRSAKey, cfg.PrivateRSAKey)
exitOnErr(err)
b, err := NewBag(cfg.BagPath)
exitOnErr(err)
list, err := b.List(args)
exitOnErr(err)
var pw []byte
if len(list) > 0 {
pw, err = promptPassword(cfg.PrivateRSAKey, os.Stderr)
exitOnErr(err)
} else {
fmt.Println("No matches found")
return
}
for name, kind := range list {
encrypted, err := b.Read(name, kind)
exitOnErr(err)
data, err := c.Decrypt(encrypted, pw)
exitOnErr(err)
edited, err := CaptureInputFromEditor(data)
exitOnErr(err)
if bytes.Equal(data, edited) {
fmt.Printf("nothing changed, done!\n")
continue
}
newEncrypted, err := c.Encrypt(edited)
exitOnErr(err)
err = b.Write(name, kind, newEncrypted)
exitOnErr(err)
fmt.Printf("done!\n")
}
}
func (a *App) mountCmd(cmd *cobra.Command, args []string) {
cfg, err := NewConfig(a.cfg.configPath)
exitOnErr(err)
c, err := NewCrypt(cfg.PublicRSAKey, cfg.PrivateRSAKey)
exitOnErr(err)
b, err := NewBag(cfg.BagPath)
exitOnErr(err)
list, err := b.List(args)
exitOnErr(err)
var pw []byte
if len(list) > 0 {
pw, err = promptPassword(cfg.PrivateRSAKey, os.Stderr)
exitOnErr(err)
} else {
fmt.Println("No matches found")
return
}
mountFiles := map[string][]byte{}
for name, kind := range list {
p, err := NewProfile(kind)
exitOnErr(err)
if !p.Capabilities().Mount {
fmt.Printf("Profile '%s' cannot be mounted because its of kind %s which does not support mount. Skipping...\n", name, kind)
continue
}
encrypted, err := b.Read(name, kind)
exitOnErr(err)
data, err := c.Decrypt(encrypted, pw)
exitOnErr(err)
err = p.Deserialize(data)
exitOnErr(err)
mountPath, mountSnippet := p.MountSnippet()
mountData := append(mountFiles[mountPath], []byte(mountSnippet)...)
mountFiles[mountPath] = mountData
}
fmt.Printf("Mounting credentials at %s\n", cfg.Mountpoint)
err = mount(cfg.Mountpoint, mountFiles, a.cfg.mountTimeout, cfg.Debug)
exitOnErr(err)
}
func (a *App) verifyCmd(cmd *cobra.Command, args []string) {
cfg, err := NewConfig(a.cfg.configPath)
exitOnErr(err)
c, err := NewCrypt(cfg.PublicRSAKey, cfg.PrivateRSAKey)
exitOnErr(err)
b, err := NewBag(cfg.BagPath)
exitOnErr(err)
list, err := b.List(args)
exitOnErr(err)
var pw []byte
if len(list) > 0 {
pw, err = promptPassword(cfg.PrivateRSAKey, os.Stderr)
exitOnErr(err)
} else {
fmt.Println("No matches found")
return
}
for name, kind := range list {
p, err := NewProfile(kind)
exitOnErr(err)
if !p.Capabilities().Verify {
fmt.Printf("Profile '%s' cannot be verified because its of kind %s which does not support verification. Skipping...\n", name, kind)
continue
}
encrypted, err := b.Read(name, kind)
exitOnErr(err)
data, err := c.Decrypt(encrypted, pw)
exitOnErr(err)
err = p.Deserialize(data)
exitOnErr(err)
getUnicode := func(b bool) string {
if b {
return "✔"
}
return "✘"
}
out, ok := p.VerifyCredentials()
fmt.Printf("%s\t%s (type %s), message: %s\n", getUnicode(ok), p.Name(), p.Type(), out)
}
}
func (a *App) rotateCmd(cmd *cobra.Command, args []string) {
cfg, err := NewConfig(a.cfg.configPath)
exitOnErr(err)
c, err := NewCrypt(cfg.PublicRSAKey, cfg.PrivateRSAKey)
exitOnErr(err)
b, err := NewBag(cfg.BagPath)
exitOnErr(err)
list, err := b.List(args)
exitOnErr(err)
var pw []byte
if len(list) > 0 {
fmt.Printf("The following credentials are going to be rotated:\n")
for name, kind := range list {
fmt.Printf("\t%s (type %s)\n", name, kind)
}
pw, err = promptPassword(cfg.PrivateRSAKey, os.Stderr)
exitOnErr(err)
} else {
fmt.Println("No matches found")
return
}
for name, kind := range list {
p, err := NewProfile(kind)
exitOnErr(err)
if !p.Capabilities().Rotate {
fmt.Printf("Profile '%s' cannot be rotated because its of kind %s which does not support key rotation. Skipping...\n", name, kind)
continue
}
fmt.Printf("Rotating %s (type %s)... ", name, p.Type())
encrypted, err := b.Read(name, kind)
exitOnErr(err)
data, err := c.Decrypt(encrypted, pw)
exitOnErr(err)
err = p.Deserialize(data)
exitOnErr(err)
newSerialized, err := p.RotateCredentials()
exitOnErr(err)
newEncrypted, err := c.Encrypt(newSerialized)
exitOnErr(err)
err = b.Write(p.Name(), p.Type(), newEncrypted)
exitOnErr(err)
fmt.Printf("done!\n")
}
}
func (a *App) versionCmd(cmd *cobra.Command, args []string) {
fmt.Println(versionInfo())
}