-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-4149: Add FLE 2 behavior for CreateCollection() and Collection.Drop(). #790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2c2f089
CSHARP-4149: Add FLE 2 behavior for CreateCollection() and Collection…
DmitryLukyanov 86630a7
CSHARP-4149: Refactoring.
DmitryLukyanov 3461943
CSHARP-4149: Update xml comment.
DmitryLukyanov 0b760f7
CSHARP-4149: Add tests.
DmitryLukyanov f2ccdaf
CSHARP-4149: Add tests.
DmitryLukyanov 215fc1d
CSHARP-4149: Code review.
DmitryLukyanov ef9cc1f
CSHARP-4149: Code review.
DmitryLukyanov 5992841
CSHARP-4149: Code review.
DmitryLukyanov c719ffc
CSHARP-4149: Code review.
DmitryLukyanov 3c271c3
CSHARP-4149: Make operation.EncryptedFields internal.
DmitryLukyanov 3431deb
Merge branch 'master' into csharp4149
DmitryLukyanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/MongoDB.Driver.Core/Core/Encryption/EncryptedCollectionHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using MongoDB.Bson; | ||
|
||
namespace MongoDB.Driver.Encryption | ||
{ | ||
internal static class EncryptedCollectionHelper | ||
{ | ||
public static BsonDocument AdditionalCreateIndexDocument { get; } = new BsonDocument("__safeContent__", 1); | ||
|
||
public static void EnsureCollectionsValid(IReadOnlyDictionary<string, BsonDocument> schemaMap, IReadOnlyDictionary<string, BsonDocument> encryptedFieldsMap) | ||
{ | ||
if (schemaMap == null || encryptedFieldsMap == null || schemaMap.Count == 0 || encryptedFieldsMap.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
var mutualKeys = schemaMap.Keys.Where(k => encryptedFieldsMap.ContainsKey(k)); | ||
if (mutualKeys.Any()) | ||
{ | ||
throw new ArgumentException($"SchemaMap and EncryptedFieldsMap cannot both contain the same collections: {string.Join(", ", mutualKeys)}."); | ||
} | ||
} | ||
|
||
public static string GetAdditionalCollectionName(BsonDocument encryptedFields, CollectionNamespace mainCollectionNamespace, HelperCollectionForEncryption helperCollection) => | ||
helperCollection switch | ||
{ | ||
HelperCollectionForEncryption.Esc => encryptedFields.GetValue("escCollection", defaultValue: $"enxcol_.{mainCollectionNamespace.CollectionName}.esc").ToString(), | ||
HelperCollectionForEncryption.Ecc => encryptedFields.GetValue("eccCollection", defaultValue: $"enxcol_.{mainCollectionNamespace.CollectionName}.ecc").ToString(), | ||
HelperCollectionForEncryption.Ecos => encryptedFields.GetValue("ecocCollection", defaultValue: $"enxcol_.{mainCollectionNamespace.CollectionName}.ecoc").ToString(), | ||
_ => throw new InvalidOperationException($"Not supported encryption helper collection {helperCollection}."), | ||
}; | ||
|
||
public static bool TryGetEffectiveEncryptedFields(CollectionNamespace collectionNamespace, BsonDocument encryptedFields, IReadOnlyDictionary<string, BsonDocument> encryptedFieldsMap, out BsonDocument effectiveEncryptedFields) | ||
{ | ||
if (encryptedFields != null) | ||
{ | ||
effectiveEncryptedFields = encryptedFields; | ||
return true; | ||
} | ||
|
||
if (encryptedFieldsMap != null) | ||
{ | ||
return encryptedFieldsMap.TryGetValue(collectionNamespace.ToString(), out effectiveEncryptedFields); | ||
} | ||
|
||
effectiveEncryptedFields = null; | ||
return false; | ||
} | ||
|
||
public static BsonDocument GetEffectiveEncryptedFields(CollectionNamespace collectionNamespace, BsonDocument encryptedFields, IReadOnlyDictionary<string, BsonDocument> encryptedFieldsMap) | ||
{ | ||
if (TryGetEffectiveEncryptedFields(collectionNamespace, encryptedFields, encryptedFieldsMap, out var effectiveEncryptedFields)) | ||
{ | ||
return effectiveEncryptedFields; | ||
} | ||
else | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
public enum HelperCollectionForEncryption | ||
{ | ||
Esc, | ||
Ecc, | ||
Ecos | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/MongoDB.Driver.Core/Core/Operations/CompositeWriteOperation.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MongoDB.Driver.Core.Bindings; | ||
using MongoDB.Driver.Core.Misc; | ||
|
||
namespace MongoDB.Driver.Core.Operations | ||
{ | ||
internal sealed class CompositeWriteOperation<TResult> : IWriteOperation<TResult> | ||
{ | ||
private readonly (IWriteOperation<TResult> Operation, bool IsMainOperation)[] _operations; | ||
|
||
public CompositeWriteOperation(params (IWriteOperation<TResult>, bool IsMainOperation)[] operations) | ||
{ | ||
_operations = Ensure.IsNotNull(operations, nameof(operations)); | ||
Ensure.IsGreaterThanZero(operations.Length, nameof(operations.Length)); | ||
Ensure.That(operations.Count(o => o.IsMainOperation) == 1, message: $"{nameof(CompositeWriteOperation<TResult>)} must have a single main operation."); | ||
} | ||
|
||
public TResult Execute(IWriteBinding binding, CancellationToken cancellationToken) | ||
{ | ||
TResult result = default; | ||
foreach (var operationInfo in _operations) | ||
{ | ||
var itemResult = operationInfo.Operation.Execute(binding, cancellationToken); | ||
if (operationInfo.IsMainOperation) | ||
{ | ||
result = itemResult; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public async Task<TResult> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken) | ||
BorisDog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
TResult result = default; | ||
foreach (var operationInfo in _operations) | ||
{ | ||
var itemResult = await operationInfo.Operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false); | ||
if (operationInfo.IsMainOperation) | ||
{ | ||
result = itemResult; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,9 @@ | |
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization.Serializers; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no real changes here, I just removed original |
||
using MongoDB.Driver.Core.Bindings; | ||
using MongoDB.Driver.Core.Connections; | ||
using MongoDB.Driver.Core.Events; | ||
using MongoDB.Driver.Core.Misc; | ||
using MongoDB.Driver.Core.WireProtocol.Messages.Encoders; | ||
|
@@ -33,8 +35,8 @@ public class CreateIndexesOperation : IWriteOperation<BsonDocument> | |
{ | ||
// fields | ||
private readonly CollectionNamespace _collectionNamespace; | ||
private CreateIndexCommitQuorum _commitQuorum; | ||
private BsonValue _comment; | ||
private CreateIndexCommitQuorum _commitQuorum; | ||
private TimeSpan? _maxTime; | ||
private readonly MessageEncoderSettings _messageEncoderSettings; | ||
private readonly IEnumerable<CreateIndexRequest> _requests; | ||
|
@@ -72,6 +74,9 @@ public CollectionNamespace CollectionNamespace | |
/// <summary> | ||
/// Gets or sets the comment. | ||
/// </summary> | ||
/// <value> | ||
/// The comment. | ||
/// </value> | ||
public BsonValue Comment | ||
{ | ||
get { return _comment; } | ||
|
@@ -122,10 +127,10 @@ public WriteConcern WriteConcern | |
} | ||
|
||
/// <summary> | ||
/// Gets or sets the max time. | ||
/// Gets or sets the MaxTime. | ||
/// </summary> | ||
/// <value> | ||
/// The max time | ||
/// <value> | ||
/// The maxtime. | ||
/// </value> | ||
public TimeSpan? MaxTime | ||
{ | ||
|
@@ -142,7 +147,7 @@ public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellatio | |
using (var channel = channelSource.GetChannel(cancellationToken)) | ||
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork())) | ||
{ | ||
var operation = CreateOperation(); | ||
var operation = CreateOperation(channelBinding.Session, channel.ConnectionDescription); | ||
return operation.Execute(channelBinding, cancellationToken); | ||
} | ||
} | ||
|
@@ -155,21 +160,38 @@ public async Task<BsonDocument> ExecuteAsync(IWriteBinding binding, Cancellation | |
using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false)) | ||
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork())) | ||
{ | ||
var operation = CreateOperation(); | ||
var operation = CreateOperation(channelBinding.Session, channel.ConnectionDescription); | ||
return await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
// internal methods | ||
internal IWriteOperation<BsonDocument> CreateOperation() | ||
// private methods | ||
internal BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription) | ||
{ | ||
return new CreateIndexesUsingCommandOperation(_collectionNamespace, _requests, _messageEncoderSettings) | ||
var maxWireVersion = connectionDescription.MaxWireVersion; | ||
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, _writeConcern); | ||
if (_commitQuorum != null) | ||
{ | ||
Comment = _comment, | ||
CommitQuorum = _commitQuorum, | ||
MaxTime = _maxTime, | ||
WriteConcern = _writeConcern | ||
Feature.CreateIndexCommitQuorum.ThrowIfNotSupported(maxWireVersion); | ||
} | ||
|
||
return new BsonDocument | ||
{ | ||
{ "createIndexes", _collectionNamespace.CollectionName }, | ||
{ "indexes", new BsonArray(_requests.Select(request => request.CreateIndexDocument())) }, | ||
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue }, | ||
{ "writeConcern", writeConcern, writeConcern != null }, | ||
{ "comment", _comment, _comment != null }, | ||
{ "commitQuorum", () => _commitQuorum.ToBsonValue(), _commitQuorum != null } | ||
}; | ||
} | ||
|
||
private WriteCommandOperation<BsonDocument> CreateOperation(ICoreSessionHandle session, ConnectionDescription connectionDescription) | ||
{ | ||
var databaseNamespace = _collectionNamespace.DatabaseNamespace; | ||
var command = CreateCommand(session, connectionDescription); | ||
var resultSerializer = BsonDocumentSerializer.Instance; | ||
return new WriteCommandOperation<BsonDocument>(databaseNamespace, command, resultSerializer, _messageEncoderSettings); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found the logic of the above method somewhat confusing and it took me a long time to convince myself that it could be correct. I think the following refactoring expresses the intent more clearly:
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done