Skip to content

Commit 6013fc2

Browse files
Add positive scenario.
1 parent c0e4ab1 commit 6013fc2

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/MongoDB.Driver/Encryption/ClientEncryption.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using MongoDB.Bson;
2121
using MongoDB.Driver.Core.Clusters;
2222
using MongoDB.Driver.Core.Configuration;
23+
using MongoDB.Driver.Core.Misc;
2324
using MongoDB.Libmongocrypt;
2425

2526
namespace MongoDB.Driver.Encryption
@@ -91,6 +92,10 @@ public Task<BsonDocument> AddAlternateKeyNameAsync(Guid id, string alternateKeyN
9192
/// </remarks>
9293
public void CreateEncryptedCollection<TCollection>(CollectionNamespace collectionNamespace, CreateCollectionOptions createCollectionOptions, string kmsProvider, DataKeyOptions dataKeyOptions, CancellationToken cancellationToken = default)
9394
{
95+
Ensure.IsNotNull(createCollectionOptions, nameof(createCollectionOptions));
96+
Ensure.IsNotNull(dataKeyOptions, nameof(dataKeyOptions));
97+
Ensure.IsNotNull(kmsProvider, nameof(kmsProvider));
98+
9499
foreach (var fieldDocument in EncryptedCollectionHelper.IterateEmptyKeyIds(collectionNamespace, createCollectionOptions.EncryptedFields))
95100
{
96101
var dataKey = CreateDataKey(kmsProvider, dataKeyOptions, cancellationToken);
@@ -115,6 +120,10 @@ public void CreateEncryptedCollection<TCollection>(CollectionNamespace collectio
115120
/// </remarks>
116121
public async Task CreateEncryptedCollectionAsync<TCollection>(CollectionNamespace collectionNamespace, CreateCollectionOptions createCollectionOptions, string kmsProvider, DataKeyOptions dataKeyOptions, CancellationToken cancellationToken = default)
117122
{
123+
Ensure.IsNotNull(createCollectionOptions, nameof(createCollectionOptions));
124+
Ensure.IsNotNull(dataKeyOptions, nameof(dataKeyOptions));
125+
Ensure.IsNotNull(kmsProvider, nameof(kmsProvider));
126+
118127
foreach (var fieldDocument in EncryptedCollectionHelper.IterateEmptyKeyIds(collectionNamespace, createCollectionOptions.EncryptedFields))
119128
{
120129
var dataKey = await CreateDataKeyAsync(kmsProvider, dataKeyOptions, cancellationToken).ConfigureAwait(false);

tests/MongoDB.Driver.Tests/Specifications/client-side-encryption/prose-tests/ClientEncryptionProseTests.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public ClientEncryptionProseTests(ITestOutputHelper testOutputHelper)
8585
// public methods
8686
[SkippableTheory]
8787
[ParameterAttributeData]
88-
public void AutomaticDataEncryptionKeys(
89-
[Range(1, 3)] int testCase,
88+
public void AutomaticDataEncryptionKeysTest(
89+
[Range(1, 4)] int testCase,
9090
[Values(false, true)] bool async)
9191
{
9292
RequireServer.Check().Supports(Feature.Csfle2).ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded, ClusterType.LoadBalanced);
@@ -138,6 +138,15 @@ void RunTestCase(int testCase)
138138
exception.Should().BeOfType<MongoCommandException>().Which.Message.Should().Contain("BSON field 'create.encryptedFields.fields.keyId' is the wrong type 'bool', expected type 'binData'");
139139
}
140140
break;
141+
case 4: // Case 4: Insert encrypted value
142+
{
143+
var createCollectionOptions = new CreateCollectionOptions { EncryptedFields = encryptedFields };
144+
var collection = CreateEncryptedCollection(client, clientEncryption, __collCollectionNamespace, createCollectionOptions, kmsProvider, async);
145+
var dataKey = createCollectionOptions.EncryptedFields["fields"].AsBsonArray[0].AsBsonDocument["keyId"].AsGuid; // get generated datakey
146+
var encryptedValue = ExplicitEncrypt(clientEncryption, new EncryptOptions(algorithm: EncryptionAlgorithm.Unindexed, keyId: dataKey), "123-45-6789", async); // use explicit encryption to encrypt data before inserting
147+
Insert(collection, async, new BsonDocument("ssn", encryptedValue));
148+
}
149+
break;
141150
default: throw new Exception($"Unexpected test case {testCase}.");
142151
}
143152
}
@@ -2267,6 +2276,11 @@ private void CreateCollection(IMongoClient client, CollectionNamespace collectio
22672276
private IMongoCollection<BsonDocument> CreateEncryptedCollection(IMongoClient client, ClientEncryption clientEncryption, CollectionNamespace collectionNamespace, BsonDocument encryptedFields, string kmsProvider, bool async)
22682277
{
22692278
var createCollectionOptions = new CreateCollectionOptions { EncryptedFields = encryptedFields };
2279+
return CreateEncryptedCollection(client, clientEncryption, collectionNamespace, createCollectionOptions, kmsProvider, async);
2280+
}
2281+
2282+
private IMongoCollection<BsonDocument> CreateEncryptedCollection(IMongoClient client, ClientEncryption clientEncryption, CollectionNamespace collectionNamespace, CreateCollectionOptions createCollectionOptions, string kmsProvider, bool async)
2283+
{
22702284
var datakeyOptions = CreateDataKeyOptions(kmsProvider);
22712285

22722286
if (async)

0 commit comments

Comments
 (0)