/
path_subkeys.go
220 lines (186 loc) · 6.39 KB
/
path_subkeys.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package kv
import (
"context"
"encoding/json"
"errors"
"net/http"
"reflect"
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/locksutil"
"github.com/hashicorp/vault/sdk/logical"
)
func pathSubkeys(b *versionedKVBackend) *framework.Path {
return &framework.Path{
Pattern: "subkeys/" + framework.MatchAllRegex("path"),
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: operationPrefixKVv2,
OperationVerb: "read",
OperationSuffix: "subkeys",
},
Fields: map[string]*framework.FieldSchema{
"path": {
Type: framework.TypeString,
Description: "Location of the secret.",
},
"depth": {
Type: framework.TypeInt,
Description: "The maximum depth to traverse. No limit will be imposed if not provided or if 0.",
},
"version": {
Type: framework.TypeInt,
Description: "Specifies which version to retrieve. If not provided, the current version will be used.",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.upgradeCheck(b.pathSubkeysRead()),
Responses: map[int][]framework.Response{
http.StatusOK: {{
Description: http.StatusText(http.StatusOK),
Fields: map[string]*framework.FieldSchema{
"subkeys": {
Type: framework.TypeMap,
Required: true,
},
"metadata": {
Type: framework.TypeMap,
Required: true,
},
},
}},
},
},
},
HelpSynopsis: subkeysHelpSyn,
HelpDescription: subkeysHelpDesc,
}
}
// removeValues recursively walks the provided secret data represented as a
// map. All leaf nodes (i.e. empty maps and non-map values) will be replaced
// with nil in an effort to remove all values. The resulting structure will
// provide all subkeys with nesting fully intact. The modifications are made
// to the input in-place. maxDepth will denote how deep to traverse. A maxDepth
// of 0 is the equivalent of no limit.
func removeValues(input map[string]interface{}, maxDepth int) {
var walk func(interface{}, int)
walk = func(in interface{}, depth int) {
val := reflect.ValueOf(in)
if val.IsValid() && val.Kind() == reflect.Map {
for _, k := range val.MapKeys() {
v := val.MapIndex(k)
if v.IsValid() {
m := in.(map[string]interface{})
switch t := v.Interface().(type) {
case map[string]interface{}:
// Only continue walking if we have not reached max depth
// and the underlying map has at least 1 key. The key is
// otherwise treated as a leaf node and thus set to nil.
// Setting to nil if the max depth is reached is crucial in
// that it prevents leaking secret data as the input map is
// being modified in-place
if currentDepth := depth + 1; (maxDepth == 0 || currentDepth <= maxDepth) && len(t) > 0 {
walk(t, currentDepth)
} else {
m[k.String()] = nil
}
default:
m[k.String()] = nil
}
}
}
}
}
walk(input, 1)
}
// pathSubkeysRead handles ReadOperation requests for a specified path. Subkeys
// that exist within the entry specified by the provided path will be retrieved.
// This is done by stripping the secret data by replacing all underlying values of
// leaf keys with null.
func (b *versionedKVBackend) pathSubkeysRead() framework.OperationFunc {
return func(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
key := data.Get("path").(string)
depth := data.Get("depth").(int)
lock := locksutil.LockForKey(b.locks, key)
lock.RLock()
defer lock.RUnlock()
meta, err := b.getKeyMetadata(ctx, req.Storage, key)
if err != nil {
return nil, err
}
if meta == nil {
return nil, nil
}
versionNum := meta.CurrentVersion
versionParam := data.Get("version").(int)
if versionParam > 0 {
versionNum = uint64(versionParam)
}
versionMetadata := meta.Versions[versionNum]
if versionMetadata == nil {
return nil, nil
}
resp := &logical.Response{
Data: map[string]interface{}{
"subkeys": nil,
"metadata": map[string]interface{}{
"version": versionNum,
"created_time": ptypesTimestampToString(versionMetadata.CreatedTime),
"deletion_time": ptypesTimestampToString(versionMetadata.DeletionTime),
"destroyed": versionMetadata.Destroyed,
"custom_metadata": meta.CustomMetadata,
},
},
}
if versionMetadata.DeletionTime != nil {
deletionTime, err := ptypes.Timestamp(versionMetadata.DeletionTime)
if err != nil {
return nil, err
}
if deletionTime.Before(time.Now()) {
return logical.RespondWithStatusCode(resp, req, http.StatusNotFound)
}
}
if versionMetadata.Destroyed {
return logical.RespondWithStatusCode(resp, req, http.StatusNotFound)
}
versionKey, err := b.getVersionKey(ctx, key, versionNum, req.Storage)
if err != nil {
return nil, err
}
raw, err := req.Storage.Get(ctx, versionKey)
if err != nil {
return nil, err
}
if raw == nil {
return nil, errors.New("could not find version data")
}
version := &Version{}
if err := proto.Unmarshal(raw.Value, version); err != nil {
return nil, err
}
versionData := map[string]interface{}{}
if err := json.Unmarshal(version.Data, &versionData); err != nil {
return nil, err
}
removeValues(versionData, depth)
resp.Data["subkeys"] = versionData
return resp, nil
}
}
const subkeysHelpSyn = `Read the structure of a secret entry from the Key-Value store with the values removed.`
const subkeysHelpDesc = `
This endpoint provides the subkeys within a secret entry that exists at the requested path.
The secret entry at this path will be retrieved and stripped of all data by replacing
underlying values of leaf keys (i.e. non-map keys or map keys with no underlying subkeys) with null.
The "version" parameter specifies which version of the secret to read when
generating the subkeys structure. If not provided, the current version will be used.
The "depth" parameter specifies the deepest nesting level to provide in the output.
The default value 0 will not impose any limit. If non-zero, keys that reside at the
specified depth value will be artificially treated as leaves and will thus be null
even if further underlying subkeys exist.
`