Skip to content
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

[PM-11516] Initial license file refactor #5002

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

cturnbull-bitwarden
Copy link
Contributor

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-11516

📔 Objective

This PR is the first, and most major iteration in a series of refactoring and cleaning up pull requests for our organization and user self-host licenses.

Currently, our licenses work based on an integer-based versioning system. Additionally, our cloud and self-host releases are at a minimum staggered by a couple of days, which can be even more if the self-host admins don't update immediately.

Suppose a new field is added to OrganizationLicense, for example, and the version is incremented by one to accommodate the new field. In that case, self-host instances will break in the intervening time because they're not expecting the new field, and their resulting hash will not match the hash in the license file.

To get around this, we've enabled Customer Success to generate licenses for any version current and prior using the Bitwarden Portal. While this workaround has been the status quo, we've decided to rectify this issue.

This pull request addresses the above problem by moving all current fields on OrganizationLicense and UserLicense into a new field, Token on each respective license. The Token is a string JWT containing a dictionary of claims that match the current fields, as well as a header and signature. In the above scenario, no additional versioning will be required from this point forward given that, if the self-host instance doesn't know to lookup a claim type, then it simply won't. The additional unused payload won't break the self-hosted instances ability to verify the signature using their public key.

I made some attempts to minimize the number of changes in this pull request by using the existing paradigms and patterns given that licenses touch a fair number of files. In future pull requests, I'll be aiming to separate out application logic from the licenses, like CanUse or VerifyData, into commands. Similarly, I'll be aiming to clean up or separate concerns in ILicensingService into commands and queries. Additionally, in a future PR I'll move billing related files to our own domain.

📸 Screenshots

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

Copy link
Contributor

github-actions bot commented Nov 7, 2024

Logo
Checkmarx One – Scan Summary & Details9a7e404c-2542-41be-b137-3ea27b4d62f0

New Issues

Severity Issue Source File / Package Checkmarx Insight
MEDIUM CSRF /src/Api/Controllers/LicensesController.cs: 44 Attack Vector
MEDIUM CSRF /src/Billing/Controllers/PayPalController.cs: 66 Attack Vector
MEDIUM Missing_HSTS_Header /src/Admin/Views/Home/Index.cshtml: 14 Attack Vector
MEDIUM Privacy_Violation /src/Api/Vault/Models/Request/CipherRequestModel.cs: 173 Attack Vector
MEDIUM Privacy_Violation /src/Api/Vault/Models/Request/CipherRequestModel.cs: 202 Attack Vector
LOW Heap_Inspection /src/Core/Billing/Licenses/LicenseConstants.cs: 33 Attack Vector
LOW Heap_Inspection /src/Core/Billing/Licenses/LicenseConstants.cs: 27 Attack Vector
LOW Log_Forging /src/Api/Vault/Controllers/CiphersController.cs: 220 Attack Vector
LOW Missing_CSP_Header /src/Core/MailTemplates/Handlebars/FamiliesForEnterprise/FamiliesForEnterpriseRemovedFromFamilyUser.html.hbs: 5 Attack Vector

Fixed Issues

Severity Issue Source File / Package
MEDIUM CSRF /src/Api/AdminConsole/Controllers/PoliciesController.cs: 68
MEDIUM CSRF /src/Api/AdminConsole/Controllers/OrganizationUsersController.cs: 106
MEDIUM CSRF /src/Billing/Controllers/RecoveryController.cs: 38
MEDIUM CSRF /src/Api/AdminConsole/Controllers/GroupsController.cs: 81
MEDIUM CSRF /src/Billing/Controllers/StripeController.cs: 164
MEDIUM CSRF /src/Api/AdminConsole/Public/Controllers/MembersController.cs: 95
MEDIUM CSRF /src/Api/AdminConsole/Controllers/OrganizationUsersController.cs: 238
MEDIUM CSRF /src/Admin/AdminConsole/Controllers/OrganizationsController.cs: 372
MEDIUM CSRF /src/Api/AdminConsole/Controllers/GroupsController.cs: 81
MEDIUM CSRF /src/Api/AdminConsole/Controllers/GroupsController.cs: 131
MEDIUM CSRF /src/Api/AdminConsole/Controllers/PoliciesController.cs: 89
MEDIUM CSRF /src/Api/AdminConsole/Public/Controllers/PoliciesController.cs: 42
MEDIUM CSRF /src/Api/AdminConsole/Public/Controllers/PoliciesController.cs: 61
MEDIUM CSRF /src/Api/Vault/Controllers/CiphersController.cs: 110
LOW Log_Forging /src/Api/AdminConsole/Controllers/ProvidersController.cs: 72
LOW Log_Forging /src/Api/AdminConsole/Controllers/ProvidersController.cs: 72
LOW Log_Forging /src/Api/AdminConsole/Controllers/ProvidersController.cs: 72

Copy link
Contributor

github-actions bot commented Nov 8, 2024

LaunchDarkly flag references

🔍 1 flag added or modified

Name Key Aliases found Info
Self host user licenses refactor pm-11516-self-host-license-refactor

Copy link

codecov bot commented Nov 8, 2024

Codecov Report

Attention: Patch coverage is 16.56442% with 408 lines in your changes missing coverage. Please review.

Project coverage is 42.87%. Comparing base (fae8692) to head (ee8763f).

Files with missing lines Patch % Lines
src/Core/Models/Business/OrganizationLicense.cs 36.53% 83 Missing and 16 partials ⚠️
...e/Billing/Licenses/Extensions/LicenseExtensions.cs 0.00% 89 Missing ⚠️
.../Core/Services/Implementations/LicensingService.cs 7.52% 86 Missing ⚠️
...mplementations/OrganizationLicenseClaimsFactory.cs 0.00% 55 Missing ⚠️
src/Core/Models/Business/UserLicense.cs 12.50% 34 Missing and 1 partial ⚠️
...rvices/Implementations/UserLicenseClaimsFactory.cs 0.00% 22 Missing ⚠️
...rvices/NoopImplementations/NoopLicensingService.cs 0.00% 9 Missing ⚠️
src/Core/Services/Implementations/UserService.cs 11.11% 7 Missing and 1 partial ⚠️
...le/Services/Implementations/OrganizationService.cs 0.00% 2 Missing ⚠️
src/Core/Billing/Licenses/Models/LicenseContext.cs 0.00% 2 Missing ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5002      +/-   ##
==========================================
- Coverage   43.09%   42.87%   -0.23%     
==========================================
  Files        1414     1419       +5     
  Lines       64808    65213     +405     
  Branches     5925     5976      +51     
==========================================
+ Hits        27929    27957      +28     
- Misses      35645    36019     +374     
- Partials     1234     1237       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

r-tome
r-tome previously approved these changes Nov 11, 2024
Copy link
Contributor

@r-tome r-tome left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, great idea and execution!

⛏️: Is it possible to add tests for LicensingService?

var expires = subscriptionInfo.UpcomingInvoice?.Date?.AddDays(7) ?? entity.PremiumExpirationDate?.AddDays(7);
var refresh = subscriptionInfo.UpcomingInvoice?.Date ?? entity.PremiumExpirationDate;
var trial = (subscriptionInfo.Subscription?.TrialEndDate.HasValue ?? false) &&
subscriptionInfo.Subscription.TrialEndDate.Value > DateTime.UtcNow;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably leverage TimeProvider here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll note it for the next set of reactors in the next PR. In this one, I wanted to keep the logic as identical as possible to how it's working right now, just with the change of using a JWT

claims.Add(new Claim(nameof(OrganizationLicenseConstants.BusinessName), entity.BusinessName));
}

return Task.FromResult(claims);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Out of curiosity, what's the purpose of this method returning a Task without doing anything asynchronously?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This came out of a discussion I had with platform on the approach. The idea was to have a single interface that could be used to generate claims for any object. If we needed to have some async IO in some speculative implementation to calculate the value for a claim, then this would allow for that.

@withinfocus
Copy link
Contributor

Had to do some quick hacking on this one -- please catch back up to main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants