-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaulter.go
306 lines (247 loc) · 7.36 KB
/
vaulter.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
package main
import (
"os"
"fmt"
"net/http"
"errors"
"strconv"
"io/ioutil"
"regexp"
"github.com/urfave/cli"
"github.com/fatih/color"
"github.com/Jeffail/gabs"
)
var result_slice []string
func status_code_handler(status_code int) error {
switch status_code {
case 200:
return nil
case 204:
return nil
case 400:
return cli.NewExitError(color.RedString("Response: Invalid request"), 3)
case 403:
return cli.NewExitError(color.RedString("Response: Forbidden"), 3)
case 404:
return cli.NewExitError(color.RedString("Response: Invalid path"), 3)
case 429:
return cli.NewExitError(color.RedString("Response: Rate limit exceeded"), 3)
case 500:
return cli.NewExitError(color.RedString("Response: Internal server error"), 3)
case 503:
return cli.NewExitError(color.RedString("Response: Vault is down"), 3)
}
return errors.New("Response: Unknown status code " + strconv.Itoa(status_code))
}
func read_http_api(url, token string) (data []byte, err error) {
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
//fmt.Println(color.YellowString(url))
req.Header.Set("X-Vault-Token", token)
res, req_err := client.Do(req)
if req_err != nil {
return []byte("0"), cli.NewExitError(color.RedString(req_err.Error()), 3)
}
resp_err := status_code_handler(res.StatusCode)
if resp_err != nil {
return []byte("0"), cli.NewExitError(resp_err.Error(), 3)
}
var body_data []byte
body_data, _ = ioutil.ReadAll(res.Body)
defer res.Body.Close()
return body_data, nil
}
func verify_connection(c *cli.Context) error {
vault_addr := c.String("vault_addr")
vault_token := c.String("vault_token")
body_data, err := read_http_api(vault_addr + "/v1/auth/token/lookup-self", vault_token)
if err != nil {
return cli.NewExitError(color.RedString(err.Error()), 3)
}
json_parsed, _ := gabs.ParseJSON(body_data)
request_id, _ := json_parsed.Path("request_id").Data().(string)
token, _ := json_parsed.Path("data.id").Data().(string)
path, _ := json_parsed.Path("data.policies").Children()
fmt.Println(color.GreenString("Lookup url: " + vault_addr + "/v1/auth/token/lookup-self"))
fmt.Println(color.GreenString("Request id: " + request_id))
fmt.Println(color.GreenString("Request token: " + token))
for _, child := range path {
fmt.Println(color.GreenString("Policies: " + child.Data().(string)))
}
return nil
}
func read_path(c *cli.Context, path string) error {
vault_addr := c.String("vault_addr")
vault_token := c.String("vault_token")
body_data, err := read_http_api(vault_addr + "/v1/" + path, vault_token)
if err != nil {
return cli.NewExitError(color.RedString(err.Error()), 3)
}
json_parsed, _ := gabs.ParseJSON(body_data)
resp, _ := json_parsed.Search("data").ChildrenMap()
for key, child := range resp {
fmt.Println(color.BlueString(key) + ":" ,child.Data().(string))
}
return nil
}
func make_tree(c *cli.Context, path string, is_silent bool) (err error, return_path []string) {
vault_addr := c.String("vault_addr")
vault_token := c.String("vault_token")
body_data, err := read_http_api(vault_addr + "/v1/" + path + "?list=true", vault_token)
if err != nil {
return cli.NewExitError(color.RedString(err.Error()), 3), nil
}
json_parsed, _ := gabs.ParseJSON(body_data)
path_obj, _ := json_parsed.Path("data.keys").Children()
for _, child := range path_obj {
//fmt.Println(color.GreenString("Keys: " + child.Data().(string)))
new_path := path + child.Data().(string)
if string(new_path[len(new_path)-1:]) != "/" {
//make_tree(c, path + "/" + child.Data().(string))
//fmt.Println(string(path[len(path)-1:]))
result_slice = append(result_slice, new_path)
if is_silent == false {
fmt.Println(color.BlueString(new_path))
}
} else {
//fmt.Println("Folder: " + new_path + " ---- " + string(path[len(path)-1:]))
make_tree(c, path + child.Data().(string), is_silent)
//fmt.Println(string(path[len(path)-1:]))
}
}
return nil, result_slice
}
func make_search(c *cli.Context, backend string, regular_exp string) (err error, return_path []string) {
err, path_slice := make_tree(c, backend, true)
for _, link := range path_slice {
ok, err := regexp.MatchString(regular_exp, link)
if ok {
fmt.Println(color.YellowString("\n" + link))
read_path(c, link)
} else if err != nil {
return cli.NewExitError(color.RedString(err.Error()), 3), nil
}
}
return nil, nil
}
func main() {
app := cli.NewApp()
app.Name = "Vaulter"
app.Version = "0.0.1"
app.Usage = "friendliest console client for vault!"
app.Commands = []cli.Command{
{
Name: "verify",
Aliases: []string{"v"},
Usage: "verify connection to vault",
Action: func(c *cli.Context) error {
err := verify_connection(c)
if err != nil {
return cli.NewExitError(err.Error(), 3)
}
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "vault_addr, addr, a",
Value: "http://localhost:8200",
Usage: "connection for vault",
EnvVar: "VAULT_ADDR",
}, cli.StringFlag{
Name: "vault_token, token, t",
Usage: "vault token",
EnvVar: "VAULT_TOKEN",
},
},
},
{
Name: "search",
Aliases: []string{"t"},
Usage: "search for regex pattern",
Action: func(c *cli.Context) error {
backend := c.Args().Get(0)
regular_exp := c.Args().Get(1)
if backend == "" {
return cli.NewExitError(color.RedString("Please, enter vault backend"), 3)
}
if regular_exp == "" {
return cli.NewExitError(color.RedString("Please, enter regular expression"), 3)
}
if backend[len(backend)-1:] != "/" {
backend += "/"
}
err, _ := make_search(c, backend, regular_exp)
if err != nil {
return cli.NewExitError(err.Error(), 3)
}
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "vault_addr, addr, a",
Value: "http://localhost:8200",
Usage: "connection for vault",
EnvVar: "VAULT_ADDR",
}, cli.StringFlag{
Name: "vault_token, token, t",
Usage: "vault token",
EnvVar: "VAULT_TOKEN",
},
},
}, {
Name: "read",
Aliases: []string{"t"},
Usage: "read from location",
Action: func(c *cli.Context) error {
err := read_path(c, c.Args().Get(0))
if err != nil {
return cli.NewExitError(err.Error(), 3)
}
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "vault_addr, addr, a",
Value: "http://localhost:8200",
Usage: "connection for vault",
EnvVar: "VAULT_ADDR",
}, cli.StringFlag{
Name: "vault_token, token, t",
Usage: "vault token",
EnvVar: "VAULT_TOKEN",
},
},
}, {
Name: "tree",
Aliases: []string{"t"},
Usage: "show all vault secrets",
Action: func(c *cli.Context) error {
path := c.Args().Get(0)
if path == "" {
return cli.NewExitError(color.RedString("Please, enter vault path"), 3)
}
if path[len(path)-1:] != "/" {
path += "/"
}
err, _ := make_tree(c, path, false)
if err != nil {
return cli.NewExitError(err.Error(), 3)
}
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "vault_addr, addr, a",
Value: "http://localhost:8200",
Usage: "connection for vault",
EnvVar: "VAULT_ADDR",
}, cli.StringFlag{
Name: "vault_token, token, t",
Usage: "vault token",
EnvVar: "VAULT_TOKEN",
},
},
},
}
app.Run(os.Args)
}