-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Arch/qa env seeding tweaks #7430
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
Open
MGibson1
wants to merge
16
commits into
main
Choose a base branch
from
arch/qa-env-seeding-tweaks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+62
β25
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2628904
[PM-34886] allow play id tracking in any non-production build
MGibson1 0acd87d
[PM-34880] Allow configuration by setting regardless of env
MGibson1 c19faae
Fixup append vs add on list
MGibson1 d6abc53
Simplify valid issues for licenses
MGibson1 05ccc89
Allow prod-signed certs in dev self host instances
MGibson1 67ed61f
Use all verification licenses for all verification methods
MGibson1 f4da360
Fixup license logic bug
MGibson1 d4802d2
ensure distinct license set
MGibson1 2749d31
avoid saving data protection certs for dev environments
MGibson1 17266e5
remove simple extension
MGibson1 6e97d75
private thumbprints
MGibson1 fd5ee89
simpler verification thumbprint validation
MGibson1 f85f0a4
switch to hashset for verification certs
MGibson1 522529e
Fixup missed play id non-prod logic
MGibson1 4028107
Update src/Core/Billing/Services/Implementations/LicensingService.cs
MGibson1 f0fa000
fixup!
MGibson1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,10 @@ namespace Bit.Core.Billing.Services; | |
|
|
||
| public class LicensingService : ILicensingService | ||
| { | ||
| private readonly X509Certificate2 _certificate; | ||
| private const string _productionCertThumbprint = "βB34876439FCDA2846505B2EFBBA6C4A951313EBE"; | ||
| private const string _developmentCertThumbprint = "207E64A231E8AA32AAF68A61037C075EBEBD553F"; | ||
| private readonly X509Certificate2 _creationCertificate; | ||
| private readonly HashSet<X509Certificate2> _verificationCertificates; | ||
| private readonly IGlobalSettings _globalSettings; | ||
| private readonly IUserRepository _userRepository; | ||
| private readonly IOrganizationRepository _organizationRepository; | ||
|
|
@@ -63,31 +66,63 @@ public LicensingService( | |
| _userLicenseClaimsFactory = userLicenseClaimsFactory; | ||
| _pushNotificationService = pushNotificationService; | ||
|
|
||
| var certThumbprint = environment.IsDevelopment() ? | ||
| "207E64A231E8AA32AAF68A61037C075EBEBD553F" : | ||
| "βB34876439FCDA2846505B2EFBBA6C4A951313EBE"; | ||
|
|
||
| // Load license creation cert | ||
| var creationCertThumbprint = environment.IsDevelopment() ? _developmentCertThumbprint : _productionCertThumbprint; | ||
| _verificationCertificates = new HashSet<X509Certificate2>(); | ||
| if (_globalSettings.SelfHosted) | ||
| { | ||
| _certificate = CoreHelpers.GetEmbeddedCertificateAsync(environment.IsDevelopment() ? "licensing_dev.cer" : "licensing.cer", null) | ||
| .GetAwaiter().GetResult(); | ||
| X509Certificate2 devCert = null; | ||
| X509Certificate2 prodCert = CoreHelpers.GetEmbeddedCertificateAsync("licensing.cer", null).GetAwaiter().GetResult(); | ||
|
|
||
| if (environment.IsDevelopment()) | ||
| { | ||
| devCert = CoreHelpers.GetEmbeddedCertificateAsync("licensing_dev.cer", null).GetAwaiter().GetResult(); | ||
| _creationCertificate = devCert; | ||
| // All self host envs accept prod cert. Creation cert added below to handle dev self-hosts | ||
| _verificationCertificates.Add(prodCert); | ||
| } | ||
| else | ||
| { | ||
| _creationCertificate = prodCert; | ||
| } | ||
|
|
||
| // non-production environments can use dev cert-generated licenses | ||
| if (!environment.IsProduction()) | ||
| { | ||
| devCert ??= CoreHelpers.GetEmbeddedCertificateAsync("licensing_dev.cer", null).GetAwaiter().GetResult(); | ||
| _verificationCertificates.Add(devCert); | ||
| } | ||
| } | ||
| else if (CoreHelpers.SettingHasValue(_globalSettings.Storage?.ConnectionString) && | ||
| CoreHelpers.SettingHasValue(_globalSettings.LicenseCertificatePassword)) | ||
| { | ||
| _certificate = CoreHelpers.GetBlobCertificateAsync(globalSettings.Storage.ConnectionString, "certificates", | ||
| _creationCertificate = CoreHelpers.GetBlobCertificateAsync(globalSettings.Storage.ConnectionString, "certificates", | ||
| "licensing.pfx", _globalSettings.LicenseCertificatePassword) | ||
| .GetAwaiter().GetResult(); | ||
| } | ||
| else | ||
| { | ||
| _certificate = CoreHelpers.GetCertificate(certThumbprint); | ||
| _creationCertificate = CoreHelpers.GetCertificate(creationCertThumbprint); | ||
| } | ||
| // Creation cert can always be used to verify | ||
| _verificationCertificates.Add(_creationCertificate); | ||
|
|
||
| if (_certificate == null || !_certificate.Thumbprint.Equals(CoreHelpers.CleanCertificateThumbprint(certThumbprint), | ||
| if (_creationCertificate == null || !_creationCertificate.Thumbprint.Equals(CoreHelpers.CleanCertificateThumbprint(creationCertThumbprint), | ||
| StringComparison.InvariantCultureIgnoreCase)) | ||
| { | ||
| throw new Exception("Invalid licensing certificate."); | ||
| } | ||
| var allowedThumbprints = new HashSet<string>(StringComparer.OrdinalIgnoreCase) | ||
| { | ||
| CoreHelpers.CleanCertificateThumbprint(_productionCertThumbprint), | ||
| CoreHelpers.CleanCertificateThumbprint(_developmentCertThumbprint) | ||
| }; | ||
| if (_verificationCertificates is null || _verificationCertificates.Count == 0 | ||
| || _verificationCertificates.Any(c => !allowedThumbprints.Contains(c.Thumbprint))) | ||
| { | ||
| throw new Exception("Invalid license verifying certificate."); | ||
| } | ||
|
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. This is clean, nice! π |
||
|
|
||
| if (_globalSettings.SelfHosted && !CoreHelpers.SettingHasValue(_globalSettings.LicenseDirectory)) | ||
| { | ||
|
|
@@ -132,7 +167,7 @@ public async Task ValidateOrganizationsAsync() | |
| continue; | ||
| } | ||
|
|
||
| if (string.IsNullOrWhiteSpace(license.Token) && !license.VerifySignature(_certificate)) | ||
| if (string.IsNullOrWhiteSpace(license.Token) && !_verificationCertificates.Any(c => license.VerifySignature(c))) | ||
| { | ||
| await DisableOrganizationAsync(org, license, "Invalid signature."); | ||
| continue; | ||
|
|
@@ -231,7 +266,7 @@ private async Task<bool> ProcessUserValidationAsync(User user) | |
| return false; | ||
| } | ||
|
|
||
| if (string.IsNullOrWhiteSpace(license.Token) && !license.VerifySignature(_certificate)) | ||
| if (string.IsNullOrWhiteSpace(license.Token) && !_verificationCertificates.Any(c => license.VerifySignature(c))) | ||
| { | ||
| await DisablePremiumAsync(user, license, "Invalid signature."); | ||
| return false; | ||
|
|
@@ -271,7 +306,7 @@ public bool VerifyLicense(ILicense license) | |
| { | ||
| if (string.IsNullOrWhiteSpace(license.Token)) | ||
| { | ||
| return license.VerifySignature(_certificate); | ||
| return _verificationCertificates.Any((c) => license.VerifySignature(c)); | ||
| } | ||
|
|
||
| try | ||
|
|
@@ -288,12 +323,12 @@ public bool VerifyLicense(ILicense license) | |
|
|
||
| public byte[] SignLicense(ILicense license) | ||
| { | ||
| if (_globalSettings.SelfHosted || !_certificate.HasPrivateKey) | ||
| if (_globalSettings.SelfHosted || !_creationCertificate.HasPrivateKey) | ||
| { | ||
| throw new InvalidOperationException("Cannot sign licenses."); | ||
| } | ||
|
|
||
| return license.Sign(_certificate); | ||
| return license.Sign(_creationCertificate); | ||
| } | ||
|
|
||
| private UserLicense ReadUserLicense(User user) | ||
|
|
@@ -341,7 +376,7 @@ public ClaimsPrincipal GetClaimsPrincipalFromLicense(ILicense license) | |
| var validationParameters = new TokenValidationParameters | ||
| { | ||
| ValidateIssuerSigningKey = true, | ||
| IssuerSigningKey = new X509SecurityKey(_certificate), | ||
| IssuerSigningKeys = _verificationCertificates.Select(c => new X509SecurityKey(c)), | ||
| ValidateIssuer = true, | ||
| ValidIssuer = "bitwarden", | ||
| ValidateAudience = true, | ||
|
|
@@ -393,7 +428,7 @@ private string GenerateToken(List<Claim> claims, string audience) | |
| claims.Add(new Claim(JwtClaimTypes.JwtId, Guid.NewGuid().ToString())); | ||
| } | ||
|
|
||
| var securityKey = new RsaSecurityKey(_certificate.GetRSAPrivateKey()); | ||
| var securityKey = new RsaSecurityKey(_creationCertificate.GetRSAPrivateKey()); | ||
| var tokenDescriptor = new SecurityTokenDescriptor | ||
| { | ||
| Subject = new ClaimsIdentity(claims), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.