Skip to content

Commit eca80a3

Browse files
committed
migration 4
1 parent c28ee2a commit eca80a3

File tree

5 files changed

+66
-75
lines changed

5 files changed

+66
-75
lines changed

DynamoDbEncryption/runtimes/python/src/aws_dbesdk_dynamodb/internaldafny/extern/InternalLegacyOverride-NewCopy.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ def Build(config: DynamoDbItemEncryptorConfig_DynamoDbItemEncryptorConfig):
6363
legacy_instance.encryption_context = maybe_encryption_context.value
6464
# Access the value property, not calling it as a function
6565
legacy_instance.attribute_actions = maybe_actions.value
66-
66+
6767
# Set the material description field name and signature field name
6868
# These values might be customized by the customer
69-
if isinstance(legacy_override.encryptor, EncryptedClient) and hasattr(legacy_override.encryptor, "_crypto_config"):
69+
if isinstance(legacy_override.encryptor, EncryptedClient) and hasattr(
70+
legacy_override.encryptor, "_crypto_config"
71+
):
7072
# Get field names from the encryptor's crypto config
7173
crypto_config = legacy_override.encryptor._crypto_config
7274
if hasattr(crypto_config, "material_description_field_name"):
@@ -76,11 +78,9 @@ def Build(config: DynamoDbItemEncryptorConfig_DynamoDbItemEncryptorConfig):
7678
else:
7779
# Use default value if not explicitly set
7880
legacy_instance.materialDescriptionFieldName = _dafny.seq_of_chars("*amzn-ddb-map-desc*")
79-
81+
8082
if hasattr(crypto_config, "signature_field_name"):
81-
legacy_instance.signatureFieldName = _dafny.seq_of_chars(
82-
crypto_config.signature_field_name
83-
)
83+
legacy_instance.signatureFieldName = _dafny.seq_of_chars(crypto_config.signature_field_name)
8484
else:
8585
# Use default value if not explicitly set
8686
legacy_instance.signatureFieldName = _dafny.seq_of_chars("*amzn-ddb-map-sig*")
@@ -137,7 +137,7 @@ def legacyActions(attribute_actions_on_encrypt):
137137

138138
@staticmethod
139139
def EncryptItem(input):
140-
print(f'Encrypting {input}')
140+
print(f"Encrypting {input}")
141141
try:
142142
# Extract components from the input
143143
item = input.plaintextItem
@@ -230,23 +230,27 @@ def IsLegacyInput(input):
230230
return False
231231

232232
# Check if we're dealing with DecryptItemInput
233-
if not hasattr(input, 'encryptedItem'):
233+
if not hasattr(input, "encryptedItem"):
234234
return False
235235

236236
# We need the instance with materialDescriptionFieldName and signatureFieldName
237-
if not hasattr(input, 'legacyOverride') or not input.legacyOverride:
237+
if not hasattr(input, "legacyOverride") or not input.legacyOverride:
238238
return False
239239

240240
legacy_override = input.legacyOverride
241-
if not hasattr(legacy_override, 'materialDescriptionFieldName') or not legacy_override.materialDescriptionFieldName:
241+
if (
242+
not hasattr(legacy_override, "materialDescriptionFieldName")
243+
or not legacy_override.materialDescriptionFieldName
244+
):
242245
return False
243-
if not hasattr(legacy_override, 'signatureFieldName') or not legacy_override.signatureFieldName:
246+
if not hasattr(legacy_override, "signatureFieldName") or not legacy_override.signatureFieldName:
244247
return False
245248

246249
# Check if the item contains both required markers
247-
return (input.encryptedItem.contains(legacy_override.materialDescriptionFieldName) and
248-
input.encryptedItem.contains(legacy_override.signatureFieldName))
249-
250+
return input.encryptedItem.contains(
251+
legacy_override.materialDescriptionFieldName
252+
) and input.encryptedItem.contains(legacy_override.signatureFieldName)
253+
250254
except:
251255
# If we encounter any error during detection, default to not using legacy
252256
return False
@@ -257,5 +261,4 @@ def CreateError(message):
257261
return Error_DynamoDbItemEncryptorException(message)
258262

259263

260-
261264
aws_dbesdk_dynamodb.internaldafny.generated.InternalLegacyOverride.InternalLegacyOverride = InternalLegacyOverride

DynamoDbEncryption/runtimes/python/src/aws_dbesdk_dynamodb/internaldafny/extern/InternalLegacyOverride.py

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
DynamoDbItemEncryptorConfig_DynamoDbItemEncryptorConfig,
55
Error_DynamoDbItemEncryptorException,
66
EncryptItemOutput_EncryptItemOutput,
7-
DecryptItemOutput_DecryptItemOutput
7+
DecryptItemOutput_DecryptItemOutput,
88
)
99
from aws_dbesdk_dynamodb.internaldafny.generated.AwsCryptographyDbEncryptionSdkStructuredEncryptionTypes import (
1010
CryptoAction_ENCRYPT__AND__SIGN,
@@ -65,10 +65,12 @@ def Build(config: DynamoDbItemEncryptorConfig_DynamoDbItemEncryptorConfig):
6565
legacy_instance.encryption_context = maybe_encryption_context.value
6666
# Access the value property, not calling it as a function
6767
legacy_instance.attribute_actions = maybe_actions.value
68-
68+
6969
# Set the material description field name and signature field name
7070
# These values might be customized by the customer
71-
if isinstance(legacy_override.encryptor, EncryptedClient) and hasattr(legacy_override.encryptor, "_crypto_config"):
71+
if isinstance(legacy_override.encryptor, EncryptedClient) and hasattr(
72+
legacy_override.encryptor, "_crypto_config"
73+
):
7274
# Get field names from the encryptor's crypto config
7375
crypto_config = legacy_override.encryptor._crypto_config
7476
if hasattr(crypto_config, "material_description_field_name"):
@@ -78,11 +80,9 @@ def Build(config: DynamoDbItemEncryptorConfig_DynamoDbItemEncryptorConfig):
7880
else:
7981
# Use default value if not explicitly set
8082
legacy_instance.materialDescriptionFieldName = _dafny.seq_of_chars("*amzn-ddb-map-desc*")
81-
83+
8284
if hasattr(crypto_config, "signature_field_name"):
83-
legacy_instance.signatureFieldName = _dafny.seq_of_chars(
84-
crypto_config.signature_field_name
85-
)
85+
legacy_instance.signatureFieldName = _dafny.seq_of_chars(crypto_config.signature_field_name)
8686
else:
8787
# Use default value if not explicitly set
8888
legacy_instance.signatureFieldName = _dafny.seq_of_chars("*amzn-ddb-map-sig*")
@@ -139,83 +139,79 @@ def legacyActions(attribute_actions_on_encrypt):
139139

140140
def EncryptItem(self, input):
141141
"""Encrypt an item using the legacy DynamoDB encryptor.
142-
142+
143143
Args:
144144
input: EncryptItemInput containing the plaintext item to encrypt
145-
145+
146146
Returns:
147147
Result containing the encrypted item or an error
148148
"""
149149
try:
150150
# Get the plaintext item from the input
151151
plaintext_item = input.plaintextItem
152-
152+
153153
# Check policy
154154
if not self.policy.is_FORCE__LEGACY__ENCRYPT__ALLOW__LEGACY__DECRYPT:
155155
return Wrappers.Result_Failure(
156156
InternalLegacyOverride.CreateError("Legacy policy does not support encrypt")
157157
)
158-
158+
159159
# Use the encryptor to encrypt the item using the instance attributes
160160
encrypted_item = self.encryptor.encrypt_item(
161-
plaintext_item,
162-
actions=self.attribute_actions,
163-
encryption_context=self.encryption_context
161+
plaintext_item, actions=self.attribute_actions, encryption_context=self.encryption_context
164162
)
165-
163+
166164
# Create the output with the encrypted item
167165
output = EncryptItemOutput_EncryptItemOutput(encrypted_item, Wrappers.Option_None())
168166
return Wrappers.Result_Success(output)
169-
167+
170168
except Exception as e:
171169
# Return an appropriate error result with the exception details
172-
return Wrappers.Result_Failure(
173-
InternalLegacyOverride.CreateError(f"Error during encryption: {str(e)}")
174-
)
170+
return Wrappers.Result_Failure(InternalLegacyOverride.CreateError(f"Error during encryption: {str(e)}"))
175171

176172
def DecryptItem(self, input):
177173
"""Decrypt an item using the legacy DynamoDB encryptor.
178174
179175
Args:
180176
input: DecryptItemInput containing the encrypted item to decrypt
181-
177+
182178
Returns:
183179
Result containing the decrypted item or an error
184180
"""
185181
try:
186182
# Get the encrypted item from the input
187183
encrypted_item = input.encryptedItem
188-
184+
189185
# Check policy
190-
if not (self.policy.is_FORCE__LEGACY__ENCRYPT__ALLOW__LEGACY__DECRYPT or
191-
self.policy.is_FORBID__LEGACY__ENCRYPT__ALLOW__LEGACY__DECRYPT):
186+
if not (
187+
self.policy.is_FORCE__LEGACY__ENCRYPT__ALLOW__LEGACY__DECRYPT
188+
or self.policy.is_FORBID__LEGACY__ENCRYPT__ALLOW__LEGACY__DECRYPT
189+
):
192190
return Wrappers.Result_Failure(
193191
InternalLegacyOverride.CreateError("Legacy policy does not support decrypt")
194192
)
195-
193+
196194
# Validate that this is indeed a legacy item with the required fields
197-
if not (encrypted_item.contains(self.materialDescriptionFieldName) and
198-
encrypted_item.contains(self.signatureFieldName)):
195+
if not (
196+
encrypted_item.contains(self.materialDescriptionFieldName)
197+
and encrypted_item.contains(self.signatureFieldName)
198+
):
199199
return Wrappers.Result_Failure(
200200
InternalLegacyOverride.CreateError("Item does not contain required legacy fields")
201201
)
202-
202+
203203
# Use the encryptor to decrypt the item using the instance attributes
204204
decrypted_item = self.encryptor.decrypt_item(
205-
encrypted_item,
206-
actions=self.attribute_actions,
207-
encryption_context=self.encryption_context
205+
encrypted_item, actions=self.attribute_actions, encryption_context=self.encryption_context
208206
)
209-
207+
210208
# Create the output with the decrypted item
211209
output = DecryptItemOutput_DecryptItemOutput(decrypted_item, Wrappers.Option_None())
212210
return Wrappers.Result_Success(output)
213-
211+
214212
except Exception as e:
215213
# Return an appropriate error result with the exception details
216-
return Wrappers.Result_Failure(
217-
InternalLegacyOverride.CreateError(f"Error during decryption: {str(e)}")
218-
)
214+
return Wrappers.Result_Failure(InternalLegacyOverride.CreateError(f"Error during decryption: {str(e)}"))
219215

220216
def __init__(self):
221217
super().__init__()
@@ -240,23 +236,27 @@ def IsLegacyInput(input):
240236
return False
241237

242238
# Check if we're dealing with DecryptItemInput
243-
if not hasattr(input, 'encryptedItem'):
239+
if not hasattr(input, "encryptedItem"):
244240
return False
245241

246242
# We need the instance with materialDescriptionFieldName and signatureFieldName
247-
if not hasattr(input, 'legacyOverride') or not input.legacyOverride:
243+
if not hasattr(input, "legacyOverride") or not input.legacyOverride:
248244
return False
249245

250246
legacy_override = input.legacyOverride
251-
if not hasattr(legacy_override, 'materialDescriptionFieldName') or not legacy_override.materialDescriptionFieldName:
247+
if (
248+
not hasattr(legacy_override, "materialDescriptionFieldName")
249+
or not legacy_override.materialDescriptionFieldName
250+
):
252251
return False
253-
if not hasattr(legacy_override, 'signatureFieldName') or not legacy_override.signatureFieldName:
252+
if not hasattr(legacy_override, "signatureFieldName") or not legacy_override.signatureFieldName:
254253
return False
255254

256255
# Check if the item contains both required markers
257-
return (input.encryptedItem.contains(legacy_override.materialDescriptionFieldName) and
258-
input.encryptedItem.contains(legacy_override.signatureFieldName))
259-
256+
return input.encryptedItem.contains(
257+
legacy_override.materialDescriptionFieldName
258+
) and input.encryptedItem.contains(legacy_override.signatureFieldName)
259+
260260
except:
261261
# If we encounter any error during detection, default to not using legacy
262262
return False
@@ -267,5 +267,4 @@ def CreateError(message):
267267
return Error_DynamoDbItemEncryptorException(message)
268268

269269

270-
271270
aws_dbesdk_dynamodb.internaldafny.generated.InternalLegacyOverride.InternalLegacyOverride = InternalLegacyOverride

Examples/runtimes/python/Migration/src/ddbec_to_awsdbe/awsdbe/migration_step_1.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,8 @@
2626
import boto3
2727

2828
# Import from new AWS Database Encryption SDK
29-
from aws_dbesdk_dynamodb.structures.dynamodb import (
30-
LegacyOverride,
31-
LegacyPolicy,
32-
)
33-
3429
# Import from legacy DynamoDB Encryption Client
35-
from dynamodb_encryption_sdk.encrypted.client import EncryptedClient as LegacyEncryptedClient
36-
from dynamodb_encryption_sdk.material_providers.aws_kms import AwsKmsCryptographicMaterialsProvider
37-
from dynamodb_encryption_sdk.structures import AttributeActions, CryptoAction
38-
39-
from .common import ATTRIBUTE_ACTIONS_ON_ENCRYPT, setup_awsdbe_client_with_legacy_override
30+
from .common import setup_awsdbe_client_with_legacy_override
4031

4132

4233
def migration_step_1(kms_key_id, ddb_table_name, sort_read_value=0):
@@ -53,9 +44,7 @@ def migration_step_1(kms_key_id, ddb_table_name, sort_read_value=0):
5344
ddb_client = boto3.client("dynamodb")
5445

5546
# 2. Create a DynamoDB Encryption SDK client with legacy override
56-
encrypted_client = setup_awsdbe_client_with_legacy_override(
57-
kms_key_id=kms_key_id, ddb_table_name=ddb_table_name
58-
)
47+
encrypted_client = setup_awsdbe_client_with_legacy_override(kms_key_id=kms_key_id, ddb_table_name=ddb_table_name)
5948

6049
# 3. Put an item in the old format since we are using a legacy override
6150
# with FORCE_LEGACY_ENCRYPT_ALLOW_DECRYPT policy

Examples/runtimes/python/Migration/src/ddbec_to_awsdbe/awsdbe/migration_step_2.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
import boto3
2828

2929
# Import from new AWS Database Encryption SDK
30-
from . import migration_step_1
31-
from .common import setup_pure_awsdbe_client, setup_awsdbe_client_with_legacy_override
30+
from .common import setup_awsdbe_client_with_legacy_override, setup_pure_awsdbe_client
3231

3332

3433
def migration_step_2(kms_key_id, ddb_table_name):

Examples/runtimes/python/Migration/test/ddbec_to_awsdbe/awsdbe/test_migration_step_3.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
import pytest
1010

1111
from ....src.ddbec_to_awsdbe.awsdbe import (
12+
common,
1213
migration_step_1,
1314
migration_step_2,
1415
migration_step_3,
15-
common,
1616
)
1717
from ....src.ddbec_to_awsdbe.ddbec import migration_step_0
1818
from ..test_utils import TEST_DDB_TABLE_NAME, TEST_KMS_KEY_ID
1919

2020
pytestmark = [pytest.mark.examples]
2121

22+
2223
def test_setup():
2324
common.setup_awsdbe_client_with_legacy_override(TEST_KMS_KEY_ID, TEST_DDB_TABLE_NAME)
2425

@@ -56,4 +57,4 @@ def test_run_migration_example():
5657
print("Migration path completed successfully!")
5758
print("======================================================")
5859
finally:
59-
pass
60+
pass

0 commit comments

Comments
 (0)