16
16
17
17
import attr
18
18
import six
19
+ from attr .validators import instance_of , optional
19
20
20
- from . .caches import (
21
+ from aws_encryption_sdk .caches import (
21
22
CryptoMaterialsCacheEntryHints ,
22
23
build_decryption_materials_cache_key ,
23
24
build_encryption_materials_cache_key ,
24
25
)
25
- from ..caches .base import CryptoMaterialsCache
26
- from ..exceptions import CacheKeyError
27
- from ..internal .defaults import MAX_BYTES_PER_KEY , MAX_MESSAGES_PER_KEY
28
- from ..internal .str_ops import to_bytes
29
- from ..key_providers .base import MasterKeyProvider
26
+ from aws_encryption_sdk .caches .base import CryptoMaterialsCache
27
+ from aws_encryption_sdk .exceptions import CacheKeyError
28
+ from aws_encryption_sdk .internal .defaults import MAX_BYTES_PER_KEY , MAX_MESSAGES_PER_KEY
29
+ from aws_encryption_sdk .internal .str_ops import to_bytes
30
+ from aws_encryption_sdk .key_providers .base import MasterKeyProvider
31
+ from aws_encryption_sdk .keyrings .base import Keyring
32
+
30
33
from . import EncryptionMaterialsRequest
31
34
from .base import CryptoMaterialsManager
32
35
from .default import DefaultCryptoMaterialsManager
@@ -59,17 +62,17 @@ class CachingCryptoMaterialsManager(CryptoMaterialsManager):
59
62
value. If no partition name is provided, a random UUID will be used.
60
63
61
64
.. note::
62
- Either `backing_materials_manager` or `master_key_provider` must be provided.
63
- `backing_materials_manager` will always be used if present.
64
-
65
- :param cache: Crypto cache to use with material manager
66
- :type cache: aws_encryption_sdk.caches.base.CryptoMaterialsCache
67
- :param backing_materials_manager: Crypto material manager to back this caching material manager
68
- (either `backing_materials_manager` or `master_key_provider` required)
69
- :type backing_materials_manager: aws_encryption_sdk.materials_managers.base.CryptoMaterialsManager
70
- :param master_key_provider: Master key provider to use (either `backing_materials_manager` or
71
- `master_key_provider` required)
72
- :type master_key_provider: aws_encryption_sdk.key_providers.base.MasterKeyProvider
65
+ Either `` backing_materials_manager``, ``keyring``, or `` master_key_provider` ` must be provided.
66
+ `` backing_materials_manager` ` will always be used if present.
67
+
68
+ :param CryptoMaterialsCache cache: Crypto cache to use with material manager
69
+ :param CryptoMaterialsManager backing_materials_manager:
70
+ Crypto material manager to back this caching material manager
71
+ (either `` backing_materials_manager``, ``keyring``, or `` master_key_provider` ` required)
72
+ :param MasterKeyProvider master_key_provider: Master key provider to use
73
+ (either `` backing_materials_manager``, ``keyring``, or ``master_key_provider`` required)
74
+ :param Keyring keyring: Keyring to use
75
+ (either ``backing_materials_manager``, ``keyring``, or `` master_key_provider`` required)
73
76
:param float max_age: Maximum time in seconds that a cache entry may be kept in the cache
74
77
:param int max_messages_encrypted: Maximum number of messages that may be encrypted under
75
78
a cache entry (optional)
@@ -78,21 +81,14 @@ class CachingCryptoMaterialsManager(CryptoMaterialsManager):
78
81
:param bytes partition_name: Partition name to use for this instance (optional)
79
82
"""
80
83
81
- cache = attr .ib (validator = attr .validators .instance_of (CryptoMaterialsCache ))
82
- max_age = attr .ib (validator = attr .validators .instance_of (float ))
83
- max_messages_encrypted = attr .ib (
84
- default = MAX_MESSAGES_PER_KEY , validator = attr .validators .instance_of (six .integer_types )
85
- )
86
- max_bytes_encrypted = attr .ib (default = MAX_BYTES_PER_KEY , validator = attr .validators .instance_of (six .integer_types ))
87
- partition_name = attr .ib (
88
- default = None , converter = to_bytes , validator = attr .validators .optional (attr .validators .instance_of (bytes ))
89
- )
90
- master_key_provider = attr .ib (
91
- default = None , validator = attr .validators .optional (attr .validators .instance_of (MasterKeyProvider ))
92
- )
93
- backing_materials_manager = attr .ib (
94
- default = None , validator = attr .validators .optional (attr .validators .instance_of (CryptoMaterialsManager ))
95
- )
84
+ cache = attr .ib (validator = instance_of (CryptoMaterialsCache ))
85
+ max_age = attr .ib (validator = instance_of (float ))
86
+ max_messages_encrypted = attr .ib (default = MAX_MESSAGES_PER_KEY , validator = instance_of (six .integer_types ))
87
+ max_bytes_encrypted = attr .ib (default = MAX_BYTES_PER_KEY , validator = instance_of (six .integer_types ))
88
+ partition_name = attr .ib (default = None , converter = to_bytes , validator = optional (instance_of (bytes )))
89
+ master_key_provider = attr .ib (default = None , validator = optional (instance_of (MasterKeyProvider )))
90
+ backing_materials_manager = attr .ib (default = None , validator = optional (instance_of (CryptoMaterialsManager )))
91
+ keyring = attr .ib (default = None , validator = optional (instance_of (Keyring )))
96
92
97
93
def __attrs_post_init__ (self ):
98
94
"""Applies post-processing which cannot be handled by attrs."""
@@ -111,10 +107,21 @@ def __attrs_post_init__(self):
111
107
if self .max_age <= 0.0 :
112
108
raise ValueError ("max_age cannot be less than or equal to 0" )
113
109
110
+ options_provided = [
111
+ option is not None for option in (self .backing_materials_manager , self .keyring , self .master_key_provider )
112
+ ]
113
+ provided_count = len ([is_set for is_set in options_provided if is_set ])
114
+
115
+ if provided_count != 1 :
116
+ raise TypeError ("Exactly one of 'materials_manager', 'keyring', or 'key_provider' must be provided" )
117
+
114
118
if self .backing_materials_manager is None :
115
- if self .master_key_provider is None :
116
- raise TypeError ("Either backing_materials_manager or master_key_provider must be defined" )
117
- self .backing_materials_manager = DefaultCryptoMaterialsManager (self .master_key_provider )
119
+ if self .master_key_provider is not None :
120
+ self .backing_materials_manager = DefaultCryptoMaterialsManager (
121
+ master_key_provider = self .master_key_provider
122
+ )
123
+ else :
124
+ self .backing_materials_manager = DefaultCryptoMaterialsManager (keyring = self .keyring )
118
125
119
126
if self .partition_name is None :
120
127
self .partition_name = to_bytes (str (uuid .uuid4 ()))
0 commit comments