forked from hashicorp/vault-plugin-secrets-gcpkms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_keys_trim_test.go
82 lines (66 loc) · 1.93 KB
/
path_keys_trim_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package gcpkms
import (
"context"
"testing"
"github.com/hashicorp/vault/sdk/logical"
"google.golang.org/api/iterator"
kmspb "google.golang.org/genproto/googleapis/cloud/kms/v1"
)
func TestPathKeysTrim_Write(t *testing.T) {
t.Run("field_validation", func(t *testing.T) {
testFieldValidation(t, logical.CreateOperation, "keys/trim/my-key")
testFieldValidation(t, logical.UpdateOperation, "keys/trim/my-key")
testFieldValidation(t, logical.DeleteOperation, "keys/trim/my-key")
})
cryptoKey, cleanup := testCreateKMSCryptoKeySymmetric(t)
defer cleanup()
kmsClient := testKMSClient(t)
b, storage := testBackend(t)
if err := storage.Put(context.Background(), &logical.StorageEntry{
Key: "keys/my-versioned-key",
Value: []byte(`{"name":"my-versioned-key", "crypto_key_id":"` + cryptoKey + `", "min_version":3, "max_version":5}`),
}); err != nil {
t.Fatal(err)
}
for i := 2; i <= 5; i++ {
ctx := context.Background()
if _, err := kmsClient.CreateCryptoKeyVersion(ctx, &kmspb.CreateCryptoKeyVersionRequest{
Parent: cryptoKey,
CryptoKeyVersion: &kmspb.CryptoKeyVersion{
State: kmspb.CryptoKeyVersion_ENABLED,
},
}); err != nil {
t.Fatal(err)
}
}
ctx := context.Background()
if _, err := b.HandleRequest(ctx, &logical.Request{
Storage: storage,
Operation: logical.UpdateOperation,
Path: "keys/trim/my-versioned-key",
}); err != nil {
t.Fatal(err)
}
var ckvs []string
it := kmsClient.ListCryptoKeyVersions(ctx, &kmspb.ListCryptoKeyVersionsRequest{
Parent: cryptoKey,
})
for {
resp, err := it.Next()
if err != nil {
if err == iterator.Done {
break
}
t.Fatal(err)
}
if resp.State != kmspb.CryptoKeyVersion_DESTROYED &&
resp.State != kmspb.CryptoKeyVersion_DESTROY_SCHEDULED {
ckvs = append(ckvs, resp.Name)
}
}
if len(ckvs) > 3 {
t.Errorf("expected less crypto key versions: %q", ckvs)
}
}