forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keystore.go
245 lines (219 loc) · 6.31 KB
/
keystore.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
package cmd
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"syscall"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
tml "golang.org/x/crypto/ssh/terminal"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/cmd/instance"
"github.com/elastic/beats/libbeat/common/cli"
"github.com/elastic/beats/libbeat/common/terminal"
"github.com/elastic/beats/libbeat/keystore"
)
func getKeystore(name, version string) (keystore.Keystore, error) {
b, err := instance.NewBeat(name, "", version)
if err != nil {
return nil, fmt.Errorf("error initializing beat: %s", err)
}
if err = b.Init(); err != nil {
return nil, fmt.Errorf("error initializing beat: %s", err)
}
return b.Keystore(), nil
}
// genKeystoreCmd initialize the Keystore command to manage the Keystore
// with the following subcommands:
// - create
// - add
// - remove
// - list
func genKeystoreCmd(
name, idxPrefix, version string,
beatCreator beat.Creator,
runFlags *pflag.FlagSet,
) *cobra.Command {
keystoreCmd := cobra.Command{
Use: "keystore",
Short: "Manage secrets keystore",
}
keystoreCmd.AddCommand(genCreateKeystoreCmd(name, version))
keystoreCmd.AddCommand(genAddKeystoreCmd(name, version))
keystoreCmd.AddCommand(genRemoveKeystoreCmd(name, version))
keystoreCmd.AddCommand(genListKeystoreCmd(name, version))
return &keystoreCmd
}
func genCreateKeystoreCmd(name, version string) *cobra.Command {
var flagForce bool
command := &cobra.Command{
Use: "create",
Short: "Create keystore",
Run: cli.RunWith(func(cmd *cobra.Command, args []string) error {
return createKeystore(name, version, flagForce)
}),
}
command.Flags().BoolVar(&flagForce, "force", false, "override the existing keystore")
return command
}
func genAddKeystoreCmd(name, version string) *cobra.Command {
var flagForce bool
var flagStdin bool
command := &cobra.Command{
Use: "add",
Short: "Add secret",
Run: cli.RunWith(func(cmd *cobra.Command, args []string) error {
store, err := getKeystore(name, version)
if err != nil {
return err
}
return addKey(store, args, flagForce, flagStdin)
}),
}
command.Flags().BoolVar(&flagStdin, "stdin", false, "Use the stdin as the source of the secret")
command.Flags().BoolVar(&flagForce, "force", false, "Override the existing key")
return command
}
func genRemoveKeystoreCmd(name, version string) *cobra.Command {
return &cobra.Command{
Use: "remove",
Short: "remove secret",
Run: cli.RunWith(func(cmd *cobra.Command, args []string) error {
store, err := getKeystore(name, version)
if err != nil {
return err
}
return removeKey(store, args)
}),
}
}
func genListKeystoreCmd(name, version string) *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List keystore",
Run: cli.RunWith(func(cmd *cobra.Command, args []string) error {
store, err := getKeystore(name, version)
if err != nil {
return err
}
return list(store)
}),
}
}
func createKeystore(name, version string, force bool) error {
store, err := getKeystore(name, version)
if err != nil {
return err
}
if store.IsPersisted() == true && force == false {
response := terminal.PromptYesNo("A keystore already exists, Overwrite?", true)
if response == true {
err := store.Create(true)
if err != nil {
return fmt.Errorf("error creating the keystore: %s", err)
}
} else {
fmt.Println("Exiting without creating keystore.")
return nil
}
} else {
err := store.Create(true)
if err != nil {
return fmt.Errorf("Error creating the keystore: %s", err)
}
}
fmt.Printf("Created %s keystore\n", name)
return nil
}
func addKey(store keystore.Keystore, keys []string, force, stdin bool) error {
if len(keys) == 0 {
return errors.New("failed to create the secret: no key provided")
}
if len(keys) > 1 {
return fmt.Errorf("could not create secret for: %s, you can only provide one key per invocation", keys)
}
if store.IsPersisted() == false {
if force == false {
answer := terminal.PromptYesNo("The keystore does not exist. Do you want to create it?", false)
if answer == false {
return errors.New("exiting without creating keystore")
}
}
err := store.Create(true)
if err != nil {
return fmt.Errorf("could not create keystore, error: %s", err)
}
fmt.Println("Created keystore")
}
key := strings.TrimSpace(keys[0])
value, err := store.Retrieve(key)
if value != nil && force == false {
if stdin == true {
return fmt.Errorf("the settings %s already exist in the keystore use `--force` to replace it", key)
}
answer := terminal.PromptYesNo(fmt.Sprintf("Setting %s already exists, Overwrite?", key), false)
if answer == false {
fmt.Println("Exiting without modifying keystore.")
return nil
}
}
var keyValue []byte
if stdin {
reader := bufio.NewReader(os.Stdin)
keyValue, err = ioutil.ReadAll(reader)
if err != nil {
return fmt.Errorf("could not read input from stdin")
}
} else {
fmt.Printf("Enter value for %s: ", key)
keyValue, err = tml.ReadPassword(int(syscall.Stdin))
fmt.Println()
if err != nil {
return fmt.Errorf("could not read value from the input, error: %s", err)
}
}
if err = store.Store(key, keyValue); err != nil {
return fmt.Errorf("could not add the key in the keystore, error: %s", err)
}
if err = store.Save(); err != nil {
return fmt.Errorf("fail to save the keystore: %s", err)
} else {
fmt.Println("Successfully updated the keystore")
}
return nil
}
func removeKey(store keystore.Keystore, keys []string) error {
if len(keys) == 0 {
return errors.New("you must supply at least one key to remove")
}
if store.IsPersisted() == false {
return errors.New("the keystore doesn't exist. Use the 'create' command to create one")
}
for _, key := range keys {
key = strings.TrimSpace(key)
_, err := store.Retrieve(key)
if err != nil {
return fmt.Errorf("could not find key '%v' in the keystore", key)
}
store.Delete(key)
err = store.Save()
if err != nil {
return fmt.Errorf("could not update the keystore with the changes, key: %s, error: %v", key, err)
}
fmt.Printf("successfully removed key: %s\n", key)
}
return nil
}
func list(store keystore.Keystore) error {
keys, err := store.List()
if err != nil {
return fmt.Errorf("could not read values from the keystore, error: %s", err)
}
for _, key := range keys {
fmt.Println(key)
}
return nil
}