Skip to content

Commit 0950c43

Browse files
authored
Updates mongosh code with createEncryptedCollection method (#3401)
* updates mongosh code with createEncryptedCollection method * reformat helper funcs * reformat helper funcs * addl comment
1 parent 7c28056 commit 0950c43

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

source/includes/qe-tutorials/mongosh/queryable-encryption-helpers.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,46 @@ function getKMSProviderCredentials(kmsProviderName) {
7777
}
7878
}
7979

80+
function getCustomerMasterKeyCredentials(kmsProviderString) {
81+
let customerMasterKeyCredentials;
82+
switch (kmsProviderString) {
83+
case "aws":
84+
// start-aws-cmk-credentials
85+
customerMasterKeyCredentials = {
86+
key: process.env["AWS_KEY_ARN"], // Your AWS Key ARN
87+
region: process.env["AWS_KEY_REGION"], // Your AWS Key Region
88+
};
89+
// end-aws-cmk-credentials
90+
return customerMasterKeyCredentials;
91+
case "azure":
92+
// start-azure-cmk-credentials
93+
customerMasterKeyCredentials = {
94+
keyVaultEndpoint: process.env["AZURE_KEY_VAULT_ENDPOINT"], // Your Azure Key Vault Endpoint
95+
keyName: process.env["AZURE_KEY_NAME"], // Your Azure Key Name
96+
};
97+
// end-azure-cmk-credentials
98+
return customerMasterKeyCredentials;
99+
case "gcp":
100+
// start-gcp-cmk-credentials
101+
customerMasterKeyCredentials = {
102+
projectId: process.env["GCP_PROJECT_ID"], // Your GCP Project ID
103+
location: process.env["GCP_LOCATION"], // Your GCP Key Location
104+
keyRing: process.env["GCP_KEY_RING"], // Your GCP Key Ring
105+
keyName: process.env["GCP_KEY_NAME"], // Your GCP Key Name
106+
};
107+
// end-gcp-cmk-credentials
108+
return customerMasterKeyCredentials;
109+
case "kmip":
110+
case "local":
111+
// start-kmip-local-cmk-credentials
112+
customerMasterKeyCredentials = {};
113+
// end-kmip-local-cmk-credentials
114+
return customerMasterKeyCredentials;
115+
default:
116+
throw new Error("Invalid KMS provider name");
117+
}
118+
}
119+
80120
async function getAutoEncryptionOptions(
81121
kmsProviderName,
82122
keyVaultNamespace,

source/includes/qe-tutorials/mongosh/queryable-encryption-tutorial.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ async function runExample() {
1515
// end-setup-application-variables
1616

1717
const kmsProviderCredentials = qeHelper.getKMSProviderCredentials(kmsProviderName);
18+
const customerMasterKeyCredentials = qeHelper.getCustomerMasterKeyCredentials(kmsProviderName);
1819

1920
const autoEncryptionOpts = await qeHelper.getAutoEncryptionOptions(
2021
kmsProviderName,
2122
keyVaultNamespace,
2223
kmsProviderCredentials
2324
);
24-
autoEncryptionOpts.bypassQueryAnalysis = false
2525

2626
// start-encrypted-fields-map
2727
const encryptedFieldsMap = {
@@ -40,7 +40,6 @@ async function runExample() {
4040
},
4141
};
4242
// end-encrypted-fields-map
43-
autoEncryptionOpts.encryptedFieldsMap = encryptedFieldsMap
4443

4544
// start-create-client
4645
const encryptedClient = Mongo(uri, autoEncryptionOpts);
@@ -52,6 +51,19 @@ async function runExample() {
5251
);
5352
await qeHelper.dropExistingCollection(encryptedClient, keyVaultDatabaseName);
5453

54+
// start-create-encrypted-collection
55+
const clientEncryption = encryptedClient.getClientEncryption()
56+
await clientEncryption.createEncryptedCollection(
57+
encryptedDatabaseName,
58+
encryptedCollectionName,
59+
{
60+
provider: kmsProviderName,
61+
createCollectionOptions: encryptedFieldsMap,
62+
masterKey: customerMasterKeyCredentials
63+
}
64+
);
65+
// end-create-encrypted-collection
66+
5567
// start-insert-document
5668
const patientDocument = {
5769
patientName: "Jon Doe",

0 commit comments

Comments
 (0)