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
|
/**
* Check functionality of KeyVault.js
*/
load("jstests/client_encrypt/lib/mock_kms.js");
load('jstests/ssl/libs/ssl_helpers.js');
(function() {
"use strict";
const mock_kms = new MockKMSServer();
mock_kms.start();
const x509_options = {
sslMode: "requireSSL",
sslPEMKeyFile: SERVER_CERT,
sslCAFile: CA_CERT
};
const conn = MongoRunner.runMongod(x509_options);
const test = conn.getDB("test");
const collection = test.coll;
const awsKMS = {
accessKeyId: "access",
secretAccessKey: "secret",
url: mock_kms.getURL(),
};
const clientSideFLEOptions = {
kmsProviders: {
aws: awsKMS,
},
keyVaultNamespace: "test.coll",
schemaMap: {}
};
const conn_str = "mongodb://" + conn.host + "/?ssl=true";
const shell = Mongo(conn_str, clientSideFLEOptions);
const keyVault = shell.getKeyVault();
keyVault.createKey("aws", "arn:aws:kms:us-east-1:fake:fake:fake", ['mongoKey']);
assert.eq(1, keyVault.getKeys().itcount());
var result = keyVault.createKey("aws", "arn:aws:kms:us-east-4:fake:fake:fake", {});
assert.eq("TypeError: key alternate names must be of Array type.", result);
result = keyVault.createKey("aws", "arn:aws:kms:us-east-5:fake:fake:fake", [1]);
assert.eq("TypeError: items in key alternate names must be of String type.", result);
assert.eq(1, keyVault.getKeyByAltName("mongoKey").itcount());
var keyId = keyVault.getKeyByAltName("mongoKey").toArray()[0]._id;
keyVault.addKeyAlternateName(keyId, "mongoKey2");
assert.eq(1, keyVault.getKeyByAltName("mongoKey2").itcount());
assert.eq(2, keyVault.getKey(keyId).toArray()[0].keyAltNames.length);
assert.eq(1, keyVault.getKeys().itcount());
result = keyVault.addKeyAlternateName(keyId, [2]);
assert.eq("TypeError: key alternate name cannot be object or array type.", result);
// Test create key with no CMK
result = keyVault.createKey("aws", ["altName"]);
assert.eq("ValueError: customerMasterKey must be defined if kmsProvider is not local.", result);
keyVault.removeKeyAlternateName(keyId, "mongoKey2");
assert.eq(1, keyVault.getKey(keyId).toArray()[0].keyAltNames.length);
result = keyVault.deleteKey(keyId);
assert.eq(0, keyVault.getKey(keyId).itcount());
assert.eq(0, keyVault.getKeys().itcount());
keyVault.createKey("aws", "arn:aws:kms:us-east-1:fake:fake:fake1");
keyVault.createKey("aws", "arn:aws:kms:us-east-2:fake:fake:fake2");
keyVault.createKey("aws", "arn:aws:kms:us-east-3:fake:fake:fake3");
assert.eq(3, keyVault.getKeys().itcount());
MongoRunner.stopMongod(conn);
mock_kms.stop();
}());
|