@@ -148,7 +148,7 @@ void RunTestCase(int testCase)
148
148
case 4 : // Case 4: Insert encrypted value
149
149
{
150
150
var createCollectionOptions = new CreateCollectionOptions { EncryptedFields = encryptedFields } ;
151
- var collection = CreateEncryptedCollection ( client , clientEncryption , __collCollectionNamespace , createCollectionOptions , kmsProvider , async , out var effectiveEncryptedFields ) ;
151
+ var collection = CreateEncryptedCollection < BsonDocument > ( client , clientEncryption , __collCollectionNamespace , createCollectionOptions , kmsProvider , async , out var effectiveEncryptedFields ) ;
152
152
var dataKey = effectiveEncryptedFields [ "fields" ] . AsBsonArray [ 0 ] . AsBsonDocument [ "keyId" ] . AsGuid ; // get generated datakey
153
153
var encryptedValue = ExplicitEncrypt ( clientEncryption , new EncryptOptions ( algorithm : EncryptionAlgorithm . Unindexed , keyId : dataKey ) , "123-45-6789" , async ) ; // use explicit encryption to encrypt data before inserting
154
154
Insert ( collection , async , new BsonDocument ( "ssn", encryptedValue) ) ;
@@ -486,6 +486,43 @@ DisposableMongoClient EnsureEnvironmentAndConfigureTestClientEncrypted()
486
486
}
487
487
}
488
488
489
+ [ Fact ]
490
+ public void ConcreteTypeDeserializationTest( )
491
+ {
492
+ RequireServer. Check ( ) . Supports ( Feature . Csfle2QEv2 ) . ClusterTypes ( ClusterType . ReplicaSet , ClusterType . Sharded , ClusterType . LoadBalanced ) ;
493
+
494
+ using var client = ConfigureClient( keyVaultNamespace : __keyVaultCollectionNamespace , kmsProviders : EncryptionTestHelper . GetKmsProviders ( "local" ) ) ;
495
+ using var clientEncryption = ConfigureClientEncryption( client , kmsProviderFilter : "local" ) ;
496
+
497
+ var datakeysCollection = GetCollection( client , __keyVaultCollectionNamespace ) ;
498
+ var externalKey = JsonFileReader. Instance . Documents [ "external.external-key.json" ] ;
499
+ datakeysCollection. InsertOne ( externalKey ) ;
500
+
501
+ var encryptedFields = new BsonDocument
502
+ {
503
+ {
504
+ "fields", new BsonArray
505
+ {
506
+ new BsonDocument
507
+ {
508
+ { "keyId" , externalKey [ "_id" ] . AsBsonBinaryData } ,
509
+ { "path" , "Ssn" } ,
510
+ { "bsonType" , "string" } ,
511
+ { "queries" , new BsonDocument ( "queryType" , "equality" ) }
512
+ } ,
513
+ }
514
+ }
515
+ } ;
516
+
517
+ var collection = CreateEncryptedCollection< Patient > ( client , clientEncryption , __collCollectionNamespace , encryptedFields , "local" , false , out _ ) ;
518
+
519
+ var patient = new Patient ( ) { Name = "Name" , Ssn = "14159265359" } ;
520
+ collection. InsertOne ( patient ) ;
521
+
522
+ var deserializedPatient = collection. Find ( FilterDefinition < Patient > . Empty ) . ToList ( ) . Single ( ) ;
523
+ deserializedPatient. Ssn . Should ( ) . Be ( patient . Ssn ) ;
524
+ }
525
+
489
526
[ Theory ]
490
527
[ ParameterAttributeData ]
491
528
public void CorpusTest(
@@ -2485,9 +2522,11 @@ private DisposableMongoClient ConfigureClient(
2485
2522
WriteConcern writeConcern = null ,
2486
2523
ReadConcern readConcern = null ,
2487
2524
CollectionNamespace mainCollectionNamespace = null ,
2488
- BsonDocument encryptedFields = null )
2525
+ BsonDocument encryptedFields = null ,
2526
+ CollectionNamespace keyVaultNamespace = null ,
2527
+ IReadOnlyDictionary < string , IReadOnlyDictionary < string , object > > kmsProviders = null )
2489
2528
{
2490
- var client = CreateMongoClient ( maxPoolSize : maxPoolSize , writeConcern : writeConcern , readConcern : readConcern ) ;
2529
+ var client = CreateMongoClient ( maxPoolSize : maxPoolSize , writeConcern : writeConcern , readConcern : readConcern , keyVaultNamespace : keyVaultNamespace , kmsProviders : kmsProviders ) ;
2491
2530
if ( clearCollections )
2492
2531
{
2493
2532
var clientKeyVaultDatabase = client . GetDatabase ( __keyVaultCollectionNamespace . DatabaseNamespace . DatabaseName ) ;
@@ -2636,10 +2675,16 @@ private void CreateCollection(IMongoClient client, CollectionNamespace collectio
2636
2675
private IMongoCollection < BsonDocument > CreateEncryptedCollection ( IMongoClient client , ClientEncryption clientEncryption , CollectionNamespace collectionNamespace , BsonDocument encryptedFields , string kmsProvider , bool async , out BsonDocument effectiveEncryptedFields )
2637
2676
{
2638
2677
var createCollectionOptions = new CreateCollectionOptions { EncryptedFields = encryptedFields } ;
2639
- return CreateEncryptedCollection ( client , clientEncryption , collectionNamespace , createCollectionOptions , kmsProvider , async , out effectiveEncryptedFields ) ;
2678
+ return CreateEncryptedCollection < BsonDocument > ( client , clientEncryption , collectionNamespace , createCollectionOptions , kmsProvider , async , out effectiveEncryptedFields ) ;
2640
2679
}
2641
2680
2642
- private IMongoCollection < BsonDocument > CreateEncryptedCollection ( IMongoClient client , ClientEncryption clientEncryption , CollectionNamespace collectionNamespace , CreateCollectionOptions createCollectionOptions , string kmsProvider , bool async , out BsonDocument effectiveEncryptedFields)
2681
+ private IMongoCollection < T > CreateEncryptedCollection < T > ( IMongoClient client , ClientEncryption clientEncryption , CollectionNamespace collectionNamespace , BsonDocument encryptedFields , string kmsProvider , bool async , out BsonDocument effectiveEncryptedFields )
2682
+ {
2683
+ var createCollectionOptions = new CreateCollectionOptions { EncryptedFields = encryptedFields } ;
2684
+ return CreateEncryptedCollection < T > ( client , clientEncryption , collectionNamespace , createCollectionOptions , kmsProvider , async , out effectiveEncryptedFields ) ;
2685
+ }
2686
+
2687
+ private IMongoCollection < T > CreateEncryptedCollection < T > ( IMongoClient client , ClientEncryption clientEncryption , CollectionNamespace collectionNamespace , CreateCollectionOptions createCollectionOptions , string kmsProvider , bool async , out BsonDocument effectiveEncryptedFields )
2643
2688
{
2644
2689
var datakeyOptions = CreateDataKeyOptions ( kmsProvider , alternateKeyNames : null ) ;
2645
2690
var database = client . GetDatabase ( collectionNamespace . DatabaseNamespace . DatabaseName ) ;
@@ -2651,7 +2696,7 @@ private IMongoCollection<BsonDocument> CreateEncryptedCollection(IMongoClient cl
2651
2696
2652
2697
effectiveEncryptedFields = result . EncryptedFields ;
2653
2698
2654
- return client. GetDatabase ( collectionNamespace . DatabaseNamespace . DatabaseName ) . GetCollection < BsonDocument > ( collectionNamespace . CollectionName ) ;
2699
+ return client . GetDatabase ( collectionNamespace . DatabaseNamespace . DatabaseName ) . GetCollection < T > ( collectionNamespace . CollectionName ) ;
2655
2700
}
2656
2701
2657
2702
private Guid CreateDataKey (
@@ -3090,6 +3135,13 @@ public Task<string> GetHttpContentAsync(HttpRequestMessage request, string excep
3090
3135
return _httpClientWrapper . GetHttpContentAsync ( request , exceptionMessage , cancellationToken ) ;
3091
3136
}
3092
3137
}
3138
+
3139
+ private class Patient
3140
+ {
3141
+ public ObjectId Id { get ; set ; }
3142
+ public string Name { get ; set ; }
3143
+ public string Ssn { get ; set ; }
3144
+ }
3093
3145
}
3094
3146
3095
3147
public static class ClientEncryptionOptionsReflector
0 commit comments