-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTransactWriteItemsTransform.dfy
135 lines (119 loc) · 7.54 KB
/
TransactWriteItemsTransform.dfy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
include "DynamoDbMiddlewareSupport.dfy"
module TransactWriteItemsTransform {
import opened DdbMiddlewareConfig
import opened DynamoDbMiddlewareSupport
import opened Wrappers
import DDB = ComAmazonawsDynamodbTypes
import opened AwsCryptographyDbEncryptionSdkDynamoDbTransformsTypes
import EncTypes = AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorTypes
import Seq
import Util = DynamoDbEncryptionUtil
predicate method {:opaque} IsValid(item : DDB.TransactWriteItem) {
|| item.Put.Some?
|| item.Update.Some?
|| item.Delete.Some?
|| item.ConditionCheck.Some?
}
method {:vcs_split_on_every_assert} Input(config: Config, input: TransactWriteItemsInputTransformInput)
returns (output: Result<TransactWriteItemsInputTransformOutput, Error>)
requires ValidConfig?(config)
ensures ValidConfig?(config)
modifies ModifiesConfig(config)
ensures output.Success? ==> |output.value.transformedInput.TransactItems| == |input.sdkInput.TransactItems|
//= specification/dynamodb-encryption-client/ddb-sdk-integration.md#encrypt-before-transactwriteitems
//# To protect against a possible fifth field being added to the TransactWriteItem structure in the future,
//# the client MUST fail if none of the `Update`, `ConditionCheck`, `Delete` and `Put` fields are set.
ensures output.Success? ==>
forall item <- input.sdkInput.TransactItems :: IsValid(item)
{
:- Need(forall item <- input.sdkInput.TransactItems :: IsValid(item), E("Each item in TransactWriteItems must specify at least one supported operation"));
var result : seq<DDB.TransactWriteItem> := [];
for x := 0 to |input.sdkInput.TransactItems|
// invariant |result| == x
{
var item := input.sdkInput.TransactItems[x];
if item.ConditionCheck.Some? && item.ConditionCheck.value.TableName in config.tableEncryptionConfigs {
var tableConfig := config.tableEncryptionConfigs[item.ConditionCheck.value.TableName];
//= specification/dynamodb-encryption-client/ddb-sdk-integration.md#encrypt-before-transactwriteitems
//# - The ConditionExpression of the `ConditionCheck` MUST be [valid](ddb-support.md#testconditionexpression).
var _ :- TestConditionExpression(tableConfig,
Some(item.ConditionCheck.value.ConditionExpression),
item.ConditionCheck.value.ExpressionAttributeNames,
item.ConditionCheck.value.ExpressionAttributeValues);
}
if item.Delete.Some? && item.Delete.value.TableName in config.tableEncryptionConfigs {
var tableConfig := config.tableEncryptionConfigs[item.Delete.value.TableName];
//= specification/dynamodb-encryption-client/ddb-sdk-integration.md#encrypt-before-transactwriteitems
//# - The ConditionExpression of the `Delete` MUST be [valid](ddb-support.md#testconditionexpression).
var _ :- TestConditionExpression(tableConfig,
item.Delete.value.ConditionExpression,
item.Delete.value.ExpressionAttributeNames,
item.Delete.value.ExpressionAttributeValues);
}
if item.Update.Some? && item.Update.value.TableName in config.tableEncryptionConfigs {
var tableConfig := config.tableEncryptionConfigs[item.Update.value.TableName];
//= specification/dynamodb-encryption-client/ddb-sdk-integration.md#encrypt-before-transactwriteitems
//# - The UpdateExpression of the `Update` MUST be [valid](ddb-support.md#testupdateexpression).
var _ :- TestUpdateExpression(tableConfig,
Some(item.Update.value.UpdateExpression),
item.Update.value.ExpressionAttributeNames,
item.Update.value.ExpressionAttributeValues);
//= specification/dynamodb-encryption-client/ddb-sdk-integration.md#encrypt-before-transactwriteitems
//# - The ConditionExpression of the `Update` MUST be [valid](ddb-support.md#testconditionexpression).
var _ :- TestConditionExpression(tableConfig,
item.Update.value.ConditionExpression,
item.Update.value.ExpressionAttributeNames,
item.Update.value.ExpressionAttributeValues);
}
if item.Put.Some? && !IsPlainWrite(config, item.Put.value.TableName) {
var tableConfig := config.tableEncryptionConfigs[item.Put.value.TableName];
//= specification/dynamodb-encryption-client/ddb-sdk-integration.md#encrypt-before-transactwriteitems
//# - The Item MUST be [writable](ddb-support.md#writable).
var _ :- IsWriteable(tableConfig, item.Put.value.Item);
//= specification/dynamodb-encryption-client/ddb-sdk-integration.md#encrypt-before-transactwriteitems
//# - The ConditionExpression `Put` MUST be [valid](ddb-support.md#testconditionexpression).
var _ :- TestConditionExpression(tableConfig,
item.Put.value.ConditionExpression,
item.Put.value.ExpressionAttributeNames,
item.Put.value.ExpressionAttributeValues);
var beaconItem :- AddSignedBeacons(tableConfig, item.Put.value.Item);
var encryptRes := tableConfig.itemEncryptor.EncryptItem(
EncTypes.EncryptItemInput(plaintextItem:=beaconItem)
);
var encrypted :- MapError(encryptRes);
var keyId :- GetKeyIdFromHeader(tableConfig, encrypted);
var beaconAttrs :- GetEncryptedBeacons(tableConfig, item.Put.value.Item, Util.MaybeFromOptionKeyId(keyId));
//= specification/dynamodb-encryption-client/ddb-sdk-integration.md#encrypt-before-transactwriteitems
//# - The PutItem request's `Item` field MUST be replaced
//# with a value that is equivalent to
//# the result [Encrypted DynamoDB Item](./encrypt-item.md#encrypted-dynamodb-item)
//# calculated above.
var put := Some(item.Put.value.(Item := encrypted.encryptedItem + beaconAttrs));
var newItem := item.(Put := put);
ghost var oldResult := result;
result := result + [newItem];
assert |result| == |oldResult| + 1;
} else {
//= specification/dynamodb-encryption-client/ddb-sdk-integration.md#encrypt-before-transactwriteitems
//# Any actions other than `Put, MUST be unchanged.
//= specification/dynamodb-encryption-client/ddb-sdk-integration.md#encrypt-before-transactwriteitems
//# Any `Put` actions with a `TableName` that does not refer to an [encrypted-table](#encrypted-table),
//# MUST be unchanged.
ghost var oldResult := result;
result := result + [item];
assert |result| == |oldResult| + 1;
}
}
:- Need(|input.sdkInput.TransactItems| == |result|, E(""));
return Success(TransactWriteItemsInputTransformOutput(transformedInput := input.sdkInput.(TransactItems := result)));
}
method Output(config: Config, input: TransactWriteItemsOutputTransformInput)
returns (output: Result<TransactWriteItemsOutputTransformOutput, Error>)
ensures output.Success? && output.value.transformedOutput == input.sdkOutput
{
var finalResult := input.sdkOutput;
return Success(TransactWriteItemsOutputTransformOutput(transformedOutput := finalResult));
}
}