-
Notifications
You must be signed in to change notification settings - Fork 0
/
second_day.js
76 lines (65 loc) · 1.63 KB
/
second_day.js
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
// run via: mongosh "mongodb://sdeadmin:s3cr3t%21@mongodb-0:27017/?replicaSet=rs0" --tls --tlsCAFile /data/pki/ca.pem --eval 'load("second_day.js")'
// Create Index
db.getSiblingDB("__encryption").getCollection("__keyVault").createIndex(
{
keyAltNames: 1
},
{
unique: true,
partialFilterExpression: {
"keyAltNames": {
"$exists": true
}
}
}
);
// Create DEK
const provider = {
"kmip": { // <-- KMS provider name
"endpoint": "kmip0:5696"
}
};
const tlsOptions = {
kmip: {
tlsCAFile: "/data/pki/ca.pem",
tlsCertificateKeyFile: "/data/pki/server.pem"
}
};
const autoEncryptionOpts = {
kmsProviders : provider,
schemaMap: {}, //no schema map
keyVaultNamespace: "__encryption.__keyVault",
tlsOptions: tlsOptions
};
encryptedClient = Mongo("mongodb://sdeadmin:s3cr3t%21@mongodb-0:27017/?replicaSet=rs0&tls=true&tlsCAFile=%data%pki%2Fca.pem", autoEncryptionOpts);
keyVault = encryptedClient.getKeyVault();
keyVault.createKey(
"kmip", // <-- KMS provider name
{
"keyId": "1"
}, // <-- CMK info (specific to AWS in this case)
["dataKey1"] // <-- Key alternative name
);
// Retrieve all the keys
keyVault.getKeys();
// Create User and Role
db.getSiblingDB('admin').createRole({
"role": "cryptoClient",
"privileges": [
{
resource: {
db: "__encryption",
collection: "__keyVault"
},
actions: [ "find" ]
}
],
"roles": [ ]
});
db.getSiblingDB('admin').createUser({
"user": "app_user",
"pwd": "SuperP@ssword123!",
"roles": ["cryptoClient", {'role': "readWrite", 'db': 'companyData'} ]
});
db.getSiblingDB("companyData").createCollection("employee");
exit;