-
Notifications
You must be signed in to change notification settings - Fork 19
chore: update internal legacy override with adapter pattern #2054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 25 commits
ec8077c
e89541c
f6f27e4
e5ea095
03d25db
ea7b465
0290cff
c3d2567
53297ce
817d8fa
f520e9d
7ebefe7
75255c0
294abef
5a57429
cab0510
018c92b
eef8a56
ce47673
befb6bd
651d343
4b157e2
07d9e4d
3298a2d
e319647
b2d2414
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -34,32 +34,26 @@ | |||||||||||
|
|
||||||||||||
| public class InternalLegacyOverride extends _ExternBase_InternalLegacyOverride { | ||||||||||||
|
|
||||||||||||
| private DynamoDBEncryptor encryptor; | ||||||||||||
| private Map<String, Set<EncryptionFlags>> actions; | ||||||||||||
| private EncryptionContext encryptionContext; | ||||||||||||
| private LegacyPolicy _policy; | ||||||||||||
| private DafnySequence<Character> materialDescriptionFieldName; | ||||||||||||
| private DafnySequence<Character> signatureFieldName; | ||||||||||||
| private final LegacyEncryptorAdapter _encryptorAdapter; | ||||||||||||
| private final LegacyPolicy _policy; | ||||||||||||
| private final DafnySequence<Character> materialDescriptionFieldNameDafnyType; | ||||||||||||
| private final DafnySequence<Character> signatureFieldNameDafnyType; | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I thought these were holding types, but they just the Java-Dafny types.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done here: b2d2414 |
||||||||||||
|
|
||||||||||||
| private InternalLegacyOverride( | ||||||||||||
| DynamoDBEncryptor encryptor, | ||||||||||||
| Map<String, Set<EncryptionFlags>> actions, | ||||||||||||
| EncryptionContext encryptionContext, | ||||||||||||
| LegacyEncryptorAdapter encryptorAdapter, | ||||||||||||
| LegacyPolicy policy | ||||||||||||
| ) { | ||||||||||||
| this.encryptor = encryptor; | ||||||||||||
| this.actions = actions; | ||||||||||||
| this.encryptionContext = encryptionContext; | ||||||||||||
| this._encryptorAdapter = encryptorAdapter; | ||||||||||||
| this._policy = policy; | ||||||||||||
| // It is possible that these values | ||||||||||||
| // have been customized by the customer. | ||||||||||||
| this.materialDescriptionFieldName = | ||||||||||||
| this.materialDescriptionFieldNameDafnyType = | ||||||||||||
| software.amazon.smithy.dafny.conversion.ToDafny.Simple.CharacterSequence( | ||||||||||||
| encryptor.getMaterialDescriptionFieldName() | ||||||||||||
| encryptorAdapter.getMaterialDescriptionFieldName() | ||||||||||||
| ); | ||||||||||||
| this.signatureFieldName = | ||||||||||||
| this.signatureFieldNameDafnyType = | ||||||||||||
| software.amazon.smithy.dafny.conversion.ToDafny.Simple.CharacterSequence( | ||||||||||||
| encryptor.getSignatureFieldName() | ||||||||||||
| encryptorAdapter.getSignatureFieldName() | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
@@ -78,8 +72,8 @@ public boolean IsLegacyInput( | |||||||||||
| //# attributes for the material description and the signature. | ||||||||||||
| return ( | ||||||||||||
| input.is_DecryptItemInput() && | ||||||||||||
| input._encryptedItem.contains(materialDescriptionFieldName) && | ||||||||||||
| input._encryptedItem.contains(signatureFieldName) | ||||||||||||
| input._encryptedItem.contains(materialDescriptionFieldNameDafnyType) && | ||||||||||||
| input._encryptedItem.contains(signatureFieldNameDafnyType) | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
@@ -111,17 +105,13 @@ > EncryptItem( | |||||||||||
|
|
||||||||||||
| final Map< | ||||||||||||
| String, | ||||||||||||
| com.amazonaws.services.dynamodbv2.model.AttributeValue | ||||||||||||
| > encryptedItem = encryptor.encryptRecord( | ||||||||||||
| V2MapToV1Map(plaintextItem), | ||||||||||||
| actions, | ||||||||||||
| encryptionContext | ||||||||||||
| ); | ||||||||||||
| software.amazon.awssdk.services.dynamodb.model.AttributeValue | ||||||||||||
| > encryptedItem = _encryptorAdapter.encryptRecord(plaintextItem); | ||||||||||||
|
|
||||||||||||
| final software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.model.EncryptItemOutput nativeOutput = | ||||||||||||
| software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.model.EncryptItemOutput | ||||||||||||
| .builder() | ||||||||||||
| .encryptedItem(V1MapToV2Map(encryptedItem)) | ||||||||||||
| .encryptedItem(encryptedItem) | ||||||||||||
| .build(); | ||||||||||||
| final software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.internaldafny.types.EncryptItemOutput dafnyOutput = | ||||||||||||
| software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.ToDafny.EncryptItemOutput( | ||||||||||||
|
|
@@ -162,19 +152,15 @@ > DecryptItem( | |||||||||||
| .DecryptItemInput(input) | ||||||||||||
| .encryptedItem(); | ||||||||||||
|
|
||||||||||||
| final Map< | ||||||||||||
| Map< | ||||||||||||
| String, | ||||||||||||
| com.amazonaws.services.dynamodbv2.model.AttributeValue | ||||||||||||
| > plaintextItem = encryptor.decryptRecord( | ||||||||||||
| V2MapToV1Map(encryptedItem), | ||||||||||||
| actions, | ||||||||||||
| encryptionContext | ||||||||||||
| ); | ||||||||||||
| software.amazon.awssdk.services.dynamodb.model.AttributeValue | ||||||||||||
| > plaintextItem = _encryptorAdapter.decryptRecord(encryptedItem); | ||||||||||||
|
|
||||||||||||
| final software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.model.DecryptItemOutput nativeOutput = | ||||||||||||
| software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.model.DecryptItemOutput | ||||||||||||
| .builder() | ||||||||||||
| .plaintextItem(V1MapToV2Map(plaintextItem)) | ||||||||||||
| .plaintextItem(plaintextItem) | ||||||||||||
| .build(); | ||||||||||||
| final software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.internaldafny.types.DecryptItemOutput dafnyOutput = | ||||||||||||
| software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.ToDafny.DecryptItemOutput( | ||||||||||||
|
|
@@ -224,13 +210,30 @@ public static Result<Option<InternalLegacyOverride>, Error> Build( | |||||||||||
| return CreateBuildFailure(maybeEncryptionContext.error()); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| final LegacyEncryptorAdapter encryptorAdapter; | ||||||||||||
| if (maybeEncryptor instanceof DynamoDBEncryptor) { | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Help a reader out?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done for this code and other similar places: b2d2414 |
||||||||||||
| encryptorAdapter = | ||||||||||||
| new V1EncryptorAdapter( | ||||||||||||
| (DynamoDBEncryptor) maybeEncryptor, | ||||||||||||
| maybeActions.value(), | ||||||||||||
| maybeEncryptionContext.value() | ||||||||||||
| ); | ||||||||||||
| } else if ( | ||||||||||||
| maybeEncryptor instanceof | ||||||||||||
| software.amazon.cryptools.dynamodbencryptionclientsdk2.encryption.DynamoDBEncryptor | ||||||||||||
| ) { | ||||||||||||
| encryptorAdapter = | ||||||||||||
| new V2EncryptorAdapter( | ||||||||||||
| (software.amazon.cryptools.dynamodbencryptionclientsdk2.encryption.DynamoDBEncryptor) maybeEncryptor, | ||||||||||||
| convertActionsV1ToV2(maybeActions.value()), | ||||||||||||
| convertEncryptionContextV1ToV2(maybeEncryptionContext.value()) | ||||||||||||
| ); | ||||||||||||
| } else { | ||||||||||||
| return CreateBuildFailure(createError("Unsupported encryptor type: " + maybeEncryptor.getClass().getName())); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| final InternalLegacyOverride internalLegacyOverride = | ||||||||||||
| new InternalLegacyOverride( | ||||||||||||
| (DynamoDBEncryptor) maybeEncryptor, | ||||||||||||
| maybeActions.value(), | ||||||||||||
| maybeEncryptionContext.value(), | ||||||||||||
| legacyOverride.dtor_policy() | ||||||||||||
| ); | ||||||||||||
| new InternalLegacyOverride(encryptorAdapter, legacyOverride.dtor_policy()); | ||||||||||||
|
|
||||||||||||
| return CreateBuildSuccess( | ||||||||||||
| CreateInternalLegacyOverrideSome(internalLegacyOverride) | ||||||||||||
|
|
@@ -250,7 +253,61 @@ public static Error createError(String message) { | |||||||||||
| public static boolean isDynamoDBEncryptor( | ||||||||||||
| software.amazon.cryptography.dbencryptionsdk.dynamodb.ILegacyDynamoDbEncryptor maybe | ||||||||||||
| ) { | ||||||||||||
| return maybe instanceof DynamoDBEncryptor; | ||||||||||||
| return ( | ||||||||||||
| maybe instanceof DynamoDBEncryptor || | ||||||||||||
| maybe instanceof | ||||||||||||
| software.amazon.cryptools.dynamodbencryptionclientsdk2.encryption.DynamoDBEncryptor | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Convert SDK V1 EncryptionFlags to SDK V2 | ||||||||||||
| private static Map< | ||||||||||||
| String, | ||||||||||||
| Set< | ||||||||||||
| software.amazon.cryptools.dynamodbencryptionclientsdk2.encryption.EncryptionFlags | ||||||||||||
| > | ||||||||||||
| > convertActionsV1ToV2(Map<String, Set<EncryptionFlags>> v1Actions) { | ||||||||||||
| Map< | ||||||||||||
| String, | ||||||||||||
| Set< | ||||||||||||
| software.amazon.cryptools.dynamodbencryptionclientsdk2.encryption.EncryptionFlags | ||||||||||||
| > | ||||||||||||
| > v2Actions = new HashMap<>(); | ||||||||||||
| for (Map.Entry<String, Set<EncryptionFlags>> entry : v1Actions.entrySet()) { | ||||||||||||
| Set< | ||||||||||||
| software.amazon.cryptools.dynamodbencryptionclientsdk2.encryption.EncryptionFlags | ||||||||||||
| > v2Flags = new HashSet<>(); | ||||||||||||
| for (EncryptionFlags v1Flag : entry.getValue()) { | ||||||||||||
| v2Flags.add( | ||||||||||||
| software.amazon.cryptools.dynamodbencryptionclientsdk2.encryption.EncryptionFlags.valueOf( | ||||||||||||
| v1Flag.name() | ||||||||||||
| ) | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
| v2Actions.put(entry.getKey(), v2Flags); | ||||||||||||
| } | ||||||||||||
| return v2Actions; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Convert SDK V1 EncryptionContext to SDK V2 | ||||||||||||
| private static software.amazon.cryptools.dynamodbencryptionclientsdk2.encryption.EncryptionContext convertEncryptionContextV1ToV2( | ||||||||||||
| final EncryptionContext v1Context | ||||||||||||
| ) { | ||||||||||||
|
|
||||||||||||
| final software.amazon.cryptools.dynamodbencryptionclientsdk2.encryption.EncryptionContext.Builder builder = software.amazon.cryptools.dynamodbencryptionclientsdk2.encryption.EncryptionContext | ||||||||||||
| .builder() | ||||||||||||
| .tableName(v1Context.getTableName()) | ||||||||||||
| .hashKeyName(v1Context.getHashKeyName()) | ||||||||||||
| .rangeKeyName(v1Context.getRangeKeyName()) | ||||||||||||
| .developerContext(v1Context.getDeveloperContext()); | ||||||||||||
|
|
||||||||||||
| if (v1Context.getMaterialDescription() != null) { | ||||||||||||
| builder.materialDescription(v1Context.getMaterialDescription()); | ||||||||||||
| } | ||||||||||||
| if (v1Context.getAttributeValues() != null) { | ||||||||||||
| builder.attributeValues(V1MapToV2Map(v1Context.getAttributeValues())); | ||||||||||||
| } | ||||||||||||
| return builder.build(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| public static String ToNativeString(DafnySequence<? extends Character> s) { | ||||||||||||
|
|
@@ -377,10 +434,10 @@ public static com.amazonaws.services.dynamodbv2.model.AttributeValue V2Attribute | |||||||||||
| case SS: | ||||||||||||
| return attribute.withSS(value.ss()); | ||||||||||||
| case UNKNOWN_TO_SDK_VERSION: | ||||||||||||
| throw new IllegalArgumentException("omfg"); | ||||||||||||
| throw new IllegalArgumentException("Unsupported AttributeValue type: UNKNOWN_TO_SDK_VERSION. This may indicate a newer DynamoDB attribute type that is not supported by this SDK version."); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| throw new IllegalArgumentException("omfg"); | ||||||||||||
| throw new IllegalArgumentException("Unexpected AttributeValue type: " + value.type() + ". Unable to convert from SDK v2 to SDK v1 format."); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| public static Map< | ||||||||||||
|
|
@@ -392,6 +449,9 @@ > V2MapToV1Map( | |||||||||||
| software.amazon.awssdk.services.dynamodb.model.AttributeValue | ||||||||||||
| > input | ||||||||||||
| ) { | ||||||||||||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||
| if (input == null) { | ||||||||||||
| return null; | ||||||||||||
| } | ||||||||||||
| return input | ||||||||||||
| .entrySet() | ||||||||||||
| .stream() | ||||||||||||
|
|
@@ -459,6 +519,9 @@ public static software.amazon.awssdk.services.dynamodb.model.AttributeValue V1At | |||||||||||
| > V1MapToV2Map( | ||||||||||||
| Map<String, com.amazonaws.services.dynamodbv2.model.AttributeValue> input | ||||||||||||
| ) { | ||||||||||||
| if (input == null) { | ||||||||||||
| return null; | ||||||||||||
| } | ||||||||||||
| return input | ||||||||||||
| .entrySet() | ||||||||||||
| .stream() | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.internaldafny.legacy; | ||
|
|
||
| import java.security.GeneralSecurityException; | ||
| import java.util.Map; | ||
| import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
|
|
||
| public interface LegacyEncryptorAdapter { | ||
| Map<String, AttributeValue> encryptRecord( | ||
| Map< | ||
| String, | ||
| software.amazon.awssdk.services.dynamodb.model.AttributeValue | ||
| > item | ||
| ) throws GeneralSecurityException; | ||
|
|
||
| Map< | ||
| String, | ||
| software.amazon.awssdk.services.dynamodb.model.AttributeValue | ||
| > decryptRecord( | ||
| Map< | ||
| String, | ||
| software.amazon.awssdk.services.dynamodb.model.AttributeValue | ||
| > item | ||
| ) throws GeneralSecurityException; | ||
|
|
||
| String getMaterialDescriptionFieldName(); | ||
| String getSignatureFieldName(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.internaldafny.legacy; | ||
|
|
||
| import static software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.internaldafny.legacy.InternalLegacyOverride.V1MapToV2Map; | ||
| import static software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.internaldafny.legacy.InternalLegacyOverride.V2MapToV1Map; | ||
|
|
||
| import com.amazonaws.services.dynamodbv2.datamodeling.encryption.DynamoDBEncryptor; | ||
| import com.amazonaws.services.dynamodbv2.datamodeling.encryption.EncryptionContext; | ||
| import com.amazonaws.services.dynamodbv2.datamodeling.encryption.EncryptionFlags; | ||
| import java.security.GeneralSecurityException; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| public class V1EncryptorAdapter implements LegacyEncryptorAdapter { | ||
|
|
||
| private final DynamoDBEncryptor encryptor; | ||
| private final Map<String, Set<EncryptionFlags>> actions; | ||
| private final EncryptionContext encryptionContext; | ||
|
|
||
| V1EncryptorAdapter( | ||
| DynamoDBEncryptor encryptor, | ||
| Map<String, Set<EncryptionFlags>> actions, | ||
| EncryptionContext encryptionContext | ||
| ) { | ||
| this.encryptor = encryptor; | ||
| this.actions = actions; | ||
| this.encryptionContext = encryptionContext; | ||
| } | ||
|
|
||
| @Override | ||
| public Map< | ||
| String, | ||
| software.amazon.awssdk.services.dynamodb.model.AttributeValue | ||
| > encryptRecord( | ||
| Map< | ||
| String, | ||
| software.amazon.awssdk.services.dynamodb.model.AttributeValue | ||
| > item | ||
| ) throws GeneralSecurityException { | ||
| return V1MapToV2Map( | ||
| encryptor.encryptRecord(V2MapToV1Map(item), actions, encryptionContext) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public Map< | ||
| String, | ||
| software.amazon.awssdk.services.dynamodb.model.AttributeValue | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This V1 class always returns in V2 terms; this means a customers MUST have both SDK v1 and SDK v2 loaded to use this V1 class.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. If using DB-ESDK, SDK V2 must be in included in the artifact.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it SDK v1 & SDK v2, or only SDK v2? |
||
| > decryptRecord( | ||
| Map< | ||
| String, | ||
| software.amazon.awssdk.services.dynamodb.model.AttributeValue | ||
| > item | ||
| ) throws GeneralSecurityException { | ||
| return V1MapToV2Map( | ||
| encryptor.decryptRecord(V2MapToV1Map(item), actions, encryptionContext) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public String getMaterialDescriptionFieldName() { | ||
| return encryptor.getMaterialDescriptionFieldName(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getSignatureFieldName() { | ||
| return encryptor.getSignatureFieldName(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to review this file locally.
There is a lot going on here.