1919using MongoDB . Bson ;
2020using MongoDB . Bson . Serialization . Serializers ;
2121using MongoDB . Driver . Core . Bindings ;
22- using MongoDB . Driver . Core . Connections ;
2322using MongoDB . Driver . Core . Misc ;
2423using MongoDB . Driver . Core . WireProtocol . Messages . Encoders ;
24+ using MongoDB . Driver . Encryption ;
25+ using static MongoDB . Driver . Encryption . EncryptedCollectionHelper ;
2526
2627namespace MongoDB . Driver . Core . Operations
2728{
@@ -30,12 +31,48 @@ namespace MongoDB.Driver.Core.Operations
3031 /// </summary>
3132 public class CreateCollectionOperation : IWriteOperation < BsonDocument >
3233 {
34+ #region static
35+ internal static IWriteOperation < BsonDocument > CreateEncryptedCreateCollectionOperationIfConfigured (
36+ CollectionNamespace collectionNamespace ,
37+ BsonDocument encryptedFields ,
38+ MessageEncoderSettings messageEncoderSettings ,
39+ Action < CreateCollectionOperation > createCollectionOperationConfigurator )
40+ {
41+ var mainOperation = new CreateCollectionOperation (
42+ collectionNamespace ,
43+ messageEncoderSettings )
44+ {
45+ EncryptedFields = encryptedFields
46+ } ;
47+
48+ createCollectionOperationConfigurator ? . Invoke ( mainOperation ) ;
49+
50+ if ( encryptedFields != null )
51+ {
52+ return new CompositeWriteOperation < BsonDocument > (
53+ ( CreateInnerCollectionOperation ( EncryptedCollectionHelper . GetAdditionalCollectionName ( encryptedFields , collectionNamespace , HelperCollectionForEncryption . Esc ) ) , IsMainOperation : false ) ,
54+ ( CreateInnerCollectionOperation ( EncryptedCollectionHelper . GetAdditionalCollectionName ( encryptedFields , collectionNamespace , HelperCollectionForEncryption . Ecc ) ) , IsMainOperation : false ) ,
55+ ( CreateInnerCollectionOperation ( EncryptedCollectionHelper . GetAdditionalCollectionName ( encryptedFields , collectionNamespace , HelperCollectionForEncryption . Ecos ) ) , IsMainOperation : false ) ,
56+ ( mainOperation , IsMainOperation : true ) ,
57+ ( new CreateIndexesOperation ( collectionNamespace , new [ ] { new CreateIndexRequest ( EncryptedCollectionHelper . AdditionalCreateIndexDocument ) } , messageEncoderSettings ) , IsMainOperation : false ) ) ;
58+ }
59+ else
60+ {
61+ return mainOperation ;
62+ }
63+
64+ CreateCollectionOperation CreateInnerCollectionOperation ( string collectionName )
65+ => new CreateCollectionOperation ( new CollectionNamespace ( collectionNamespace . DatabaseNamespace . DatabaseName , collectionName ) , messageEncoderSettings ) ;
66+ }
67+ #endregion
68+
3369 // fields
3470 private bool ? _autoIndexId ;
3571 private bool ? _capped ;
3672 private Collation _collation ;
3773 private readonly CollectionNamespace _collectionNamespace ;
3874 private BsonValue _comment ;
75+ private BsonDocument _encryptedFields ;
3976 private TimeSpan ? _expireAfter ;
4077 private BsonDocument _indexOptionDefaults ;
4178 private long ? _maxDocuments ;
@@ -125,6 +162,12 @@ public CollectionNamespace CollectionNamespace
125162 get { return _collectionNamespace ; }
126163 }
127164
165+ internal BsonDocument EncryptedFields
166+ {
167+ get { return _encryptedFields ; }
168+ private set { _encryptedFields = value ; }
169+ }
170+
128171 /// <summary>
129172 /// Gets or sets the expiration timespan for time series collections. Used to automatically delete documents in time series collections.
130173 /// See https://www.mongodb.com/docs/manual/reference/command/create/ for supported options and https://www.mongodb.com/docs/manual/core/timeseries-collections/
@@ -282,7 +325,7 @@ public WriteConcern WriteConcern
282325 }
283326
284327 // methods
285- internal BsonDocument CreateCommand ( ICoreSessionHandle session , ConnectionDescription connectionDescription )
328+ internal BsonDocument CreateCommand ( ICoreSessionHandle session )
286329 {
287330 var flags = GetFlags ( ) ;
288331 var writeConcern = WriteConcernHelper . GetEffectiveWriteConcern ( session , _writeConcern ) ;
@@ -303,7 +346,8 @@ internal BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescri
303346 { "comment" , _comment , _comment != null } ,
304347 { "writeConcern" , writeConcern , writeConcern != null } ,
305348 { "expireAfterSeconds" , ( ) => _expireAfter . Value . TotalSeconds , _expireAfter . HasValue } ,
306- { "timeseries" , ( ) => _timeSeriesOptions . ToBsonDocument ( ) , _timeSeriesOptions != null }
349+ { "timeseries" , ( ) => _timeSeriesOptions . ToBsonDocument ( ) , _timeSeriesOptions != null } ,
350+ { "encryptedFields" , _encryptedFields , _encryptedFields != null }
307351 } ;
308352 }
309353
@@ -337,7 +381,7 @@ public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellatio
337381 using ( var channel = channelSource . GetChannel ( cancellationToken ) )
338382 using ( var channelBinding = new ChannelReadWriteBinding ( channelSource . Server , channel , binding . Session . Fork ( ) ) )
339383 {
340- var operation = CreateOperation ( channelBinding . Session , channel . ConnectionDescription ) ;
384+ var operation = CreateOperation ( channelBinding . Session ) ;
341385 return operation . Execute ( channelBinding , cancellationToken ) ;
342386 }
343387 }
@@ -351,14 +395,14 @@ public async Task<BsonDocument> ExecuteAsync(IWriteBinding binding, Cancellation
351395 using ( var channel = await channelSource . GetChannelAsync ( cancellationToken ) . ConfigureAwait ( false ) )
352396 using ( var channelBinding = new ChannelReadWriteBinding ( channelSource . Server , channel , binding . Session . Fork ( ) ) )
353397 {
354- var operation = CreateOperation ( channelBinding . Session , channel . ConnectionDescription ) ;
398+ var operation = CreateOperation ( channelBinding . Session ) ;
355399 return await operation . ExecuteAsync ( channelBinding , cancellationToken ) . ConfigureAwait ( false ) ;
356400 }
357401 }
358402
359- private WriteCommandOperation < BsonDocument > CreateOperation ( ICoreSessionHandle session , ConnectionDescription connectionDescription )
403+ private WriteCommandOperation < BsonDocument > CreateOperation ( ICoreSessionHandle session )
360404 {
361- var command = CreateCommand ( session , connectionDescription ) ;
405+ var command = CreateCommand ( session ) ;
362406 return new WriteCommandOperation < BsonDocument > ( _collectionNamespace . DatabaseNamespace , command , BsonDocumentSerializer . Instance , _messageEncoderSettings ) ;
363407 }
364408
0 commit comments