17
17
import botocore .client
18
18
19
19
from dynamodb_encryption_sdk .internal .utils import (
20
+ crypto_config_from_cache , crypto_config_from_kwargs ,
20
21
decrypt_batch_get_item , decrypt_get_item , decrypt_multi_get ,
21
22
encrypt_batch_write_item , encrypt_put_item , TableInfoCache
22
23
)
23
24
from dynamodb_encryption_sdk .material_providers import CryptographicMaterialsProvider
24
- from dynamodb_encryption_sdk .structures import AttributeActions , EncryptionContext
25
- from . import CryptoConfig
25
+ from dynamodb_encryption_sdk .structures import AttributeActions
26
26
from .item import decrypt_dynamodb_item , encrypt_dynamodb_item
27
27
28
28
__all__ = ('EncryptedClient' ,)
29
29
30
30
31
31
@attr .s
32
32
class EncryptedClient (object ):
33
- # pylint: disable=too-few-public-methods
33
+ # pylint: disable=too-few-public-methods,too-many-instance-attributes
34
34
"""High-level helper class to provide a familiar interface to encrypted tables.
35
35
36
36
.. note::
@@ -68,40 +68,50 @@ def __attrs_post_init__(self):
68
68
client = self ._client ,
69
69
auto_refresh_table_indexes = self ._auto_refresh_table_indexes
70
70
)
71
+ self ._table_crypto_config = partial ( # attrs confuses pylint: disable=attribute-defined-outside-init
72
+ crypto_config_from_cache ,
73
+ materials_provider = self ._materials_provider ,
74
+ attribute_actions = self ._attribute_actions ,
75
+ table_info_cache = self ._table_info_cache
76
+ )
77
+ self ._item_crypto_config = partial ( # attrs confuses pylint: disable=attribute-defined-outside-init
78
+ crypto_config_from_kwargs ,
79
+ fallback = self ._table_crypto_config
80
+ )
71
81
self .get_item = partial ( # attrs confuses pylint: disable=attribute-defined-outside-init
72
82
decrypt_get_item ,
73
83
decrypt_method = decrypt_dynamodb_item ,
74
- crypto_config_method = self ._table_crypto_config ,
84
+ crypto_config_method = self ._item_crypto_config ,
75
85
read_method = self ._client .get_item
76
86
)
77
87
self .put_item = partial ( # attrs confuses pylint: disable=attribute-defined-outside-init
78
88
encrypt_put_item ,
79
89
encrypt_method = encrypt_dynamodb_item ,
80
- crypto_config_method = self ._table_crypto_config ,
90
+ crypto_config_method = self ._item_crypto_config ,
81
91
write_method = self ._client .put_item
82
92
)
83
93
self .query = partial ( # attrs confuses pylint: disable=attribute-defined-outside-init
84
94
decrypt_multi_get ,
85
95
decrypt_method = decrypt_dynamodb_item ,
86
- crypto_config_method = self ._table_crypto_config ,
96
+ crypto_config_method = self ._item_crypto_config ,
87
97
read_method = self ._client .query
88
98
)
89
99
self .scan = partial ( # attrs confuses pylint: disable=attribute-defined-outside-init
90
100
decrypt_multi_get ,
91
101
decrypt_method = decrypt_dynamodb_item ,
92
- crypto_config_method = self ._table_crypto_config ,
102
+ crypto_config_method = self ._item_crypto_config ,
93
103
read_method = self ._client .scan
94
104
)
95
105
self .batch_get_item = partial ( # attrs confuses pylint: disable=attribute-defined-outside-init
96
106
decrypt_batch_get_item ,
97
107
decrypt_method = decrypt_dynamodb_item ,
98
- crypto_config_method = self ._batch_crypto_config ,
108
+ crypto_config_method = self ._table_crypto_config ,
99
109
read_method = self ._client .batch_get_item
100
110
)
101
111
self .batch_write_item = partial ( # attrs confuses pylint: disable=attribute-defined-outside-init
102
112
encrypt_batch_write_item ,
103
113
encrypt_method = encrypt_dynamodb_item ,
104
- crypto_config_method = self ._batch_crypto_config ,
114
+ crypto_config_method = self ._table_crypto_config ,
105
115
write_method = self ._client .batch_write_item
106
116
)
107
117
@@ -115,47 +125,6 @@ def __getattr__(self, name):
115
125
"""
116
126
return getattr (self ._client , name )
117
127
118
- def _crypto_config (self , table_name , ** kwargs ):
119
- """Pull all encryption-specific parameters from the request and use them to build a crypto config.
120
-
121
- :returns: crypto config and updated kwargs
122
- :rtype: dynamodb_encryption_sdk.encrypted.CryptoConfig and dict
123
- """
124
- crypto_config = kwargs .pop ('crypto_config' , None )
125
-
126
- if crypto_config is not None :
127
- return crypto_config , kwargs
128
-
129
- table_info = self ._table_info_cache .table_info (table_name )
130
-
131
- attribute_actions = self ._attribute_actions .copy ()
132
- attribute_actions .set_index_keys (* table_info .protected_index_keys ())
133
-
134
- crypto_config = CryptoConfig (
135
- materials_provider = self ._materials_provider ,
136
- encryption_context = EncryptionContext (** table_info .encryption_context_values ),
137
- attribute_actions = attribute_actions
138
- )
139
- return crypto_config , kwargs
140
-
141
- def _table_crypto_config (self , ** kwargs ):
142
- """Pull all encryption-specific parameters from the request and use them to build
143
- a crypto config for a single-table operation.
144
-
145
- :returns: crypto config and updated kwargs
146
- :rtype: dynamodb_encryption_sdk.encrypted.CryptoConfig and dict
147
- """
148
- return self ._crypto_config (kwargs ['TableName' ], ** kwargs )
149
-
150
- def _batch_crypto_config (self , table_name ):
151
- """Build a crypto config for a specific table.
152
-
153
- :param str table_name: Table for which to build crypto config
154
- :returns: crypto config
155
- :rtype: dynamodb_encryption_sdk.encrypted.CryptoConfig
156
- """
157
- return self ._crypto_config (table_name )[0 ]
158
-
159
128
def update_item (self , ** kwargs ):
160
129
"""Update item is not yet supported."""
161
130
raise NotImplementedError ('"update_item" is not yet implemented' )
0 commit comments