Skip to content

Commit d7045a5

Browse files
Update documentdb compatibility.
1 parent 7212938 commit d7045a5

File tree

5 files changed

+46
-25
lines changed

5 files changed

+46
-25
lines changed

backend/src/Squidex.Data.MongoDb/Domain/Apps/Entities/Contents/Text/DocumentDbTextIndex.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Squidex.Domain.Apps.Core.Apps;
1111
using Squidex.Infrastructure;
1212
using Squidex.Infrastructure.ObjectPool;
13-
using Squidex.Infrastructure.Translations;
1413

1514
namespace Squidex.Domain.Apps.Entities.Contents.Text;
1615

@@ -38,21 +37,19 @@ await collection.Indexes.CreateOneAsync(
3837
Index.Text(x => x.Texts)),
3938
cancellationToken: ct);
4039

41-
4240
await collection.Indexes.CreateManyAsync(
4341
[
4442
new CreateIndexModel<MongoTextIndexEntity<string>>(
45-
Index
46-
.Ascending(x => x.AppId)
47-
.Ascending(x => x.ContentId)),
48-
49-
new CreateIndexModel<MongoTextIndexEntity<string>>(
50-
Index
51-
.Ascending(x => x.AppId)
52-
.Ascending(x => x.SchemaId)
53-
.Ascending(x => x.GeoField)
54-
.Geo2DSphere(x => x.GeoObject)),
55-
], ct);
43+
Index
44+
.Ascending(x => x.AppId)
45+
.Ascending(x => x.ContentId)),
46+
new CreateIndexModel<MongoTextIndexEntity<string>>(
47+
Index
48+
.Ascending(x => x.AppId)
49+
.Ascending(x => x.SchemaId)
50+
.Ascending(x => x.GeoField)
51+
.Geo2DSphere(x => x.GeoObject)),
52+
], ct);
5653
}
5754

5855
public override async Task<List<DomainId>?> SearchAsync(App app, GeoQuery query, SearchScope scope,

backend/src/Squidex.Data.MongoDb/ServiceExtensions.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// ==========================================================================
77

88
using System.Net;
9+
using System.Security.Cryptography.X509Certificates;
910
using System.Text.Json;
1011
using Microsoft.AspNetCore.Identity;
1112
using 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());

backend/src/Squidex.Data.MongoDb/Squidex.Data.MongoDb.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<PackageReference Include="Squidex.Flows.Mongo" Version="7.33.0" />
3232
<PackageReference Include="Squidex.Hosting" Version="7.33.0" />
3333
<PackageReference Include="Squidex.Messaging.Mongo" Version="7.33.0" />
34-
<PackageReference Include="Squidex.OpenIddict.MongoDb" Version="5.8.4" />
34+
<PackageReference Include="Squidex.OpenIddict.MongoDb" Version="5.8.5" />
3535
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="all" />
3636
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
3737
<PackageReference Include="YDotNet.Server.MongoDB" Version="0.4.3" />

backend/src/Squidex/appsettings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,9 @@
538538
// Read More: https://docs.mongodb.com/manual/reference/connection-string/
539539
"configuration": "mongodb://localhost",
540540

541+
// The path to the certificate.
542+
"certificate": "",
543+
541544
// The name of the event store database.
542545
"database": "SquidexAssets",
543546

@@ -582,6 +585,9 @@
582585
// Read More: https://docs.mongodb.com/manual/reference/connection-string/
583586
"configuration": "mongodb://localhost",
584587

588+
// The path to the certificate.
589+
"certificate": "",
590+
585591
// The name of the event store database.
586592
"database": "Squidex"
587593
},
@@ -627,6 +633,9 @@
627633
// Read More: https://docs.mongodb.com/manual/reference/connection-string/
628634
"configuration": "mongodb://localhost",
629635

636+
// The path to the certificate.
637+
"certificate": "",
638+
630639
// The database for all your content collections (one collection per app).
631640
"contentDatabase": "SquidexContent",
632641

backend/tests/Squidex.Data.Tests/MongoDb/TestHelpers/DocumentDbFixture.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ public DocumentDbFixture()
3434
TestConfig.Configuration.GetValue<string>("documentDb:configuration")
3535
);
3636

37-
var cert = new X509Certificate2(TestConfig.Configuration.GetValue<string>("documentDb:keyFile")!);
37+
var certPath = TestConfig.Configuration.GetValue<string>("documentDb:keyFile")!;
38+
var certFile = new X509Certificate2(certPath);
3839

3940
settings.RetryWrites = false;
4041
settings.RetryReads = false;
4142
settings.SslSettings = new SslSettings
4243
{
43-
ClientCertificates = [cert],
44+
ClientCertificates = [certFile],
4445
CheckCertificateRevocation = false,
4546
ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true,
4647
};

0 commit comments

Comments
 (0)