forked from hashicorp/vault-plugin-secrets-kv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_config.go
199 lines (174 loc) · 5.59 KB
/
path_config.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package kv
import (
"context"
"net/http"
"path"
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
// pathConfig returns the path configuration for CRUD operations on the backend
// configuration.
func pathConfig(b *versionedKVBackend) *framework.Path {
return &framework.Path{
Pattern: "config$",
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: operationPrefixKVv2,
},
Fields: map[string]*framework.FieldSchema{
"max_versions": {
Type: framework.TypeInt,
Description: "The number of versions to keep for each key. Defaults to 10",
},
"cas_required": {
Type: framework.TypeBool,
Description: "If true, the backend will require the cas parameter to be set for each write",
},
"delete_version_after": {
Type: framework.TypeSignedDurationSecond,
Description: `
If set, the length of time before a version is deleted. A negative duration
disables the use of delete_version_after on all keys. A zero duration
clears the current setting. Accepts a Go duration format string.`,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.upgradeCheck(b.pathConfigWrite()),
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "configure",
},
Summary: "Configure backend level settings that are applied to every key in the key-value store.",
Responses: map[int][]framework.Response{
http.StatusNoContent: {{
Description: http.StatusText(http.StatusNoContent),
}},
},
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.upgradeCheck(b.pathConfigRead()),
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "read",
OperationSuffix: "configuration",
},
Summary: "Read the backend level settings.",
Responses: map[int][]framework.Response{
http.StatusOK: {{
Description: http.StatusText(http.StatusOK),
Fields: map[string]*framework.FieldSchema{
"max_versions": {
Type: framework.TypeInt,
Description: "The number of versions to keep for each key.",
Required: true,
},
"cas_required": {
Type: framework.TypeBool,
Description: "If true, the backend will require the cas parameter to be set for each write",
Required: true,
},
"delete_version_after": {
Type: framework.TypeSignedDurationSecond,
Description: "The length of time before a version is deleted.",
Required: true,
},
},
}},
},
},
},
HelpSynopsis: confHelpSyn,
HelpDescription: confHelpDesc,
}
}
// pathConfigWrite handles create and update commands to the config
func (b *versionedKVBackend) pathConfigRead() framework.OperationFunc {
return func(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
config, err := b.config(ctx, req.Storage)
if err != nil {
return nil, err
}
rdata := map[string]interface{}{
"max_versions": config.MaxVersions,
"cas_required": config.CasRequired,
}
var deleteVersionAfter time.Duration
if config.GetDeleteVersionAfter() != nil {
deleteVersionAfter, err = ptypes.Duration(config.GetDeleteVersionAfter())
if err != nil {
return nil, err
}
}
rdata["delete_version_after"] = deleteVersionAfter.String()
return &logical.Response{
Data: rdata,
}, nil
}
}
// pathConfigWrite handles create and update commands to the config
func (b *versionedKVBackend) pathConfigWrite() framework.OperationFunc {
return func(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
maxRaw, mOk := data.GetOk("max_versions")
casRaw, cOk := data.GetOk("cas_required")
dvaRaw, dvaOk := data.GetOk("delete_version_after")
// Fast path validation
if !mOk && !cOk && !dvaOk {
return nil, nil
}
config, err := b.config(ctx, req.Storage)
if err != nil {
return nil, err
}
if mOk {
config.MaxVersions = uint32(maxRaw.(int))
}
if cOk {
config.CasRequired = casRaw.(bool)
}
if dvaOk {
dva := dvaRaw.(int)
switch {
case dva < 0:
config.DisableDeleteVersionAfter()
case dva == 0:
config.ResetDeleteVersionAfter()
default:
config.DeleteVersionAfter = ptypes.DurationProto(time.Duration(dva) * time.Second)
}
}
bytes, err := proto.Marshal(config)
if err != nil {
return nil, err
}
err = req.Storage.Put(ctx, &logical.StorageEntry{
Key: path.Join(b.storagePrefix, configPath),
Value: bytes,
})
if err != nil {
return nil, err
}
b.globalConfigLock.Lock()
defer b.globalConfigLock.Unlock()
b.globalConfig = config
kvEvent(ctx, b.Backend, "config-write", configPath, configPath, true, 2)
return nil, nil
}
}
const (
confHelpSyn = `Configures settings for the KV store`
confHelpDesc = `
This path configures backend level settings that are applied to every key in the
key-value store. This parameter accepts:
* max_versions (int) - The number of versions to keep for each key. Defaults
to 10
* cas_required (bool) - If true, the backend will require the cas parameter
to be set for each write
* delete_version_after (duration) - If set, the length of time before a
version is deleted. A negative duration disables the use of
delete_version_after on all keys. A zero duration clears the current
setting. Accepts a Go duration format string.
`
)