-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathqueryable-encryption-helpers.js
226 lines (212 loc) · 6.68 KB
/
queryable-encryption-helpers.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
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
import "dotenv/config";
import { writeFileSync, readFileSync, existsSync } from "fs";
import { randomBytes } from "crypto";
import { ClientEncryption } from "mongodb";
export async function dropExistingCollection(client, databaseName) {
const database = client.db(databaseName);
await database.dropDatabase();
}
export function getKMSProviderCredentials(kmsProviderName) {
let kmsProviders;
switch (kmsProviderName) {
case "aws":
// start-aws-kms-credentials
kmsProviders = {
aws: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID, // Your AWS access key ID
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, // Your AWS secret access key
},
};
// end-aws-kms-credentials
return kmsProviders;
case "azure":
// start-azure-kms-credentials
kmsProviders = {
azure: {
tenantId: process.env.AZURE_TENANT_ID, // Your Azure tenant ID
clientId: process.env.AZURE_CLIENT_ID, // Your Azure client ID
clientSecret: process.env.AZURE_CLIENT_SECRET, // Your Azure client secret
},
};
// end-azure-kms-credentials
return kmsProviders;
case "gcp":
// start-gcp-kms-credentials
kmsProviders = {
gcp: {
email: process.env.GCP_EMAIL, // Your GCP email
privateKey: process.env.GCP_PRIVATE_KEY, // Your GCP private key
},
};
// end-gcp-kms-credentials
return kmsProviders;
case "kmip":
// start-kmip-kms-credentials
kmsProviders = {
kmip: {
endpoint: process.env.KMIP_KMS_ENDPOINT, // Your KMIP KMS endpoint
},
};
// end-kmip-kms-credentials
return kmsProviders;
case "local":
(function () {
// start-generate-local-key
if (!existsSync("./customer-master-key.txt")) {
try {
writeFileSync("customer-master-key.txt", randomBytes(96));
} catch (err) {
throw new Error(
`Unable to write Customer Master Key to file due to the following error: ${err}`
);
}
}
// end-generate-local-key
})();
try {
// start-get-local-key
// WARNING: Do not use a local key file in a production application
const localMasterKey = readFileSync("./customer-master-key.txt");
if (localMasterKey.length !== 96) {
throw new Error(
"Expected the customer master key file to be 96 bytes."
);
}
kmsProviders = {
local: {
key: localMasterKey,
},
};
// end-get-local-key
} catch (err) {
throw new Error(
`Unable to read the Customer Master Key due to the following error: ${err}`
);
}
return kmsProviders;
default:
throw new Error(
`Unrecognized value for KMS provider name \"${kmsProviderName}\" encountered while retrieving KMS credentials.`
);
}
}
export function getCustomerMasterKeyCredentials(kmsProviderName) {
let customerMasterKeyCredentials;
switch (kmsProviderName) {
case "aws":
// start-aws-cmk-credentials
customerMasterKeyCredentials = {
key: process.env.AWS_KEY_ARN, // Your AWS Key ARN
region: process.env.AWS_KEY_REGION, // Your AWS Key Region
};
// end-aws-cmk-credentials
return customerMasterKeyCredentials;
case "azure":
// start-azure-cmk-credentials
customerMasterKeyCredentials = {
keyVaultEndpoint: process.env.AZURE_KEY_VAULT_ENDPOINT, // Your Azure Key Vault Endpoint
keyName: process.env.AZURE_KEY_NAME, // Your Azure Key Name
};
// end-azure-cmk-credentials
return customerMasterKeyCredentials;
case "gcp":
// start-gcp-cmk-credentials
customerMasterKeyCredentials = {
projectId: process.env.GCP_PROJECT_ID, // Your GCP Project ID
location: process.env.GCP_LOCATION, // Your GCP Key Location
keyRing: process.env.GCP_KEY_RING, // Your GCP Key Ring
keyName: process.env.GCP_KEY_NAME, // Your GCP Key Name
};
// end-gcp-cmk-credentials
return customerMasterKeyCredentials;
case "kmip":
case "local":
// start-kmip-local-cmk-credentials
customerMasterKeyCredentials = {};
// end-kmip-local-cmk-credentials
return customerMasterKeyCredentials;
default:
throw new Error(
`Unrecognized value for KMS provider name \"${kmsProviderName}\" encountered while retrieving Customer Master Key credentials.`
);
}
}
export async function getAutoEncryptionOptions(
kmsProviderName,
keyVaultNamespace,
kmsProviders
) {
if (kmsProviderName === "kmip") {
const tlsOptions = getKmipTlsOptions();
// start-kmip-encryption-options
const extraOptions = {
cryptSharedLibPath: process.env.SHARED_LIB_PATH, // Path to your Automatic Encryption Shared Library
};
const autoEncryptionOptions = {
keyVaultNamespace,
kmsProviders,
extraOptions,
tlsOptions,
};
// end-kmip-encryption-options
return autoEncryptionOptions;
} else {
// start-auto-encryption-options
const extraOptions = {
cryptSharedLibPath: process.env.SHARED_LIB_PATH, // Path to your Automatic Encryption Shared Library
};
const autoEncryptionOptions = {
keyVaultNamespace,
kmsProviders,
extraOptions,
};
// end-auto-encryption-options
return autoEncryptionOptions;
}
}
function getKmipTlsOptions() {
// start-tls-options
const tlsOptions = {
kmip: {
tlsCAFile: process.env.KMIP_TLS_CA_FILE, // Path to your TLS CA file
tlsCertificateKeyFile: process.env.KMIP_TLS_CERT_FILE, // Path to your TLS certificate key file
},
};
// end-tls-options
return tlsOptions;
}
export function getClientEncryption(encryptedClient, autoEncryptionOptions) {
// start-client-encryption
const clientEncryption = new ClientEncryption(
encryptedClient,
autoEncryptionOptions
);
// end-client-encryption
return clientEncryption;
}
export async function createEncryptedCollection(
clientEncryption,
encryptedDatabase,
encryptedCollectionName,
kmsProviderName,
encryptedFieldsMap,
customerMasterKeyCredentials
) {
try {
// start-create-encrypted-collection
await clientEncryption.createEncryptedCollection(
encryptedDatabase,
encryptedCollectionName,
{
provider: kmsProviderName,
createCollectionOptions: encryptedFieldsMap,
masterKey: customerMasterKeyCredentials,
}
);
// end-create-encrypted-collection
} catch (err) {
throw new Error(
`Unable to create encrypted collection due to the following error: ${err}`
);
}
}