Skip to content

Commit 3d46bb8

Browse files
authored
feat: Integration tests for KMS v2 (#238)
<!-- Thank you for helping KMS Plugin for Key Vault with a pull request! --> **Reason for Change**: <!-- What does this PR improve or fix in KMS Plugin for Key Vault? Why is it needed? --> **Issue Fixed**: <!-- If this PR fixes GitHub issue 1234, add "Fixes #1234" to the next line. --> Fixes #237 **Notes for Reviewers**: Signed-off-by: Nilekh Chaudhari <1626598+nilekhc@users.noreply.github.com>
1 parent c0e39e7 commit 3d46bb8

File tree

1 file changed

+96
-23
lines changed

1 file changed

+96
-23
lines changed

tests/client/client_test.go

+96-23
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import (
55
"fmt"
66
"net"
77
"testing"
8+
"time"
89

910
"golang.org/x/net/context"
1011
"google.golang.org/grpc"
1112
"google.golang.org/grpc/credentials/insecure"
13+
"k8s.io/apimachinery/pkg/util/uuid"
1214
kmsv1 "k8s.io/kms/apis/v1beta1"
15+
kmsv2 "k8s.io/kms/apis/v2"
1316
)
1417

1518
const (
@@ -19,19 +22,28 @@ const (
1922
)
2023

2124
var (
22-
client kmsv1.KeyManagementServiceClient
25+
v1Client kmsv1.KeyManagementServiceClient
26+
v2Client kmsv2.KeyManagementServiceClient
2327
connection *grpc.ClientConn
28+
t *testing.T
2429
err error
2530
)
2631

27-
func setupTestCase(t *testing.T) func(t *testing.T) {
28-
t.Log("setup test case")
29-
connection, err = newUnixSocketConnection(pathToUnixSocket)
30-
if err != nil {
31-
fmt.Printf("%s", err)
32+
func setupTestCase() {
33+
if t != nil {
34+
t.Log("setup test case")
35+
connection, err = newUnixSocketConnection(pathToUnixSocket)
36+
if err != nil {
37+
fmt.Printf("%s", err)
38+
}
39+
40+
v1Client = kmsv1.NewKeyManagementServiceClient(connection)
41+
v2Client = kmsv2.NewKeyManagementServiceClient(connection)
3242
}
33-
client = kmsv1.NewKeyManagementServiceClient(connection)
34-
return func(t *testing.T) {
43+
}
44+
45+
func teardownTestCase() {
46+
if t != nil {
3547
t.Log("teardown test case")
3648
connection.Close()
3749
}
@@ -49,29 +61,57 @@ func TestEncryptDecrypt(t *testing.T) {
4961
{"GUID", []byte("b32a58c6-48c1-4552-8ff0-47680f3416d0"), []byte("b32a58c6-48c1-4552-8ff0-47680f3416d0")},
5062
}
5163

52-
teardownTestCase := setupTestCase(t)
53-
defer teardownTestCase(t)
54-
5564
for _, tc := range cases {
5665
t.Run(tc.name, func(t *testing.T) {
57-
encryptRequest := kmsv1.EncryptRequest{Version: version, Plain: tc.want}
58-
encryptResponse, err := client.Encrypt(context.Background(), &encryptRequest)
66+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
67+
t.Cleanup(cancel)
68+
69+
v1EncryptRequest := kmsv1.EncryptRequest{Version: version, Plain: tc.want}
70+
v1EncryptResponse, err := v1Client.Encrypt(ctx, &v1EncryptRequest)
5971
if err != nil {
60-
t.Fatalf("encrypt request failed with error: %+v", err)
72+
t.Fatalf("encrypt request for KMS v1 failed with error: %+v", err)
73+
}
74+
75+
v1DecryptRequest := kmsv1.DecryptRequest{Version: version, Cipher: v1EncryptResponse.Cipher}
76+
v1DecryptResponse, err := v1Client.Decrypt(ctx, &v1DecryptRequest)
77+
if !bytes.Equal(v1DecryptResponse.Plain, tc.want) {
78+
t.Fatalf("Expected secret, but got %s - %v", string(v1DecryptResponse.Plain), err)
6179
}
6280

63-
decryptRequest := kmsv1.DecryptRequest{Version: version, Cipher: encryptResponse.Cipher}
64-
decryptResponse, err := client.Decrypt(context.Background(), &decryptRequest)
65-
if !bytes.Equal(decryptResponse.Plain, tc.want) {
66-
t.Fatalf("Expected secret, but got %s - %v", string(decryptResponse.Plain), err)
81+
uid := "integration-test-" + string(uuid.NewUUID())
82+
v2EncryptRequest := kmsv2.EncryptRequest{
83+
Plaintext: tc.want,
84+
Uid: uid,
85+
}
86+
v2EncryptResponse, err := v2Client.Encrypt(ctx, &v2EncryptRequest)
87+
if err != nil {
88+
t.Fatalf("encrypt request for KMS v2 failed with error: %+v", err)
89+
}
90+
if v2EncryptResponse.KeyId == "" {
91+
t.Fatalf("Returned KeyId is empty")
92+
}
93+
94+
if v2EncryptResponse.Annotations == nil {
95+
t.Fatalf("Returned Annotations is nil")
96+
}
97+
98+
v2DecryptRequest := kmsv2.DecryptRequest{
99+
Ciphertext: v2EncryptResponse.Ciphertext,
100+
KeyId: v2EncryptResponse.KeyId,
101+
Uid: uid,
102+
Annotations: v2EncryptResponse.Annotations,
103+
}
104+
v2DecryptResponse, err := v2Client.Decrypt(ctx, &v2DecryptRequest)
105+
if !bytes.Equal(v2DecryptResponse.Plaintext, tc.want) {
106+
t.Fatalf("Expected secret, but got %s - %v", string(v2DecryptResponse.Plaintext), err)
67107
}
68108
})
69109
}
70110
}
71111

72112
// Check the KMS provider API version.
73113
// Only matching version is supported now.
74-
func TestVersion(t *testing.T) {
114+
func TestV1Version(t *testing.T) {
75115
cases := []struct {
76116
name string
77117
want string
@@ -80,13 +120,13 @@ func TestVersion(t *testing.T) {
80120
{"v1beta1", "v1beta1", "v1beta1"},
81121
}
82122

83-
teardownTestCase := setupTestCase(t)
84-
defer teardownTestCase(t)
85-
86123
for _, tc := range cases {
87124
t.Run(tc.name, func(t *testing.T) {
125+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
126+
t.Cleanup(cancel)
127+
88128
request := &kmsv1.VersionRequest{Version: tc.want}
89-
response, err := client.Version(context.Background(), request)
129+
response, err := v1Client.Version(ctx, request)
90130
if err != nil {
91131
t.Fatalf("failed get version from remote KMS provider: %v", err)
92132
}
@@ -97,6 +137,39 @@ func TestVersion(t *testing.T) {
97137
}
98138
}
99139

140+
func TestV2Version(t *testing.T) {
141+
cases := []struct {
142+
name string
143+
want string
144+
expected string
145+
}{
146+
{"v2beta1", "v2beta1", "v2beta1"},
147+
}
148+
149+
for _, tc := range cases {
150+
t.Run(tc.name, func(t *testing.T) {
151+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
152+
t.Cleanup(cancel)
153+
154+
request := &kmsv2.StatusRequest{}
155+
response, err := v2Client.Status(ctx, request)
156+
if err != nil {
157+
t.Fatalf("failed get status of remote KMS v2 provider: %v", err)
158+
}
159+
if response.Version != tc.want {
160+
t.Fatalf("KMS v2 provider api version %s is not supported, only %s is supported now", tc.want, version)
161+
}
162+
})
163+
}
164+
}
165+
166+
func TestMain(m *testing.M) {
167+
t = &testing.T{}
168+
setupTestCase()
169+
m.Run()
170+
teardownTestCase()
171+
}
172+
100173
func newUnixSocketConnection(path string) (*grpc.ClientConn, error) {
101174
addr := path
102175
dialer := func(ctx context.Context, addr string) (net.Conn, error) {

0 commit comments

Comments
 (0)