-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[PM-27731] Updated organization licenses to save the correct values from the token #6546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
a0ae1ee
fe0b5d0
331c987
6d33da8
2dc1349
216ddcb
1977151
35a9845
90a35de
4bb7046
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| ๏ปฟusing System.Text.Json; | ||
| using Bit.Core.AdminConsole.Entities; | ||
| using Bit.Core.Billing.Enums; | ||
| using Bit.Core.Billing.Licenses; | ||
| using Bit.Core.Billing.Licenses.Extensions; | ||
| using Bit.Core.Billing.Organizations.Models; | ||
|
|
@@ -46,6 +47,46 @@ public async Task UpdateLicenseAsync(SelfHostedOrganizationDetails selfHostedOrg | |
| } | ||
|
|
||
| var claimsPrincipal = _licensingService.GetClaimsPrincipalFromLicense(license); | ||
|
|
||
| // If the license has a Token (claims-based), extract all properties from claims BEFORE validation | ||
| // This ensures that CanUseLicense validation has access to the correct values from claims | ||
| // Otherwise, fall back to using the properties already on the license object (backward compatibility) | ||
| if (claimsPrincipal != null) | ||
| { | ||
| license.Name = claimsPrincipal.GetValue<string>(OrganizationLicenseConstants.Name); | ||
| license.BillingEmail = claimsPrincipal.GetValue<string>(OrganizationLicenseConstants.BillingEmail); | ||
| license.BusinessName = claimsPrincipal.GetValue<string>(OrganizationLicenseConstants.BusinessName); | ||
| license.PlanType = claimsPrincipal.GetValue<PlanType>(OrganizationLicenseConstants.PlanType); | ||
| license.Seats = claimsPrincipal.GetValue<int?>(OrganizationLicenseConstants.Seats); | ||
| license.MaxCollections = claimsPrincipal.GetValue<short?>(OrganizationLicenseConstants.MaxCollections); | ||
| license.UsePolicies = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UsePolicies); | ||
| license.UseSso = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseSso); | ||
| license.UseKeyConnector = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseKeyConnector); | ||
| license.UseScim = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseScim); | ||
| license.UseGroups = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseGroups); | ||
| license.UseDirectory = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseDirectory); | ||
| license.UseEvents = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseEvents); | ||
| license.UseTotp = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseTotp); | ||
| license.Use2fa = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.Use2fa); | ||
| license.UseApi = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseApi); | ||
| license.UseResetPassword = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseResetPassword); | ||
| license.Plan = claimsPrincipal.GetValue<string>(OrganizationLicenseConstants.Plan); | ||
| license.SelfHost = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.SelfHost); | ||
| license.UsersGetPremium = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UsersGetPremium); | ||
| license.UseCustomPermissions = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseCustomPermissions); | ||
| license.Enabled = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.Enabled); | ||
| license.Expires = claimsPrincipal.GetValue<DateTime?>(OrganizationLicenseConstants.Expires); | ||
| license.LicenseKey = claimsPrincipal.GetValue<string>(OrganizationLicenseConstants.LicenseKey); | ||
| license.UsePasswordManager = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UsePasswordManager); | ||
| license.UseSecretsManager = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseSecretsManager); | ||
| license.SmSeats = claimsPrincipal.GetValue<int?>(OrganizationLicenseConstants.SmSeats); | ||
| license.SmServiceAccounts = claimsPrincipal.GetValue<int?>(OrganizationLicenseConstants.SmServiceAccounts); | ||
| license.UseRiskInsights = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseRiskInsights); | ||
| license.UseOrganizationDomains = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseOrganizationDomains); | ||
| license.UseAdminSponsoredFamilies = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseAdminSponsoredFamilies); | ||
| license.UseAutomaticUserConfirmation = claimsPrincipal.GetValue<bool>(OrganizationLicenseConstants.UseAutomaticUserConfirmation); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code extracts 29 properties, but two constants appear to be missing from OrganizationLicenseConstants:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, it wasn't. Those two seem to be new additions from when I opened this PR. I'll get them added, good catch
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch @cyprain-okeke , I just pushed a commit addressing this. I also added a set of unit tests to guide future developers through the process of adding a property to the organization license |
||
|
|
||
| var canUse = license.CanUse(_globalSettings, _licensingService, claimsPrincipal, out var exception) && | ||
| selfHostedOrganization.CanUseLicense(license, out exception); | ||
|
|
||
|
|
@@ -54,12 +95,6 @@ public async Task UpdateLicenseAsync(SelfHostedOrganizationDetails selfHostedOrg | |
| throw new BadRequestException(exception); | ||
| } | ||
|
|
||
| var useAutomaticUserConfirmation = claimsPrincipal? | ||
| .GetValue<bool>(OrganizationLicenseConstants.UseAutomaticUserConfirmation) ?? false; | ||
|
|
||
| selfHostedOrganization.UseAutomaticUserConfirmation = useAutomaticUserConfirmation; | ||
| license.UseAutomaticUserConfirmation = useAutomaticUserConfirmation; | ||
|
|
||
| await WriteLicenseFileAsync(selfHostedOrganization, license); | ||
| await UpdateOrganizationAsync(selfHostedOrganization, license); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| ๏ปฟusing System.Security.Claims; | ||
| using Bit.Core.AdminConsole.Entities; | ||
| using Bit.Core.Billing.Enums; | ||
| using Bit.Core.Billing.Licenses; | ||
| using Bit.Core.Billing.Organizations.Commands; | ||
| using Bit.Core.Billing.Organizations.Models; | ||
| using Bit.Core.Billing.Services; | ||
|
|
@@ -87,8 +89,7 @@ await sutProvider.GetDependency<IOrganizationService>() | |
| "Id", "MaxStorageGb", "Issued", "Refresh", "Version", "Trial", "LicenseType", | ||
| "Hash", "Signature", "SignatureBytes", "InstallationId", "Expires", | ||
| "ExpirationWithoutGracePeriod", "Token", "LimitCollectionCreationDeletion", | ||
| "LimitCollectionCreation", "LimitCollectionDeletion", "AllowAdminAccessToAllCollectionItems", | ||
| "UseOrganizationDomains", "UseAdminSponsoredFamilies", "UseAutomaticUserConfirmation") && | ||
| "LimitCollectionCreation", "LimitCollectionDeletion", "AllowAdminAccessToAllCollectionItems") && | ||
| // Same property but different name, use explicit mapping | ||
| org.ExpirationDate == license.Expires)); | ||
| } | ||
|
|
@@ -99,6 +100,139 @@ await sutProvider.GetDependency<IOrganizationService>() | |
| } | ||
| } | ||
|
|
||
| [Theory, BitAutoData] | ||
cturnbull-bitwarden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public async Task UpdateLicenseAsync_WithClaimsPrincipal_ExtractsAllPropertiesFromClaims( | ||
| SelfHostedOrganizationDetails selfHostedOrg, | ||
| OrganizationLicense license, | ||
| SutProvider<UpdateOrganizationLicenseCommand> sutProvider) | ||
| { | ||
| var globalSettings = sutProvider.GetDependency<IGlobalSettings>(); | ||
| globalSettings.LicenseDirectory = LicenseDirectory; | ||
| globalSettings.SelfHosted = true; | ||
|
|
||
| // Setup license for CanUse validation | ||
| license.Enabled = true; | ||
| license.Issued = DateTime.Now.AddDays(-1); | ||
| license.Expires = DateTime.Now.AddDays(1); | ||
| license.Version = OrganizationLicense.CurrentLicenseFileVersion; | ||
| license.InstallationId = globalSettings.Installation.Id; | ||
| license.LicenseType = LicenseType.Organization; | ||
| license.Token = "test-token"; // Indicates this is a claims-based license | ||
| sutProvider.GetDependency<ILicensingService>().VerifyLicense(license).Returns(true); | ||
|
|
||
| // Create a ClaimsPrincipal with all organization license claims | ||
| var claims = new List<Claim> | ||
| { | ||
| new(OrganizationLicenseConstants.LicenseType, ((int)LicenseType.Organization).ToString()), | ||
| new(OrganizationLicenseConstants.InstallationId, globalSettings.Installation.Id.ToString()), | ||
| new(OrganizationLicenseConstants.Name, "Test Organization"), | ||
| new(OrganizationLicenseConstants.BillingEmail, "[email protected]"), | ||
| new(OrganizationLicenseConstants.BusinessName, "Test Business"), | ||
| new(OrganizationLicenseConstants.PlanType, ((int)PlanType.EnterpriseAnnually).ToString()), | ||
| new(OrganizationLicenseConstants.Seats, "100"), | ||
| new(OrganizationLicenseConstants.MaxCollections, "50"), | ||
| new(OrganizationLicenseConstants.UsePolicies, "true"), | ||
| new(OrganizationLicenseConstants.UseSso, "true"), | ||
| new(OrganizationLicenseConstants.UseKeyConnector, "true"), | ||
| new(OrganizationLicenseConstants.UseScim, "true"), | ||
| new(OrganizationLicenseConstants.UseGroups, "true"), | ||
| new(OrganizationLicenseConstants.UseDirectory, "true"), | ||
| new(OrganizationLicenseConstants.UseEvents, "true"), | ||
| new(OrganizationLicenseConstants.UseTotp, "true"), | ||
| new(OrganizationLicenseConstants.Use2fa, "true"), | ||
| new(OrganizationLicenseConstants.UseApi, "true"), | ||
| new(OrganizationLicenseConstants.UseResetPassword, "true"), | ||
| new(OrganizationLicenseConstants.Plan, "Enterprise"), | ||
| new(OrganizationLicenseConstants.SelfHost, "true"), | ||
| new(OrganizationLicenseConstants.UsersGetPremium, "true"), | ||
| new(OrganizationLicenseConstants.UseCustomPermissions, "true"), | ||
| new(OrganizationLicenseConstants.Enabled, "true"), | ||
| new(OrganizationLicenseConstants.Expires, DateTime.Now.AddDays(1).ToString("O")), | ||
| new(OrganizationLicenseConstants.LicenseKey, "test-license-key"), | ||
| new(OrganizationLicenseConstants.UsePasswordManager, "true"), | ||
| new(OrganizationLicenseConstants.UseSecretsManager, "true"), | ||
| new(OrganizationLicenseConstants.SmSeats, "25"), | ||
| new(OrganizationLicenseConstants.SmServiceAccounts, "10"), | ||
| new(OrganizationLicenseConstants.UseRiskInsights, "true"), | ||
| new(OrganizationLicenseConstants.UseOrganizationDomains, "true"), | ||
| new(OrganizationLicenseConstants.UseAdminSponsoredFamilies, "true"), | ||
| new(OrganizationLicenseConstants.UseAutomaticUserConfirmation, "true") | ||
| }; | ||
| var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims)); | ||
|
|
||
| sutProvider.GetDependency<ILicensingService>() | ||
| .GetClaimsPrincipalFromLicense(license) | ||
| .Returns(claimsPrincipal); | ||
|
|
||
| // Setup selfHostedOrg for CanUseLicense validation | ||
| selfHostedOrg.OccupiedSeatCount = 50; // Less than the 100 seats in the license | ||
| selfHostedOrg.CollectionCount = 10; // Less than the 50 max collections in the license | ||
| selfHostedOrg.GroupCount = 1; | ||
| selfHostedOrg.UseGroups = true; | ||
| selfHostedOrg.UsePolicies = true; | ||
| selfHostedOrg.UseSso = true; | ||
| selfHostedOrg.UseKeyConnector = true; | ||
| selfHostedOrg.UseScim = true; | ||
| selfHostedOrg.UseCustomPermissions = true; | ||
| selfHostedOrg.UseResetPassword = true; | ||
|
|
||
| try | ||
| { | ||
| await sutProvider.Sut.UpdateLicenseAsync(selfHostedOrg, license, null); | ||
|
|
||
| // Assertion: license file should be written to disk | ||
| var filePath = Path.Combine(LicenseDirectory, "organization", $"{selfHostedOrg.Id}.json"); | ||
| await using var fs = File.OpenRead(filePath); | ||
| var licenseFromFile = await JsonSerializer.DeserializeAsync<OrganizationLicense>(fs); | ||
|
|
||
| AssertHelper.AssertPropertyEqual(license, licenseFromFile, "SignatureBytes"); | ||
|
|
||
| // Assertion: organization should be updated with ALL properties extracted from claims | ||
| await sutProvider.GetDependency<IOrganizationService>() | ||
| .Received(1) | ||
| .ReplaceAndUpdateCacheAsync(Arg.Is<Organization>(org => | ||
| org.Name == "Test Organization" && | ||
| org.BillingEmail == "[email protected]" && | ||
| org.BusinessName == "Test Business" && | ||
| org.PlanType == PlanType.EnterpriseAnnually && | ||
| org.Seats == 100 && | ||
| org.MaxCollections == 50 && | ||
| org.UsePolicies == true && | ||
| org.UseSso == true && | ||
| org.UseKeyConnector == true && | ||
| org.UseScim == true && | ||
| org.UseGroups == true && | ||
| org.UseDirectory == true && | ||
| org.UseEvents == true && | ||
| org.UseTotp == true && | ||
| org.Use2fa == true && | ||
| org.UseApi == true && | ||
| org.UseResetPassword == true && | ||
| org.Plan == "Enterprise" && | ||
| org.SelfHost == true && | ||
| org.UsersGetPremium == true && | ||
| org.UseCustomPermissions == true && | ||
| org.Enabled == true && | ||
| org.LicenseKey == "test-license-key" && | ||
| org.UsePasswordManager == true && | ||
| org.UseSecretsManager == true && | ||
| org.SmSeats == 25 && | ||
| org.SmServiceAccounts == 10 && | ||
| org.UseRiskInsights == true && | ||
| org.UseOrganizationDomains == true && | ||
| org.UseAdminSponsoredFamilies == true && | ||
| org.UseAutomaticUserConfirmation == true)); | ||
| } | ||
| finally | ||
| { | ||
| // Clean up temporary directory | ||
| if (Directory.Exists(OrganizationLicenseDirectory.Value)) | ||
| { | ||
| Directory.Delete(OrganizationLicenseDirectory.Value, true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Wrapper to compare 2 objects that are different types | ||
| private bool AssertPropertyEqual(OrganizationLicense expected, Organization actual, params string[] excludedPropertyStrings) | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.