Skip to content

Commit 5ea391c

Browse files
Add a license status check to ensure the RavenDB licence is correctly applied after the database is created before configuring expiration
1 parent 99d8a55 commit 5ea391c

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.Net.Http;
6+
using System.Text.Json;
57
using System.Threading;
68
using System.Threading.Tasks;
79
using Raven.Client.Documents;
@@ -25,6 +27,7 @@ public async Task Execute(IDocumentStore documentStore, CancellationToken cancel
2527

2628
await CreateIndexes(documentStore, configuration.EnableFullTextSearch, cancellationToken);
2729

30+
await LicenseStatusCheck.WaitForLicenseOrThrow(configuration, cancellationToken);
2831
await ConfigureExpiration(documentStore, cancellationToken);
2932
}
3033

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace ServiceControl.Audit.Persistence.RavenDB;
2+
3+
using System;
4+
using System.Net.Http;
5+
using System.Text.Json;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
static class LicenseStatusCheck
10+
{
11+
class LicenseStatusFragment
12+
{
13+
public string Id { get; set; }
14+
public string LicensedTo { get; set; }
15+
public string Status { get; set; }
16+
public bool Expired { get; set; }
17+
}
18+
19+
public static async Task WaitForLicenseOrThrow(DatabaseConfiguration configuration, CancellationToken cancellationToken)
20+
{
21+
var client = new HttpClient() { BaseAddress = new Uri(configuration.ServerConfiguration.ConnectionString ?? configuration.ServerConfiguration.ServerUrl) };
22+
var licenseCorrectlySetup = false;
23+
var attempts = 0;
24+
while (!licenseCorrectlySetup)
25+
{
26+
var httpResponse = await client.GetAsync("/license/status", cancellationToken);
27+
var responseJsonString = await httpResponse.Content.ReadAsStringAsync(cancellationToken);
28+
var licenseStatus = JsonSerializer.Deserialize<LicenseStatusFragment>(responseJsonString);
29+
if (licenseStatus.Expired)
30+
{
31+
throw new NotSupportedException("The current RavenDB license is expired. Please, contact support");
32+
}
33+
34+
if (licenseStatus.LicensedTo != null && licenseStatus.Id != null)
35+
{
36+
licenseCorrectlySetup = true;
37+
}
38+
39+
if (++attempts > 10)
40+
{
41+
throw new NotSupportedException("Cannot validate the current RavenDB license. Please, contact support");
42+
}
43+
44+
await Task.Delay(500, cancellationToken);
45+
}
46+
}
47+
}

src/ServiceControl.Persistence.RavenDB/DatabaseSetup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +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);
2627
await ConfigureExpiration(settings, cancellationToken);
2728
}
2829

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace ServiceControl.Persistence.RavenDB;
2+
3+
using System;
4+
using System.Net.Http;
5+
using System.Text.Json;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
static class LicenseStatusCheck
10+
{
11+
class LicenseStatusFragment
12+
{
13+
public string Id { get; set; }
14+
public string LicensedTo { get; set; }
15+
public string Status { get; set; }
16+
public bool Expired { get; set; }
17+
}
18+
19+
public static async Task WaitForLicenseOrThrow(RavenPersisterSettings configuration, CancellationToken cancellationToken)
20+
{
21+
var client = new HttpClient() { BaseAddress = new Uri(configuration.ConnectionString ?? configuration.ServerUrl) };
22+
var licenseCorrectlySetup = false;
23+
var attempts = 0;
24+
while (!licenseCorrectlySetup)
25+
{
26+
var httpResponse = await client.GetAsync("/license/status", cancellationToken);
27+
var responseJsonString = await httpResponse.Content.ReadAsStringAsync(cancellationToken);
28+
var licenseStatus = JsonSerializer.Deserialize<LicenseStatusFragment>(responseJsonString);
29+
if (licenseStatus.Expired)
30+
{
31+
throw new NotSupportedException("The current RavenDB license is expired. Please, contact support");
32+
}
33+
34+
if (licenseStatus.LicensedTo != null && licenseStatus.Id != null)
35+
{
36+
licenseCorrectlySetup = true;
37+
}
38+
39+
if (++attempts > 10)
40+
{
41+
throw new NotSupportedException("Cannot validate the current RavenDB license. Please, contact support");
42+
}
43+
44+
await Task.Delay(500, cancellationToken);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)