This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
kv.go
80 lines (68 loc) · 1.99 KB
/
kv.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
package vaultapi
import (
"fmt"
"sort"
"github.com/pkg/errors"
)
// A KV represents the key-value store built into vault.
//
// Although vault supports arbitrary bytes as keys and values,
// this library assumes all keys and values are strings. This
// helps simplify code for clients, the 99% use case for which
// is writing secret passwords and other stringy information
// into vault for safe keeping.
type KV interface {
// Get will return the value defined at path.
Get(path string) (string, error)
// Put will set value at path.
Put(path, value string) error
// Delete will remove the value at path.
Delete(path string) error
// Keys will list all of the subpaths under path in asciibetical
// order. The returned paths may be terminal (ie, the value is
// stored content) or they may traversable like a directory.
Keys(path string) ([]string, error)
}
var (
// ErrNoValue indicates that no value exists for a requested path.
ErrNoValue = errors.New("no value defined for given path")
)
func (c *client) Get(path string) (string, error) {
fullpath := fixup("/v1/secret", path, [2]string{"list", "false"})
var data keyData
err := c.get(fullpath, &data)
if err != nil {
return "", err
}
value, exists := data.Data["value"]
if !exists {
return "", ErrNoValue
}
return value, nil
}
func (c *client) Put(path, value string) error {
fullpath := fixup("/v1/secret", path, [2]string{})
body := fmt.Sprintf(`{%q:%q}`, "value", value)
return c.post(fullpath, body, nil)
}
func (c *client) Delete(path string) error {
fullpath := fixup("/v1/secret", path, [2]string{})
return c.delete(fullpath)
}
func (c *client) Keys(path string) ([]string, error) {
fullpath := fixup("/v1/secret", path, [2]string{"list", "true"})
var data keysData
err := c.get(fullpath, &data)
if err != nil {
return nil, err
}
keys := data.Data["keys"]
sort.Strings(keys)
return keys, nil
}
type keyData struct {
Data map[string]string `json:"data"`
}
type keysData struct {
Data map[string][]string `json:"data"`
}