-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_pubkey_test.go
153 lines (126 loc) · 3.79 KB
/
path_pubkey_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
package awskms
import (
"context"
"crypto/x509"
"encoding/pem"
"strings"
"testing"
"github.com/hashicorp/vault/sdk/logical"
kmspb "google.golang.org/genproto/googleapis/cloud/kms/v1"
)
func TestPathPubkey_Read(t *testing.T) {
t.Parallel()
t.Run("field_validation", func(t *testing.T) {
t.Parallel()
testFieldValidation(t, logical.ReadOperation, "pubkey/my-key")
})
t.Run("asymmetric_decrypt", func(t *testing.T) {
t.Parallel()
algorithms := []kmspb.CryptoKeyVersion_CryptoKeyVersionAlgorithm{
kmspb.CryptoKeyVersion_RSA_DECRYPT_OAEP_2048_SHA256,
kmspb.CryptoKeyVersion_RSA_DECRYPT_OAEP_3072_SHA256,
kmspb.CryptoKeyVersion_RSA_DECRYPT_OAEP_4096_SHA256,
}
for _, algo := range algorithms {
algo := algo
name := strings.ToLower(algo.String())
t.Run(name, func(t *testing.T) {
t.Parallel()
cryptoKey, cleanup := testCreateKMSCryptoKeyAsymmetricDecrypt(t, algo)
defer cleanup()
b, storage := testBackend(t)
ctx := context.Background()
if err := storage.Put(ctx, &logical.StorageEntry{
Key: "keys/my-key",
Value: []byte(`{"name":"my-key", "crypto_key_id":"` + cryptoKey + `"}`),
}); err != nil {
t.Fatal(err)
}
// Get the public key
resp, err := b.HandleRequest(ctx, &logical.Request{
Storage: storage,
Operation: logical.ReadOperation,
Path: "pubkey/my-key",
Data: map[string]interface{}{
"key_version": 1,
},
})
if err != nil {
t.Fatal(err)
}
// Verify it's a pem public key (this is kinda testing KMS, but it's
// a good test to ensure the API doesn't change).
pemStr, ok := resp.Data["pem"].(string)
if !ok {
t.Fatal("missing pem")
}
// Extract the PEM-encoded data block
block, _ := pem.Decode([]byte(pemStr))
if block == nil {
t.Fatalf("not pem: %s", pemStr)
}
// Decode the public key
if _, err := x509.ParsePKIXPublicKey(block.Bytes); err != nil {
t.Fatal(err)
}
})
}
})
t.Run("asymmetric_sign", func(t *testing.T) {
t.Parallel()
algorithms := []kmspb.CryptoKeyVersion_CryptoKeyVersionAlgorithm{
kmspb.CryptoKeyVersion_RSA_SIGN_PSS_2048_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PSS_3072_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PSS_4096_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_2048_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_3072_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_4096_SHA256,
kmspb.CryptoKeyVersion_EC_SIGN_P256_SHA256,
kmspb.CryptoKeyVersion_EC_SIGN_P384_SHA384,
}
for _, algo := range algorithms {
algo := algo
name := strings.ToLower(algo.String())
t.Run(name, func(t *testing.T) {
t.Parallel()
cryptoKey, cleanup := testCreateKMSCryptoKeyAsymmetricSign(t, algo)
defer cleanup()
b, storage := testBackend(t)
ctx := context.Background()
if err := storage.Put(ctx, &logical.StorageEntry{
Key: "keys/my-key",
Value: []byte(`{"name":"my-key", "crypto_key_id":"` + cryptoKey + `"}`),
}); err != nil {
t.Fatal(err)
}
// Get the public key
resp, err := b.HandleRequest(ctx, &logical.Request{
Storage: storage,
Operation: logical.ReadOperation,
Path: "pubkey/my-key",
Data: map[string]interface{}{
"key_version": 1,
},
})
if err != nil {
t.Fatal(err)
}
// Verify it's a pem public key (this is kinda testing KMS, but it's
// a good test to ensure the API doesn't change).
pemStr, ok := resp.Data["pem"].(string)
if !ok {
t.Fatal("missing pem")
}
// Extract the PEM-encoded data block
block, _ := pem.Decode([]byte(pemStr))
if block == nil {
t.Fatalf("not pem: %s", pemStr)
}
// Decode the public key
if _, err := x509.ParsePKIXPublicKey(block.Bytes); err != nil {
t.Fatal(err)
}
})
}
})
}