-
Notifications
You must be signed in to change notification settings - Fork 30
/
passthrough_test.go
337 lines (301 loc) · 8.24 KB
/
passthrough_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package kv
import (
"context"
"encoding/json"
"reflect"
"testing"
"time"
"github.com/hashicorp/go-secure-stdlib/parseutil"
"github.com/hashicorp/vault/sdk/helper/testhelpers/schema"
"github.com/hashicorp/vault/sdk/logical"
)
func testPassthroughBackendWithStorage() (logical.Backend, logical.Storage) {
storage := &logical.InmemStorage{}
b, _ := PassthroughBackendFactory(context.Background(), &logical.BackendConfig{
Logger: nil,
System: logical.StaticSystemView{
DefaultLeaseTTLVal: time.Hour * 24,
MaxLeaseTTLVal: time.Hour * 24 * 32,
},
StorageView: storage,
})
return b, storage
}
func TestPassthroughBackend_RootPaths(t *testing.T) {
b := testPassthroughBackend()
test := func(b logical.Backend) {
root := b.SpecialPaths()
if len(root.Root) != 0 {
t.Fatalf("unexpected: %v", root)
}
}
test(b)
b = testPassthroughLeasedBackend()
test(b)
}
func TestPassthroughBackend_Write(t *testing.T) {
test := func(b logical.Backend) {
req := logical.TestRequest(t, logical.UpdateOperation, "foo")
req.Data["raw"] = "test"
resp, err := b.HandleRequest(context.Background(), req)
if err != nil {
t.Fatalf("err: %v", err)
}
if resp != nil {
t.Fatalf("bad: %v", resp)
}
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*PassthroughBackend).Route(req.Path), req.Operation),
resp,
true,
)
out, err := req.Storage.Get(context.Background(), "foo")
if err != nil {
t.Fatalf("err: %v", err)
}
if out == nil {
t.Fatalf("failed to write to view")
}
}
b := testPassthroughBackend()
test(b)
b = testPassthroughLeasedBackend()
test(b)
}
func TestPassthroughBackend_Read(t *testing.T) {
test := func(b logical.Backend, ttlType string, ttl interface{}, leased bool) {
req := logical.TestRequest(t, logical.UpdateOperation, "foo")
req.Data["raw"] = "test"
var reqTTL interface{}
switch ttl.(type) {
case int64:
reqTTL = ttl.(int64)
case string:
reqTTL = ttl.(string)
default:
t.Fatal("unknown ttl type")
}
req.Data[ttlType] = reqTTL
storage := req.Storage
if _, err := b.HandleRequest(context.Background(), req); err != nil {
t.Fatalf("err: %v", err)
}
req = logical.TestRequest(t, logical.ReadOperation, "foo")
req.Storage = storage
resp, err := b.HandleRequest(context.Background(), req)
if err != nil {
t.Fatalf("err: %v", err)
}
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*PassthroughBackend).Route(req.Path), req.Operation),
resp,
true,
)
expectedTTL, err := parseutil.ParseDurationSecond(ttl)
if err != nil {
t.Fatal(err)
}
// What comes back if an int is passed in is a json.Number which is
// actually aliased as a string so to make the deep equal happy if it's
// actually a number we set it to an int64
var respTTL interface{} = resp.Data[ttlType]
_, ok := respTTL.(json.Number)
if ok {
respTTL, err = respTTL.(json.Number).Int64()
if err != nil {
t.Fatal(err)
}
resp.Data[ttlType] = respTTL
}
expected := &logical.Response{
Secret: &logical.Secret{
LeaseOptions: logical.LeaseOptions{
Renewable: true,
TTL: expectedTTL,
},
},
Data: map[string]interface{}{
"raw": "test",
ttlType: reqTTL,
},
}
if !leased {
expected.Secret.Renewable = false
}
resp.Secret.InternalData = nil
resp.Secret.LeaseID = ""
if !reflect.DeepEqual(resp, expected) {
t.Fatalf("bad response.\n\nexpected:\n%#v\n\nGot:\n%#v", expected, resp)
}
}
b := testPassthroughLeasedBackend()
test(b, "lease", "1h", true)
test(b, "ttl", "5", true)
b = testPassthroughBackend()
test(b, "lease", int64(10), false)
test(b, "ttl", "40s", false)
}
func TestPassthroughBackend_Delete(t *testing.T) {
for name, f := range map[string]func(*mockEventsSender) logical.Backend{
"no lease": testPassthroughBackendWithEvents,
"leased": testPassthroughLeasedBackendWithEvents,
} {
t.Run(name, func(t *testing.T) {
events := &mockEventsSender{}
b := f(events)
req := logical.TestRequest(t, logical.UpdateOperation, "foo")
req.Data["raw"] = "test"
storage := req.Storage
if _, err := b.HandleRequest(context.Background(), req); err != nil {
t.Fatalf("err: %v", err)
}
req = logical.TestRequest(t, logical.DeleteOperation, "foo")
req.Storage = storage
resp, err := b.HandleRequest(context.Background(), req)
if err != nil {
t.Fatalf("err: %v", err)
}
if resp != nil {
t.Fatalf("bad: %v", resp)
}
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*PassthroughBackend).Route(req.Path), req.Operation),
resp,
true,
)
req = logical.TestRequest(t, logical.ReadOperation, "foo")
req.Storage = storage
resp, err = b.HandleRequest(context.Background(), req)
if err != nil {
t.Fatalf("err: %v", err)
}
if resp != nil {
t.Fatalf("bad: %v", resp)
}
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*PassthroughBackend).Route(req.Path), req.Operation),
resp,
true,
)
events.expectEvents(t, []expectedEvent{
{"kv-v1/write", "foo", "foo"},
{"kv-v1/delete", "foo", ""},
})
})
}
}
func TestPassthroughBackend_List(t *testing.T) {
test := func(b logical.Backend) {
req := logical.TestRequest(t, logical.UpdateOperation, "foo")
req.Data["raw"] = "test"
storage := req.Storage
if _, err := b.HandleRequest(context.Background(), req); err != nil {
t.Fatalf("err: %v", err)
}
req = logical.TestRequest(t, logical.ListOperation, "")
req.Storage = storage
resp, err := b.HandleRequest(context.Background(), req)
if err != nil {
t.Fatalf("err: %v", err)
}
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*PassthroughBackend).Route(req.Path), req.Operation),
resp,
true,
)
expected := &logical.Response{
Data: map[string]interface{}{
"keys": []string{"foo"},
},
}
if !reflect.DeepEqual(resp, expected) {
t.Fatalf("bad response.\n\nexpected: %#v\n\nGot: %#v", expected, resp)
}
}
b := testPassthroughBackend()
test(b)
b = testPassthroughLeasedBackend()
test(b)
}
func TestPassthroughBackend_Revoke(t *testing.T) {
test := func(b logical.Backend) {
req := logical.TestRequest(t, logical.RevokeOperation, "kv")
req.Secret = &logical.Secret{
InternalData: map[string]interface{}{
"secret_type": "kv",
},
}
if _, err := b.HandleRequest(context.Background(), req); err != nil {
t.Fatalf("err: %v", err)
}
}
b := testPassthroughBackend()
test(b)
b = testPassthroughLeasedBackend()
test(b)
}
func TestPassthroughBackend_Renew(t *testing.T) {
b := testPassthroughLeasedBackend()
req := logical.TestRequest(t, logical.CreateOperation, "foo")
req.Data = map[string]interface{}{
"ttl": "4h",
"payload": "alpha",
}
storage := req.Storage
if _, err := b.HandleRequest(context.Background(), req); err != nil {
t.Fatalf("err: %v", err)
}
req = logical.TestRequest(t, logical.RenewOperation, "foo")
req.Storage = storage
req.Secret = &logical.Secret{
InternalData: map[string]interface{}{
"secret_type": "kv",
},
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil {
t.Fatalf("err: %v", err)
}
expected := map[string]interface{}{
"ttl": "4h",
"payload": "alpha",
}
if !reflect.DeepEqual(resp.Data, expected) {
t.Fatalf("bad response.\n\nexpected: %#v\n\nGot: %#v", expected, resp)
}
}
func testPassthroughBackend() logical.Backend {
return testPassthroughBackendWithEvents(nil)
}
func testPassthroughLeasedBackend() logical.Backend {
return testPassthroughLeasedBackendWithEvents(nil)
}
func testPassthroughBackendWithEvents(events *mockEventsSender) logical.Backend {
b, _ := PassthroughBackendFactory(context.Background(), &logical.BackendConfig{
Logger: nil,
System: logical.StaticSystemView{
DefaultLeaseTTLVal: time.Hour * 24,
MaxLeaseTTLVal: time.Hour * 24 * 32,
},
EventsSender: events,
})
return b
}
func testPassthroughLeasedBackendWithEvents(events *mockEventsSender) logical.Backend {
b, _ := LeasedPassthroughBackendFactory(context.Background(), &logical.BackendConfig{
Logger: nil,
System: logical.StaticSystemView{
DefaultLeaseTTLVal: time.Hour * 24,
MaxLeaseTTLVal: time.Hour * 24 * 32,
},
EventsSender: events,
})
return b
}