-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
445 lines (398 loc) Β· 13.8 KB
/
integration_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
// Copyright 2020 - MinIO, Inc. All rights reserved.
// Use of this source code is governed by the AGPLv3
// license that can be found in the LICENSE file.
package kes_test
import (
"bytes"
"context"
"crypto/rand"
"crypto/tls"
"encoding/base64"
"flag"
"fmt"
"sort"
"strings"
"testing"
"github.com/minio/kes"
)
// The integration tests require a KES server instance to run against.
// By default, this instance is a KES server running at https://play.min.io:7373.
//
// The client running the test has to have extensive privileges since it calls
// most/all server APIs. Typically the root identity is used for this purpose.
// By default, the client is the root identity of the https://play.min.io:7373 instance.
//
// When running the integration test the endpoint, client key and client certificate
// can be customized with the corresponding flags listed below.
//
// To run the integration tests (in addition to unit tests) the '-integration' flag has
// to be set. By default, only unit tests and no integration tests are executed.
//
// If the KES server uses an e.g. self-signed TLS certificate, i.e. when running a local
// KES server, the client-side TLS certificate verification can be disabled with the '-k' flag.
//
// Examples:
//
// Run the integration tests against https://play.min.io using the default root identity:
// go test -v -integration
//
// Only run the 'CreateKey' integration test against https://play.min.io using the default root identity:
// go test -v -integration -run=CreateKey
//
// Run the integration tests against a local KES server (https://127.0.0.1:7373) using
// a custom root identity.
// go test -v -k -integration -endpoint=https://127.0.0.1:7373 -key=<client.key> -cert<client.cert>
var (
IsIntegrationTest = flag.Bool("integration", false, "Run integration tests in addition to unit tests")
Endpoint = flag.String("endpoint", "https://play.min.io:7373", "The KES server endpoint for integration tests")
ClientKey = flag.String("key", "root.key", "Path to the client private key for integration tests")
ClientCert = flag.String("cert", "root.cert", "Path to the client certificate for integration tests")
InsecureSkipVerify = flag.Bool("k", false, "Disable X.509 certificate verification")
)
func TestVersion(t *testing.T) {
if !*IsIntegrationTest {
t.SkipNow()
}
client, err := newClient()
if err != nil {
t.Fatalf("Failed to create KES client: %v", err)
}
if _, err := client.Version(context.Background()); err != nil {
t.Fatalf("Failed to fetch KES server version: %v", err)
}
}
func TestStatus(t *testing.T) {
if !*IsIntegrationTest {
t.SkipNow()
}
client, err := newClient()
if err != nil {
t.Fatalf("Failed to create KES client: %v", err)
}
state, err := client.Status(context.Background())
if err != nil {
t.Fatalf("Failed to fetch KES server state: %v", err)
}
if state.UpTime == 0 {
t.Fatal("The KES server up time cannot be 0")
}
}
func TestCreateKey(t *testing.T) {
if !*IsIntegrationTest {
t.SkipNow()
}
client, err := newClient()
if err != nil {
t.Fatalf("Failed to create KES client: %v", err)
}
key := fmt.Sprintf("KES-test-%x", randomBytes(12))
if err := client.CreateKey(context.Background(), key); err != nil {
t.Fatalf("Failed to create key '%s': %v", key, err)
}
defer client.DeleteKey(context.Background(), key) // Cleanup
if err := client.CreateKey(context.Background(), key); err != kes.ErrKeyExists {
t.Fatalf("Creating the key '%s' twice should have failed: got %v - want %v", key, err, kes.ErrKeyExists)
}
}
func TestDeleteKey(t *testing.T) {
if !*IsIntegrationTest {
t.SkipNow()
}
client, err := newClient()
if err != nil {
t.Fatalf("Failed to create KES client: %v", err)
}
key := fmt.Sprintf("KES-test-%x", randomBytes(12))
if err := client.CreateKey(context.Background(), key); err != nil {
t.Fatalf("Failed to create key '%s': %v", key, err)
}
if err := client.DeleteKey(context.Background(), key); err != nil {
t.Fatalf("Failed to delete key '%s': %v", key, err)
}
if err := client.DeleteKey(context.Background(), key); err != nil {
t.Fatalf("Failed to delete key '%s' a 2nd time: %v", key, err)
}
}
var importKeyTests = []struct {
Key []byte
Plaintext []byte
Context []byte
Ciphertext []byte
}{
{
Key: make([]byte, 32),
Plaintext: make([]byte, 32),
Context: nil,
Ciphertext: mustDecodeB64("eyJhZWFkIjoiQUVTLTI1Ni1HQ00tSE1BQy1TSEEtMjU2IiwiaXYiOiJ1SUlmSG1OanY2MGRBbUlRL0haT3JBPT0iLCJub25jZSI6IlNEdi8wTlpWaG02R1lGS0wiLCJieXRlcyI6InBqU204UDkyRXlzZE5GZW4rQWdJUEQxeWl4KzNmWTZvUkE0SGdXYzdlZ1J5ckZtNzJ0Z1dYUitFTVlrRHZxYmUifQ=="),
},
{
Key: make([]byte, 32),
Plaintext: mustDecodeB64("FO+Mnrs7Lm/+ejCikk2Xxh1ptfPK8eBwk08WqOTIQ38="),
Context: nil,
Ciphertext: mustDecodeB64("eyJhZWFkIjoiQUVTLTI1Ni1HQ00tSE1BQy1TSEEtMjU2IiwiaXYiOiJURWR5c0RaSlpBUExRd1FXdnhTL2R3PT0iLCJub25jZSI6ImIxbGphZVBiR0RnUUtwVkkiLCJieXRlcyI6IkxRWHBSS0Jra1UzbjJ0bVVzT09hOS9YN1lJRGdTU2VWNXZCcm9NWXhDNGtvMkNWd25MaFB5WXNrZVN6UkM1MWwifQ=="),
},
{
Key: mustDecodeB64("Ocxv4Vf3eur17x6R0mO6P15KPj+L7h2qpe6ZxRy5eiE="),
Plaintext: mustDecodeB64("WKDdYkXJ21/HD9lNNBdbUJ3UuwoND/a7eC5bh+0Tbn2DeFSp5IzDe8bOgqK+7F7ortyViprO7Zwt5GF67/ooXQ=="),
Context: mustDecodeB64("Eb2sb9zyRPKXbgu5"),
Ciphertext: mustDecodeB64("eyJhZWFkIjoiQUVTLTI1Ni1HQ00tSE1BQy1TSEEtMjU2IiwiaXYiOiJGd043WU04ZlVzU1loUFdzZVBmRUt3PT0iLCJub25jZSI6ImFoeG9GYmh1V0IzVHZma1oiLCJieXRlcyI6Im9rY241MUZwNUJsZEoxbGN3ZThLREJXZUhzZEhVQllaaUNkUWxrQXREak9rV1R6TlZvWW05ZEswRXRPZmw3MG1zNVZWSmxqdnZWNTF0VFFhSWFDK2NZTndUSjl5VXNYdHpkUUR2L0lKdHFvPSJ9"),
},
}
func TestImportKey(t *testing.T) {
if !*IsIntegrationTest {
t.SkipNow()
}
client, err := newClient()
if err != nil {
t.Fatalf("Failed to create KES client: %v", err)
}
key := fmt.Sprintf("KES-test-%x", randomBytes(12))
for i, test := range importKeyTests {
if err := client.ImportKey(context.Background(), key, test.Key); err != nil {
t.Fatalf("Failed to import key '%s': %v", key, err)
}
plaintext, err := client.Decrypt(context.Background(), key, test.Ciphertext, test.Context)
if err != nil {
client.DeleteKey(context.Background(), key) // Cleanup
t.Fatalf("Test %d: Failed to decrypt ciphertext: %v", i, err)
}
if !bytes.Equal(plaintext, test.Plaintext) {
client.DeleteKey(context.Background(), key) // Cleanup
t.Fatalf("Test %d: Plaintext mismatch: got '%s' - want '%s'", i, plaintext, test.Plaintext)
}
client.DeleteKey(context.Background(), key) // Cleanup
}
}
var generateKeyTests = []struct {
Context []byte
ShouldFail bool
}{
{Context: nil, ShouldFail: false},
{Context: []byte("Request made by MinIO instance 3afe...49ff"), ShouldFail: false},
{Context: make([]byte, 512*1024), ShouldFail: false},
{Context: make([]byte, 1024*1024), ShouldFail: true}, // exceeds request size limit
}
func TestGenerateKey(t *testing.T) {
if !*IsIntegrationTest {
t.SkipNow()
}
client, err := newClient()
if err != nil {
t.Fatalf("Failed to create KES client: %v", err)
}
key := fmt.Sprintf("KES-test-%x", randomBytes(12))
if err := client.CreateKey(context.Background(), key); err != nil {
t.Fatalf("Failed to create key '%s': %v", key, err)
}
defer client.DeleteKey(context.Background(), key) // Cleanup
for i, test := range generateKeyTests {
dek, err := client.GenerateKey(context.Background(), key, test.Context)
if err == nil && test.ShouldFail {
t.Fatalf("Test %d: Test should have failed but succeeded", i)
}
if err != nil && !test.ShouldFail {
t.Fatalf("Test %d: Failed to generate DEK: %v", i, err)
}
if !test.ShouldFail {
plaintext, err := client.Decrypt(context.Background(), key, dek.Ciphertext, test.Context)
if err != nil {
t.Fatalf("Test %d: Failed to decrypt ciphertext: %v", i, err)
}
if !bytes.Equal(plaintext, dek.Plaintext) {
t.Fatalf("Test %d: Plaintext mismatch: got '%s' - want '%s'", i, plaintext, dek.Plaintext)
}
}
}
}
var encryptKeyTests = []struct {
Plaintext []byte
Context []byte
ShouldFail bool
}{
{Plaintext: nil, Context: nil, ShouldFail: false},
{Plaintext: []byte("Hello World"), Context: nil, ShouldFail: false},
{Plaintext: nil, Context: []byte("Request made by MinIO instance 3afe...49ff"), ShouldFail: false},
{Plaintext: []byte("Hello World"), Context: []byte("Request made by MinIO instance 3afe...49ff"), ShouldFail: false},
{Plaintext: make([]byte, 512*1024), Context: make([]byte, 512*1024), ShouldFail: true}, // exceeds request size limit
{Plaintext: make([]byte, 1024*1024), Context: nil, ShouldFail: true}, // exceeds request size limit
}
func TestEncryptKey(t *testing.T) {
if !*IsIntegrationTest {
t.SkipNow()
}
client, err := newClient()
if err != nil {
t.Fatalf("Failed to create KES client: %v", err)
}
key := fmt.Sprintf("KES-test-%x", randomBytes(12))
if err := client.CreateKey(context.Background(), key); err != nil {
t.Fatalf("Failed to create key '%s': %v", key, err)
}
defer client.DeleteKey(context.Background(), key) // Cleanup
for i, test := range encryptKeyTests {
_, err = client.Encrypt(context.Background(), key, test.Plaintext, test.Context)
if err == nil && test.ShouldFail {
t.Fatalf("Test %d: Test should have failed but succeeded", i)
}
if err != nil && !test.ShouldFail {
t.Fatalf("Test %d: Failed to generate DEK: %v", i, err)
}
}
}
var decryptKeyTests = []struct {
Plaintext []byte
Context []byte
}{
{Plaintext: nil, Context: nil},
{Plaintext: []byte("Hello World"), Context: nil},
{Plaintext: nil, Context: []byte("Request made by MinIO instance 3afe...49ff")},
{Plaintext: []byte("Hello World"), Context: []byte("Request made by MinIO instance 3afe...49ff")},
}
func TestDecryptKey(t *testing.T) {
if !*IsIntegrationTest {
t.SkipNow()
}
client, err := newClient()
if err != nil {
t.Fatalf("Failed to create KES client: %v", err)
}
key := fmt.Sprintf("KES-test-%x", randomBytes(12))
if err := client.CreateKey(context.Background(), key); err != nil {
t.Fatalf("Failed to create key '%s': %v", key, err)
}
defer client.DeleteKey(context.Background(), key) // Cleanup
for i, test := range decryptKeyTests {
ciphertext, err := client.Encrypt(context.Background(), key, test.Plaintext, test.Context)
if err != nil {
t.Fatalf("Test %d: Failed to encrypt plaintext: %v", i, err)
}
plaintext, err := client.Decrypt(context.Background(), key, ciphertext, test.Context)
if err != nil {
t.Fatalf("Test %d: Failed to decrypt ciphertext: %v", i, err)
}
if !bytes.Equal(plaintext, test.Plaintext) {
t.Fatalf("Test %d: Plaintext mismatch: got '%s' - want '%s'", i, plaintext, test.Plaintext)
}
}
}
var listKeysTests = []struct {
Keys []string
Pattern string
Listing []kes.KeyInfo
}{
{
Keys: []string{"my-key", "my-key1", "my-key2", "my-key3"},
Pattern: "*",
Listing: []kes.KeyInfo{
{Name: "my-key"},
{Name: "my-key1"},
{Name: "my-key2"},
{Name: "my-key3"},
},
},
{
Keys: []string{"my-key", "my-key1", "my-key2", "my-key3"},
Pattern: "my-key*",
Listing: []kes.KeyInfo{
{Name: "my-key"},
{Name: "my-key1"},
{Name: "my-key2"},
{Name: "my-key3"},
},
},
}
func TestListKeys(t *testing.T) {
if !*IsIntegrationTest {
t.SkipNow()
}
client, err := newClient()
if err != nil {
t.Fatalf("Failed to create KES client: %v", err)
}
f := func(t *testing.T, i int, names []string, pattern string, listing ...kes.KeyInfo) {
for _, name := range names {
if err := client.CreateKey(context.Background(), name); err != nil && err != kes.ErrKeyExists {
t.Fatalf("Test %d: Failed to create key %q: %v", i, name, err)
}
defer client.DeleteKey(context.Background(), name)
}
keys, err := client.ListKeys(context.Background(), pattern)
if err != nil {
t.Fatalf("Test %d: Failed to list keys: %v", i, err)
}
var descriptions []kes.KeyInfo
for keys.Next() {
descriptions = append(descriptions, keys.Value())
}
if err = keys.Close(); err != nil {
t.Fatalf("Test %d: Failed to list keys: %v", i, err)
}
if len(descriptions) != len(listing) {
t.Fatalf("Test %d: Listings don't match: got %d elements - want %d", i, len(descriptions), len(listing))
}
sort.Slice(descriptions, func(j, k int) bool {
return strings.Compare(descriptions[j].Name, descriptions[k].Name) < 0
})
for j := range descriptions {
if descriptions[j] != listing[j] {
t.Fatalf("Test %d: Listings don't match: got %d-th element '%v' - want '%v'", i, j, descriptions[j], listing[j])
}
}
}
// The random prefix is added to the pattern and the key names
// to ensure that the test does not fail on a KES server with
// existing keys.
prefix := fmt.Sprintf("%x-", randomBytes(12))
for i, test := range listKeysTests {
test.Pattern = prefix + test.Pattern
for j := range test.Keys {
test.Keys[j] = prefix + test.Keys[j]
}
for j := range test.Listing {
test.Listing[j].Name = prefix + test.Listing[j].Name
}
f(t, i, test.Keys, test.Pattern, test.Listing...)
}
}
func TestMetrics(t *testing.T) {
if !*IsIntegrationTest {
t.SkipNow()
}
client, err := newClient()
if err != nil {
t.Fatalf("Failed to create KES client: %v", err)
}
metric, err := client.Metrics(context.Background())
if err != nil {
t.Fatalf("Failed to fetch KES metrics: %v", err)
}
N := metric.RequestOK + metric.RequestErr + metric.RequestFail
if n := metric.RequestN(); n != N {
t.Fatalf("Invalid server metrics: incorrect request count: got %d - want %d", n, N)
}
}
func newClient() (*kes.Client, error) {
certificate, err := tls.LoadX509KeyPair(*ClientCert, *ClientKey)
if err != nil {
return nil, err
}
return kes.NewClientWithConfig(*Endpoint, &tls.Config{
Certificates: []tls.Certificate{certificate},
InsecureSkipVerify: *InsecureSkipVerify,
}), nil
}
func mustDecodeB64(s string) []byte {
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return b
}
func randomBytes(length int) []byte {
b := make([]byte, length)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
return b
}