Skip to content

Commit ab13987

Browse files
committed
Fix some Java datatype creation calls
1 parent a7f3c5b commit ab13987

File tree

4 files changed

+65
-28
lines changed
  • .github/actions/polymorph_codegen
  • DynamoDbEncryption
  • TestVectors/runtimes/java/src/main/java/software/amazon/cryptography/dbencryptionsdk/dynamodb/internaldafny/wrapped

4 files changed

+65
-28
lines changed

.github/actions/polymorph_codegen/action.yml

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ runs:
116116
make -C submodules/MaterialProviders/AwsCryptographicMaterialProviders setup_prettier
117117
make -C submodules/MaterialProviders/ComAmazonawsKms setup_prettier
118118
make -C submodules/MaterialProviders/ComAmazonawsDynamodb setup_prettier
119+
make -C submodules/MaterialProviders/TestVectorsAwsCryptographicMaterialProviders setup_prettier
119120
120121
- name: Regenerate Java code using smithy-dafny
121122
# npx seems to be unavailable on Windows GHA runners,

DynamoDbEncryption/dafny/DynamoDbItemEncryptor/src/InternalLegacyOverride.dfy

+37
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,43 @@ module {:extern "software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencry
2222

2323
predicate method {:extern} IsLegacyInput(input: Types.DecryptItemInput)
2424

25+
// The following functions are for the benefit of the extern implementation to call,
26+
// avoiding direct references to generic datatype constructors
27+
// since their calling pattern is different between different versions of Dafny
28+
// (i.e. after 4.2, explicit type descriptors are required).
29+
30+
static function method CreateBuildSuccess(value: Option<InternalLegacyOverride>): Result<Option<InternalLegacyOverride>, Types.Error> {
31+
Success(value)
32+
}
33+
34+
static function method CreateBuildFailure(error: Types.Error): Result<Option<InternalLegacyOverride>, Types.Error> {
35+
Failure(error)
36+
}
37+
38+
static function method CreateInternalLegacyOverrideSome(value: InternalLegacyOverride): Option<InternalLegacyOverride> {
39+
Some(value)
40+
}
41+
42+
static function method CreateInternalLegacyOverrideNone(): Option<InternalLegacyOverride> {
43+
None
44+
}
45+
46+
function method CreateEncryptItemSuccess(value: Types.EncryptItemOutput): Result<Types.EncryptItemOutput, Types.Error> {
47+
Success(value)
48+
}
49+
50+
function method CreateEncryptItemFailure(error: Types.Error): Result<Types.EncryptItemOutput, Types.Error> {
51+
Failure(error)
52+
}
53+
54+
function method CreateDecryptItemSuccess(value: Types.DecryptItemOutput): Result<Types.DecryptItemOutput, Types.Error> {
55+
Success(value)
56+
}
57+
58+
function method CreateDecryptItemFailure(error: Types.Error): Result<Types.DecryptItemOutput, Types.Error> {
59+
Failure(error)
60+
}
61+
2562
}
2663

2764
}

DynamoDbEncryption/runtimes/java/src/main/java/software/amazon/cryptography/dbencryptionsdk/dynamodb/itemencryptor/internaldafny/legacy/InternalLegacyOverride.java

+26-27
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* IDEs tend to fight this so I'm sorry.
1414
*/
1515

16+
import Dafny.Aws.Cryptography.Primitives.Types.InternalResult;
1617
import software.amazon.cryptography.dbencryptionsdk.structuredencryption.internaldafny.types.CryptoAction;
1718
import software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.internaldafny.types.Error;
1819
import software.amazon.cryptography.dbencryptionsdk.dynamodb.internaldafny.types.LegacyPolicy;
@@ -26,13 +27,12 @@
2627
import dafny.TypeDescriptor;
2728
import software.amazon.awssdk.core.SdkBytes;
2829
import software.amazon.cryptography.dbencryptionsdk.dynamodb.ILegacyDynamoDbEncryptor;
29-
import software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.ToNative;
3030

3131
import java.util.*;
3232
import java.util.function.BiConsumer;
3333
import java.util.stream.Collectors;
3434

35-
public class InternalLegacyOverride {
35+
public class InternalLegacyOverride extends _ExternBase_InternalLegacyOverride {
3636
private DynamoDBEncryptor encryptor;
3737
private Map<String, Set<EncryptionFlags>> actions;
3838
private EncryptionContext encryptionContext;
@@ -74,7 +74,7 @@ public static TypeDescriptor<InternalLegacyOverride> _typeDescriptor() {
7474
return TypeDescriptor.referenceWithInitializer(InternalLegacyOverride.class, () -> null);
7575
}
7676

77-
public Boolean IsLegacyInput(
77+
public boolean IsLegacyInput(
7878
software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.internaldafny.types.DecryptItemInput input
7979
) {
8080
//= specification/dynamodb-encryption-client/decrypt-item.md#determining-legacy-items
@@ -95,7 +95,7 @@ public Result<software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencrypto
9595

9696
// Precondition: Policy MUST allow the caller to encrypt.
9797
if (!_policy.is_FORCE__LEGACY__ENCRYPT__ALLOW__LEGACY__DECRYPT()) {
98-
return createFailure("Legacy Policy does not support encrypt.");
98+
return CreateEncryptItemFailure(createError("Legacy Policy does not support encrypt."));
9999
}
100100

101101
try {
@@ -135,9 +135,9 @@ public Result<software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencrypto
135135
.itemencryptor
136136
.ToDafny
137137
.EncryptItemOutput(nativeOutput);
138-
return Result.create_Success(dafnyOutput);
138+
return CreateEncryptItemSuccess(dafnyOutput);
139139
} catch (Exception ex) {
140-
return Result.create_Failure(Error.create_Opaque(ex));
140+
return CreateEncryptItemFailure(Error.create_Opaque(ex));
141141
}
142142
}
143143

@@ -152,7 +152,7 @@ public Result<software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencrypto
152152
//# and the input item [is an item written in the legacy format](#determining-legacy-items),
153153
//# this operation MUST fail.
154154
if (!_policy.is_FORCE__LEGACY__ENCRYPT__ALLOW__LEGACY__DECRYPT() && !_policy.is_FORBID__LEGACY__ENCRYPT__ALLOW__LEGACY__DECRYPT()) {
155-
return createFailure("Legacy Policy does not support decrypt.");
155+
return CreateDecryptItemFailure(createError("Legacy Policy does not support decrypt."));
156156
}
157157
try {
158158
Map<String, software.amazon.awssdk.services.dynamodb.model.AttributeValue> encryptedItem = software
@@ -191,9 +191,9 @@ public Result<software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencrypto
191191
.itemencryptor
192192
.ToDafny
193193
.DecryptItemOutput(nativeOutput);
194-
return Result.create_Success(dafnyOutput);
194+
return CreateDecryptItemSuccess(dafnyOutput);
195195
} catch (Exception ex) {
196-
return Result.create_Failure(Error.create_Opaque(ex));
196+
return CreateDecryptItemFailure(Error.create_Opaque(ex));
197197
}
198198
}
199199

@@ -203,7 +203,7 @@ public static Result<Option<InternalLegacyOverride>, Error> Build(
203203

204204
// Check for early return (Postcondition): If there is no legacyOverride there is nothing to do.
205205
if (encryptorConfig.dtor_legacyOverride().is_None()) {
206-
return Result.create_Success(Option.create_None());
206+
return CreateBuildSuccess(CreateInternalLegacyOverrideNone());
207207
}
208208
final software.amazon.cryptography.dbencryptionsdk.dynamodb.internaldafny.types.LegacyOverride legacyOverride = encryptorConfig
209209
.dtor_legacyOverride()
@@ -214,35 +214,34 @@ public static Result<Option<InternalLegacyOverride>, Error> Build(
214214

215215
// Precondition: The encryptor MUST be a DynamoDBEncryptor
216216
if (!isDynamoDBEncryptor(maybeEncryptor)) {
217-
return createFailure("Legacy encryptor is not supported");
217+
return CreateBuildFailure(createError("Legacy encryptor is not supported"));
218218
}
219219
// Preconditions: MUST be able to create valid encryption context
220220
final Result<EncryptionContext, Error> maybeEncryptionContext = legacyEncryptionContext(encryptorConfig);
221221
if (maybeEncryptionContext.is_Failure()) {
222-
return Result.create_Failure(maybeEncryptionContext.dtor_error());
222+
return CreateBuildFailure(maybeEncryptionContext.dtor_error());
223223
}
224224
// Precondition: All actions MUST be supported types
225-
final Result<Map<String, Set<EncryptionFlags>>, Error> maybeActions = legacyActions(legacyOverride.dtor_attributeActionsOnEncrypt());
226-
if (maybeActions.is_Failure()) {
227-
return Result.create_Failure(maybeEncryptionContext.dtor_error());
225+
final InternalResult<Map<String, Set<EncryptionFlags>>, Error> maybeActions = legacyActions(legacyOverride.dtor_attributeActionsOnEncrypt());
226+
if (maybeActions.isFailure()) {
227+
return CreateBuildFailure(maybeEncryptionContext.dtor_error());
228228
}
229229

230230
final InternalLegacyOverride internalLegacyOverride = new InternalLegacyOverride(
231231
(DynamoDBEncryptor) maybeEncryptor,
232-
maybeActions.dtor_value(),
232+
maybeActions.value(),
233233
maybeEncryptionContext.dtor_value(),
234234
legacyOverride.dtor_policy()
235235
);
236236

237-
return Result.create_Success(Option.create_Some(internalLegacyOverride));
237+
return CreateBuildSuccess(CreateInternalLegacyOverrideSome(internalLegacyOverride));
238238
}
239239

240240
// Everything below this point is an implementation detail
241241

242-
public static <T> Result<T, Error>createFailure(String message) {
242+
public static Error createError(String message) {
243243
final DafnySequence<Character> dafnyMessage = software.amazon.smithy.dafny.conversion.ToDafny.Simple.CharacterSequence(message);
244-
final Error dafnyEx = Error.create_DynamoDbItemEncryptorException(dafnyMessage);
245-
return Result.create_Failure(dafnyEx);
244+
return Error.create_DynamoDbItemEncryptorException(dafnyMessage);
246245
}
247246

248247
public static boolean isDynamoDBEncryptor(
@@ -262,7 +261,7 @@ public static DafnySequence<Character> ToDafnyString(String s)
262261
return software.amazon.smithy.dafny.conversion.ToDafny.Simple.CharacterSequence(s);
263262
}
264263

265-
public static Result<EncryptionContext, Error> legacyEncryptionContext(
264+
public static InternalResult<EncryptionContext, Error> legacyEncryptionContext(
266265
software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.internaldafny.types.DynamoDbItemEncryptorConfig config
267266
) {
268267
try {
@@ -275,13 +274,13 @@ public static Result<EncryptionContext, Error> legacyEncryptionContext(
275274
? encryptionContextBuilder.withRangeKeyName(ToNativeString(config.dtor_sortKeyName().dtor_value())).build()
276275
: encryptionContextBuilder.build();
277276

278-
return Result.create_Success(encryptionContext);
277+
return InternalResult.success(encryptionContext);
279278
} catch (Exception ex) {
280-
return Result.create_Failure(Error.create_Opaque(ex));
279+
return InternalResult.failure(Error.create_Opaque(ex));
281280
}
282281
}
283282

284-
public static Result<Map<String, Set<EncryptionFlags>>, Error> legacyActions(
283+
public static InternalResult<Map<String, Set<EncryptionFlags>>, Error> legacyActions(
285284
DafnyMap<? extends DafnySequence<? extends Character>, ? extends CryptoAction> attributeActionsOnEncrypt
286285
) {
287286
try {
@@ -303,12 +302,12 @@ public static Result<Map<String, Set<EncryptionFlags>>, Error> legacyActions(
303302
}
304303
};
305304
attributeActionsOnEncrypt.forEach(buildLegacyActions);
306-
return Result.create_Success(legacyActions);
305+
return InternalResult.success(legacyActions);
307306
} catch (IllegalArgumentException ex) {
308307
final Error dafnyEx = Error.create_DynamoDbItemEncryptorException(ToDafnyString(ex.getMessage()));
309-
return Result.create_Failure(dafnyEx);
308+
return InternalResult.failure(dafnyEx);
310309
} catch (Exception ex) {
311-
return Result.create_Failure(Error.create_Opaque(ex));
310+
return InternalResult.failure(Error.create_Opaque(ex));
312311
}
313312
}
314313

TestVectors/runtimes/java/src/main/java/software/amazon/cryptography/dbencryptionsdk/dynamodb/internaldafny/wrapped/__default.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ public static Result<IDynamoDbEncryptionClient, Error> WrappedDynamoDbEncryption
1414
software.amazon.cryptography.dbencryptionsdk.dynamodb.model.DynamoDbEncryptionConfig wrappedConfig = ToNative.DynamoDbEncryptionConfig(config);
1515
software.amazon.cryptography.dbencryptionsdk.dynamodb.DynamoDbEncryption impl = DynamoDbEncryption.builder().DynamoDbEncryptionConfig(wrappedConfig).build();
1616
TestDynamoDbEncryption wrappedClient = TestDynamoDbEncryption.builder().impl(impl).build();
17-
return Result.create_Success(wrappedClient);
17+
return CreateSuccessOfClient(wrappedClient);
1818
}
1919
}

0 commit comments

Comments
 (0)