Skip to content

Commit 23d88cf

Browse files
The license status check fails when checking license against a secured RavenDB instance (#5274) (#5277)
1 parent d9a10e7 commit 23d88cf

File tree

4 files changed

+53
-45
lines changed

4 files changed

+53
-45
lines changed

src/ServiceControl.Audit.Persistence.RavenDB/DatabaseSetup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task Execute(IDocumentStore documentStore, CancellationToken cancel
2727

2828
await CreateIndexes(documentStore, configuration.EnableFullTextSearch, cancellationToken);
2929

30-
await LicenseStatusCheck.WaitForLicenseOrThrow(configuration, cancellationToken);
30+
await LicenseStatusCheck.WaitForLicenseOrThrow(documentStore, cancellationToken);
3131
await ConfigureExpiration(documentStore, cancellationToken);
3232
}
3333

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
11
namespace ServiceControl.Audit.Persistence.RavenDB;
22

33
using System;
4-
using System.Net.Http;
54
using System.Net.Http.Json;
65
using System.Threading;
76
using System.Threading.Tasks;
7+
using Raven.Client.Documents;
88

99
static class LicenseStatusCheck
1010
{
1111
record LicenseStatusFragment(string Id, string LicensedTo, string Status, bool Expired);
1212

13-
public static async Task WaitForLicenseOrThrow(DatabaseConfiguration configuration, CancellationToken cancellationToken)
13+
public static async Task WaitForLicenseOrThrow(IDocumentStore documentStore, CancellationToken cancellationToken)
1414
{
15-
using var client = new HttpClient
16-
{
17-
BaseAddress = new Uri(configuration.ServerConfiguration.ConnectionString ?? configuration.ServerConfiguration.ServerUrl)
18-
};
15+
var ravenConfiguredHttpClient = documentStore.GetRequestExecutor().HttpClient;
16+
var licenseCheckUrl = documentStore.Urls[0].TrimEnd('/') + "/license/status";
17+
18+
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
19+
cts.CancelAfter(30_000);
1920

20-
// Not linking to the incoming cancellationToken to ensure no OperationCancelledException prevents the last InvalidOperationException to be thrown
21-
using var cts = new CancellationTokenSource(30_000);
22-
while (!cts.IsCancellationRequested)
21+
try
2322
{
24-
var httpResponse = await client.GetAsync("license/status", cancellationToken);
25-
var licenseStatus = await httpResponse.Content.ReadFromJsonAsync<LicenseStatusFragment>(cancellationToken);
26-
if (licenseStatus.Expired)
23+
while (true)
2724
{
28-
throw new InvalidOperationException("The current RavenDB license is expired. Please, contact support");
29-
}
25+
var httpResponse = await ravenConfiguredHttpClient.GetAsync(licenseCheckUrl, cts.Token);
26+
var licenseStatus = await httpResponse.Content.ReadFromJsonAsync<LicenseStatusFragment>(cts.Token);
27+
if (licenseStatus.Expired)
28+
{
29+
throw new InvalidOperationException("The current RavenDB license is expired. Please, contact support");
30+
}
3031

31-
if (licenseStatus.LicensedTo != null && licenseStatus.Id != null)
32-
{
33-
return;
34-
}
32+
if (licenseStatus.LicensedTo != null && licenseStatus.Id != null)
33+
{
34+
return;
35+
}
3536

36-
await Task.Delay(200, cancellationToken);
37+
await Task.Delay(200, cts.Token);
38+
}
39+
}
40+
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
41+
{
42+
throw new InvalidOperationException("Cannot validate the current RavenDB license. Please, contact support");
3743
}
38-
39-
throw new InvalidOperationException("Cannot validate the current RavenDB license. Please, contact support");
4044
}
41-
}
45+
}

src/ServiceControl.Persistence.RavenDB/DatabaseSetup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public async Task Execute(CancellationToken cancellationToken)
2323

2424
await IndexCreation.CreateIndexesAsync(typeof(DatabaseSetup).Assembly, documentStore, null, null, cancellationToken);
2525

26-
await LicenseStatusCheck.WaitForLicenseOrThrow(settings, cancellationToken);
26+
await LicenseStatusCheck.WaitForLicenseOrThrow(documentStore, cancellationToken);
2727
await ConfigureExpiration(settings, cancellationToken);
2828
}
2929

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
11
namespace ServiceControl.Persistence.RavenDB;
22

33
using System;
4-
using System.Net.Http;
54
using System.Net.Http.Json;
65
using System.Threading;
76
using System.Threading.Tasks;
7+
using Raven.Client.Documents;
88

99
static class LicenseStatusCheck
1010
{
1111
record LicenseStatusFragment(string Id, string LicensedTo, string Status, bool Expired);
1212

13-
public static async Task WaitForLicenseOrThrow(RavenPersisterSettings configuration, CancellationToken cancellationToken)
13+
public static async Task WaitForLicenseOrThrow(IDocumentStore documentStore, CancellationToken cancellationToken)
1414
{
15-
using var client = new HttpClient
16-
{
17-
BaseAddress = new Uri(configuration.ConnectionString ?? configuration.ServerUrl)
18-
};
15+
var ravenConfiguredHttpClient = documentStore.GetRequestExecutor().HttpClient;
16+
var licenseCheckUrl = documentStore.Urls[0].TrimEnd('/') + "/license/status";
17+
18+
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
19+
cts.CancelAfter(30_000);
1920

20-
// Not linking to the incoming cancellationToken to ensure no OperationCancelledException prevents the last InvalidOperationException to be thrown
21-
using var cts = new CancellationTokenSource(30_000);
22-
while (!cts.IsCancellationRequested)
21+
try
2322
{
24-
var httpResponse = await client.GetAsync("license/status", cancellationToken);
25-
var licenseStatus = await httpResponse.Content.ReadFromJsonAsync<LicenseStatusFragment>(cancellationToken);
26-
if (licenseStatus.Expired)
23+
while (!cts.IsCancellationRequested)
2724
{
28-
throw new InvalidOperationException("The current RavenDB license is expired. Please, contact support");
29-
}
25+
var httpResponse = await ravenConfiguredHttpClient.GetAsync(licenseCheckUrl, cancellationToken);
26+
var licenseStatus = await httpResponse.Content.ReadFromJsonAsync<LicenseStatusFragment>(cancellationToken);
27+
if (licenseStatus.Expired)
28+
{
29+
throw new InvalidOperationException("The current RavenDB license is expired. Please, contact support");
30+
}
3031

31-
if (licenseStatus.LicensedTo != null && licenseStatus.Id != null)
32-
{
33-
return;
34-
}
32+
if (licenseStatus.LicensedTo != null && licenseStatus.Id != null)
33+
{
34+
return;
35+
}
3536

36-
await Task.Delay(200, cancellationToken);
37+
await Task.Delay(200, cts.Token);
38+
}
39+
}
40+
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
41+
{
42+
throw new InvalidOperationException("Cannot validate the current RavenDB license. Please, contact support");
3743
}
38-
39-
throw new InvalidOperationException("Cannot validate the current RavenDB license. Please, contact support");
4044
}
4145
}

0 commit comments

Comments
 (0)