-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate licence monitoring mechanism into its own class
Going forward Plugins aren't the only thing that might need licensing, built in features may also.
- Loading branch information
1 parent
c9e60bd
commit b7dfb96
Showing
8 changed files
with
196 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace EventStore.Plugins; | ||
|
||
public class LicenseException(string featureName, Exception? inner = null) : Exception( | ||
$"A license is required to use the {featureName} feature, but was not found. " + | ||
"Please obtain a license or disable the feature.", | ||
inner | ||
) { | ||
public string FeatureName { get; } = featureName; | ||
} | ||
|
||
public class LicenseEntitlementException(string featureName, string entitlement) : Exception( | ||
$"{featureName} feature requires the {entitlement} entitlement. Please contact EventStore support.") { | ||
public string FeatureName { get; } = featureName; | ||
public string MissingEntitlement { get; } = entitlement; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace EventStore.Plugins.Licensing; | ||
|
||
public static class LicenseConstants { | ||
public const string LicensePublicKey = "MEgCQQDGtRXIWmeJqkdpQryJdKBFVvLaMNHFkDcVXSoaDzg1ahrtCrAgwYpARAvGyFs0bcwYJZaZSt9aNwpgkAPOPQM5AgMBAAE="; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace EventStore.Plugins.Licensing; | ||
|
||
public static class LicenseMonitor { | ||
|
||
// the EULA prevents tampering with the license mechanism. we make the license mechanism | ||
// robust enough that circumventing it requires intentional tampering. | ||
public static async Task<IDisposable> MonitorAsync( | ||
string featureName, | ||
string[] requiredEntitlements, | ||
ILicenseService licenseService, | ||
ILogger logger, | ||
string licensePublicKey = LicenseConstants.LicensePublicKey, | ||
Action<int>? onCriticalError = null) { | ||
|
||
onCriticalError ??= Environment.Exit; | ||
|
||
// authenticate the license service itself so that we can trust it to | ||
// 1. send us any licences at all | ||
// 2. respect our decision to reject licences | ||
var authentic = await licenseService.SelfLicense.TryValidateAsync(licensePublicKey); | ||
if (!authentic) { | ||
// this should never happen, but could if we end up with some unknown LicenseService. | ||
logger.LogCritical("LicenseService could not be authenticated"); | ||
onCriticalError(11); | ||
} | ||
|
||
// authenticate the licenses that the license service sends us | ||
return licenseService.Licenses.Subscribe( | ||
onNext: async license => { | ||
if (await license.TryValidateAsync(licensePublicKey)) { | ||
// got an authentic license. check required entitlements | ||
if (license.HasEntitlement("ALL")) | ||
return; | ||
|
||
if (!license.HasEntitlements(requiredEntitlements, out var missing)) { | ||
licenseService.RejectLicense(new LicenseEntitlementException(featureName, missing)); | ||
} | ||
} else { | ||
// this should never happen | ||
logger.LogCritical("ESDB License was not valid"); | ||
licenseService.RejectLicense(new LicenseException(featureName, new Exception("ESDB License was not valid"))); | ||
onCriticalError(12); | ||
} | ||
}, | ||
onError: ex => { | ||
licenseService.RejectLicense(new LicenseException(featureName, ex)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
106 changes: 106 additions & 0 deletions
106
test/EventStore.Plugins.Tests/Licensing/LicenseMonitorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using EventStore.Plugins.Licensing; | ||
using Microsoft.Extensions.Logging.Testing; | ||
|
||
namespace EventStore.Plugins.Tests.Licensing; | ||
|
||
public class LicenseMonitorTests { | ||
[Fact] | ||
public async Task valid_license_with_correct_entitlements() { | ||
var licenseService = new PluginBaseTests.FakeLicenseService( | ||
createLicense: true, | ||
entitlements: ["MY_ENTITLEMENT"]); | ||
|
||
var criticalError = false; | ||
|
||
using var subscription = await LicenseMonitor.MonitorAsync( | ||
featureName: "TestFeature", | ||
requiredEntitlements: ["MY_ENTITLEMENT"], | ||
licenseService: licenseService, | ||
logger: new FakeLogger(), | ||
licensePublicKey: licenseService.PublicKey, | ||
onCriticalError: _ => criticalError = true); | ||
|
||
licenseService.RejectionException.Should().BeNull(); | ||
criticalError.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public async Task valid_license_with_all_entitlement() { | ||
var licenseService = new PluginBaseTests.FakeLicenseService( | ||
createLicense: true, | ||
entitlements: ["ALL"]); | ||
|
||
var criticalError = false; | ||
|
||
using var subscription = await LicenseMonitor.MonitorAsync( | ||
featureName: "TestFeature", | ||
requiredEntitlements: ["MY_ENTITLEMENT"], | ||
licenseService: licenseService, | ||
logger: new FakeLogger(), | ||
licensePublicKey: licenseService.PublicKey, | ||
onCriticalError: _ => criticalError = true); | ||
|
||
licenseService.RejectionException.Should().BeNull(); | ||
criticalError.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public async Task valid_license_with_missing_entitlement() { | ||
var licenseService = new PluginBaseTests.FakeLicenseService( | ||
createLicense: true, | ||
entitlements: []); | ||
|
||
var criticalError = false; | ||
|
||
using var subscription = await LicenseMonitor.MonitorAsync( | ||
featureName: "TestFeature", | ||
requiredEntitlements: ["MY_ENTITLEMENT"], | ||
licenseService: licenseService, | ||
logger: new FakeLogger(), | ||
licensePublicKey: licenseService.PublicKey, | ||
onCriticalError: _ => criticalError = true); | ||
|
||
licenseService.RejectionException.Should().BeOfType<LicenseEntitlementException>() | ||
.Which.MissingEntitlement.Should().Be("MY_ENTITLEMENT"); | ||
criticalError.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public async Task no_license() { | ||
var licenseService = new PluginBaseTests.FakeLicenseService( | ||
createLicense: false); | ||
|
||
var criticalError = false; | ||
|
||
using var subscription = await LicenseMonitor.MonitorAsync( | ||
featureName: "TestFeature", | ||
requiredEntitlements: [], | ||
licenseService: licenseService, | ||
logger: new FakeLogger(), | ||
licensePublicKey: licenseService.PublicKey, | ||
onCriticalError: _ => criticalError = true); | ||
|
||
licenseService.RejectionException.Should().BeOfType<LicenseException>(); | ||
criticalError.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public async Task license_is_not_valid() { | ||
var licenseService = new PluginBaseTests.FakeLicenseService( | ||
createLicense: true, | ||
entitlements: []); | ||
|
||
var criticalError = false; | ||
|
||
using var subscription = await LicenseMonitor.MonitorAsync( | ||
featureName: "TestFeature", | ||
requiredEntitlements: [], | ||
licenseService: licenseService, | ||
logger: new FakeLogger(), | ||
licensePublicKey: "a_different_public_key", | ||
onCriticalError: _ => criticalError = true); | ||
|
||
licenseService.RejectionException.Should().BeOfType<LicenseException>(); | ||
criticalError.Should().BeTrue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters