-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathTestAdminHV2.dfy
330 lines (286 loc) · 15.4 KB
/
TestAdminHV2.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
320
321
322
323
324
325
326
327
328
329
330
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
include "../src/Index.dfy"
include "../../AwsCryptographyKeyStore/test/CleanupItems.dfy"
include "../../AwsCryptographyKeyStore/test/Fixtures.dfy"
include "../../AwsCryptographyKeyStore/test/TestGetKeys.dfy"
include "../../AwsCryptographyKeyStore/Model/AwsCryptographyKeyStoreTypes.dfy"
include "AdminFixtures.dfy"
// TODO-HV-2-M2 : remove HV-1 restriction for Mutations
// TODO-HV-2-M3 : rename TestAdminHV2 -> TestAdminCreateKeyHV2
// and move HappyCases for version & mutations to it's own test modules
module {:options "/functionSyntax:4" } TestAdminHV2 {
import Types = AwsCryptographyKeyStoreAdminTypes
import KeyStoreAdmin
import KeyStore
import KeyStoreTypes = AwsCryptographyKeyStoreTypes
import ComAmazonawsKmsTypes
import KMS = Com.Amazonaws.Kms
import DDB = Com.Amazonaws.Dynamodb
import DefaultKeyStorageInterface
import opened Wrappers
import opened Fixtures
import UUID
import CleanupItems
import AdminFixtures
import TestGetKeys
method {:test} TestCreateKeyHV2HappyCase()
{
var ddbClient :- expect Fixtures.ProvideDDBClient();
var kmsClient :- expect Fixtures.ProvideKMSClient();
var storage :- expect Fixtures.DefaultStorage(ddbClient?:=Some(ddbClient));
var keyStore :- expect Fixtures.DefaultKeyStore(ddbClient?:=Some(ddbClient), kmsClient?:=Some(kmsClient));
var strategy :- expect AdminFixtures.SimpleKeyManagerStrategy(kmsClient?:=Some(kmsClient));
var underTest :- expect AdminFixtures.DefaultAdmin(ddbClient?:=Some(ddbClient));
var input := Types.CreateKeyInput(
Identifier := None,
EncryptionContext := None,
KmsArn := Types.KmsSymmetricKeyArn.KmsKeyArn(keyArn),
Strategy := Some(strategy),
HierarchyVersion := Some(KeyStoreTypes.HierarchyVersion.v2)
);
var createKeyOutput? :- expect underTest.CreateKey(input);
expect createKeyOutput?.HierarchyVersion == KeyStoreTypes.HierarchyVersion.v2;
// Get branch key items from storage
TestGetKeys.VerifyGetKeys(
identifier := createKeyOutput?.Identifier,
keyStore := keyStore,
storage := 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:=createKeyOutput?.Identifier, ddbClient:=ddbClient);
}
const happyCaseId := "test-create-key-hv2-happy-case"
method {:test} TestCreateKeyHV2WithOptions() {
var ddbClient :- expect Fixtures.ProvideDDBClient();
var kmsClient :- expect Fixtures.ProvideKMSClient();
var storage :- expect Fixtures.DefaultStorage(ddbClient?:=Some(ddbClient));
var keyStore :- expect Fixtures.DefaultKeyStore(ddbClient?:=Some(ddbClient), kmsClient?:=Some(kmsClient));
var strategy :- expect AdminFixtures.SimpleKeyManagerStrategy(kmsClient?:=Some(kmsClient));
var underTest :- expect AdminFixtures.DefaultAdmin(ddbClient?:=Some(ddbClient));
// Create key with Custom EC & Branch Key Identifier
var uuid :- expect UUID.GenerateUUID();
var branchKeyId := happyCaseId + "-" + uuid;
var customEC := map[UTF8.EncodeAscii("Koda") := UTF8.EncodeAscii("Is a dog.")];
var input := Types.CreateKeyInput(
Identifier := Some(branchKeyId),
EncryptionContext := Some(customEC),
KmsArn := Types.KmsSymmetricKeyArn.KmsKeyArn(keyArn),
Strategy := Some(strategy),
HierarchyVersion := Some(KeyStoreTypes.HierarchyVersion.v2)
);
var createKeyOutput? :- expect underTest.CreateKey(input);
expect branchKeyId == createKeyOutput?.Identifier;
expect createKeyOutput?.HierarchyVersion == KeyStoreTypes.HierarchyVersion.v2;
// Get branch key items from storage
TestGetKeys.VerifyGetKeys(
identifier := branchKeyId,
keyStore := keyStore,
storage := 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, ddbClient:=ddbClient);
}
// TODO-HV-2-M2?: Document that BKSA ONLY calls KMS/DDB for the region of the supplied clients.
// or Ensure BKSA has similar behavior as BKS in terms of MRK region.
/*
method {:test} TestCreateMRKForHV2()
{
var ddbClient :- expect Fixtures.ProvideDDBClient();
var kmsClient :- expect Fixtures.ProvideKMSClient();
var storage :- expect Fixtures.DefaultStorage(ddbClient?:=Some(ddbClient));
var keyStore :- expect Fixtures.DefaultKeyStore(ddbClient?:=Some(ddbClient), kmsClient?:=Some(kmsClient));
var strategy :- expect AdminFixtures.SimpleKeyManagerStrategy(kmsClient?:=Some(kmsClient));
var underTest :- expect AdminFixtures.DefaultAdmin(ddbClient?:=Some(ddbClient));
// Create key with Custom EC & Branch Key Identifier
var uuid :- expect UUID.GenerateUUID();
var branchKeyIdWest := happyCaseId + "-" + WestBranchKey + "-" + uuid;
var branchKeyIdEast := happyCaseId + "-" + EastBranchKey + "-" + uuid;
var customEC := map[UTF8.EncodeAscii("Koda") := UTF8.EncodeAscii("Is a dog.")];
var westOutput? :- expect underTest.CreateKey(Types.CreateKeyInput(
Identifier := Some(branchKeyIdWest),
EncryptionContext := Some(customEC),
KmsArn := Types.KmsSymmetricKeyArn.KmsMRKeyArn(MrkArnWest),
Strategy := Some(strategy),
HierarchyVersion := Some(KeyStoreTypes.HierarchyVersion.v2)
));
expect westOutput?.Identifier == branchKeyIdWest;
expect westOutput?.HierarchyVersion == KeyStoreTypes.HierarchyVersion.v2;
print branchKeyIdWest + "\n";
var eastOutput? :- expect underTest.CreateKey(Types.CreateKeyInput(
Identifier := Some(branchKeyIdEast),
EncryptionContext := Some(customEC),
KmsArn := Types.KmsSymmetricKeyArn.KmsMRKeyArn(MrkArnEast),
Strategy := Some(strategy),
HierarchyVersion := Some(KeyStoreTypes.HierarchyVersion.v2)
));
expect eastOutput?.Identifier == branchKeyIdEast;
expect eastOutput?.HierarchyVersion == KeyStoreTypes.HierarchyVersion.v2;
print branchKeyIdEast + "\n";
}
*/
const testCreateKeyWithBKIDandNoECFailsId := "create-key-hv2-bkid-no-ec-fail"
method {:test} TestCreateKeyBKIDNoECFails()
{
var ddbClient :- expect Fixtures.ProvideDDBClient();
var kmsClient :- expect Fixtures.ProvideKMSClient();
var strategy :- expect AdminFixtures.SimpleKeyManagerStrategy(kmsClient?:=Some(kmsClient));
var underTest :- expect AdminFixtures.DefaultAdmin(ddbClient?:=Some(ddbClient));
var uuid :- expect UUID.GenerateUUID();
var branchKeyId := testCreateKeyWithBKIDandNoECFailsId + "-" + uuid;
var customEC := map[UTF8.EncodeAscii("Koda") := UTF8.EncodeAscii("Is a dog.")];
var input := Types.CreateKeyInput(
Identifier := Some(branchKeyId),
EncryptionContext := None,
KmsArn := Types.KmsSymmetricKeyArn.KmsKeyArn(keyArn),
Strategy := Some(strategy),
HierarchyVersion := Some(KeyStoreTypes.HierarchyVersion.v2)
);
var createKeyOutput := underTest.CreateKey(input);
expect createKeyOutput.Failure?, "Expected a failure when provided with Branch Key Identifier with no Custom Encryption Context";
}
method {:test} TestCreateKeyCustomECOnly()
{
var ddbClient :- expect Fixtures.ProvideDDBClient();
var kmsClient :- expect Fixtures.ProvideKMSClient();
var storage :- expect Fixtures.DefaultStorage(ddbClient?:=Some(ddbClient));
var keyStore :- expect Fixtures.DefaultKeyStore(ddbClient?:=Some(ddbClient), kmsClient?:=Some(kmsClient));
var strategy :- expect AdminFixtures.SimpleKeyManagerStrategy(kmsClient?:=Some(kmsClient));
var underTest :- expect AdminFixtures.DefaultAdmin(ddbClient?:=Some(ddbClient));
var customEC := map[UTF8.EncodeAscii("Koda") := UTF8.EncodeAscii("Is a dog.")];
var input := Types.CreateKeyInput(
Identifier := None,
EncryptionContext := Some(customEC),
KmsArn := Types.KmsSymmetricKeyArn.KmsKeyArn(keyArn),
Strategy := Some(strategy),
HierarchyVersion := Some(KeyStoreTypes.HierarchyVersion.v2)
);
var createKeyOutput? :- expect underTest.CreateKey(input);
expect createKeyOutput?.HierarchyVersion == KeyStoreTypes.HierarchyVersion.v2;
// Get branch key items from storage
TestGetKeys.VerifyGetKeys(
identifier := createKeyOutput?.Identifier,
keyStore := keyStore,
storage := 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:=createKeyOutput?.Identifier, ddbClient:=ddbClient);
}
// TODO-HV-2-M2 : Probably make this a happy test?
const testMutateForHV2FailsCaseId := "dafny-initialize-mutation-hv-2-rejection"
method {:test} TestMutateForHV2Fails()
{
var uuid :- expect UUID.GenerateUUID();
var testId := testMutateForHV2FailsCaseId + "-" + uuid;
var ddbClient :- expect Fixtures.ProvideDDBClient();
var kmsClient :- expect Fixtures.ProvideKMSClient();
var underTest :- expect AdminFixtures.DefaultAdmin();
var strategy :- expect AdminFixtures.DefaultKeyManagerStrategy(kmsClient?:=Some(kmsClient));
var systemKey := Types.SystemKey.trustStorage(trustStorage := Types.TrustStorage());
Fixtures.CreateHappyCaseId(id:=testId);
var mutationsRequest := Types.Mutations(
TerminalKmsArn := Some(Fixtures.postalHornKeyArn),
TerminalHierarchyVersion := Some(KeyStoreTypes.HierarchyVersion.v2)
);
var initInput := Types.InitializeMutationInput(
Identifier := testId,
Mutations := mutationsRequest,
Strategy := Some(strategy),
SystemKey := systemKey,
DoNotVersion := Some(true));
var initializeOutput := underTest.InitializeMutation(initInput);
var _ := CleanupItems.DeleteBranchKey(Identifier:=testId, ddbClient:=ddbClient);
expect initializeOutput.Failure?, "Should have failed to InitializeMutation HV-2.";
}
// TODO-HV-2-M2 : Probably make this a happy test?
const testMutateForHV1WithAwsKmsSimpleFailsCaseId := "dafny-initialize-mutation-hv-1-simpleKms-rejection"
method {:test} TestMutateForHV1WithAwsKmsSimpleFails()
{
var uuid :- expect UUID.GenerateUUID();
var testId := testMutateForHV1WithAwsKmsSimpleFailsCaseId + "-" + uuid;
var ddbClient :- expect Fixtures.ProvideDDBClient();
var kmsClient :- expect Fixtures.ProvideKMSClient();
var underTest :- expect AdminFixtures.DefaultAdmin();
var simpleStrategy :- expect AdminFixtures.SimpleKeyManagerStrategy(kmsClient?:=Some(kmsClient));
var systemKey := Types.SystemKey.trustStorage(trustStorage := Types.TrustStorage());
Fixtures.CreateHappyCaseId(id:=testId);
var mutationsRequest := Types.Mutations(
TerminalKmsArn := Some(Fixtures.postalHornKeyArn),
TerminalHierarchyVersion := Some(KeyStoreTypes.HierarchyVersion.v1)
);
var initInput := Types.InitializeMutationInput(
Identifier := testId,
Mutations := mutationsRequest,
Strategy := Some(simpleStrategy),
SystemKey := systemKey,
DoNotVersion := Some(true));
var initializeOutput := underTest.InitializeMutation(initInput);
var _ := CleanupItems.DeleteBranchKey(Identifier:=testId, ddbClient:=ddbClient);
expect initializeOutput.Failure?, "Should have failed to InitializeMutation for HV-1 with Simple.";
}
// TODO-HV-2-M2 : Probably make this a happy test?
const testMutateInitEncountersHV2FailsCaseId := "dafny-initialize-mutation-encounters-hv-2-rejection"
const logPrefix := "\n" + testMutateInitEncountersHV2FailsCaseId + " :: "
method {:test} TestMutateInitEncountersHV2FailsCaseId()
{
var uuid :- expect UUID.GenerateUUID();
var testId := testMutateInitEncountersHV2FailsCaseId + "-" + uuid;
var ddbClient :- expect Fixtures.ProvideDDBClient();
var kmsClient :- expect Fixtures.ProvideKMSClient();
var underTest :- expect AdminFixtures.DefaultAdmin();
var strategy :- expect AdminFixtures.DefaultKeyManagerStrategy(kmsClient?:=Some(kmsClient));
var systemKey := Types.SystemKey.trustStorage(trustStorage := Types.TrustStorage());
Fixtures.CreateHappyCaseId(id:=testId);
var _ :- expect AdminFixtures.AddAttributeWithoutLibrary(
id:=testId,
keyValue:=AdminFixtures.KeyValue(key:=Structure.HIERARCHY_VERSION, value:=Structure.HIERARCHY_VERSION_VALUE_2),
alsoViolateBeacon? := true, ddbClient? := Some(ddbClient),
kmsClient?:=Some(kmsClient), violateReservedAttribute:=true);
print logPrefix + "re-wrote the item to be described as HV-2 even though it's not" + "\n";
var mutationsRequest := Types.Mutations(TerminalKmsArn := Some(Fixtures.postalHornKeyArn));
var initInput := Types.InitializeMutationInput(
Identifier := testId,
Mutations := mutationsRequest,
Strategy := Some(strategy),
SystemKey := systemKey,
DoNotVersion := Some(true));
var initializeOutput := underTest.InitializeMutation(initInput);
print logPrefix + "initializeOutput :: ", initializeOutput, "\n";
var _ := CleanupItems.DeleteBranchKey(Identifier:=testId, ddbClient:=ddbClient);
expect initializeOutput.Failure?, "Should have failed InitializeMutation when HV-2 encountered by InitMutation.";
}
// TODO-HV-2-M2 : Probably make this a happy test?
const testVersionKeyEncountersHV2FailsCaseId := "dafny-version-key-encounters-hv-2-rejection"
const logPrefixVersion := "\n" + testVersionKeyEncountersHV2FailsCaseId + " :: "
method {:test} TestVersionKeyEncountersHV2Fails()
{
var uuid :- expect UUID.GenerateUUID();
var testId := testVersionKeyEncountersHV2FailsCaseId + "-" + uuid;
var ddbClient :- expect Fixtures.ProvideDDBClient();
var kmsClient :- expect Fixtures.ProvideKMSClient();
var underTest :- expect AdminFixtures.DefaultAdmin();
var strategy :- expect AdminFixtures.DefaultKeyManagerStrategy(kmsClient?:=Some(kmsClient));
var systemKey := Types.SystemKey.trustStorage(trustStorage := Types.TrustStorage());
Fixtures.CreateHappyCaseId(id:=testId);
var _ :- expect AdminFixtures.AddAttributeWithoutLibrary(
id:=testId,
keyValue:=AdminFixtures.KeyValue(key:=Structure.HIERARCHY_VERSION, value:=Structure.HIERARCHY_VERSION_VALUE_2),
alsoViolateBeacon? := true, ddbClient? := Some(ddbClient),
kmsClient?:=Some(kmsClient), violateReservedAttribute:=true);
print logPrefixVersion + "re-wrote the item to be described as HV-2 even though it's not" + "\n";
var versionInput := Types.VersionKeyInput(
Identifier := testId,
KmsArn := Types.KmsSymmetricKeyArn.KmsKeyArn(keyArn),
Strategy := Some(strategy));
var actualOutput := underTest.VersionKey(versionInput);
print logPrefixVersion + "actualOutput :: ", actualOutput, "\n";
var _ := CleanupItems.DeleteBranchKey(Identifier:=testId, ddbClient:=ddbClient);
expect actualOutput.Failure?, "Should have failed VersionKey when HV-2 encountered by VersionKey.";
}
}