-
Notifications
You must be signed in to change notification settings - Fork 47
/
parameterized_test.go
503 lines (400 loc) · 14.2 KB
/
parameterized_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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package appencryption
import (
"context"
"math/rand"
"sort"
"strconv"
"testing"
"time"
"github.com/godaddy/asherah/go/securememory"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/godaddy/asherah/go/appencryption/internal"
)
const (
RETIRED = "RETIRED"
VALID = "VALID"
EMPTY = "EMPTY"
PRODUCT = "PRODUCT"
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
var keyStates = [...]string{RETIRED, VALID, EMPTY}
func TestParameters(t *testing.T) {
for i := 0; i < len(keyStates); i++ {
for j := 0; j < len(keyStates); j++ {
for k := 0; k < len(keyStates); k++ {
for l := 0; l < len(keyStates); l++ {
runParameterizedTest(t, keyStates[i], keyStates[j], keyStates[k], keyStates[l])
}
}
}
}
}
func runParameterizedTest(t *testing.T, cacheIK, metaIK, cacheSK, metaSK string) {
testID, partitionID, serviceID := generateTestID(cacheIK, metaIK, cacheSK, metaSK)
t.Run(testID, func(t *testing.T) {
payload := generatePayload()
crypto := createCryptoMock(payload)
sk, _ := internal.GenerateKey(secretFactory, time.Now().Add(-10*time.Hour).Unix(), AES256KeySize)
defer sk.Close()
ik, _ := internal.GenerateKey(secretFactory, time.Now().Add(-10*time.Hour).Unix(), AES256KeySize)
defer ik.Close()
km := new(MockKMS)
km.On("EncryptKey", mock.Anything, mock.Anything).Return([]byte("encryptedSK"), nil)
km.On("DecryptKey", mock.Anything, []byte("encryptedSK")).Return([]byte("decryptedSK"), nil)
partition := newPartition(partitionID, serviceID, PRODUCT)
metastore := createMetastoreSpy(context.Background(), metaIK, metaSK, ik, sk, crypto, partition, km)
policy := NewCryptoPolicy()
ikCacheEncrypt, skCacheEncrypt := createCache(partition, cacheIK, cacheSK, ik, sk, policy)
defer skCacheEncrypt.Close()
sessionEncrypt := createSession(crypto, metastore, km, secretFactory, policy, partition, ikCacheEncrypt, skCacheEncrypt)
defer sessionEncrypt.Close() // closing the session closes the ikCache
record, err := sessionEncrypt.Encrypt(context.Background(), payload)
if assert.NoError(t, err) && assert.NotNil(t, record) {
ekStates := &encryptKeyStates{
cacheIK: cacheIK,
metaIK: metaIK,
cacheSK: cacheSK,
metaSK: metaSK,
}
verifyEncryptFlow(t, &metastore.Mock, ekStates, partition.IntermediateKeyID(), partition.SystemKeyID())
ikCacheDecrypt, skCacheDecrypt := createCache(partition, cacheIK, cacheSK, ik, sk, policy)
defer skCacheDecrypt.Close()
sessionDecrypt := createSession(crypto, metastore, km, secretFactory, policy, partition, ikCacheDecrypt, skCacheDecrypt)
defer sessionDecrypt.Close() // closing the session closes the ikCache
bytes, err := sessionDecrypt.Decrypt(context.Background(), *record)
if assert.NoError(t, err) && assert.NotNil(t, bytes) {
assert.Equal(t, payload, bytes)
dkStates := &decryptKeyStates{
cacheIK: cacheIK,
cacheSK: cacheSK,
}
verifyDecryptFlow(t, &metastore.Mock, dkStates, partition.IntermediateKeyID(), partition.SystemKeyID())
}
}
})
}
type spyMetastore struct {
mock.Mock
envelopes map[string]map[int64]*EnvelopeKeyRecord
}
func (s *spyMetastore) Load(ctx context.Context, id string, created int64) (*EnvelopeKeyRecord, error) {
ret := s.Called(ctx, id, created)
// If the Load call is not mocked, retrieve the relevant key from the backing map
if ret == nil {
if ret, ok := s.envelopes[id][created]; ok {
return ret, nil
}
return nil, nil
}
return ret.Get(0).(*EnvelopeKeyRecord), ret.Error(1)
}
func (s *spyMetastore) LoadLatest(ctx context.Context, id string) (*EnvelopeKeyRecord, error) {
ret := s.Called(ctx, id)
// If the LoadLatest call is not mocked, retrieve the latest key from the backing map
if ret == nil {
if keyIDMap, ok := s.envelopes[id]; ok {
// Sort submap by key since it is the created time
var createdKeys []int64
for created := range keyIDMap {
createdKeys = append(createdKeys, created)
}
sort.Slice(createdKeys, func(i, j int) bool { return createdKeys[i] < createdKeys[j] })
latestCreated := createdKeys[len(createdKeys)-1]
if ret, ok := keyIDMap[latestCreated]; ok {
return ret, nil
}
}
return nil, nil
}
return ret.Get(0).(*EnvelopeKeyRecord), ret.Error(1)
}
func (s *spyMetastore) Store(ctx context.Context, id string, created int64, envelope *EnvelopeKeyRecord) (bool, error) {
ret := s.Called(ctx, id, created, envelope)
// If the Store call is not mocked, store the key to the backing map
if ret == nil {
// If a value already exists, do not store to the map
if _, ok := s.envelopes[id][created]; ok {
return false, nil
}
// If first time, need to initialize nested map
if _, ok := s.envelopes[id]; !ok {
s.envelopes[id] = make(map[int64]*EnvelopeKeyRecord)
}
// We populate a map so that we can actually decrypt the record in case a new key gets created
s.envelopes[id][created] = envelope
return true, nil
}
return ret.Get(0).(bool), ret.Error(1)
}
func createRevokedKey(src *internal.CryptoKey, factory securememory.SecretFactory) *internal.CryptoKey {
bytes, _ := internal.WithKeyFunc(src, func(bytes []byte) ([]byte, error) {
bytesCopy := make([]byte, len(bytes))
copy(bytesCopy, bytes)
return bytesCopy, nil
})
key, err := internal.NewCryptoKey(factory, src.Created(), true, bytes)
if err != nil {
panic(err)
}
return key
}
func createSession(crypto AEAD, metastore Metastore, kms KeyManagementService, factory securememory.SecretFactory, policy *CryptoPolicy, partition partition, ikCache keyCacher, skCache keyCacher) *Session {
return &Session{
encryption: &envelopeEncryption{
partition: partition,
Metastore: metastore,
KMS: kms,
Policy: policy,
Crypto: crypto,
SecretFactory: factory,
skCache: skCache,
ikCache: ikCache,
},
}
}
func createCache(partition partition, cacheIK, cacheSK string, intermediateKey, systemKey *internal.CryptoKey, policy *CryptoPolicy) (keyCacher, keyCacher) {
var ikCache, skCache keyCacher
skCache = newKeyCache(CacheTypeSystemKeys, policy)
ikCache = newKeyCache(CacheTypeIntermediateKeys, policy)
sk := systemKey
ik := intermediateKey
if cacheSK != EMPTY {
if cacheSK == RETIRED {
// We create a revoked copy of the same key
sk = createRevokedKey(sk, secretFactory)
}
meta := &KeyMeta{
ID: partition.SystemKeyID(),
Created: sk.Created(),
}
// Preload the cache with the system keys
_, _ = skCache.GetOrLoad(*meta, func(_ KeyMeta) (*internal.CryptoKey, error) {
return sk, nil
})
}
if cacheIK != EMPTY {
if cacheIK == RETIRED {
// We create a revoked copy of the same key
ik = createRevokedKey(ik, secretFactory)
}
meta := &KeyMeta{
ID: partition.IntermediateKeyID(),
Created: ik.Created(),
}
// Preload the cache with the intermediate keys
_, _ = ikCache.GetOrLoad(*meta, func(_ KeyMeta) (*internal.CryptoKey, error) {
return ik, nil
})
}
return ikCache, skCache
}
func createMetastoreSpy(ctx context.Context, metaIK, metaSK string, intermediateKey, systemKey *internal.CryptoKey, crypto AEAD, partition partition, km KeyManagementService) *spyMetastore {
metastore := spyMetastore{
envelopes: make(map[string]map[int64]*EnvelopeKeyRecord),
}
metastore.On("Load", mock.Anything, mock.Anything, mock.Anything).Return()
metastore.On("LoadLatest", mock.Anything, mock.Anything).Return()
metastore.On("Store", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return()
sk := systemKey
ik := intermediateKey
if metaSK != EMPTY {
if metaSK == RETIRED {
// We create a revoked copy of the same key
sk = createRevokedKey(sk, secretFactory)
}
encKey, _ := internal.WithKeyFunc(sk, func(keyBytes []byte) ([]byte, error) {
return km.EncryptKey(ctx, keyBytes)
})
ekr := &EnvelopeKeyRecord{
ID: partition.SystemKeyID(),
Revoked: sk.Revoked(),
Created: sk.Created(),
EncryptedKey: encKey,
}
metastore.Store(context.Background(), partition.SystemKeyID(), sk.Created(), ekr)
}
if metaIK != EMPTY {
if metaIK == RETIRED {
// We create a revoked copy of the same key
ik = createRevokedKey(ik, secretFactory)
}
encKey, _ := internal.WithKeyFunc(ik, func(keyBytes []byte) ([]byte, error) {
return internal.WithKeyFunc(sk, func(systemKeyBytes []byte) ([]byte, error) {
return crypto.Encrypt(keyBytes, systemKeyBytes)
})
})
ekr := &EnvelopeKeyRecord{
Revoked: ik.Revoked(),
ID: partition.IntermediateKeyID(),
Created: ik.Created(),
EncryptedKey: encKey,
ParentKeyMeta: &KeyMeta{
ID: partition.SystemKeyID(),
Created: sk.Created(),
},
}
metastore.Store(context.Background(), partition.IntermediateKeyID(), ik.Created(), ekr)
}
metastore.Calls = nil
return &metastore
}
func createCryptoMock(payload []byte) *MockCrypto {
crypto := new(MockCrypto)
// Setup mocking for drr
crypto.On("Encrypt", payload, mock.Anything).Return([]byte("encryptedPayload"), nil)
crypto.On("Decrypt", []byte("encryptedPayload"), mock.Anything).Return(payload, nil)
// Setup mocking for ik and sk
crypto.On("Encrypt", mock.Anything, mock.Anything).Return([]byte("encryptedKey"), nil)
crypto.On("Decrypt", []byte("encryptedKey"), mock.Anything).Return([]byte("decryptedKey"), nil)
return crypto
}
func generatePayload() []byte {
rand.Seed(time.Now().UnixNano())
payload := make([]byte, 32)
for i := range payload {
payload[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return payload
}
func generateTestID(cacheIK, metaIK, cacheSK, metaSK string) (string, string, string) {
testID := cacheIK + "CacheIK_" + metaIK + "MetastoreIK_" + cacheSK + "CacheSK_" + metaSK + "MetastoreSK"
partitionID := cacheIK + "CacheIK_" + metaIK + "MetastoreIK_" + strconv.FormatInt(time.Now().Unix(), 10)
serviceID := cacheSK + "CacheSK_" + metaSK + "MetastoreSK_" + strconv.FormatInt(time.Now().Unix(), 10)
return testID, partitionID, serviceID
}
func verifyEncryptFlow(t *testing.T, metastoreMock *mock.Mock, states *encryptKeyStates, ikID, skID string) {
numberOfCalls := 0
// If IK is stored to metastore
if states.shouldStoreIK() {
metastoreMock.AssertCalled(t, "Store", context.Background(), ikID, mock.Anything, mock.Anything)
numberOfCalls++
} else {
metastoreMock.AssertNotCalled(t, "Store", context.Background(), ikID, mock.Anything, mock.Anything)
}
// If SK is stored to metastore
if states.shouldStoreSK() {
metastoreMock.AssertCalled(t, "Store", context.Background(), skID, mock.Anything, mock.Anything)
numberOfCalls++
} else {
metastoreMock.AssertNotCalled(t, "Store", context.Background(), skID, mock.Anything, mock.Anything)
}
// If neither IK nor SK is stored
if !states.shouldStoreIK() && !states.shouldStoreSK() {
metastoreMock.AssertNotCalled(t, "Store", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
}
metastoreMock.AssertNumberOfCalls(t, "Store", numberOfCalls)
// NOTE: We do not read IK from the metastore in case of Encrypt
numberOfCalls = 0
// If SK is loaded from metastore
if states.shouldLoadSK() {
metastoreMock.AssertCalled(t, "Load", context.Background(), skID, mock.Anything)
numberOfCalls++
} else {
metastoreMock.AssertNotCalled(t, "Load", mock.Anything, mock.Anything, mock.Anything)
}
metastoreMock.AssertNumberOfCalls(t, "Load", numberOfCalls)
numberOfCalls = 0
// If latest IK is loaded from metastore
if states.shouldLoadLatestIK() {
metastoreMock.AssertCalled(t, "LoadLatest", context.Background(), ikID)
numberOfCalls++
} else {
metastoreMock.AssertNotCalled(t, "LoadLatest", context.Background(), ikID)
}
// If latest SK is loaded from metastore
if states.shouldLoadLatestSK() {
metastoreMock.AssertCalled(t, "LoadLatest", context.Background(), skID)
numberOfCalls++
} else {
metastoreMock.AssertNotCalled(t, "LoadLatest", context.Background(), skID)
}
// If neither latest IK or SK is loaded from metastore
if !states.shouldLoadLatestSK() && !states.shouldLoadLatestIK() {
metastoreMock.AssertNotCalled(t, "LoadLatest", mock.Anything, mock.Anything)
}
metastoreMock.AssertNumberOfCalls(t, "LoadLatest", numberOfCalls)
}
func verifyDecryptFlow(t *testing.T, metastoreMock *mock.Mock, states *decryptKeyStates, ikID, skID string) {
// If IK is loaded from metastore
if states.shouldLoadIK() {
metastoreMock.AssertCalled(t, "Load", context.Background(), ikID, mock.Anything)
}
// If SK is loaded from metastore
if states.shouldLoadSK() {
metastoreMock.AssertCalled(t, "Load", context.Background(), skID, mock.Anything)
}
}
type encryptKeyStates struct {
cacheIK, metaIK, cacheSK, metaSK string
}
func (s encryptKeyStates) shouldStoreIK() bool {
if s.cacheIK == VALID {
return false
}
if s.metaIK != VALID {
return true
}
// At this stage IK is valid in metastore
// The existing IK can only be used if the SK is valid in cache
// or if the SK is missing from the cache but is valid in metastore
if s.cacheSK == VALID {
return false
}
if s.cacheSK == EMPTY {
if s.metaSK == VALID {
return false
}
}
return true
}
func (s encryptKeyStates) shouldStoreSK() bool {
if s.cacheIK == VALID {
return false
}
return s.cacheSK != VALID && s.metaSK != VALID
}
func (s encryptKeyStates) shouldLoadSK() bool {
if s.cacheIK == VALID {
return false
}
if s.metaIK != VALID {
return false
}
if s.cacheSK == EMPTY {
return true
}
return false
}
func (s encryptKeyStates) shouldLoadLatestIK() bool {
return s.cacheIK != VALID
}
func (s encryptKeyStates) shouldLoadLatestSK() bool {
if s.cacheIK == VALID {
return false
}
if s.metaIK == VALID {
// Since the cache SK is retired, we create a new IK.
// Because the cache SK happens to be the latest one in cache,
// we need to load the latest SK from metastore during IK creation flow
if s.cacheSK == RETIRED {
return true
}
// Since the SK is not in the cache and not valid in the metastore, we have to create a new IK.
// This requires loading the latest SK from metastore during IK creation flow
return s.cacheSK == EMPTY && s.metaSK != VALID
}
return s.cacheSK != VALID
}
type decryptKeyStates struct {
cacheIK, cacheSK string
}
func (s decryptKeyStates) shouldLoadIK() bool {
return s.cacheIK == EMPTY
}
func (s decryptKeyStates) shouldLoadSK() bool {
if s.shouldLoadIK() {
return s.cacheSK == EMPTY
}
return false
}