Skip to content

Commit

Permalink
Licensing cleanup (#4253)
Browse files Browse the repository at this point in the history
* Stop trying to validate and import license from Setup command

* Clean up LicenseManager after removing code from Setup commands

* Use standard license validation
  • Loading branch information
bording committed Jun 25, 2024
1 parent 6c7f4c8 commit f3a8c06
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using LicenseManagement;
using Microsoft.Extensions.Hosting;
using NServiceBus.Logging;
using Settings;
Expand All @@ -15,12 +14,6 @@ public override async Task Execute(HostArguments args, Settings settings)
{
settings.SkipQueueCreation = args.SkipQueueCreation;

// Validate license:
if (!ValidateLicense(settings))
{
return;
}

if (settings.IngestAuditMessages)
{
if (settings.SkipQueueCreation)
Expand Down Expand Up @@ -57,35 +50,6 @@ public override async Task Execute(HostArguments args, Settings settings)
await host.StopAsync();
}

bool ValidateLicense(Settings settings)
{
if (!string.IsNullOrWhiteSpace(settings.LicenseFileText))
{
if (!LicenseManager.IsLicenseValidForServiceControlInit(settings.LicenseFileText, out var errorMessageForLicenseText))
{
Logger.Error(errorMessageForLicenseText);
return false;
}

if (!LicenseManager.TryImportLicenseFromText(settings.LicenseFileText, out var importErrorMessage))
{
Logger.Error(importErrorMessage);
return false;
}
}
else
{
var license = LicenseManager.FindLicense();
if (!LicenseManager.IsLicenseValidForServiceControlInit(license, out var errorMessageForFoundLicense))
{
Logger.Error(errorMessageForFoundLicense);
return false;
}
}

return true;
}

static readonly ILog Logger = LogManager.GetLogger<SetupCommand>();
}
}
88 changes: 9 additions & 79 deletions src/ServiceControl.LicenseManagement/LicenseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,71 +18,28 @@ public static DetectedLicense FindLicense()
return detectedLicense;
}

public static bool IsLicenseValidForServiceControlInit(DetectedLicense license, out string errorMessage)
{
if (!license.Details.ValidForServiceControl)
{
errorMessage = "License is not for ServiceControl";
return false;
}

if (license.Details.HasLicenseExpired())
{
errorMessage = "License has expired";
return false;
}

// E.g. Cannot set up a new instance of ServiceControl on a trial license when running in a docker container
if (IsRunningInDocker && license.IsEvaluationLicense)
{
errorMessage = "Cannot run ServiceControl in a container with a trial license";
return false;
}

errorMessage = "";
return true;
}

public static bool IsLicenseValidForServiceControlInit(string licenseText, out string errorMessage)
{
if (!TryDeserializeLicense(licenseText, out var license))
{
errorMessage = "Invalid license file";
return false;
}

return IsLicenseValidForServiceControlInit(new DetectedLicense("", LicenseDetails.FromLicense(license)), out errorMessage);
}

public static bool TryImportLicenseFromText(string licenseText, out string errorMessage)
public static bool TryImportLicense(string licenseFile, out string errorMessage)
{
if (!LicenseVerifier.TryVerify(licenseText, out _))
{
errorMessage = "Invalid license file";
return false;
}

if (!TryDeserializeLicense(licenseText, out var license))
{
errorMessage = "Invalid license file";
return false;
}
var license = new LicenseSourceFilePath(licenseFile);
var result = license.Find("ServiceControl");

if (!license.ValidForApplication("ServiceControl"))
if (result.License is null)
{
errorMessage = "License is not for ServiceControl";
errorMessage = result.Result;
return false;
}

if (license.HasExpired())
if (result.License.HasExpired())
{
errorMessage = "Failed to import because the license has expired";
return false;
}

try
{
new FilePathLicenseStore().StoreLicense(GetMachineLevelLicenseLocation(), licenseText);
var licenseText = NonBlockingReader.ReadAllTextWithoutLocking(licenseFile);
var machineLevelLicenseLocation = LicenseFileLocationResolver.GetPathFor(Environment.SpecialFolder.CommonApplicationData);
new FilePathLicenseStore().StoreLicense(machineLevelLicenseLocation, licenseText);
}
catch (Exception)
{
Expand All @@ -95,32 +52,5 @@ public static bool TryImportLicenseFromText(string licenseText, out string error
}

public static DateTime GetReleaseDate() => ReleaseDateReader.GetReleaseDate();

public static bool TryImportLicense(string licenseFile, out string errorMessage)
{
var licenseText = NonBlockingReader.ReadAllTextWithoutLocking(licenseFile);
return TryImportLicenseFromText(licenseText, out errorMessage);
}

static string GetMachineLevelLicenseLocation()
{
return LicenseFileLocationResolver.GetPathFor(Environment.SpecialFolder.CommonApplicationData);
}

static bool IsRunningInDocker => bool.TryParse(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER"), out bool isDocker) && isDocker;

static bool TryDeserializeLicense(string licenseText, out License license)
{
license = null;
try
{
license = LicenseDeserializer.Deserialize(licenseText);
return true;
}
catch
{
return false;
}
}
}
}
35 changes: 0 additions & 35 deletions src/ServiceControl.Monitoring/Hosting/Commands/SetupCommand.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
namespace ServiceControl.Monitoring
{
using System.Threading.Tasks;
using LicenseManagement;
using NServiceBus.Logging;
using Transports;

class SetupCommand : AbstractCommand
{
public override Task Execute(Settings settings)
{
if (!ValidateLicense(settings))
{
return Task.CompletedTask;
}

if (settings.SkipQueueCreation)
{
Logger.Info("Skipping queue creation");
Expand All @@ -26,35 +20,6 @@ public override Task Execute(Settings settings)
return transportCustomization.ProvisionQueues(transportSettings, []);
}

bool ValidateLicense(Settings settings)
{
if (!string.IsNullOrWhiteSpace(settings.LicenseFileText))
{
if (!LicenseManager.IsLicenseValidForServiceControlInit(settings.LicenseFileText, out var errorMessageForLicenseText))
{
Logger.Error(errorMessageForLicenseText);
return false;
}

if (!LicenseManager.TryImportLicenseFromText(settings.LicenseFileText, out var importErrorMessage))
{
Logger.Error(importErrorMessage);
return false;
}
}
else
{
var license = LicenseManager.FindLicense();
if (!LicenseManager.IsLicenseValidForServiceControlInit(license, out var errorMessageForFoundLicense))
{
Logger.Error(errorMessageForFoundLicense);
return false;
}
}

return true;
}

static readonly ILog Logger = LogManager.GetLogger<SetupCommand>();
}
}
36 changes: 0 additions & 36 deletions src/ServiceControl/Hosting/Commands/SetupCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using LicenseManagement;
using Microsoft.Extensions.Hosting;
using NServiceBus.Logging;
using Particular.ServiceControl;
Expand All @@ -17,12 +16,6 @@ public override async Task Execute(HostArguments args, Settings settings)
{
settings.SkipQueueCreation = args.SkipQueueCreation;

// Validate license:
if (!ValidateLicense(settings))
{
return;
}

var hostBuilder = Host.CreateApplicationBuilder();
hostBuilder.AddServiceControlInstallers(settings);

Expand Down Expand Up @@ -58,35 +51,6 @@ public override async Task Execute(HostArguments args, Settings settings)
await host.StopAsync();
}

static bool ValidateLicense(Settings settings)
{
if (!string.IsNullOrWhiteSpace(settings.LicenseFileText))
{
if (!LicenseManager.IsLicenseValidForServiceControlInit(settings.LicenseFileText, out var errorMessageForLicenseText))
{
Logger.Error(errorMessageForLicenseText);
return false;
}

if (!LicenseManager.TryImportLicenseFromText(settings.LicenseFileText, out var importErrorMessage))
{
Logger.Error(importErrorMessage);
return false;
}
}
else
{
var license = LicenseManager.FindLicense();
if (!LicenseManager.IsLicenseValidForServiceControlInit(license, out var errorMessageForFoundLicense))
{
Logger.Error(errorMessageForFoundLicense);
return false;
}
}

return true;
}

static readonly ILog Logger = LogManager.GetLogger<SetupCommand>();
}
}

0 comments on commit f3a8c06

Please sign in to comment.