in src/aws_encryption_sdk/materials_managers/caching.py [0:0]
def get_encryption_materials(self, request):
"""Provides encryption materials appropriate for the request.
:param request: Encryption materials request
:type request: aws_encryption_sdk.materials_managers.EncryptionMaterialsRequest
:returns: encryption materials
:rtype: aws_encryption_sdk.materials_managers.EncryptionMaterials
"""
if not self._should_cache_encryption_request(request):
return self.backing_materials_manager.get_encryption_materials(request)
# Inner request strips any information about the plaintext from the actual request.
# This is done because the resulting encryption materials may be used to encrypt
# multiple plaintexts.
inner_request = EncryptionMaterialsRequest(
encryption_context=request.encryption_context,
frame_length=request.frame_length,
algorithm=request.algorithm,
commitment_policy=request.commitment_policy,
)
cache_key = build_encryption_materials_cache_key(partition=self.partition_name, request=inner_request)
# Attempt to retrieve from cache
try:
cache_entry = self.cache.get_encryption_materials(
cache_key=cache_key, plaintext_length=request.plaintext_length
)
except CacheKeyError:
pass
else:
if self._cache_entry_has_exceeded_limits(cache_entry):
self.cache.remove(cache_entry)
else:
return cache_entry.value
# Nothing found in cache: try the material manager
new_result = self.backing_materials_manager.get_encryption_materials(inner_request)
if not new_result.algorithm.safe_to_cache() or request.plaintext_length >= self.max_bytes_encrypted:
return new_result
# Add results into cache
self.cache.put_encryption_materials(
cache_key=cache_key,
encryption_materials=new_result,
plaintext_length=request.plaintext_length,
entry_hints=CryptoMaterialsCacheEntryHints(lifetime=self.max_age),
)
return new_result