-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathTestCreateKeys.dfy
319 lines (271 loc) · 11.1 KB
/
TestCreateKeys.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
include "../src/Index.dfy"
include "Fixtures.dfy"
include "CleanupItems.dfy"
include "TestGetKeys.dfy"
module {:options "/functionSyntax:4" } TestCreateKeys {
import Types = AwsCryptographyKeyStoreTypes
import ComAmazonawsKmsTypes
import KMS = Com.Amazonaws.Kms
import DDB = Com.Amazonaws.Dynamodb
import KeyStore
import opened Wrappers
import opened Fixtures
import UTF8
import CleanupItems
import Structure
import DefaultKeyStorageInterface
import KmsArn
import UUID
import AwsArnParsing
import TestGetKeys
const happyCaseId := "test-happy-case-create-key-hv-1"
method {:test} TestCreateMRKForHV1()
{
var ddbClient :- expect DDB.DynamoDBClient();
var keyStoreConfigEast := Types.KeyStoreConfig(
id := None,
kmsConfiguration := KmsConfigEast,
logicalKeyStoreName := logicalKeyStoreName,
grantTokens := None,
storage := Some(
Types.ddb(
Types.DynamoDBTable(
ddbTableName := branchKeyStoreName,
ddbClient := Some(ddbClient)
)))
);
var keyStoreConfigWest := Types.KeyStoreConfig(
id := None,
kmsConfiguration := KmsConfigWest,
logicalKeyStoreName := logicalKeyStoreName,
grantTokens := None,
storage := Some(
Types.ddb(
Types.DynamoDBTable(
ddbTableName := branchKeyStoreName,
ddbClient := Some(ddbClient)
)))
);
// Create key with Custom EC & Branch Key Identifier
var uuid :- expect UUID.GenerateUUID();
var branchKeyIdWest := happyCaseId + "-" + "west" + "-" + uuid;
// print branchKeyIdWest;
var branchKeyIdEast := happyCaseId + "-" + "east" + "-" + uuid;
// print branchKeyIdEast;
var keyStoreEast :- expect KeyStore.KeyStore(keyStoreConfigEast);
var keyStoreWest :- expect KeyStore.KeyStore(keyStoreConfigWest);
var branchKeyIdWest? :- expect keyStoreWest.CreateKey(Types.CreateKeyInput(
branchKeyIdentifier := Some(branchKeyIdWest),
encryptionContext := Some(KmsMrkEC)
));
var branchKeyIdEast? :- expect keyStoreEast.CreateKey(Types.CreateKeyInput(
branchKeyIdentifier := Some(branchKeyIdEast),
encryptionContext := Some(KmsMrkEC)
));
expect branchKeyIdEast?.branchKeyIdentifier == branchKeyIdEast;
expect branchKeyIdWest?.branchKeyIdentifier == branchKeyIdWest;
var _ := CleanupItems.DeleteBranchKey(Identifier:=branchKeyIdWest, ddbClient:=ddbClient);
var _ := CleanupItems.DeleteBranchKey(Identifier:=branchKeyIdEast, ddbClient:=ddbClient);
}
method {:test} TestCreateBranchAndBeaconKeys()
{
var kmsClient :- expect KMS.KMSClient();
var ddbClient :- expect DDB.DynamoDBClient();
var kmsConfig := Types.KMSConfiguration.kmsKeyArn(keyArn);
var keyStoreConfig := Types.KeyStoreConfig(
id := None,
kmsConfiguration := kmsConfig,
logicalKeyStoreName := logicalKeyStoreName,
storage := Some(
Types.ddb(
Types.DynamoDBTable(
ddbTableName := branchKeyStoreName,
ddbClient := Some(ddbClient)
)))
// keyManagement := Some(
// Types.kms(
// Types.AwsKms(
// kmsClient := Some(kmsClient)
// )))
);
var keyStore :- expect KeyStore.KeyStore(keyStoreConfig);
var branchKeyId :- expect keyStore.CreateKey(Types.CreateKeyInput(
branchKeyIdentifier := None,
encryptionContext := None
));
// Get branch key items from storage
TestGetKeys.VerifyGetKeys(
identifier := branchKeyId.branchKeyIdentifier,
keyStore := keyStore,
storage := keyStore.config.storage
);
// Since this process uses a read DDB table,
// the number of records will forever increase.
// To avoid this, remove the items.
var _ := CleanupItems.DeleteBranchKey(Identifier:=branchKeyId.branchKeyIdentifier, ddbClient:=ddbClient);
}
method {:test} TestCreateOptions()
{
var kmsClient :- expect KMS.KMSClient();
var ddbClient :- expect DDB.DynamoDBClient();
var kmsConfig := Types.KMSConfiguration.kmsKeyArn(keyArn);
var keyStoreConfig := Types.KeyStoreConfig(
id := None,
kmsConfiguration := kmsConfig,
logicalKeyStoreName := logicalKeyStoreName,
storage := Some(
Types.ddb(
Types.DynamoDBTable(
ddbTableName := branchKeyStoreName,
ddbClient := Some(ddbClient)
))),
keyManagement := Some(
Types.kms(
Types.AwsKms(
kmsClient := Some(kmsClient)
)))
);
var keyStore :- expect KeyStore.KeyStore(keyStoreConfig);
// Use a UUID so multiple tests can run :)
var id :- expect UUID.GenerateUUID();
var encryptionContext :- expect EncodeEncryptionContext(map[
"some" := "encryption",
"context" := "values"
]);
var branchKeyId :- expect keyStore.CreateKey(Types.CreateKeyInput(
branchKeyIdentifier := Some(id),
encryptionContext := Some(encryptionContext)
));
var beaconKeyResult :- expect keyStore.GetBeaconKey(
Types.GetBeaconKeyInput(
branchKeyIdentifier := branchKeyId.branchKeyIdentifier
));
var activeResult :- expect keyStore.GetActiveBranchKey(
Types.GetActiveBranchKeyInput(
branchKeyIdentifier := branchKeyId.branchKeyIdentifier
));
var branchKeyVersion :- expect UTF8.Decode(activeResult.branchKeyMaterials.branchKeyVersion);
var versionResult :- expect keyStore.GetBranchKeyVersion(
Types.GetBranchKeyVersionInput(
branchKeyIdentifier := branchKeyId.branchKeyIdentifier,
branchKeyVersion := branchKeyVersion
));
// Since this process uses a read DDB table,
// the number of records will forever increase.
// To avoid this, remove the items.
var _ := CleanupItems.DeleteBranchKey(Identifier:=branchKeyId.branchKeyIdentifier, ddbClient:=ddbClient);
expect id
== versionResult.branchKeyMaterials.branchKeyIdentifier
== activeResult.branchKeyMaterials.branchKeyIdentifier
== beaconKeyResult.beaconKeyMaterials.beaconKeyIdentifier;
expect encryptionContext
== versionResult.branchKeyMaterials.encryptionContext
== activeResult.branchKeyMaterials.encryptionContext
== beaconKeyResult.beaconKeyMaterials.encryptionContext;
}
method {:test} TestCreateDuplicate()
{
var kmsClient :- expect KMS.KMSClient();
var ddbClient :- expect DDB.DynamoDBClient();
var kmsConfig := Types.KMSConfiguration.kmsKeyArn(keyArn);
var keyStoreConfig := Types.KeyStoreConfig(
id := None,
kmsConfiguration := kmsConfig,
logicalKeyStoreName := logicalKeyStoreName,
storage := Some(
Types.ddb(
Types.DynamoDBTable(
ddbTableName := branchKeyStoreName,
ddbClient := Some(ddbClient)
))),
keyManagement := Some(
Types.kms(
Types.AwsKms(
kmsClient := Some(kmsClient)
)))
);
var keyStore :- expect KeyStore.KeyStore(keyStoreConfig);
var attempt := keyStore.CreateKey(
Types.CreateKeyInput(
branchKeyIdentifier := Some(branchKeyId),
encryptionContext := None
));
expect attempt.Failure?;
}
method {:test} InsertingADuplicateWillFail()
{
var ddbClient :- expect DDB.DynamoDBClient();
var customEC := map[];
expect 0 < |branchKeyId|;
expect 0 < |branchKeyIdActiveVersion|;
expect forall k <- customEC :: DDB.Types.IsValid_AttributeName(Structure.ENCRYPTION_CONTEXT_PREFIX + k);
expect KMS.Types.IsValid_KeyIdType(keyArn);
expect AwsArnParsing.ParseAwsKmsArn(keyArn).Success?;
expect KmsArn.ValidKmsArn?(keyArn);
var encryptionContext := Structure.DecryptOnlyBranchKeyEncryptionContext(
branchKeyId,
branchKeyIdActiveVersion,
"",
"",
keyArn,
Types.HierarchyVersion.v1,
customEC
);
var ddbTableNameUtf8 :- expect UTF8.Encode(branchKeyStoreName);
var logicalKeyStoreNameUtf8 :- expect UTF8.Encode("");
var storage := new DefaultKeyStorageInterface.DynamoDBKeyStorageInterface(
ddbTableName := branchKeyStoreName,
ddbClient := ddbClient,
logicalKeyStoreName := "",
ddbTableNameUtf8 := ddbTableNameUtf8,
logicalKeyStoreNameUtf8 := logicalKeyStoreNameUtf8
);
var output := storage.WriteNewEncryptedBranchKey(
Types.WriteNewEncryptedBranchKeyInput(
Version := Structure.ConstructEncryptedHierarchicalKey(encryptionContext, [1]),
Active := Structure.ConstructEncryptedHierarchicalKey(Structure.ActiveBranchKeyEncryptionContext(encryptionContext), [2]),
Beacon := Structure.ConstructEncryptedHierarchicalKey(Structure.BeaconKeyEncryptionContext(encryptionContext), [2])
)
);
expect output.Failure?;
}
method {:test} InsertingADuplicateWillWithADifferentVersionFail()
{
var ddbClient :- expect DDB.DynamoDBClient();
var customEC := map[];
expect 0 < |branchKeyId|;
expect 0 < |branchKeyIdActiveVersion|;
expect forall k <- customEC :: DDB.Types.IsValid_AttributeName(Structure.ENCRYPTION_CONTEXT_PREFIX + k);
expect KMS.Types.IsValid_KeyIdType(keyArn);
expect AwsArnParsing.ParseAwsKmsArn(keyArn).Success?;
expect KmsArn.ValidKmsArn?(keyArn);
var encryptionContext := Structure.DecryptOnlyBranchKeyEncryptionContext(
branchKeyId,
"!= branchKeyIdActiveVersion",
"",
"",
keyArn,
Types.HierarchyVersion.v1,
customEC
);
var ddbTableNameUtf8 :- expect UTF8.Encode(branchKeyStoreName);
var logicalKeyStoreNameUtf8 :- expect UTF8.Encode("");
var storage := new DefaultKeyStorageInterface.DynamoDBKeyStorageInterface(
ddbTableName := branchKeyStoreName,
ddbClient := ddbClient,
logicalKeyStoreName := "",
ddbTableNameUtf8 := ddbTableNameUtf8,
logicalKeyStoreNameUtf8 := logicalKeyStoreNameUtf8
);
var output := storage.WriteNewEncryptedBranchKey(
Types.WriteNewEncryptedBranchKeyInput(
Version := Structure.ConstructEncryptedHierarchicalKey(encryptionContext, [1]),
Active := Structure.ConstructEncryptedHierarchicalKey(Structure.ActiveBranchKeyEncryptionContext(encryptionContext), [2]),
Beacon := Structure.ConstructEncryptedHierarchicalKey(Structure.BeaconKeyEncryptionContext(encryptionContext), [2])
)
);
expect output.Failure?;
}
}