forked from volck/raven
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault_test.go
executable file
·475 lines (407 loc) · 15.1 KB
/
vault_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
package main
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
sealedSecretPkg "github.com/bitnami-labs/sealed-secrets/pkg/apis/sealed-secrets/v1alpha1"
. "github.com/hashicorp/vault-plugin-secrets-kv"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/serializer"
"testing"
"github.com/hashicorp/vault/api"
vaulthttp "github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/sdk/logical"
hashivault "github.com/hashicorp/vault/vault"
)
func TestGetAllKVs(t *testing.T) {
t.Parallel()
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client := cluster.Cores[0].Client
config := config{
vaultEndpoint: cluster.Cores[0].Client.Address(),
secretEngine: "kv",
token: client.Token(),
destEnv: "kv",
pemFile: "cert.pem",
}
secretName := "secretsecretsecret"
generateTestSecrets(t, client, config, secretName)
// Pass the client to the code under test.
_, err := getAllKVs(client, config)
if err != nil {
t.Fatal(err)
}
}
func TestGetSingleKV(t *testing.T) {
t.Parallel()
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client := cluster.Cores[0].Client
config := config{
vaultEndpoint: cluster.Cores[0].Client.Address(),
secretEngine: "kv",
token: client.Token(),
destEnv: "kv",
pemFile: "cert.crt",
}
// make testable secrets for cluster
secrets := map[string]interface{}{
"data": map[string]interface{}{"secretKey": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
client.Logical().Write("kv/data/TestGetSingleKVSecret", secrets)
list, err := getAllKVs(client, config)
if err != nil {
fmt.Println(err)
}
secretList := list.Data["keys"].([]interface{})
persistVaultChanges(secretList, client, config)
}
func TestReadAllKV(t *testing.T) {
//t.Parallel()
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client := cluster.Cores[0].Client
// make testable secrets for cluster
secretOne := map[string]interface{}{
"data": map[string]interface{}{"SecretOne": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
secretTwo := map[string]interface{}{
"data": map[string]interface{}{"secretTwo": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
secretThree := map[string]interface{}{
"data": map[string]interface{}{"secretThree": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
secretThreev2 := map[string]interface{}{
"data": map[string]interface{}{"secretThreev2": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
config := config{
vaultEndpoint: cluster.Cores[0].Client.Address(),
secretEngine: "kv",
token: client.Token(),
destEnv: "kv",
pemFile: "cert.crt",
}
firstPath := "kv/data/TestReadAllKVsubpathone"
secondPath := "kv/data/subpathone/TestReadAllKVsubpathtwo"
thirdPath := "kv/data/subpathone/subpathtwo/TestReadAllKVsubpaththree"
thirdPathv2 := "kv/data/subpathone/subpathtwo/TestReadAllKVsubpaththree"
client.Logical().Write(firstPath, secretOne)
client.Logical().Write(secondPath, secretTwo)
client.Logical().Write(thirdPath, secretThree)
client.Logical().Write(thirdPathv2, secretThreev2)
list, err := getAllKVs(client, config)
if err != nil {
fmt.Println(err)
}
secretList := list.Data["keys"].([]interface{})
mySecretList = map[string]*api.Secret{}
persistVaultChanges(secretList, client, config)
fmt.Println("TestReadAllKV len of mysecretlist:", len(mySecretList))
for k, v := range mySecretList {
fmt.Printf("TestReadAll - key: %v, value: %v \n", k, v)
}
if len(mySecretList) != 3 {
t.Fatal("TestReadAll list != 3. should be 3\n")
}
}
func TestPersistVaultChanges(t *testing.T) {
t.Parallel()
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client := cluster.Cores[0].Client
config := config{
vaultEndpoint: cluster.Cores[0].Client.Address(),
secretEngine: "kv",
token: client.Token(),
destEnv: "kv",
pemFile: "cert.crt",
}
// make testable secrets for cluster
secretOne := map[string]interface{}{
"data": map[string]interface{}{"SecretOne": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
secretTwo := map[string]interface{}{
"data": map[string]interface{}{"secretTwo": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
secretThree := map[string]interface{}{
"data": map[string]interface{}{"secretThree": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
secretThreev2 := map[string]interface{}{
"data": map[string]interface{}{"secretThreev2": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
firstPath := "kv/data/subpathone"
secondPath := "kv/data/subpathone/secretSecondPath"
thirdPath := "kv/data/subpathone/subpathtwo/secretThirdPath"
thirdPathv2 := "kv/data/subpathone/subpathtwo/secretThirdPathV2"
client.Logical().Write(firstPath, secretOne)
client.Logical().Write(secondPath, secretTwo)
client.Logical().Write(thirdPath, secretThree)
client.Logical().Write(thirdPathv2, secretThreev2)
list, err := getAllKVs(client, config)
if err != nil {
fmt.Println(err)
}
secretList := list.Data["keys"].([]interface{})
fmt.Println(secretList)
}
func TestGetSingleKVMultipleSubPath(t *testing.T) {
t.Parallel()
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client := cluster.Cores[0].Client
// make testable secrets for cluster
secretOne := map[string]interface{}{
"data": map[string]interface{}{"SecretOne": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
secretTwo := map[string]interface{}{
"data": map[string]interface{}{"secretTwo": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
secretThree := map[string]interface{}{
"data": map[string]interface{}{"secretThree": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
secretThreev2 := map[string]interface{}{
"data": map[string]interface{}{"secretThreev2": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
firstPath := "kv/data/subpathone/"
secondPath := "kv/data/subpathone/subpathtwo/"
thirdPath := "kv/data/subpathone/subpathtwo/subpaththree/"
thirdPathv2 := "kv/data/subpathone/subpathtwo/subpaththree/"
client.Logical().Write(firstPath, secretOne)
client.Logical().Write(secondPath, secretTwo)
client.Logical().Write(thirdPath, secretThree)
client.Logical().Write(thirdPathv2, secretThreev2)
c := config{secretEngine: "kv"}
secretList, err := getAllKVs(client, c)
if err != nil {
fmt.Println("getallKVS", err)
}
for _, secret := range secretList.Data["keys"].([]interface{}) {
secret := getSingleKV(client, "kv", secret.(string))
if secret == nil {
fmt.Println("nil pointer exception")
} else {
fmt.Println("got secret", secret)
}
}
}
func ReturnPrivateKey(t *testing.T) map[string]*rsa.PrivateKey {
t.Helper()
rsaPrivateKey := `-----BEGIN RSA PRIVATE KEY-----
MIIJJwIBAAKCAgEAtK0o7yNwZjco/4lOtoAx+ozJeJs7KLQSD5L5H5OhMIitCgGl
79wDtP5skC2jLDmmFBDYgh5q0AidOYpFGYylgMktdL7qI6nqKJD2WyrsvbRYXF8n
kkIbE/SDE8mBvL9pVNxD06teIVB2IiPE5ftoVvJamMtPHdE5EIIKjsTAdzi+ykK4
93bJAvCmutRntaMoguBKLJCHmFWgTcRns5mBc9UCnD4ELTHmj+ueBkpXGIGfHgnB
MtI1ZKC0jpU28h6MgTR//0XnyjFRjeEtUiIR6w8ZoQnKIhNur0VuW8+haTqmL18z
WEIpXVczm0D9MsGFA4Ol8hwjAInKxBxdCoFtXJNr8kc4/xiGL1FEdXAQwjxfLG5c
FHkZjOt311wLI6dEnHt7AaWEtiNF+GUUBmmGVmv8bIntjyT6p+1ZNBsQt5VFepKN
uZjeLDM0iRWmx+zS2vS0IP4m3FJfVcXa8iIqXvqBKaVgtz+1Y8ottqMvWIwHB0zN
Pu1tHzFm2M+ji/ZR+1waE3p6ZCFNL9jl168mzCg7nyuE1I7deGlC42NtMaQzL5q8
QV2FWEsInNr0ASAqX7DSnpkoyMH9ZG6C77QEdxBqEBQoipsdvG75b6cJ+34fW0PR
r3Sj6IZE0wijeQFgXw6ZM+GfRZi113B8eGLd9mtvZ2sxlMP6k2MPVfqnDT0CAwEA
AQKCAgA3V1rUdPx2sqqiKwKrL/UfE4SapqGmRBHMJ26bV3LmFNc722liYPfZtpy2
RORYJYhTWR2YLYz2D81o1It52fTPz89WlSvOpLsOleh/4FQHf3gZQyQxzoHQyPJl
WzGcCN1Qmu9DpJf+iFDHAEHNWAaLq0xIxO2E6pMaFVr6hBWX7w+xkGJrmjzT47aO
P4VMw58jB2Rw5gxCgufJKkHBZ2GZg0N7bBuHZGOE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mrcZ4CC9kZzqSzk/anK/t633LNStxRKnnd
GcBNYzovELWgwrTkcln68AOhJ5cRqQav/yWnTyyd6wlmtSR7nOnqKx32Uw==
-----END RSA PRIVATE KEY-----
`
block, _ := pem.Decode([]byte(rsaPrivateKey))
if block == nil {
fmt.Println("failed to decode pem")
}
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
fmt.Println("parsePKCS1PrivateKey failed", err)
}
privateKeys := make(map[string]*rsa.PrivateKey)
privateKeys["mykey"] = privateKey
return privateKeys
}
func generateTestSecrets(t *testing.T, client *api.Client, config config, secretName string) {
t.Helper()
// make testable secrets for cluster
secrets := map[string]interface{}{
"data": map[string]interface{}{"secretKey": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
writePath := fmt.Sprintf("%s/data/%s", config.secretEngine, secretName)
client.Logical().Write(writePath, secrets)
}
func deleteTestSecrets(t *testing.T, client *api.Client, config config, secretName string) {
t.Helper()
dataPath := fmt.Sprintf("%s/data/%s", config.secretEngine, secretName)
client.Logical().Delete(dataPath)
metadataPath := fmt.Sprintf("%s/metadata/%s", config.secretEngine, secretName)
client.Logical().Delete(metadataPath)
}
func ReadConvertKVFromVault(t *testing.T, client *api.Client, secretEngine string, secretName string, destEnv string, pemFile string) (*sealedSecretPkg.SealedSecret, v1.Secret) {
t.Helper()
// make testable secrets for cluster
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client = cluster.Cores[0].Client
config := config{
vaultEndpoint: cluster.Cores[0].Client.Address(),
secretEngine: "kv",
token: client.Token(),
destEnv: "kv",
}
generateTestSecrets(t, client, config, secretName)
SingleKVFromVault := getSingleKV(client, secretEngine, secretName)
k8sSecret := createK8sSecret(secretName, config, SingleKVFromVault)
SealedSecret := createSealedSecret(pemFile, &k8sSecret)
return SealedSecret, k8sSecret
}
func UnsealSecretAndReturn(t *testing.T, SealedSecret *sealedSecretPkg.SealedSecret, codecs serializer.CodecFactory, privateKeys map[string]*rsa.PrivateKey) *v1.Secret {
t.Helper()
UnsealedSecret, err := SealedSecret.Unseal(codecs, privateKeys)
if err != nil {
fmt.Println("unsealedSecret:", err)
}
return UnsealedSecret
}
func TestPickRipeSecretsReturnsOne(t *testing.T) {
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client := cluster.Cores[0].Client
config := config{
vaultEndpoint: cluster.Cores[0].Client.Address(),
secretEngine: "kv",
token: client.Token(),
destEnv: "kv",
pemFile: "cert.pem",
}
secretName := "secret"
secretNameTwo := "secrettwo"
generateTestSecrets(t, client, config, secretName)
generateTestSecrets(t, client, config, secretNameTwo)
PreviousKV, err := getAllKVs(client, config)
if err != nil {
fmt.Println(err)
}
previousKV := PreviousKV.Data["keys"].([]interface{})
persistVaultChanges(previousKV, client, config)
fmt.Println("pre: setting state to this", mySecretList)
firstState := mySecretList
deleteTestSecrets(t, client, config, secretName)
NewKV, err := getAllKVs(client, config)
if err != nil {
fmt.Println(err)
}
newKV := NewKV.Data["keys"].([]interface{})
mySecretList = map[string]*api.Secret{}
persistVaultChanges(newKV, client, config)
fmt.Println("post: setting state to this", mySecretList)
secondState := mySecretList
picked := PickRipeSecrets(firstState, secondState)
fmt.Println(picked, len(picked))
if len(picked) == 0 {
t.Fatal("PickRipeSecrets should have returned 1 here")
}
}
func TestPickRipeSecretsReturnsNoRipe(t *testing.T) {
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client := cluster.Cores[0].Client
config := config{
vaultEndpoint: cluster.Cores[0].Client.Address(),
secretEngine: "kv",
token: client.Token(),
destEnv: "kv",
pemFile: "cert.pem",
}
secretName := "secret"
secretNameTwo := "secrettwo"
generateTestSecrets(t, client, config, secretName)
generateTestSecrets(t, client, config, secretNameTwo)
PreviousKV, err := getAllKVs(client, config)
if err != nil {
fmt.Println(err)
}
previousKV := PreviousKV.Data["keys"].([]interface{})
persistVaultChanges(previousKV, client, config)
firstState := mySecretList
deleteTestSecrets(t, client, config, secretName)
NewKV, err := getAllKVs(client, config)
if err != nil {
fmt.Println(err)
}
newKV := NewKV.Data["keys"].([]interface{})
persistVaultChanges(newKV, client, config)
secondState := mySecretList
picked := PickRipeSecrets(firstState, secondState)
fmt.Println(picked, len(picked))
if len(picked) != 0 {
t.Fatal("PickRipeSecrets should have returned 0 here")
}
}
func createVaultTestCluster(t *testing.T) *hashivault.TestCluster {
t.Helper()
coreConfig := &hashivault.CoreConfig{
LogicalBackends: map[string]logical.Factory{
"kv": Factory,
},
}
cluster := hashivault.NewTestCluster(t, coreConfig, &hashivault.TestClusterOptions{
HandlerFunc: vaulthttp.Handler,
})
cluster.Start()
// Create KV V2 mount
if err := cluster.Cores[0].Client.Sys().Mount("kv", &api.MountInput{
Type: "kv",
Options: map[string]string{
"version": "2",
},
}); err != nil {
t.Fatal(err)
}
return cluster
}