-
Notifications
You must be signed in to change notification settings - Fork 2
/
mount.go
241 lines (209 loc) · 6.58 KB
/
mount.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
package vaultkv
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
)
const (
//MountTypeGeneric is what the key value backend was called prior to 0.8.0
MountTypeGeneric = "generic"
//MountTypeKV is the type string to get a Key Value backend
MountTypeKV = "kv"
)
//Mount represents a backend mounted at a point in Vault.
type Mount struct {
//The type of mount at this point
Type string
Description string
Config *MountConfig
Options map[string]interface{}
}
//MountConfig specifies configuration options given when initializing a backend.
type MountConfig struct {
DefaultLeaseTTL time.Duration
MaxLeaseTTL time.Duration
PluginName string
ForceNoCache bool
}
type mountListAPI struct {
Type string `json:"type"`
Description string `json:"description"`
Config mountConfigListAPI `json:"config"`
Options map[string]interface{} `json:"options"`
}
func (m mountListAPI) Parse() Mount {
return Mount{
Type: m.Type,
Description: m.Description,
Config: m.Config.Parse(),
Options: m.Options,
}
}
type mountConfigListAPI struct {
//time in seconds
DefaultLeaseTTL int `json:"default_lease_ttl"`
MaxLeaseTTL int `json:"max_lease_ttl"`
PluginName string `json:"plugin_name"`
ForceNoCache bool `json:"force_no_cache"`
}
func (m mountConfigListAPI) Parse() *MountConfig {
return &MountConfig{
DefaultLeaseTTL: time.Duration(m.DefaultLeaseTTL) * time.Second,
MaxLeaseTTL: time.Duration(m.MaxLeaseTTL) * time.Second,
PluginName: m.PluginName,
ForceNoCache: m.ForceNoCache,
}
}
type mountConfigEnableAPI struct {
DefaultLeaseTTL string `json:"default_lease_ttl,omitempty"`
MaxLeaseTTL string `json:"max_lease_ttl,omitempty"`
PluginName string `json:"plugin_name,omitempty"`
ForceNoCache bool `json:"force_no_cache,omitempty"`
}
func newMountConfigEnableAPI(conf *MountConfig) *mountConfigEnableAPI {
if conf == nil {
return nil
}
return &mountConfigEnableAPI{
DefaultLeaseTTL: func() string {
if conf.DefaultLeaseTTL == 0 {
return ""
}
return conf.DefaultLeaseTTL.String()
}(),
MaxLeaseTTL: func() string {
if conf.MaxLeaseTTL == 0 {
return ""
}
return conf.DefaultLeaseTTL.String()
}(),
PluginName: conf.PluginName,
ForceNoCache: conf.ForceNoCache,
}
}
//ListMounts queries the Vault backend for a list of active mounts that can
// be seen with the current authentication token. It is returned as a map
// of mount points to mount information.
func (c *Client) ListMounts() (map[string]Mount, error) {
output := map[string]interface{}{}
//Prior to 1.10, the mount names were top level keys. Then, they duplicated the
// information into "data" with other metadata in the top level keys. So we need
// to check if the data key is there (and isn't just a mount name)
err := c.doRequest("GET", "/sys/mounts", nil, &output)
if err != nil {
return nil, err
}
var mounts map[string]mountListAPI
if dataKey, ok := output["data"]; ok {
mounts = getMountList(dataKey)
}
if mounts == nil {
mounts := getMountList(output)
if mounts == nil {
return nil, fmt.Errorf("Could not parse mount list")
}
}
ret := map[string]Mount{}
for k, v := range mounts {
ret[strings.TrimRight(k, "/")] = v.Parse()
}
return ret, err
}
func getMountList(candidate interface{}) map[string]mountListAPI {
//check if data key is not a mount name
b, err := json.Marshal(&candidate)
if err != nil {
return nil
}
tmpOutput := map[string]mountListAPI{}
err = json.Unmarshal(b, &tmpOutput)
if err != nil {
return nil
}
return tmpOutput
}
//KVMountOptions is a map[string]interface{} that can be given as the options
//when mounting a backend. It has Version manipulation functions to make life
//easier.
type KVMountOptions map[string]interface{}
//GetVersion retruns the version held in the KVMountOptions object
func (o KVMountOptions) GetVersion() int {
if o == nil {
return 1
}
version, hasExplicitVersion := o["version"]
if !hasExplicitVersion {
return 1
}
vStr := version.(string)
ret, _ := strconv.Atoi(vStr)
return ret
}
//WithVersion returns a new KVMountOptions object with the given version
func (o KVMountOptions) WithVersion(version int) KVMountOptions {
if o == nil {
o = make(map[string]interface{}, 1)
}
o["version"] = strconv.Itoa(version)
return o
}
//EnableSecretsMount mounts a secrets backend at the given path, configured with
// the given Mount configuration.
func (c *Client) EnableSecretsMount(path string, config Mount) error {
input := struct {
Type string `json:"type"`
Description string `json:"description"`
Config *mountConfigEnableAPI `json:"config,omitempty"`
Options interface{} `json:"options,omitempty"`
}{
Type: config.Type,
Description: config.Description,
Config: newMountConfigEnableAPI(config.Config),
Options: config.Options,
}
return c.doRequest("POST", fmt.Sprintf("/sys/mounts/%s", path), &input, nil)
}
//DisableSecretsMount deletes the mount at the given path.
func (c *Client) DisableSecretsMount(path string) error {
return c.doRequest("DELETE", fmt.Sprintf("/sys/mounts/%s", path), nil, nil)
}
//TuneMountOptions are parameters to be sent to the Vault when editing the
// configuration of a mount. Only non-empty values will be sent.
type TuneMountOptions struct {
Description string
DefaultLeaseTTL time.Duration
MaxLeaseTTL time.Duration
Options map[string]interface{}
}
//TuneSecretsMount updates the configuration of the mount at the given path.
func (c *Client) TuneSecretsMount(path string, opts TuneMountOptions) error {
rawTuneMountOptions := struct {
Description string `json:"description,omitempty"`
DefaultLeaseTTL int `json:"default_lease_ttl,omitempty"`
MaxLeaseTTL int `json:"max_lease_ttl,omitempty"`
Options map[string]interface{} `json:"options,omitempty"`
}{
Description: opts.Description,
DefaultLeaseTTL: int(opts.DefaultLeaseTTL / time.Second),
MaxLeaseTTL: int(opts.MaxLeaseTTL / time.Second),
Options: opts.Options,
}
return c.doRequest("POST",
fmt.Sprintf("/sys/mounts/%s/tune", path),
rawTuneMountOptions,
nil,
)
}
//UpgradeKVToV2 sets the version of the mount (presumably KV mount) to the
// version 2. Just a shorthand wrapper for TuneSecretsMount with the
// appropriate opts structure.
func (c *Client) UpgradeKVToV2(path string) error {
return c.TuneSecretsMount(
path,
TuneMountOptions{
Options: KVMountOptions{}.WithVersion(2),
},
)
}