66// ==========================================================================
77
88using System . Net ;
9+ using System . Security . Cryptography . X509Certificates ;
910using System . Text . Json ;
1011using Microsoft . AspNetCore . Identity ;
1112using Microsoft . Extensions . Caching . Distributed ;
@@ -63,14 +64,13 @@ public static class ServiceExtensions
6364{
6465 public static void AddSquidexMongoEventStore ( this IServiceCollection services , IConfiguration config )
6566 {
66- var mongoConfiguration = config . GetRequiredValue ( "eventStore:mongoDb:configuration" ) ;
6767 var mongoDatabaseName = config . GetRequiredValue ( "eventStore:mongoDb:database" ) ;
6868
6969 services . AddMongoEventStore ( config ) ;
7070 services . AddSingletonAs ( c =>
7171 {
7272 var options = c . GetRequiredService < IOptions < MongoEventStoreOptions > > ( ) ;
73- var mongoClient = GetMongoClient ( mongoConfiguration ) ;
73+ var mongoClient = GetMongoClient ( config , "eventStore:mongoDb" ) ;
7474 var mongoDatabase = mongoClient . GetDatabase ( mongoDatabaseName ) ;
7575
7676 return new MongoEventStore ( mongoDatabase , options ) ;
@@ -80,13 +80,12 @@ public static void AddSquidexMongoEventStore(this IServiceCollection services, I
8080
8181 public static void AddSquidexMongoAssetStore ( this IServiceCollection services , IConfiguration config )
8282 {
83- var mongoConfiguration = config . GetRequiredValue ( "assetStore:mongoDb:configuration" ) ;
8483 var mongoDatabaseName = config . GetRequiredValue ( "assetStore:mongoDb:database" ) ;
8584 var mongoGridFsBucketName = config . GetRequiredValue ( "assetStore:mongoDb:bucket" ) ;
8685
8786 services . AddMongoAssetStore ( c =>
8887 {
89- var mongoClient = GetMongoClient ( mongoConfiguration ) ;
88+ var mongoClient = GetMongoClient ( config , "assetStore:mongoDb" ) ;
9089 var mongoDatabase = mongoClient . GetDatabase ( mongoDatabaseName ) ;
9190
9291 return new GridFSBucket < string > ( mongoDatabase , new GridFSBucketOptions
@@ -98,7 +97,6 @@ public static void AddSquidexMongoAssetStore(this IServiceCollection services, I
9897
9998 public static void AddSquidexMongoStore ( this IServiceCollection services , IConfiguration config )
10099 {
101- var mongoConfiguration = config . GetRequiredValue ( "store:mongoDb:configuration" ) ! ;
102100 var mongoDatabaseName = config . GetRequiredValue ( "store:mongoDb:database" ) ! ;
103101 var mongoContentDatabaseName = config . GetOptionalValue ( "store:mongoDb:contentDatabase" , mongoDatabaseName ) ! ;
104102
@@ -107,7 +105,7 @@ public static void AddSquidexMongoStore(this IServiceCollection services, IConfi
107105 return GetDatabase ( c , mongoDatabaseName ) ;
108106 } ) ;
109107
110- services . AddSingletonAs ( c => GetMongoClient ( mongoConfiguration ) )
108+ services . AddSingletonAs ( c => GetMongoClient ( config , "store:mongoDb" ) )
111109 . As < IMongoClient > ( ) ;
112110
113111 services . AddSingletonAs ( c => GetDatabase ( c , mongoDatabaseName ) )
@@ -289,12 +287,28 @@ private static void AddMigrations(this IServiceCollection services, Func<IServic
289287 . As < IMigration > ( ) ;
290288 }
291289
292- private static IMongoClient GetMongoClient ( string configuration )
290+ private static IMongoClient GetMongoClient ( IConfiguration config , string prefix )
293291 {
294- return Singletons < IMongoClient > . GetOrAdd ( configuration , connectionString =>
292+ var mongoConfiguration = config . GetRequiredValue ( $ "{ prefix } :configuration") ! ;
293+ var mongoCertificate = config . GetValue < string > ( $ "{ prefix } :certificate") ;
294+ var cacheKey = $ "{ mongoConfiguration } _{ mongoCertificate } ";
295+
296+ return Singletons < IMongoClient > . GetOrAdd ( cacheKey , _ =>
295297 {
296- return MongoClientFactory . Create ( connectionString , settings =>
298+ return MongoClientFactory . Create ( mongoConfiguration , settings =>
297299 {
300+ if ( ! string . IsNullOrWhiteSpace ( mongoCertificate ) )
301+ {
302+ var certFile = new X509Certificate2 ( mongoCertificate ) ;
303+
304+ settings . SslSettings = new SslSettings
305+ {
306+ ClientCertificates = [ certFile ] ,
307+ CheckCertificateRevocation = false ,
308+ ServerCertificateValidationCallback = ( sender , certificate , chain , errors ) => true ,
309+ } ;
310+ }
311+
298312 settings . ClusterConfigurator = builder =>
299313 {
300314 builder . Subscribe ( new DiagnosticsActivityEventSubscriber ( ) ) ;
0 commit comments