Skip to content

Commit 17a2f8d

Browse files
jordan-smith721mongoKartChris Cho
authored
Qe equality follows (#3492)
* move return statement * remove helpers from displayed code (#3414) * capitalize initialisms in Go variable names * customer-master-key instead of master-key * update narrative to customer-master-key.txt and test highlighting * remove emphasize-lines for now, no staging available on this branch * remove duplicate tls code (#3427) * fix admonition and add some highlighting * update Go and Java driver versions * additional emphasize lines in Quick Start * aws fixes * remove extra line * azure updates * gcp changes * kmip changes * kmip fixes * always regen local key * update dependencies * add local CMK * more fixes * white space * cc feedback * update display for insert and find (#3444) * switch direnv to dotenv in Node sample app (#3468) * switch direnv to dotenv in Node sample app * remove helper code from C# display * Combine error message formatting with call to panic for Go tutorial * improve auto encrypt sections (#3469) * improve auto encrypt sections * cc feedback * Remove env variables (#3478) * use appsettings * add call to drop encrypted collection before creating (#3486) * autobuilder * Fix readmes and address comments * kmip return line fix --------- Co-authored-by: Mike Woofter <[email protected]> Co-authored-by: Mike Woofter <[email protected]> Co-authored-by: Chris Cho <[email protected]>
1 parent 7e10c32 commit 17a2f8d

35 files changed

+1161
-1014
lines changed

source/core/queryable-encryption/quick-start.txt

Lines changed: 161 additions & 119 deletions
Large diffs are not rendered by default.

source/core/queryable-encryption/tutorials/aws/aws-automatic.txt

Lines changed: 183 additions & 141 deletions
Large diffs are not rendered by default.

source/core/queryable-encryption/tutorials/azure/azure-automatic.txt

Lines changed: 183 additions & 141 deletions
Large diffs are not rendered by default.

source/core/queryable-encryption/tutorials/gcp/gcp-automatic.txt

Lines changed: 183 additions & 141 deletions
Large diffs are not rendered by default.

source/core/queryable-encryption/tutorials/kmip/kmip-automatic.txt

Lines changed: 198 additions & 217 deletions
Large diffs are not rendered by default.

source/includes/in-use-encryption/cmk-bash.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. note:: Generate a CMK from the Command Line
1+
.. tip:: Generate a CMK from the Command Line
22

33
Use the following command to generate a {+cmk-abbr+}
44
from a Unix shell or PowerShell:
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
namespace QueryableEncryption
1+
namespace QueryableEncryption;
2+
3+
public class Program
24
{
3-
public class Program
5+
public static void Main(string[] args)
46
{
5-
public static void Main(string[] args)
6-
{
7-
QueryableEncryptionTutorial.RunExample();
8-
}
7+
QueryableEncryptionTutorial.RunExample();
98
}
109
}

source/includes/qe-tutorials/csharp/QueryableEncryptionExample.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
12+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
1113
<PackageReference Include="MongoDB.Driver" Version="2.20.0" />
1214
</ItemGroup>
15+
16+
<ItemGroup>
17+
<None Update="appsettings.json">
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
1321

1422
</Project>

source/includes/qe-tutorials/csharp/QueryableEncryptionHelpers.cs

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
using System.Security.Cryptography;
22
using System.Security.Cryptography.X509Certificates;
3+
using Microsoft.Extensions.Configuration;
34
using MongoDB.Bson;
45
using MongoDB.Driver;
56
using MongoDB.Driver.Encryption;
67

78
namespace QueryableEncryption;
89

9-
public static class QueryableEncryptionHelpers
10+
public class QueryableEncryptionHelpers
1011
{
11-
public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProviderCredentials(string kmsProvider,
12+
private readonly IConfigurationRoot _appSettings;
13+
public QueryableEncryptionHelpers(IConfigurationRoot appSettings)
14+
{
15+
_appSettings = appSettings;
16+
}
17+
18+
public Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProviderCredentials(string kmsProvider,
1219
bool generateNewLocalKey)
1320
{
1421
if (kmsProvider == "aws")
@@ -17,8 +24,8 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
1724
var kmsProviderCredentials = new Dictionary<string, IReadOnlyDictionary<string, object>>();
1825
var awsKmsOptions = new Dictionary<string, object>
1926
{
20-
{ "accessKeyId", Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID")! }, // Your AWS access key ID
21-
{ "secretAccessKey", Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY")! } // Your AWS secret access key
27+
{ "accessKeyId", _appSettings["Aws:AccessKeyId"] }, // Your AWS access key ID
28+
{ "secretAccessKey", _appSettings["Aws:SecretAccessKey"] } // Your AWS secret access key
2229
};
2330
kmsProviderCredentials.Add(kmsProvider, awsKmsOptions);
2431
// end-aws-kms-credentials
@@ -30,9 +37,9 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
3037
var kmsProviderCredentials = new Dictionary<string, IReadOnlyDictionary<string, object>>();
3138
var azureKmsOptions = new Dictionary<string, object>
3239
{
33-
{ "tenantId", Environment.GetEnvironmentVariable("AZURE_TENANT_ID")! }, // Your Azure tenant ID
34-
{ "clientId", Environment.GetEnvironmentVariable("AZURE_CLIENT_ID")! }, // Your Azure client ID
35-
{ "clientSecret", Environment.GetEnvironmentVariable("AZURE_CLIENT_SECRET")! } // Your Azure client secret
40+
{ "tenantId", _appSettings["Azure:TenantId"] }, // Your Azure tenant ID
41+
{ "clientId", _appSettings["Azure:ClientId"] }, // Your Azure client ID
42+
{ "clientSecret", _appSettings["Azure:ClientSecret"] } // Your Azure client secret
3643
};
3744
kmsProviderCredentials.Add(kmsProvider, azureKmsOptions);
3845
// end-azure-kms-credentials
@@ -44,8 +51,8 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
4451
var kmsProviderCredentials = new Dictionary<string, IReadOnlyDictionary<string, object>>();
4552
var gcpKmsOptions = new Dictionary<string, object>
4653
{
47-
{ "email", Environment.GetEnvironmentVariable("GCP_EMAIL")! }, // Your GCP email
48-
{ "privateKey", Environment.GetEnvironmentVariable("GCP_PRIVATE_KEY")! } // Your GCP private key
54+
{ "email", _appSettings["Gcp:Email"] }, // Your GCP email
55+
{ "privateKey", _appSettings["Gcp:PrivateKey"] } // Your GCP private key
4956
};
5057
kmsProviderCredentials.Add(kmsProvider, gcpKmsOptions);
5158
// end-gcp-kms-credentials
@@ -57,7 +64,7 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
5764
var kmsProviderCredentials = new Dictionary<string, IReadOnlyDictionary<string, object>>();
5865
var kmipKmsOptions = new Dictionary<string, object>
5966
{
60-
{ "endpoint", Environment.GetEnvironmentVariable("KMIP_KMS_ENDPOINT")! } // Your KMIP KMS endpoint
67+
{ "endpoint", _appSettings["Kmip:KmsEndpoint"] } // Your KMIP KMS endpoint
6168
};
6269
kmsProviderCredentials.Add(kmsProvider, kmipKmsOptions);
6370
// end-kmip-kms-credentials
@@ -67,7 +74,7 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
6774
{
6875
if (generateNewLocalKey)
6976
{
70-
File.Delete("master-key.txt");
77+
File.Delete("customer-master-key.txt");
7178

7279
// start-generate-local-key
7380
using var randomNumberGenerator = RandomNumberGenerator.Create();
@@ -76,7 +83,7 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
7683
var bytes = new byte[96];
7784
randomNumberGenerator.GetBytes(bytes);
7885
var localCustomerMasterKeyBase64Write = Convert.ToBase64String(bytes);
79-
File.WriteAllText("master-key.txt", localCustomerMasterKeyBase64Write);
86+
File.WriteAllText("customer-master-key.txt", localCustomerMasterKeyBase64Write);
8087
}
8188
catch (Exception e)
8289
{
@@ -90,7 +97,7 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
9097
var kmsProviderCredentials = new Dictionary<string, IReadOnlyDictionary<string, object>>();
9198
try
9299
{
93-
var localCustomerMasterKeyBase64Read = File.ReadAllText("master-key.txt");
100+
var localCustomerMasterKeyBase64Read = File.ReadAllText("customer-master-key.txt");
94101
var localCustomerMasterKeyBytes = Convert.FromBase64String(localCustomerMasterKeyBase64Read);
95102

96103
var localOptions = new Dictionary<string, object>
@@ -100,27 +107,27 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
100107

101108
kmsProviderCredentials.Add(kmsProvider, localOptions);
102109
}
110+
// end-get-local-key
103111
catch (Exception e)
104112
{
105113
Console.WriteLine(e.Message);
106114
}
107-
// end-get-local-key
108115
return kmsProviderCredentials;
109116

110117
}
111118

112119
throw new Exception("Invalid KMS provider string");
113120
}
114121

115-
public static BsonDocument GetCustomerMasterKeyCredentials(string kmsProvider)
122+
public BsonDocument GetCustomerMasterKeyCredentials(string kmsProvider)
116123
{
117124
if (kmsProvider == "aws")
118125
{
119126
// start-aws-cmk-credentials
120127
var customerMasterKeyCredentials = new BsonDocument
121128
{
122-
{ "key", Environment.GetEnvironmentVariable("AWS_KEY_ARN")}, // Your AWS Key ARN
123-
{ "region", Environment.GetEnvironmentVariable("AWS_KEY_REGION") } // Your AWS Key Region
129+
{ "key", _appSettings["Aws:KeyArn"] }, // Your AWS Key ARN
130+
{ "region", _appSettings["Aws:KeyRegion"] } // Your AWS Key Region
124131
};
125132
// end-aws-cmk-credentials
126133
return customerMasterKeyCredentials;
@@ -130,8 +137,8 @@ public static BsonDocument GetCustomerMasterKeyCredentials(string kmsProvider)
130137
// start-azure-cmk-credentials
131138
var customerMasterKeyCredentials = new BsonDocument
132139
{
133-
{ "keyVaultEndpoint", Environment.GetEnvironmentVariable("AZURE_KEY_VAULT_ENDPOINT") }, // Your Azure Key Vault Endpoint
134-
{ "keyName", Environment.GetEnvironmentVariable("AZURE_KEY_NAME") } // Your Azure Key Name
140+
{ "keyVaultEndpoint", _appSettings["Azure:KeyVaultEndpoint"] }, // Your Azure Key Vault Endpoint
141+
{ "keyName", _appSettings["Azure:KeyName"] } // Your Azure Key Name
135142
};
136143
// end-azure-cmk-credentials
137144
return customerMasterKeyCredentials;
@@ -141,43 +148,44 @@ public static BsonDocument GetCustomerMasterKeyCredentials(string kmsProvider)
141148
// start-gcp-cmk-credentials
142149
var customerMasterKeyCredentials = new BsonDocument
143150
{
144-
{ "projectId", Environment.GetEnvironmentVariable("GCP_PROJECT_ID") }, // Your GCP Project ID
145-
{ "location", Environment.GetEnvironmentVariable("GCP_LOCATION") }, // Your GCP Key Location
146-
{ "keyRing", Environment.GetEnvironmentVariable("GCP_KEY_RING") }, // Your GCP Key Ring
147-
{ "keyName", Environment.GetEnvironmentVariable("GCP_KEY_NAME")} // Your GCP Key Name
151+
{ "projectId", _appSettings["Gcp:ProjectId"] }, // Your GCP Project ID
152+
{ "location", _appSettings["Gcp:Location"] }, // Your GCP Key Location
153+
{ "keyRing", _appSettings["Gcp:KeyRing"] }, // Your GCP Key Ring
154+
{ "keyName", _appSettings["Gcp:KeyName"] } // Your GCP Key Name
148155
};
149156
// end-gcp-cmk-credentials
150-
return customerMasterKeyCredentials;
157+
return customerMasterKeyCredentials;
151158
}
152-
else if (kmsProvider == "kmip")
159+
else if (kmsProvider == "kmip" || kmsProvider == "local")
153160
{
154161
// start-kmip-local-cmk-credentials
155162
var customerMasterKeyCredentials = new BsonDocument();
156163
// end-kmip-local-cmk-credentials
157164
return customerMasterKeyCredentials;
158-
} else
159-
{
165+
}
166+
else
167+
{
160168
throw new Exception("Invalid KMS provider string");
161169
}
162170
}
163171

164-
public static AutoEncryptionOptions GetAutoEncryptionOptions(CollectionNamespace keyVaultNamespace,
172+
public AutoEncryptionOptions GetAutoEncryptionOptions(CollectionNamespace keyVaultNamespace,
165173
IReadOnlyDictionary<string, IReadOnlyDictionary<string, object>> kmsProviderCredentials)
166174
{
167175
var kmsProvider = kmsProviderCredentials.Keys.First();
168176

169177
if (kmsProvider == "kmip")
170178
{
179+
var tlsOptions = GetKmipTlsOptions();
180+
171181
// start-kmip-encryption-options
172182
var extraOptions = new Dictionary<string, object>
173183
{
174-
{ "cryptSharedLibPath", Environment.GetEnvironmentVariable(
175-
"CRYPT_SHARED_LIB_PATH") } // Path to your Automatic Encryption Shared Library
184+
{ "cryptSharedLibPath", _appSettings["CryptSharedLibPath"] } // Path to your Automatic Encryption Shared Library
176185
};
177186

178-
var tlsOptions = GetKmipTlsOptions();
179-
180-
var autoEncryptionOptions = new AutoEncryptionOptions(keyVaultNamespace,
187+
var autoEncryptionOptions = new AutoEncryptionOptions(
188+
keyVaultNamespace,
181189
kmsProviderCredentials,
182190
extraOptions: extraOptions,
183191
tlsOptions: tlsOptions);
@@ -189,11 +197,11 @@ public static AutoEncryptionOptions GetAutoEncryptionOptions(CollectionNamespace
189197
// start-auto-encryption-options
190198
var extraOptions = new Dictionary<string, object>
191199
{
192-
{ "cryptSharedLibPath", Environment.GetEnvironmentVariable(
193-
"CRYPT_SHARED_LIB_PATH") } // Path to your Automatic Encryption Shared Library
200+
{ "cryptSharedLibPath", _appSettings["CryptSharedLibPath"] } // Path to your Automatic Encryption Shared Library
194201
};
195202

196-
var autoEncryptionOptions = new AutoEncryptionOptions(keyVaultNamespace,
203+
var autoEncryptionOptions = new AutoEncryptionOptions(
204+
keyVaultNamespace,
197205
kmsProviderCredentials,
198206
extraOptions: extraOptions);
199207
// end-auto-encryption-options
@@ -202,7 +210,7 @@ public static AutoEncryptionOptions GetAutoEncryptionOptions(CollectionNamespace
202210
}
203211
}
204212

205-
public static ClientEncryption GetClientEncryption(IMongoClient keyVaultClient,
213+
public ClientEncryption GetClientEncryption(IMongoClient keyVaultClient,
206214
CollectionNamespace keyVaultNamespace, Dictionary<string, IReadOnlyDictionary<string, object>> kmsProviderCredentials)
207215
{
208216
var kmsProvider = kmsProviderCredentials.Keys.First();
@@ -236,12 +244,12 @@ public static ClientEncryption GetClientEncryption(IMongoClient keyVaultClient,
236244
}
237245
}
238246

239-
private static Dictionary<string, SslSettings> GetKmipTlsOptions()
247+
private Dictionary<string, SslSettings> GetKmipTlsOptions()
240248
{
241249
// start-tls-options
242250
var tlsOptions = new Dictionary<string, SslSettings>();
243251
var sslSettings = new SslSettings();
244-
var clientCertificate = new X509Certificate2(Environment.GetEnvironmentVariable("KMIP_TLS_CERT_P12")!); // Full path to your client certificate p12 file
252+
var clientCertificate = new X509Certificate2(_appSettings["Kmip:TlsCertP12"]!); // Full path to your client certificate p12 file
245253
sslSettings.ClientCertificates = new[] { clientCertificate };
246254
tlsOptions.Add("kmip", sslSettings);
247255
// end-tls-options

source/includes/qe-tutorials/csharp/QueryableEncryptionTutorial.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
using Microsoft.Extensions.Configuration;
12
using MongoDB.Bson;
23
using MongoDB.Bson.Serialization.Conventions;
34
using MongoDB.Driver;
4-
using static QueryableEncryption.QueryableEncryptionHelpers;
55

66
namespace QueryableEncryption;
77

@@ -11,7 +11,7 @@ public static async void RunExample()
1111
{
1212
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
1313
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
14-
14+
1515
// start-setup-application-variables
1616
// KMS provider name should be one of the following: "aws", "gcp", "azure", "kmip" or "local"
1717
const string kmsProviderName = "<your KMS provider name>";
@@ -21,20 +21,23 @@ public static async void RunExample()
2121
CollectionNamespace.FromFullName($"{keyVaultDatabaseName}.{keyVaultCollectionName}");
2222
const string encryptedDatabaseName = "medicalRecords";
2323
const string encryptedCollectionName = "patients";
24-
var uri = Environment.GetEnvironmentVariable("MONGODB_URI"); // Your connection URI
24+
25+
var appSettings = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
26+
var uri = appSettings["MongoDbUri"];
2527
// end-setup-application-variables
26-
27-
var kmsProviderCredentials = GetKmsProviderCredentials(kmsProviderName,
28-
generateNewLocalKey: false);
29-
28+
29+
var qeHelpers = new QueryableEncryptionHelpers(appSettings);
30+
var kmsProviderCredentials = qeHelpers.GetKmsProviderCredentials(kmsProviderName,
31+
generateNewLocalKey: true);
32+
3033
// start-create-client
3134
var clientSettings = MongoClientSettings.FromConnectionString(uri);
32-
clientSettings.AutoEncryptionOptions = GetAutoEncryptionOptions(
35+
clientSettings.AutoEncryptionOptions = qeHelpers.GetAutoEncryptionOptions(
3336
keyVaultNamespace,
3437
kmsProviderCredentials);
3538
var encryptedClient = new MongoClient(clientSettings);
3639
// end-create-client
37-
40+
3841
var keyDatabase = encryptedClient.GetDatabase(keyVaultDatabaseName);
3942

4043
// Drop the collection in case you created it in a previous run of this application.
@@ -51,7 +54,7 @@ public static async void RunExample()
5154
{ "keyId", BsonNull.Value },
5255
{ "path", "record.ssn" },
5356
{ "bsonType", "string" },
54-
{ "queries", new BsonDocument("queryType", "equality") }
57+
{ "queries", new BsonDocument("queryType", "equality") }
5558
},
5659
new BsonDocument
5760
{
@@ -63,25 +66,29 @@ public static async void RunExample()
6366
}
6467
};
6568
// end-encrypted-fields-map
66-
69+
6770
var patientDatabase = encryptedClient.GetDatabase(encryptedDatabaseName);
68-
var clientEncryption = GetClientEncryption(encryptedClient,
71+
patientDatabase.DropCollection(encryptedCollectionName);
72+
73+
var clientEncryption = qeHelpers.GetClientEncryption(encryptedClient,
6974
keyVaultNamespace,
7075
kmsProviderCredentials);
71-
76+
77+
var customerMasterKeyCredentials = qeHelpers.GetCustomerMasterKeyCredentials(kmsProviderName);
78+
7279
// start-create-encrypted-collection
7380
var createCollectionOptions = new CreateCollectionOptions<Patient>
7481
{
75-
EncryptedFields = encryptedFields
82+
EncryptedFields = encryptedFields
7683
};
77-
78-
clientEncryption.CreateEncryptedCollection(patientDatabase,
84+
85+
clientEncryption.CreateEncryptedCollection(patientDatabase,
7986
encryptedCollectionName,
8087
createCollectionOptions,
8188
kmsProviderName,
82-
GetCustomerMasterKeyCredentials(kmsProviderName));
89+
customerMasterKeyCredentials);
8390
// end-create-encrypted-collection
84-
91+
8592
// start-insert-document
8693
var patient = new Patient
8794
{
@@ -100,10 +107,10 @@ public static async void RunExample()
100107

101108
var encryptedCollection = encryptedClient.GetDatabase(encryptedDatabaseName).
102109
GetCollection<Patient>(encryptedCollectionName);
103-
110+
104111
encryptedCollection.InsertOne(patient);
105112
// end-insert-document
106-
113+
107114
// start-find-document
108115
var ssnFilter = Builders<Patient>.Filter.Eq("record.ssn", patient.Record.Ssn);
109116
var findResult = await encryptedCollection.Find(ssnFilter).ToCursorAsync();

0 commit comments

Comments
 (0)