-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathtest_most_recent.py
585 lines (419 loc) · 22.5 KB
/
test_most_recent.py
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Functional tests for ``dynamodb_encryption_sdk.material_providers.most_recent``."""
import time
from collections import defaultdict
import pytest
from mock import MagicMock, sentinel
from dynamodb_encryption_sdk.exceptions import NoKnownVersionError
from dynamodb_encryption_sdk.material_providers import CryptographicMaterialsProvider
from dynamodb_encryption_sdk.material_providers.most_recent import CachingMostRecentProvider, TtlActions
from dynamodb_encryption_sdk.material_providers.store import ProviderStore
from ..functional_test_utils import example_table # noqa=F401 pylint: disable=unused-import
from ..functional_test_utils import mock_ddb_service # noqa=F401 pylint: disable=unused-import
from ..functional_test_utils import mock_metastore # noqa=F401 pylint: disable=unused-import
from ..functional_test_utils import TEST_TABLE_NAME, check_metastore_cache_use_encrypt
pytestmark = [pytest.mark.functional, pytest.mark.local]
class SentinelCryptoMaterialsProvider(CryptographicMaterialsProvider):
def __init__(self, name, version):
self.name = name
self.version = version
self._material_description = version
self.provider_calls = []
def encryption_materials(self, encryption_context):
self.provider_calls.append(("encryption_materials", encryption_context))
return getattr(sentinel, "{name}_{version}_encryption".format(name=self.name, version=self.version))
def decryption_materials(self, encryption_context):
self.provider_calls.append(("decryption_materials", encryption_context))
return getattr(sentinel, "{name}_{version}_decryption".format(name=self.name, version=self.version))
class MockProviderStore(ProviderStore):
def __init__(self):
self.provider_calls = []
self._providers = defaultdict(dict)
def get_or_create_provider(self, material_name, version):
self.provider_calls.append(("get_or_create_provider", material_name, version))
try:
return self._providers[material_name][version]
except KeyError:
self._providers[material_name][version] = SentinelCryptoMaterialsProvider(material_name, version)
return self._providers[material_name][version]
def max_version(self, material_name):
self.provider_calls.append(("max_version", material_name))
try:
return sorted(self._providers[material_name].keys())[-1]
except IndexError:
raise NoKnownVersionError('No known version for name: "{}"'.format(material_name))
def version_from_material_description(self, material_description):
self.provider_calls.append(("version_from_material_description", material_description))
return material_description
def test_constructor():
"""Tests that when the cache is expired on encrypt, we evict the entry from the cache."""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=1.0, cache_size=42)
assert provider._provider_store == store
assert provider._material_name == name
assert provider._version_ttl == 1.0
assert provider._cache.capacity == 42
def test_ttl_action_first_encrypt():
"""Test that when _last_updated has never been set, ttl_action returns TtlActions.EXPIRED."""
store = MagicMock(__class__=ProviderStore)
provider = CachingMostRecentProvider(provider_store=store, material_name="my material", version_ttl=10.0)
assert provider._last_updated is None
ttl_action = provider._ttl_action(0, "encrypt")
assert ttl_action is TtlActions.EXPIRED
def test_ttl_action_first_encrypt_previous_decrypt():
"""Test that on the first call to encrypt, ttl_action returns TtlActions.EXPIRED."""
version = 0
store = MagicMock(__class__=ProviderStore)
provider = CachingMostRecentProvider(provider_store=store, material_name="my material", version_ttl=10.0)
provider._cache.put(version, "bar")
assert provider._last_updated is None
ttl_action = provider._ttl_action(version, "encrypt")
assert ttl_action is TtlActions.EXPIRED
def test_ttl_action_not_in_cache():
"""Test that when a version is not in the cache, ttl_action returns TtlActions.EXPIRED."""
store = MagicMock(__class__=ProviderStore)
provider = CachingMostRecentProvider(provider_store=store, material_name="my material", version_ttl=10.0)
assert provider._last_updated is None
ttl_action = provider._ttl_action(0, "decrypt")
assert ttl_action is TtlActions.EXPIRED
def test_ttl_action_live():
"""Test that when a version is within the ttl, ttl_action returns TtlActions.LIVE."""
version = 0
store = MagicMock(__class__=ProviderStore)
provider = CachingMostRecentProvider(provider_store=store, material_name="my material", version_ttl=10.0)
provider._cache.put(version, (time.time(), "value"))
assert provider._last_updated is None
ttl_action = provider._ttl_action(version, "decrypt")
assert ttl_action is TtlActions.LIVE
def test_ttl_action_grace_period():
"""Test that when a version is in the grace period, ttl_action returns TtlActions.GRACE_PERIOD."""
version = 0
store = MagicMock(__class__=ProviderStore)
provider = CachingMostRecentProvider(provider_store=store, material_name="my material", version_ttl=0.0)
provider._grace_period = 10.0
provider._cache.put(version, (time.time(), "value"))
assert provider._last_updated is None
ttl_action = provider._ttl_action(version, "decrypt")
assert ttl_action is TtlActions.GRACE_PERIOD
def test_ttl_action_expired():
"""Test that when a version is expired and not in the grace period, ttl_action returns TtlActions.EXPIRED."""
version = 0
store = MagicMock(__class__=ProviderStore)
provider = CachingMostRecentProvider(provider_store=store, material_name="my material", version_ttl=0.0)
provider._grace_period = 0.0
provider._cache.put(version, (time.time(), "value"))
assert provider._last_updated is None
ttl_action = provider._ttl_action(version, "decrypt")
assert ttl_action is TtlActions.EXPIRED
def test_get_provider_with_grace_period_expired():
"""Test for _get_provider_with_grace_period when entry is expired.
When the entry is expired, we should check the cache before going to the provider store.
"""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
provider._cache = MagicMock()
provider._cache.get.return_value = (sentinel.timestamp, sentinel.provider)
test1 = provider._get_provider_with_grace_period(sentinel.version, TtlActions.EXPIRED)
assert test1 == sentinel.provider
expected_calls = []
assert store.provider_calls == expected_calls
def test_get_provider_with_grace_period_grace_period_lock_acquired():
"""Test for _get_provider_with_grace_period when entry is in grace period.
When the entry is in grace_period and we acquire the lock, we should go to the provider store
"""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
provider._get_provider_with_grace_period(sentinel.version, TtlActions.GRACE_PERIOD)
assert len(provider._cache._cache) == 1
expected_calls = [("get_or_create_provider", name, sentinel.version)]
assert store.provider_calls == expected_calls
def test_get_provider_with_grace_period_grace_period_lock_not_acquired():
"""Test for _get_provider_with_grace_period when entry is in grace period.
When the entry is in grace_period and we do not acquire the lock, we should not go to the provider store
"""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
provider._cache = MagicMock()
provider._cache.get.return_value = (sentinel.timestamp, sentinel.provider)
provider._lock = MagicMock()
provider._lock.acquire.return_value = False
test = provider._get_provider_with_grace_period(sentinel.version, TtlActions.GRACE_PERIOD)
assert test == sentinel.provider
expected_calls = []
assert store.provider_calls == expected_calls
def test_get_most_recent_version_expired():
"""Test for _get_most_recent_version when entry is expired.
When the entry is expired, we should check the cache before going to the provider store.
"""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
provider._cache = MagicMock()
provider._cache.get.return_value = (sentinel.timestamp, sentinel.provider)
test1 = provider._get_most_recent_version(TtlActions.EXPIRED)
assert test1 == sentinel.provider
expected_calls = []
assert store.provider_calls == expected_calls
def test_get_most_recent_version_grace_period_lock_acquired():
"""Test for _get_most_recent_version when entry is in grace period.
When the entry is in grace_period and we acquire the lock, we should go to the provider store
"""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
provider._get_most_recent_version(TtlActions.GRACE_PERIOD)
assert len(provider._cache._cache) == 1
expected_calls = [
("max_version", name),
("get_or_create_provider", name, 0),
("version_from_material_description", 0),
]
assert store.provider_calls == expected_calls
def test_get_most_recent_version_grace_period_lock_not_acquired():
"""Test for _get_most_recent_version when entry is in grace period.
When the entry is in grace_period and we do not acquire the lock, we should not go to the provider store
"""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
provider._cache = MagicMock()
provider._cache.get.return_value = (sentinel.timestamp, sentinel.provider)
provider._lock = MagicMock()
provider._lock.acquire.return_value = False
test = provider._get_most_recent_version(TtlActions.GRACE_PERIOD)
assert test == sentinel.provider
expected_calls = []
assert store.provider_calls == expected_calls
def test_failed_lock_acquisition():
store = MagicMock(__class__=ProviderStore)
provider = CachingMostRecentProvider(provider_store=store, material_name="my material", version_ttl=10.0)
provider._version = 9
provider._cache.put(provider._version, (time.time(), sentinel.nine))
with provider._lock:
test = provider._get_most_recent_version(ttl_action=TtlActions.GRACE_PERIOD)
assert test is sentinel.nine
assert not store.mock_calls
def test_encryption_materials_cache_use():
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=10.0)
test1 = provider.encryption_materials(sentinel.encryption_context_1)
assert test1 is sentinel.material_0_encryption
assert provider._version == 0
assert len(provider._cache._cache) == 1
expected_calls = [
("max_version", name),
("get_or_create_provider", name, 0),
("version_from_material_description", 0),
]
assert store.provider_calls == expected_calls
test2 = provider.encryption_materials(sentinel.encryption_context_1)
assert test2 is sentinel.material_0_encryption
assert provider._version == 0
assert len(provider._cache._cache) == 1
assert store.provider_calls == expected_calls
def test_encryption_materials_cache_expired():
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
test1 = provider.encryption_materials(sentinel.encryption_context_1)
assert test1 is sentinel.material_0_encryption
assert provider._version == 0
assert len(provider._cache._cache) == 1
# On the first call, we expect calls to each of the provider's APIs
expected_calls = [
("max_version", name),
("get_or_create_provider", name, 0),
("version_from_material_description", 0),
]
assert store.provider_calls == expected_calls
test2 = provider.encryption_materials(sentinel.encryption_context_1)
assert test2 is sentinel.material_0_encryption
assert provider._version == 0
assert len(provider._cache._cache) == 1
# On the second call, we don't call get_or_create because max_version matches the version in the cache.
expected_calls.append(("max_version", name))
expected_calls.append(("version_from_material_description", 0))
assert store.provider_calls == expected_calls
def test_encryption_materials_cache_expired_cache_removed():
"""Tests that when the cache is expired on encrypt, we evict the entry from the cache."""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
provider._cache = MagicMock()
provider._cache.get.return_value = (0.0, MagicMock())
provider.encryption_materials(sentinel.encryption_context_1)
provider._cache.evict.assert_called_once()
def test_decryption_materials_cache_expired_cache_removed():
"""Tests that when the cache is expired on decrypt, we evict the entry from the cache."""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
provider._cache = MagicMock()
provider._cache.get.return_value = (0.0, MagicMock())
provider.encryption_materials(sentinel.encryption_context_1)
provider._cache.evict.assert_called_once()
def test_encryption_materials_cache_in_grace_period_acquire_lock():
"""Test encryption grace period behavior.
When the TTL is GRACE_PERIOD and we successfully acquire the lock for retrieving new materials,
we call to the provider store for new materials.
"""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
provider._grace_period = 10.0
test1 = provider.encryption_materials(sentinel.encryption_context_1)
assert test1 is sentinel.material_0_encryption
assert provider._version == 0
assert len(provider._cache._cache) == 1
# On the first call, we expect calls to each of the provider's APIs
expected_calls = [
("max_version", name),
("get_or_create_provider", name, 0),
("version_from_material_description", 0),
]
assert store.provider_calls == expected_calls
provider._lock = MagicMock()
provider._lock.acquire.return_value = True
test2 = provider.encryption_materials(sentinel.encryption_context_1)
assert test2 is sentinel.material_0_encryption
assert provider._version == 0
assert len(provider._cache._cache) == 1
# On the second call, we acquired the lock so we should have tried to retrieve new materials (note no extra call
# to get_or_create_provider, because the version has not changed)
expected_calls.append(("max_version", name))
expected_calls.append(("version_from_material_description", 0))
assert store.provider_calls == expected_calls
def test_encryption_materials_cache_in_grace_period_fail_to_acquire_lock():
"""Test encryption grace period behavior.
When the TTL is GRACE_PERIOD and we fail to acquire the lock for retrieving new materials,
we use the materials from the cache.
"""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
provider._grace_period = 10.0
test1 = provider.encryption_materials(sentinel.encryption_context_1)
assert test1 is sentinel.material_0_encryption
assert provider._version == 0
assert len(provider._cache._cache) == 1
# On the first call, we expect calls to each of the provider's APIs
expected_calls = [
("max_version", name),
("get_or_create_provider", name, 0),
("version_from_material_description", 0),
]
assert store.provider_calls == expected_calls
# Now that the cache is populated, pretend the lock cannot be acquired; grace_period should allow the cached value
provider._lock = MagicMock()
provider._lock.acquire.return_value = False
test2 = provider.encryption_materials(sentinel.encryption_context_1)
assert test2 is sentinel.material_0_encryption
assert provider._version == 0
assert len(provider._cache._cache) == 1
# On the second call, we expect no additional calls because we are in our grace period.
assert store.provider_calls == expected_calls
def test_decryption_materials_cache_use():
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=10.0)
context = MagicMock(material_description=0)
test1 = provider.decryption_materials(context)
assert test1 is sentinel.material_0_decryption
assert len(provider._cache._cache) == 1
expected_calls = [("version_from_material_description", 0), ("get_or_create_provider", name, 0)]
assert store.provider_calls == expected_calls
test2 = provider.decryption_materials(context)
assert test2 is sentinel.material_0_decryption
assert len(provider._cache._cache) == 1
expected_calls.append(("version_from_material_description", 0))
assert store.provider_calls == expected_calls
def test_caching_provider_decryption_materials_cache_expired():
"""Test decryption expiration behavior for CachingMostRecentProvider.
When using a CachingMostRecentProvider and the cache is expired on decryption, we retrieve materials
from the provider store again.
Note that this test only runs for CachingMostRecentProvider, as MostRecentProvider does not use TTL on decryption.
"""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
context = MagicMock(material_description=0)
test1 = provider.decryption_materials(context)
assert test1 is sentinel.material_0_decryption
assert len(provider._cache._cache) == 1
expected_calls = [("version_from_material_description", 0), ("get_or_create_provider", name, 0)]
assert store.provider_calls == expected_calls
test2 = provider.decryption_materials(context)
assert test2 is sentinel.material_0_decryption
assert len(provider._cache._cache) == 1
# With the cache expired, we should see another call to get_or_create_provider
expected_calls.append(("version_from_material_description", 0))
expected_calls.append(("get_or_create_provider", name, 0))
assert store.provider_calls == expected_calls
def test_caching_provider_decryption_materials_cache_in_grace_period_acquire_lock():
"""Test decryption grace period behavior for CachingMostRecentProvider.
When using a CachingMostRecentProvider and the cache is in grace period on decryption and we
successfully acquire the lock, we retrieve new materials.
Note that this test only runs for CachingMostRecentProvider, as MostRecentProvider does not use TTL on decryption.
"""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
provider._grace_period = 10.0
context = MagicMock(material_description=0)
test1 = provider.decryption_materials(context)
assert test1 is sentinel.material_0_decryption
assert len(provider._cache._cache) == 1
expected_calls = [("version_from_material_description", 0), ("get_or_create_provider", name, 0)]
assert store.provider_calls == expected_calls
provider._lock = MagicMock()
provider._lock.acquire.return_value = True
test2 = provider.decryption_materials(context)
assert test2 is sentinel.material_0_decryption
assert len(provider._cache._cache) == 1
# Since we successfully acquired the lock we should have made a new call to the provider store
expected_calls.append(("version_from_material_description", 0))
expected_calls.append(("get_or_create_provider", name, 0))
assert store.provider_calls == expected_calls
def test_caching_provider_decryption_materials_cache_in_grace_period_fail_to_acquire_lock():
"""Test decryption grace period behavior for CachingMostRecentProvider.
When using a CachingMostRecentProvider and the cache is in grace period on decryption and we fail to
acquire the lock, we use materials from the cache.
Note that this test only runs for CachingMostRecentProvider, as MostRecentProvider does not use TTL on decryption.
"""
store = MockProviderStore()
name = "material"
provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0)
provider._grace_period = 10.0
context = MagicMock(material_description=0)
test1 = provider.decryption_materials(context)
assert test1 is sentinel.material_0_decryption
assert len(provider._cache._cache) == 1
expected_calls = [("version_from_material_description", 0), ("get_or_create_provider", name, 0)]
assert store.provider_calls == expected_calls
# Now that the cache is populated, pretend the lock cannot be acquired; grace_period should allow the cached value
provider._lock = MagicMock()
provider._lock.acquire.return_value = False
test2 = provider.decryption_materials(context)
assert test2 is sentinel.material_0_decryption
assert len(provider._cache._cache) == 1
# Since we used the cache value, we should not see another call to get_or_create_provider
expected_calls.append(("version_from_material_description", 0))
assert store.provider_calls == expected_calls
def test_cache_use_encrypt(mock_metastore, example_table, caplog):
check_metastore_cache_use_encrypt(mock_metastore, TEST_TABLE_NAME, caplog)