forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_map.go
283 lines (242 loc) · 6.93 KB
/
path_map.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
package framework
import (
"context"
"fmt"
"strings"
"sync"
saltpkg "github.com/hashicorp/vault/helper/salt"
"github.com/hashicorp/vault/logical"
)
// PathMap can be used to generate a path that stores mappings in the
// storage. It is a structure that also exports functions for querying the
// mappings.
//
// The primary use case for this is for credential providers to do their
// mapping to policies.
type PathMap struct {
Prefix string
Name string
Schema map[string]*FieldSchema
CaseSensitive bool
Salt *saltpkg.Salt
SaltFunc func(context.Context) (*saltpkg.Salt, error)
once sync.Once
}
func (p *PathMap) init() {
if p.Prefix == "" {
p.Prefix = "map"
}
if p.Schema == nil {
p.Schema = map[string]*FieldSchema{
"value": &FieldSchema{
Type: TypeString,
Description: fmt.Sprintf("Value for %s mapping", p.Name),
},
}
}
}
// pathStruct returns the pathStruct for this mapping
func (p *PathMap) pathStruct(ctx context.Context, s logical.Storage, k string) (*PathStruct, error) {
p.once.Do(p.init)
// If we don't care about casing, store everything lowercase
if !p.CaseSensitive {
k = strings.ToLower(k)
}
// The original key before any salting
origKey := k
// If we have a salt, apply it before lookup
salt := p.Salt
var err error
if p.SaltFunc != nil {
salt, err = p.SaltFunc(ctx)
if err != nil {
return nil, err
}
}
if salt != nil {
k = "s" + salt.SaltIDHashFunc(k, saltpkg.SHA256Hash)
}
finalName := fmt.Sprintf("map/%s/%s", p.Name, k)
ps := &PathStruct{
Name: finalName,
Schema: p.Schema,
}
if !strings.HasPrefix(origKey, "s") && k != origKey {
// Ensure that no matter what happens what is returned is the final
// path
defer func() {
ps.Name = finalName
}()
//
// Check for unsalted version and upgrade if so
//
// Generate the unsalted name
unsaltedName := fmt.Sprintf("map/%s/%s", p.Name, origKey)
// Set the path struct to use the unsalted name
ps.Name = unsaltedName
val, err := ps.Get(ctx, s)
if err != nil {
return nil, err
}
// If not nil, we have an unsalted entry -- upgrade it
if val != nil {
// Set the path struct to use the desired final name
ps.Name = finalName
err = ps.Put(ctx, s, val)
if err != nil {
return nil, err
}
// Set it back to the old path and delete
ps.Name = unsaltedName
err = ps.Delete(ctx, s)
if err != nil {
return nil, err
}
// We'll set this in the deferred function but doesn't hurt here
ps.Name = finalName
}
//
// Check for SHA1 hashed version and upgrade if so
//
// Generate the SHA1 hash suffixed path name
sha1SuffixedName := fmt.Sprintf("map/%s/%s", p.Name, salt.SaltID(origKey))
// Set the path struct to use the SHA1 hash suffixed path name
ps.Name = sha1SuffixedName
val, err = ps.Get(ctx, s)
if err != nil {
return nil, err
}
// If not nil, we have an SHA1 hash suffixed entry -- upgrade it
if val != nil {
// Set the path struct to use the desired final name
ps.Name = finalName
err = ps.Put(ctx, s, val)
if err != nil {
return nil, err
}
// Set it back to the old path and delete
ps.Name = sha1SuffixedName
err = ps.Delete(ctx, s)
if err != nil {
return nil, err
}
// We'll set this in the deferred function but doesn't hurt here
ps.Name = finalName
}
}
return ps, nil
}
// Get reads a value out of the mapping
func (p *PathMap) Get(ctx context.Context, s logical.Storage, k string) (map[string]interface{}, error) {
ps, err := p.pathStruct(ctx, s, k)
if err != nil {
return nil, err
}
return ps.Get(ctx, s)
}
// Put writes a value into the mapping
func (p *PathMap) Put(ctx context.Context, s logical.Storage, k string, v map[string]interface{}) error {
ps, err := p.pathStruct(ctx, s, k)
if err != nil {
return err
}
return ps.Put(ctx, s, v)
}
// Delete removes a value from the mapping
func (p *PathMap) Delete(ctx context.Context, s logical.Storage, k string) error {
ps, err := p.pathStruct(ctx, s, k)
if err != nil {
return err
}
return ps.Delete(ctx, s)
}
// List reads the keys under a given path
func (p *PathMap) List(ctx context.Context, s logical.Storage, prefix string) ([]string, error) {
stripPrefix := fmt.Sprintf("struct/map/%s/", p.Name)
fullPrefix := fmt.Sprintf("%s%s", stripPrefix, prefix)
out, err := s.List(ctx, fullPrefix)
if err != nil {
return nil, err
}
stripped := make([]string, len(out))
for idx, k := range out {
stripped[idx] = strings.TrimPrefix(k, stripPrefix)
}
return stripped, nil
}
// Paths are the paths to append to the Backend paths.
func (p *PathMap) Paths() []*Path {
p.once.Do(p.init)
// Build the schema by simply adding the "key"
schema := make(map[string]*FieldSchema)
for k, v := range p.Schema {
schema[k] = v
}
schema["key"] = &FieldSchema{
Type: TypeString,
Description: fmt.Sprintf("Key for the %s mapping", p.Name),
}
return []*Path{
&Path{
Pattern: fmt.Sprintf("%s/%s/?$", p.Prefix, p.Name),
Callbacks: map[logical.Operation]OperationFunc{
logical.ListOperation: p.pathList(),
logical.ReadOperation: p.pathList(),
},
HelpSynopsis: fmt.Sprintf("Read mappings for %s", p.Name),
},
&Path{
Pattern: fmt.Sprintf(`%s/%s/(?P<key>[-\w]+)`, p.Prefix, p.Name),
Fields: schema,
Callbacks: map[logical.Operation]OperationFunc{
logical.CreateOperation: p.pathSingleWrite(),
logical.ReadOperation: p.pathSingleRead(),
logical.UpdateOperation: p.pathSingleWrite(),
logical.DeleteOperation: p.pathSingleDelete(),
},
HelpSynopsis: fmt.Sprintf("Read/write/delete a single %s mapping", p.Name),
ExistenceCheck: p.pathSingleExistenceCheck(),
},
}
}
func (p *PathMap) pathList() OperationFunc {
return func(ctx context.Context, req *logical.Request, d *FieldData) (*logical.Response, error) {
keys, err := p.List(ctx, req.Storage, "")
if err != nil {
return nil, err
}
return logical.ListResponse(keys), nil
}
}
func (p *PathMap) pathSingleRead() OperationFunc {
return func(ctx context.Context, req *logical.Request, d *FieldData) (*logical.Response, error) {
v, err := p.Get(ctx, req.Storage, d.Get("key").(string))
if err != nil {
return nil, err
}
return &logical.Response{
Data: v,
}, nil
}
}
func (p *PathMap) pathSingleWrite() OperationFunc {
return func(ctx context.Context, req *logical.Request, d *FieldData) (*logical.Response, error) {
err := p.Put(ctx, req.Storage, d.Get("key").(string), d.Raw)
return nil, err
}
}
func (p *PathMap) pathSingleDelete() OperationFunc {
return func(ctx context.Context, req *logical.Request, d *FieldData) (*logical.Response, error) {
err := p.Delete(ctx, req.Storage, d.Get("key").(string))
return nil, err
}
}
func (p *PathMap) pathSingleExistenceCheck() ExistenceFunc {
return func(ctx context.Context, req *logical.Request, d *FieldData) (bool, error) {
v, err := p.Get(ctx, req.Storage, d.Get("key").(string))
if err != nil {
return false, err
}
return v != nil, nil
}
}