/
path_subkeys_test.go
596 lines (509 loc) · 15.4 KB
/
path_subkeys_test.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package kv
import (
"context"
"encoding/json"
"net/http"
"testing"
"github.com/go-test/deep"
"github.com/hashicorp/vault/sdk/helper/testhelpers/schema"
"github.com/hashicorp/vault/sdk/logical"
)
// TestVersionedKV_Subkeys_NotFound verifies that a nil logical.Response is
// returned when an entry that does not exist is requested
func TestVersionedKV_Subkeys_NotFound(t *testing.T) {
b, storage := getBackend(t)
req := &logical.Request{
Operation: logical.ReadOperation,
Path: "subkeys/foo",
Storage: storage,
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || resp != nil {
t.Fatalf("unexpected ReadOperation response, err: %v, resp %#v", err, resp)
}
}
// TestVersionedKV_Subkeys_CurrentVersion verifies that the current
// version of an entry is read if the version param is not provided
func TestVersionedKV_Subkeys_CurrentVersion(t *testing.T) {
b, storage := getBackend(t)
data := map[string]interface{}{
"data": map[string]interface{}{
"foo": "does-not-matter",
"bar": map[string]interface{}{
"a": map[string]interface{}{
"c": map[string]interface{}{
"d": "does-not-matter",
},
},
"b": map[string]interface{}{},
},
"baz": map[string]interface{}{
"e": 3.14,
},
"quux": 123,
"quuz": []string{"does-not-matter"},
},
}
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "data/foo",
Storage: storage,
Data: data,
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("CreateOperation request failed, err: %v, resp %#v", err, resp)
}
req = &logical.Request{
Operation: logical.ReadOperation,
Path: "subkeys/foo",
Storage: storage,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("ReadOperation request failed, err: %v, resp %#v", err, resp)
}
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*versionedKVBackend).Route(req.Path), req.Operation),
resp,
true,
)
expectedRespKeys := map[string]struct{}{
"subkeys": {},
"metadata": {},
}
if diff := deep.Equal(getKeySet(resp.Data), expectedRespKeys); len(diff) > 0 {
t.Fatalf("expected top-level resp keys mismatch, diff: %#v", diff)
}
metadata, ok := resp.Data["metadata"].(map[string]interface{})
if !ok {
t.Fatalf("expected metadata to be map, actual: %#v", metadata)
}
if diff := deep.Equal(getKeySet(metadata), expectedMetadataKeys()); len(diff) > 0 {
t.Fatalf("metadata map keys mismatch, diff: %#v", diff)
}
expectedSubkeys := map[string]interface{}{
"foo": nil,
"bar": map[string]interface{}{
"a": map[string]interface{}{
"c": map[string]interface{}{
"d": nil,
},
},
"b": nil,
},
"baz": map[string]interface{}{
"e": nil,
},
"quux": nil,
"quuz": nil,
}
if diff := deep.Equal(resp.Data["subkeys"], expectedSubkeys); len(diff) > 0 {
t.Fatalf("resp and expected data mismatch, diff: %#v", diff)
}
}
// TestVersionedKV_Subkeys_VersionParam verifies that the correct
// version is read when the version flag is provided
func TestVersionedKV_Subkeys_VersionParam(t *testing.T) {
b, storage := getBackend(t)
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "data/foo",
Storage: storage,
Data: map[string]interface{}{
"data": map[string]interface{}{
"foo": "abc",
},
},
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("data CreateOperation request failed, err: %v, resp %#v", err, resp)
}
req = &logical.Request{
Operation: logical.CreateOperation,
Path: "data/foo",
Storage: storage,
Data: map[string]interface{}{
"data": map[string]interface{}{
"foo": "abc",
"bar": "def",
},
},
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("data CreateOperation request failed, err: %v, resp %#v", err, resp)
}
req = &logical.Request{
Operation: logical.ReadOperation,
Path: "subkeys/foo",
Storage: storage,
Data: map[string]interface{}{
"version": 1,
},
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("subkeys ReadOperation request failed, err: %v, resp %#v", err, resp)
}
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*versionedKVBackend).Route(req.Path), req.Operation),
resp,
true,
)
expectedSubkeys := map[string]interface{}{
"foo": nil,
}
if diff := deep.Equal(resp.Data["subkeys"], expectedSubkeys); len(diff) > 0 {
t.Fatalf("resp and expected data mismatch, diff: %#v", diff)
}
}
// TestVersionedKV_Subkeys_VersionParamDoesNotExist verifies that a nil
// logical.Response is returned if the requested version does not exist
func TestVersionedKV_Subkeys_VersionParamDoesNotExist(t *testing.T) {
b, storage := getBackend(t)
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "data/foo",
Storage: storage,
Data: map[string]interface{}{
"data": map[string]interface{}{
"foo": "abc",
},
},
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("data CreateOperation request failed, err: %v, resp %#v", err, resp)
}
req = &logical.Request{
Operation: logical.CreateOperation,
Path: "data/foo",
Storage: storage,
Data: map[string]interface{}{
"data": map[string]interface{}{
"foo": "abc",
"bar": "def",
},
},
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("data CreateOperation request failed, err: %v, resp %#v", err, resp)
}
req = &logical.Request{
Operation: logical.ReadOperation,
Path: "subkeys/foo",
Storage: storage,
Data: map[string]interface{}{
"version": 10,
},
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || resp != nil {
t.Fatalf("unexpected subkeys ReadOperation response, err: %v, resp %#v", err, resp)
}
}
// TestVersionedKV_Subkeys_DepthParam verifies that the depth param
// handling is correct. Failure to parse the value will result in an
// error response. No limit will be imposed if the param is not
// provided or its value is 0.
func TestVersionedKV_Subkeys_DepthParam(t *testing.T) {
cases := []struct {
name string
depth interface{}
expected map[string]interface{}
expectErr bool
}{
{
name: "invalid",
depth: "not-an-integer",
expected: nil,
expectErr: true,
},
{
name: "not_provided",
depth: nil,
expected: map[string]interface{}{
"foo": map[string]interface{}{
"bar": map[string]interface{}{
"baz": nil,
},
},
},
expectErr: false,
},
{
name: "zero",
depth: 0,
expected: map[string]interface{}{
"foo": map[string]interface{}{
"bar": map[string]interface{}{
"baz": nil,
},
},
},
expectErr: false,
},
{
name: "non_zero",
depth: 2,
expected: map[string]interface{}{
"foo": map[string]interface{}{
"bar": nil,
},
},
expectErr: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tc := tc
t.Parallel()
b, storage := getBackend(t)
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "data/foo",
Storage: storage,
Data: map[string]interface{}{
"data": map[string]interface{}{
"foo": map[string]interface{}{
"bar": map[string]interface{}{
"baz": 123,
},
},
},
},
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("data CreateOperation request failed, err: %v, resp %#v", err, resp)
}
subkeysData := map[string]interface{}{}
if tc.depth != nil {
subkeysData["depth"] = tc.depth
}
req = &logical.Request{
Operation: logical.ReadOperation,
Path: "subkeys/foo",
Storage: storage,
Data: subkeysData,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || resp == nil {
t.Fatalf("subkeys ReadOperation request failed, err: %v, resp %#v", err, resp)
}
if tc.expectErr != resp.IsError() {
t.Fatalf("unexpected ReadOperation request response, expected err: %t, is error: %t, resp %#v", tc.expectErr, resp.IsError(), resp)
}
if tc.expected != nil {
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*versionedKVBackend).Route(req.Path), req.Operation),
resp,
true,
)
if diff := deep.Equal(resp.Data["subkeys"], tc.expected); len(diff) > 0 {
t.Fatalf("resp and expected data mismatch, diff: %#v", diff)
}
}
})
}
}
// TestVersionedKV_Subkeys_EmptyData verifies that an empty map is
// returned if the underlying data is also empty
func TestVersionedKV_Subkeys_EmptyData(t *testing.T) {
b, storage := getBackend(t)
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "data/foo",
Storage: storage,
Data: map[string]interface{}{
"data": map[string]interface{}{},
},
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("CreateOperation request failed, err: %v, resp %#v", err, resp)
}
req = &logical.Request{
Operation: logical.ReadOperation,
Path: "subkeys/foo",
Storage: storage,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("ReadOperation request failed, err: %v, resp %#v", err, resp)
}
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*versionedKVBackend).Route(req.Path), req.Operation),
resp,
true,
)
if diff := deep.Equal(resp.Data["subkeys"], map[string]interface{}{}); len(diff) > 0 {
t.Fatalf("resp and expected data mismatch, diff: %#v", diff)
}
}
// TestVersionedKV_Subkeys_VersionDeleted verifies that a 404 HTTP response
// is returned if the requested entry has been deleted
func TestVersionedKV_Subkeys_VersionDeleted(t *testing.T) {
b, storage := getBackend(t)
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "data/foo",
Storage: storage,
Data: map[string]interface{}{
"data": map[string]interface{}{
"foo": "bar",
},
},
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("data CreateOperation request failed, err: %v, resp %#v", err, resp)
}
req = &logical.Request{
Operation: logical.ReadOperation,
Path: "subkeys/foo",
Storage: storage,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("subkeys ReadOperation request failed, err: %v, resp %#v", err, resp)
}
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*versionedKVBackend).Route(req.Path), req.Operation),
resp,
true,
)
expectedSubkeys := map[string]interface{}{
"foo": nil,
}
if diff := deep.Equal(resp.Data["subkeys"], expectedSubkeys); len(diff) > 0 {
t.Fatalf("resp and expected data mismatch, diff: %#v", diff)
}
req = &logical.Request{
Operation: logical.DeleteOperation,
Path: "data/foo",
Storage: storage,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("metadata DeleteOperation request failed - err: %v, resp: %#v\n", err, resp)
}
req = &logical.Request{
Operation: logical.ReadOperation,
Path: "subkeys/foo",
Storage: storage,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("subkeys ReadOperation request failed, err: %v, resp %#v", err, resp)
}
// Use of logical.RespondWithStatusCode in handler will
// serialize the JSON response body as a string
respBody := map[string]interface{}{}
if resp.Data["http_status_code"] != http.StatusNotFound {
t.Fatalf("expected 404 response for subkeys ReadOperation: resp: %#v", resp)
}
if rawRespBody, ok := resp.Data[logical.HTTPRawBody]; ok {
err = json.Unmarshal([]byte(rawRespBody.(string)), &respBody)
}
respDataRaw, ok := respBody["data"]
if !ok {
t.Fatalf("no data provided in subkeys response, resp: %#v\n", resp)
}
respData := respDataRaw.(map[string]interface{})
respMetadataRaw, ok := respData["metadata"]
if !ok {
t.Fatalf("no metadata provided in subkeys response, resp: %#v\n", resp)
}
respMetadata := respMetadataRaw.(map[string]interface{})
if respMetadata["deletion_time"] == "" {
t.Fatalf("expected deletion_time to be set, resp: %#v\n", resp)
}
}
// TestVersionedKV_Subkeys_VersionDestroyed verifies that a 404 HTTP response
// is returned if the requested entry has been destroyed
func TestVersionedKV_Subkeys_VersionDestroyed(t *testing.T) {
b, storage := getBackend(t)
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "data/foo",
Storage: storage,
Data: map[string]interface{}{
"data": map[string]interface{}{
"foo": "bar",
},
},
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("data CreateOperation request failed, err: %v, resp %#v", err, resp)
}
req = &logical.Request{
Operation: logical.ReadOperation,
Path: "subkeys/foo",
Storage: storage,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("subkeys ReadOperation request failed, err: %v, resp %#v", err, resp)
}
expectedSubkeys := map[string]interface{}{
"foo": nil,
}
if diff := deep.Equal(resp.Data["subkeys"], expectedSubkeys); len(diff) > 0 {
t.Fatalf("resp and expected data mismatch, diff: %#v", diff)
}
req = &logical.Request{
Operation: logical.UpdateOperation,
Path: "destroy/foo",
Storage: storage,
Data: map[string]interface{}{
"versions": []int{1},
},
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("destroy UpdateOperation request failed - err: %v resp:%#v\n", err, resp)
}
req = &logical.Request{
Operation: logical.ReadOperation,
Path: "subkeys/foo",
Storage: storage,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("subkeys ReadOperation request failed, err: %v, resp %#v", err, resp)
}
// Use of logical.RespondWithStatusCode in handler will
// serialize the JSON response body as a string
respBody := map[string]interface{}{}
if resp.Data["http_status_code"] != http.StatusNotFound {
t.Fatalf("expected 404 response for subkeys ReadOperation: resp:%#v", resp)
}
if rawRespBody, ok := resp.Data[logical.HTTPRawBody]; ok {
err = json.Unmarshal([]byte(rawRespBody.(string)), &respBody)
}
respDataRaw, ok := respBody["data"]
if !ok {
t.Fatalf("no data provided in subkeys response, resp: %#v\n", resp)
}
respData := respDataRaw.(map[string]interface{})
respMetadataRaw, ok := respData["metadata"]
if !ok {
t.Fatalf("no metadata provided in subkeys response, resp: %#v\n", resp)
}
respMetadata := respMetadataRaw.(map[string]interface{})
if respMetadata["destroyed"] == nil || !respMetadata["destroyed"].(bool) {
t.Fatalf("expected version to be destroyed, resp: %#v\n", resp)
}
}