Skip to content

Commit e2a64cd

Browse files
terakilobyteChris ChoChris Cho
authored
C# additions to the CSFLE guide (#696)
* add dotnet gcp info; cleanup node note * fix found typo * adds c# driver to csfle guide * add steps for mongoclient * DOCSP-13035: CSFLE Azure and Java (#687) * DOCSP-12575: add Azure instructions for Java * DOCSP-13038: Google Cloud KMS and Python (#688) * DOCSP-13038- update steps for Python and GCP * Update source/includes/steps-fle-configure-the-mongodb-client.yaml Co-authored-by: Chris Cho <[email protected]> * fix found typo * adds c# driver to csfle guide * add steps for mongoclient * Update source/includes/steps-fle-configure-the-mongodb-client.yaml Co-authored-by: Chris Cho <[email protected]> * update placeholder links * conform to style guide * dataKeyId paste note Co-authored-by: Chris Cho <[email protected]> Co-authored-by: Chris Cho <[email protected]>
1 parent 11c0f31 commit e2a64cd

4 files changed

+298
-8
lines changed

source/includes/steps-fle-configure-the-mongodb-client.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ content: |
2626
.. code-block:: python
2727
2828
key_vault_namespace = "encryption.__keyVault"
29+
.. tab::
30+
:tabid: csharp
31+
32+
.. code-block:: csharp
33+
34+
var keyVaultNamespace = CollectionNamespace.FromFullName("encryption.__keyVault");
35+
2936
---
3037
title: Specify the Local Master Encryption Key
3138
ref: specify-the-local-master-encryption-key
@@ -70,6 +77,20 @@ content: |
7077
"key": local_master_key
7178
}
7279
}
80+
.. tab::
81+
:tabid: csharp
82+
83+
.. code-block:: csharp
84+
85+
var kmsProviders = new Dictionary<string, IReadOnlyDictionary<string, object>>();
86+
var localMasterKeyBase64 = File.ReadAllText(__localMasterKeyPath);
87+
var localMasterKeyBytes = Convert.FromBase64String(localMasterKeyBase64);
88+
var localOptions = new Dictionary<string, object>
89+
{
90+
{ "key", localMasterKeyBytes }
91+
};
92+
kmsProviders.Add("local", localOptions);
93+
7394
---
7495
title: Map the JSON Schema to the Patients Collection
7596
ref: map-the-json-schema-to-the-patients-collection
@@ -107,6 +128,15 @@ content: |
107128
patient_schema = {
108129
"medicalRecords.patients": json_schema
109130
}
131+
132+
.. tab::
133+
:tabid: csharp
134+
135+
.. code-block:: csharp
136+
137+
// JsonSchemaCreator is a utility class found in the C# companion
138+
// project to this guide
139+
var schema = JsonSchemaCreator.CreateJsonSchema(keyIdBase64);
110140
---
111141
title: Specify the Location of the Encryption Binary
112142
ref: specify-the-location-of-the-encryption-binary
@@ -184,6 +214,14 @@ content: |
184214
:emphasize-lines: 1
185215
186216
extra_options['mongocryptd_bypass_spawn'] = True
217+
.. tab::
218+
:tabid: csharp
219+
220+
221+
.. note:: Encryption Library
222+
223+
Ensure ``mongocrypt.dll` is included with your application's build
224+
assets.
187225
188226
---
189227
title: Create the MongoClient
@@ -242,3 +280,17 @@ content: |
242280
**extra_options
243281
)
244282
client = MongoClient(connection_string, auto_encryption_opts=fle_opts)
283+
284+
.. tab::
285+
:tabid: csharp
286+
287+
.. code-block:: csharp
288+
289+
var clientSettings = MongoClientSettings.FromConnectionString(_connectionString);
290+
var autoEncryptionOptions = new AutoEncryptionOptions(
291+
keyVaultNamespace: keyVaultNamespace,
292+
kmsProviders: kmsProviders,
293+
schemaMap: schemaMap,
294+
extraOptions: extraOptions);
295+
clientSettings.AutoEncryptionOptions = autoEncryptionOptions;
296+
var client = new MongoClient(clientSettings);

source/includes/steps-fle-convert-to-a-remote-master-key-azure.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ content: |
132132
.. tab::
133133
:tabid: csharp
134134
135-
.. code-block:: chsarp
135+
.. code-block:: csharp
136136
137137
var kmsProviders = new Dictionary<string, IReadOnlyDictionary<string, object>>();
138138

source/includes/steps-fle-create-data-encryption-key.yaml

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ content: |
4747
with open(path, "rb") as f:
4848
local_master_key = f.read()
4949
50+
.. tab::
51+
:tabid: csharp
52+
53+
.. code-block:: csharp
54+
55+
string localMasterKeyBase64 = File.ReadAllText(__localMasterKeyPath);
56+
var localMasterKeyBytes = Convert.FromBase64String(localMasterKeyBase64);
57+
5058
---
5159
title: Specify KMS Provider Settings
5260
ref: specify-kms-provider-settings
@@ -91,11 +99,23 @@ content: |
9199
.. code-block:: python
92100
:emphasize-lines: 2,3
93101
94-
kms_providers = {
95-
"local": {
96-
"key": local_master_key # local_master_key variable from the previous step
97-
},
98-
}
102+
kms_providers = {
103+
"local": {
104+
"key": local_master_key # local_master_key variable from the previous step
105+
},
106+
}
107+
108+
.. tab::
109+
:tabid: csharp
110+
111+
.. code-block:: csharp
112+
113+
var kmsProviders = new Dictionary<string, IReadOnlyDictionary<string, object>>();
114+
var localOptions = new Dictionary<string, object>
115+
{
116+
{ "key", localMasterKeyBytes }
117+
};
118+
kmsProviders.Add("local", localOptions);
99119
100120
---
101121
title: Create a Data Encryption Key
@@ -104,8 +124,8 @@ level: 4
104124
content: |
105125
Construct a client with the MongoDB connection string and key vault
106126
namespace configuration, create a unique index on the ``keyAltNames`` field
107-
in that collection, and create a data encryption key with the following
108-
**code snippet**. The key vault in this example uses the ``encryption``
127+
in that collection, and create a data encryption key with the following
128+
**code snippet**. The key vault in this example uses the ``encryption``
109129
database and ``__keyVault`` collection.
110130
111131
.. tabs-drivers::
@@ -213,6 +233,23 @@ content: |
213233
214234
data_key_id = create_data_encryption_key()
215235
236+
.. tab::
237+
:tabid: csharp
238+
239+
.. code-block:: csharp
240+
241+
var keyVaultClient = new MongoClient(_connectionString);
242+
var clientEncryptionOptions = new ClientEncryptionOptions(
243+
keyVaultClient: keyVaultClient,
244+
keyVaultNamespace: _keyVaultNamespace,
245+
kmsProviders: kmsProviders);
246+
var clientEncryption = new ClientEncryption(kmsProviders);
247+
var dataKeyId = clientEncryption.CreateDataKey("local", new DataKeyOptions(), CancellationToken.None);
248+
Console.WriteLine($"Local DataKeyId [UUID]: {dataKeyId}");
249+
var dataKeyIdBase64 = Convert.ToBase64String(GuidConverter.ToBytes(dataKeyId, GuidRepresentation.Standard));
250+
Console.WriteLine($"Local DataKeyId [base64]: {dataKeyIdBase64}");
251+
252+
216253
217254
The ``_id`` field of the data encryption key is represented as a **UUID**
218255
and is encoded in **Base64** format. Use your **Base64**-encoded data key
@@ -387,6 +424,49 @@ content: |
387424
}
388425
}
389426
427+
.. tab::
428+
:tabid: csharp
429+
430+
.. code-block:: csharp
431+
432+
433+
var client = new MongoClient("mongodb://localhost:27017");
434+
// dataKeyId is the UUID from the previous step that was printed
435+
// to the console
436+
var dataKeyId = "<paste your dataKeyId value here>"
437+
var collection = client
438+
.GetDatabase(_keyVaultNamespace.DatabaseNamespace.DatabaseName)
439+
.GetCollection<BsonDocument>(
440+
_keyVaultNamespace.CollectionName,
441+
new MongoCollectionSettings
442+
{
443+
#pragma warning disable CS0618
444+
GuidRepresentation = GuidRepresentation.Standard
445+
#pragma warning restore CS0618
446+
});
447+
var query = Builders<BsonDocument>.Filter.Eq("_id", new BsonBinaryData(dataKeyId, GuidRepresentation.Standard));
448+
var keyDocument = collection
449+
.Find(query)
450+
.Single();
451+
452+
Console.WriteLine(keyDocument);
453+
454+
This code example should print a retrieved document that resembles the
455+
following:
456+
457+
.. code-block:: none
458+
459+
{
460+
"_id" : CSUUID("aae4f3b4-91b6-4cef-8867-3113a6dfb27b"),
461+
"keyMaterial" : new BinData(0, "rcfTQLRxF1mg98/Jr7iFwXWshvAVIQY6JCswrW+4bSqvLwa8bQrc65w7+3P3k+TqFS+1Ce6FW4Epf5o/eqDyT//I73IRc+yPUoZew7TB1pyIKmxL6ABPXJDkUhvGMiwwkRABzZcU9NNpFfH+HhIXjs324FuLzylIhAmJA/gvXcuz6QSD2vFpSVTRBpNu1sq0C9eZBSBaOxxotMZAcRuqMA=="),
462+
"creationDate" : ISODate("2020-11-08T17:58:36.372Z"),
463+
"updateDate" : ISODate("2020-11-08T17:58:36.372Z"),
464+
"status" : 0,
465+
"masterKey" : {
466+
"provider" : "local"
467+
}
468+
}
469+
390470
391471
This retrieved document contains the following data:
392472

0 commit comments

Comments
 (0)