From 8718f22ab2787c9dbba6f3001cbe3f644236eacf Mon Sep 17 00:00:00 2001 From: Justin Baur <19896123+justindbaur@users.noreply.github.com> Date: Wed, 30 Nov 2022 08:40:12 -0500 Subject: [PATCH] [PS-1909] Make LicenseKey check null safe (#2444) * Make LicenseKey check null safe * Catch Exception during Organization Validation * Use null-safe check in UpdateLicense * Formatting --- .../Implementations/LicensingService.cs | 56 ++++++++++++------- .../Implementations/OrganizationService.cs | 4 +- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/Core/Services/Implementations/LicensingService.cs b/src/Core/Services/Implementations/LicensingService.cs index 893ea2268..fc8cfe573 100644 --- a/src/Core/Services/Implementations/LicensingService.cs +++ b/src/Core/Services/Implementations/LicensingService.cs @@ -81,35 +81,49 @@ public class LicensingService : ILicensingService var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync(); _logger.LogInformation(Constants.BypassFiltersEventId, null, - "Validating licenses for {0} organizations.", enabledOrgs.Count); + "Validating licenses for {NumberOfOrganizations} organizations.", enabledOrgs.Count); + + var exceptions = new List(); foreach (var org in enabledOrgs) { - var license = await ReadOrganizationLicenseAsync(org); - if (license == null) + try { - await DisableOrganizationAsync(org, null, "No license file."); - continue; - } + var license = await ReadOrganizationLicenseAsync(org); + if (license == null) + { + await DisableOrganizationAsync(org, null, "No license file."); + continue; + } - var totalLicensedOrgs = enabledOrgs.Count(o => o.LicenseKey.Equals(license.LicenseKey)); - if (totalLicensedOrgs > 1) - { - await DisableOrganizationAsync(org, license, "Multiple organizations."); - continue; - } + var totalLicensedOrgs = enabledOrgs.Count(o => string.Equals(o.LicenseKey, license.LicenseKey)); + if (totalLicensedOrgs > 1) + { + await DisableOrganizationAsync(org, license, "Multiple organizations."); + continue; + } - if (!license.VerifyData(org, _globalSettings)) - { - await DisableOrganizationAsync(org, license, "Invalid data."); - continue; - } + if (!license.VerifyData(org, _globalSettings)) + { + await DisableOrganizationAsync(org, license, "Invalid data."); + continue; + } - if (!license.VerifySignature(_certificate)) - { - await DisableOrganizationAsync(org, license, "Invalid signature."); - continue; + if (!license.VerifySignature(_certificate)) + { + await DisableOrganizationAsync(org, license, "Invalid signature."); + continue; + } } + catch (Exception ex) + { + exceptions.Add(ex); + } + } + + if (exceptions.Any()) + { + throw new AggregateException("There were one or more exceptions while validating organizations.", exceptions); } } diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index 5f38cc520..d798a85ff 100644 --- a/src/Core/Services/Implementations/OrganizationService.cs +++ b/src/Core/Services/Implementations/OrganizationService.cs @@ -700,7 +700,7 @@ public class OrganizationService : IOrganizationService } var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync(); - if (enabledOrgs.Any(o => o.LicenseKey.Equals(license.LicenseKey))) + if (enabledOrgs.Any(o => string.Equals(o.LicenseKey, license.LicenseKey))) { throw new BadRequestException("License is already in use by another organization."); } @@ -852,7 +852,7 @@ public class OrganizationService : IOrganizationService } var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync(); - if (enabledOrgs.Any(o => o.LicenseKey.Equals(license.LicenseKey) && o.Id != organizationId)) + if (enabledOrgs.Any(o => string.Equals(o.LicenseKey, license.LicenseKey) && o.Id != organizationId)) { throw new BadRequestException("License is already in use by another organization."); }