mirror of
https://github.com/bitwarden/server.git
synced 2024-12-25 17:27:45 +01:00
[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
This commit is contained in:
parent
297f0c8b38
commit
8718f22ab2
@ -81,35 +81,49 @@ public class LicensingService : ILicensingService
|
|||||||
|
|
||||||
var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync();
|
var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync();
|
||||||
_logger.LogInformation(Constants.BypassFiltersEventId, null,
|
_logger.LogInformation(Constants.BypassFiltersEventId, null,
|
||||||
"Validating licenses for {0} organizations.", enabledOrgs.Count);
|
"Validating licenses for {NumberOfOrganizations} organizations.", enabledOrgs.Count);
|
||||||
|
|
||||||
|
var exceptions = new List<Exception>();
|
||||||
|
|
||||||
foreach (var org in enabledOrgs)
|
foreach (var org in enabledOrgs)
|
||||||
{
|
{
|
||||||
var license = await ReadOrganizationLicenseAsync(org);
|
try
|
||||||
if (license == null)
|
|
||||||
{
|
{
|
||||||
await DisableOrganizationAsync(org, null, "No license file.");
|
var license = await ReadOrganizationLicenseAsync(org);
|
||||||
continue;
|
if (license == null)
|
||||||
}
|
{
|
||||||
|
await DisableOrganizationAsync(org, null, "No license file.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
var totalLicensedOrgs = enabledOrgs.Count(o => o.LicenseKey.Equals(license.LicenseKey));
|
var totalLicensedOrgs = enabledOrgs.Count(o => string.Equals(o.LicenseKey, license.LicenseKey));
|
||||||
if (totalLicensedOrgs > 1)
|
if (totalLicensedOrgs > 1)
|
||||||
{
|
{
|
||||||
await DisableOrganizationAsync(org, license, "Multiple organizations.");
|
await DisableOrganizationAsync(org, license, "Multiple organizations.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!license.VerifyData(org, _globalSettings))
|
if (!license.VerifyData(org, _globalSettings))
|
||||||
{
|
{
|
||||||
await DisableOrganizationAsync(org, license, "Invalid data.");
|
await DisableOrganizationAsync(org, license, "Invalid data.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!license.VerifySignature(_certificate))
|
if (!license.VerifySignature(_certificate))
|
||||||
{
|
{
|
||||||
await DisableOrganizationAsync(org, license, "Invalid signature.");
|
await DisableOrganizationAsync(org, license, "Invalid signature.");
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
exceptions.Add(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exceptions.Any())
|
||||||
|
{
|
||||||
|
throw new AggregateException("There were one or more exceptions while validating organizations.", exceptions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -700,7 +700,7 @@ public class OrganizationService : IOrganizationService
|
|||||||
}
|
}
|
||||||
|
|
||||||
var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync();
|
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.");
|
throw new BadRequestException("License is already in use by another organization.");
|
||||||
}
|
}
|
||||||
@ -852,7 +852,7 @@ public class OrganizationService : IOrganizationService
|
|||||||
}
|
}
|
||||||
|
|
||||||
var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync();
|
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.");
|
throw new BadRequestException("License is already in use by another organization.");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user