From 7cfa28e7214bf4399ef70d2e5ca569fdd80a2360 Mon Sep 17 00:00:00 2001 From: BorisDog Date: Thu, 15 Jun 2023 14:08:00 -0700 Subject: [PATCH 1/2] CSHARP-4678: Additional collections are created in QE v2 on older servers --- .../fle2v2-CreateCollection-OldServer.json | 32 +++++++++++++++++++ .../fle2v2-CreateCollection-OldServer.yml | 22 +++++++++++++ .../Operations/CreateCollectionOperation.cs | 14 ++++++-- 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/specifications/client-side-encryption/tests/legacy/fle2v2-CreateCollection-OldServer.json b/specifications/client-side-encryption/tests/legacy/fle2v2-CreateCollection-OldServer.json index d5b04b3ea5f..c266aa6b835 100644 --- a/specifications/client-side-encryption/tests/legacy/fle2v2-CreateCollection-OldServer.json +++ b/specifications/client-side-encryption/tests/legacy/fle2v2-CreateCollection-OldServer.json @@ -55,6 +55,38 @@ "result": { "errorContains": "Driver support of Queryable Encryption is incompatible with server. Upgrade server to use Queryable Encryption." } + }, + { + "name": "assertCollectionNotExists", + "object": "testRunner", + "arguments": { + "database": "default", + "collection": "enxcol_.encryptedCollection.esc" + } + }, + { + "name": "assertCollectionNotExists", + "object": "testRunner", + "arguments": { + "database": "default", + "collection": "enxcol_.encryptedCollection.ecc" + } + }, + { + "name": "assertCollectionNotExists", + "object": "testRunner", + "arguments": { + "database": "default", + "collection": "enxcol_.encryptedCollection.ecoc" + } + }, + { + "name": "assertCollectionNotExists", + "object": "testRunner", + "arguments": { + "database": "default", + "collection": "encryptedCollection" + } } ] } diff --git a/specifications/client-side-encryption/tests/legacy/fle2v2-CreateCollection-OldServer.yml b/specifications/client-side-encryption/tests/legacy/fle2v2-CreateCollection-OldServer.yml index f55001a4294..5cc6ead0f6d 100644 --- a/specifications/client-side-encryption/tests/legacy/fle2v2-CreateCollection-OldServer.yml +++ b/specifications/client-side-encryption/tests/legacy/fle2v2-CreateCollection-OldServer.yml @@ -37,3 +37,25 @@ tests: collection: "encryptedCollection" result: errorContains: "Driver support of Queryable Encryption is incompatible with server. Upgrade server to use Queryable Encryption." + # Assert no collections were created. + - name: assertCollectionNotExists + object: testRunner + arguments: + database: *database_name + collection: &esc_collection_name "enxcol_.encryptedCollection.esc" + # ecc collection is no longer created for QEv2 + - name: assertCollectionNotExists + object: testRunner + arguments: + database: *database_name + collection: &ecc_collection_name "enxcol_.encryptedCollection.ecc" + - name: assertCollectionNotExists + object: testRunner + arguments: + database: *database_name + collection: &ecoc_collection_name "enxcol_.encryptedCollection.ecoc" + - name: assertCollectionNotExists + object: testRunner + arguments: + database: *database_name + collection: encryptedCollection diff --git a/src/MongoDB.Driver.Core/Core/Operations/CreateCollectionOperation.cs b/src/MongoDB.Driver.Core/Core/Operations/CreateCollectionOperation.cs index 8d1c2218313..77362dac33b 100644 --- a/src/MongoDB.Driver.Core/Core/Operations/CreateCollectionOperation.cs +++ b/src/MongoDB.Driver.Core/Core/Operations/CreateCollectionOperation.cs @@ -62,7 +62,10 @@ internal static IWriteOperation CreateEncryptedCreateCollectionOpe } CreateCollectionOperation CreateInnerCollectionOperation(string collectionName) - => new CreateCollectionOperation(new CollectionNamespace(collectionNamespace.DatabaseNamespace.DatabaseName, collectionName), messageEncoderSettings) { ClusteredIndex = new BsonDocument { { "key", new BsonDocument("_id", 1) }, { "unique", true } } }; + => new(new CollectionNamespace(collectionNamespace.DatabaseNamespace.DatabaseName, collectionName), messageEncoderSettings, Feature.Csfle2QEv2) + { + ClusteredIndex = new BsonDocument { { "key", new BsonDocument("_id", 1) }, { "unique", true } } + }; } #endregion @@ -89,6 +92,8 @@ CreateCollectionOperation CreateInnerCollectionOperation(string collectionName) private BsonDocument _validator; private WriteConcern _writeConcern; + private readonly Feature _supportedFeature; + // constructors /// /// Initializes a new instance of the class. @@ -109,6 +114,7 @@ private CreateCollectionOperation( { _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace)); _messageEncoderSettings = messageEncoderSettings; + _supportedFeature = supportedFeature; } // properties @@ -431,7 +437,6 @@ public async Task ExecuteAsync(IWriteBinding binding, Cancellation { Ensure.IsNotNull(binding, nameof(binding)); - using (BeginOperation()) using (var channelSource = await binding.GetWriteChannelSourceAsync(cancellationToken).ConfigureAwait(false)) using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false)) { @@ -459,6 +464,11 @@ private void EnsureServerIsValid(int maxWireVersion) { Feature.Csfle2QEv2.ThrowIfNotSupported(maxWireVersion); } + + if (_supportedFeature != null && _supportedFeature != Feature.Csfle2QEv2) + { + _supportedFeature.ThrowIfNotSupported(maxWireVersion); + } } [Flags] From 57b3020efe049774a101ed06a7a5b6ff950c1cb9 Mon Sep 17 00:00:00 2001 From: BorisDog Date: Fri, 16 Jun 2023 12:52:03 -0700 Subject: [PATCH 2/2] - Bugs --- .../Core/Operations/CreateCollectionOperation.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/MongoDB.Driver.Core/Core/Operations/CreateCollectionOperation.cs b/src/MongoDB.Driver.Core/Core/Operations/CreateCollectionOperation.cs index 77362dac33b..c55d8d4fabc 100644 --- a/src/MongoDB.Driver.Core/Core/Operations/CreateCollectionOperation.cs +++ b/src/MongoDB.Driver.Core/Core/Operations/CreateCollectionOperation.cs @@ -41,7 +41,8 @@ internal static IWriteOperation CreateEncryptedCreateCollectionOpe { var mainOperation = new CreateCollectionOperation( collectionNamespace, - messageEncoderSettings) + messageEncoderSettings, + encryptedFields != null ? Feature.Csfle2QEv2 : null) { EncryptedFields = encryptedFields }; @@ -437,6 +438,7 @@ public async Task ExecuteAsync(IWriteBinding binding, Cancellation { Ensure.IsNotNull(binding, nameof(binding)); + using (BeginOperation()) using (var channelSource = await binding.GetWriteChannelSourceAsync(cancellationToken).ConfigureAwait(false)) using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false)) { @@ -460,15 +462,7 @@ private WriteCommandOperation CreateOperation(ICoreSessionHandle s private void EnsureServerIsValid(int maxWireVersion) { - if (_encryptedFields != null) - { - Feature.Csfle2QEv2.ThrowIfNotSupported(maxWireVersion); - } - - if (_supportedFeature != null && _supportedFeature != Feature.Csfle2QEv2) - { - _supportedFeature.ThrowIfNotSupported(maxWireVersion); - } + _supportedFeature?.ThrowIfNotSupported(maxWireVersion); } [Flags]