-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[PM-32068] - Org Ability Extended Cache #7443
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
jrmccannon
wants to merge
3
commits into
main
Choose a base branch
from
jmccannon/ac/pm-32068-org-ability-cache
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
43 changes: 43 additions & 0 deletions
43
src/Core/AdminConsole/AbilitiesCache/ExtendedOrganizationAbilityCacheService.cs
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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| ο»Ώusing Bit.Core.AdminConsole.Entities; | ||
| using Bit.Core.Models.Data.Organizations; | ||
| using Bit.Core.Repositories; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using ZiggyCreatures.Caching.Fusion; | ||
| using static Bit.Core.AdminConsole.AbilitiesCache.ExtendedOrganizationAbilityCacheConstants; | ||
|
|
||
| namespace Bit.Core.AdminConsole.AbilitiesCache; | ||
|
|
||
| public static class ExtendedOrganizationAbilityCacheConstants | ||
| { | ||
| public const string CacheName = "OrganizationAbilities"; | ||
| } | ||
|
|
||
| public class ExtendedOrganizationAbilityCacheService( | ||
| [FromKeyedServices(CacheName)] IFusionCache cache, | ||
| IOrganizationRepository organizationRepository) | ||
| : IOrganizationAbilityCacheService | ||
| { | ||
|
|
||
| public async Task<OrganizationAbility?> GetOrganizationAbilityAsync(Guid orgId) | ||
| { | ||
| var cacheKey = BuildCacheKeyForOrganizationAbility(orgId); | ||
| return await cache.GetOrSetAsync<OrganizationAbility?>( | ||
| cacheKey, | ||
| async _ => await organizationRepository.GetAbilityAsync(orgId)); | ||
| } | ||
|
|
||
| public async Task UpsertOrganizationAbilityAsync(Organization organization) | ||
| { | ||
| var cacheKey = BuildCacheKeyForOrganizationAbility(organization.Id); | ||
| await cache.SetAsync<OrganizationAbility?>(cacheKey, new OrganizationAbility(organization)); | ||
| } | ||
|
|
||
| public async Task DeleteOrganizationAbilityAsync(Guid organizationId) | ||
| { | ||
| var cacheKey = BuildCacheKeyForOrganizationAbility(organizationId); | ||
| await cache.RemoveAsync(cacheKey); | ||
| } | ||
|
|
||
| private static string BuildCacheKeyForOrganizationAbility(Guid organizationId) | ||
| => $"org-ability:{organizationId:N}"; | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/Core/AdminConsole/AbilitiesCache/IOrganizationAbilityCacheService.cs
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ο»Ώusing Bit.Core.AdminConsole.Entities; | ||
| using Bit.Core.Models.Data.Organizations; | ||
|
|
||
| namespace Bit.Core.AdminConsole.AbilitiesCache; | ||
|
|
||
| public interface IOrganizationAbilityCacheService | ||
| { | ||
| Task<OrganizationAbility?> GetOrganizationAbilityAsync(Guid orgId); | ||
| Task UpsertOrganizationAbilityAsync(Organization organization); | ||
| Task DeleteOrganizationAbilityAsync(Guid organizationId); | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/Core/AdminConsole/AbilitiesCache/OrganizationAbilityServiceCollectionsExtension.cs
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| ο»Ώusing Bit.Core.Settings; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace Bit.Core.AdminConsole.AbilitiesCache; | ||
|
|
||
| public static class OrganizationAbilityServiceCollectionsExtension | ||
| { | ||
| public static IServiceCollection AddOrganizationAbilityCache(this IServiceCollection serviceCollection, | ||
| GlobalSettings globalSettings) => | ||
| serviceCollection.AddExtendedCache(ExtendedOrganizationAbilityCacheConstants.CacheName, globalSettings) | ||
| .AddScoped<IOrganizationAbilityCacheService, ExtendedOrganizationAbilityCacheService>(); | ||
| } |
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
132 changes: 132 additions & 0 deletions
132
test/Api.IntegrationTest/AdminConsole/Controllers/OrganizationAbilityCacheTests.cs
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 |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| ο»Ώusing System.Net; | ||
| using Bit.Api.AdminConsole.Models.Request.Organizations; | ||
| using Bit.Api.Auth.Models.Request.Accounts; | ||
| using Bit.Api.IntegrationTest.Factories; | ||
| using Bit.Api.IntegrationTest.Helpers; | ||
| using Bit.Core; | ||
| using Bit.Core.AdminConsole.Entities; | ||
| using Bit.Core.Billing.Enums; | ||
| using Bit.Core.Enums; | ||
| using Bit.Core.Services; | ||
| using NSubstitute; | ||
| using Xunit; | ||
|
|
||
| namespace Bit.Api.IntegrationTest.AdminConsole.Controllers; | ||
|
|
||
| public class OrganizationAbilityCacheTests : IClassFixture<ApiApplicationFactory>, IAsyncLifetime | ||
| { | ||
| private readonly HttpClient _client; | ||
| private readonly ApiApplicationFactory _factory; | ||
| private readonly LoginHelper _loginHelper; | ||
|
|
||
| private Organization _organization = null!; | ||
| private string _ownerEmail = null!; | ||
|
|
||
| public OrganizationAbilityCacheTests(ApiApplicationFactory factory) | ||
| { | ||
| _factory = factory; | ||
| _factory.SubstituteService<IFeatureService>(featureService => | ||
| { | ||
| featureService | ||
| .IsEnabled(FeatureFlagKeys.OrgAbilityExtendedCache) | ||
| .Returns(true); | ||
| }); | ||
| _client = factory.CreateClient(); | ||
| _loginHelper = new LoginHelper(_factory, _client); | ||
| } | ||
|
|
||
| public async Task InitializeAsync() | ||
| { | ||
| _ownerEmail = $"cache-test-{Guid.NewGuid()}@example.com"; | ||
| await _factory.LoginWithNewAccount(_ownerEmail); | ||
|
|
||
| var result = await OrganizationTestHelpers.SignUpAsync( | ||
| _factory, | ||
| plan: PlanType.EnterpriseAnnually, | ||
| ownerEmail: _ownerEmail, | ||
| passwordManagerSeats: 5, | ||
| paymentMethod: PaymentMethodType.Card); | ||
| _organization = result.Item1; | ||
| } | ||
|
|
||
| public Task DisposeAsync() | ||
| { | ||
| _client.Dispose(); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task SignUp_PopulatesCache_GetOrganizationAbilityReturnsAbility() | ||
| { | ||
| // Arrange - organization already created in InitializeAsync via SignUpAsync, | ||
| // which calls UpsertOrganizationAbilityAsync | ||
|
|
||
| // Act - read the cached ability directly | ||
| var cacheService = _factory.GetService<IApplicationCacheService>(); | ||
| var ability = await cacheService.GetOrganizationAbilityAsync(_organization.Id); | ||
|
|
||
| // Assert - cache was populated by the sign-up flow | ||
| Assert.NotNull(ability); | ||
| Assert.Equal(_organization.Id, ability.Id); | ||
| Assert.True(ability.Enabled); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Put_UpdatesOrganization_CacheReflectsUpdatedValues() | ||
| { | ||
| // Arrange - setup in InitializeAsync() | ||
| await _loginHelper.LoginAsync(_ownerEmail); | ||
| var updateRequest = new OrganizationUpdateRequestModel | ||
| { | ||
| Name = "Updated Cache Test Org", | ||
| BillingEmail = "updated-cache@example.com" | ||
| }; | ||
|
|
||
| // Act - update the organization via the HTTP endpoint | ||
| var response = await _client.PutAsJsonAsync($"/organizations/{_organization.Id}", updateRequest); | ||
|
|
||
| // Assert - endpoint succeeded and cache was updated | ||
| Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
|
|
||
| var cacheService = _factory.GetService<IApplicationCacheService>(); | ||
| var ability = await cacheService.GetOrganizationAbilityAsync(_organization.Id); | ||
| Assert.NotNull(ability); | ||
| Assert.Equal(_organization.Id, ability.Id); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Delete_RemovesOrganization_CacheReturnsNull() | ||
| { | ||
| // Arrange - create a separate org for deletion so we don't affect other tests | ||
| var deleteOwnerEmail = $"delete-test-{Guid.NewGuid()}@example.com"; | ||
| await _factory.LoginWithNewAccount(deleteOwnerEmail); | ||
|
|
||
| var signUpResult = await OrganizationTestHelpers.SignUpAsync( | ||
| _factory, | ||
| plan: PlanType.EnterpriseAnnually, | ||
| ownerEmail: deleteOwnerEmail, | ||
| passwordManagerSeats: 5, | ||
| paymentMethod: PaymentMethodType.Card); | ||
| var orgToDelete = signUpResult.Item1; | ||
|
|
||
| // Verify cache is populated before delete | ||
| var cacheService = _factory.GetService<IApplicationCacheService>(); | ||
| var abilityBeforeDelete = await cacheService.GetOrganizationAbilityAsync(orgToDelete.Id); | ||
| Assert.NotNull(abilityBeforeDelete); | ||
|
|
||
| // Act - delete the organization via the HTTP endpoint | ||
| await _loginHelper.LoginAsync(deleteOwnerEmail); | ||
| var deleteRequest = new SecretVerificationRequestModel | ||
| { | ||
| MasterPasswordHash = "master_password_hash" | ||
| }; | ||
| var response = await _client.PostAsJsonAsync( | ||
| $"/organizations/{orgToDelete.Id}/delete", deleteRequest); | ||
|
|
||
| // Assert - endpoint succeeded and cache was cleared | ||
| Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
|
|
||
| var abilityAfterDelete = await cacheService.GetOrganizationAbilityAsync(orgToDelete.Id); | ||
| Assert.Null(abilityAfterDelete); | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better not to expose this at the top level here. Can we have a services extension method - e.g.
AddOrganizationAbilityCache- which registers both? Similar to how our commands/queries/handlers are registered.