Repository navigation
[PM-41931] feat(dirt): Add browser health exclusion storage #8669
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
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
40d49f7
[PM-41931] feat(dirt): Add ReportExclusions column to Cipher
AlexRubik 1ffa045
[PM-41931] feat(dirt): Return the per-user report exclusion date on cβ¦
AlexRubik 562b531
[PM-41931] feat(dirt): Add the cipher report exclusion repository
AlexRubik cbc2cb9
[PM-41931] feat(dirt): Add the exclude from reports endpoint
AlexRubik 12e2aca
[PM-41931] test(dirt): Cover health report exclusions
AlexRubik 72dc897
fixup! [PM-41931] feat(dirt): Add the cipher report exclusion repository
AlexRubik a7cb596
fixup! [PM-41931] test(dirt): Cover health report exclusions
AlexRubik e160edf
[PM-41931] fix(dirt): Return report exclusion date in EF sync withoutβ¦
AlexRubik 0112576
[PM-41931] refactor(dirt): Store report exclusions as a per-user flag
AlexRubik 17ad7d9
[PM-41931] refactor(dirt): Move the exclude endpoint to its own PR
AlexRubik 36ec325
[PM-41931] chore(dirt): Re-date migrations after syncing with main
AlexRubik 93dee7a
[PM-41931] refactor(dirt): Rename report exclusions to browser healthβ¦
AlexRubik fdf9e49
[PM-41931] refactor(dirt): Rename the exclusion procedure to UpdateMany
AlexRubik 26f470e
[PM-41931] style(dirt): Align the column types in Cipher.sql
AlexRubik 26496f6
[PM-41931] refactor(dirt): Return early when no cipher ids are passed
AlexRubik 478d9a1
[PM-41931] chore(dirt): Regenerate EF migrations on current main
AlexRubik 64a5baa
[PM-41931] chore(dirt): Re-date the SQL migration after main
AlexRubik 21ab4c8
[PM-41931] feat(dirt): Add an Exclude parameter to the exclusion procβ¦
AlexRubik ebf5066
[PM-41931] feat(dirt): Store the excluded risk categories per user
AlexRubik 1c8b82a
[PM-41931] feat(dirt): Add the risk category to the user's exclusions
AlexRubik 81daeaf
[PM-41931] refactor(dirt): Rename the column to Cipher.Exclusions
AlexRubik 8ad269a
[PM-41931] refactor(dirt): Return the user's exclusions as exclusions
AlexRubik 7bbf30b
[PM-41931] test(dirt): Cover an exclude with no cipher ids
AlexRubik a884832
[PM-41931] fix(dirt): Match the risk category exactly on SQL Server
AlexRubik e3e4216
[PM-41931] refactor(dirt): Rename the exclusions read parameter
AlexRubik 8fcc161
[PM-41931] refactor(dirt): Accept the risk category as NVARCHAR
AlexRubik 0d82e85
[PM-41931] fix(dirt): Run the exclusions migration after the permissiβ¦
AlexRubik 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
7 changes: 7 additions & 0 deletions
7
src/Core/Dirt/Repositories/ICipherBrowserHealthExclusionRepository.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,7 @@ | ||
| ο»Ώnamespace Bit.Core.Dirt.Repositories; | ||
|
|
||
| public interface ICipherBrowserHealthExclusionRepository | ||
| { | ||
| /// <summary>Excludes the user's personal, not-deleted logins from browser health, adding the risk category to the user's list once. The category must not be empty. Returns how many were excluded.</summary> | ||
| Task<int> ExcludeAsync(IEnumerable<Guid> cipherIds, Guid userId, string riskCategory); | ||
| } |
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
33 changes: 33 additions & 0 deletions
33
src/Infrastructure.Dapper/Dirt/CipherBrowserHealthExclusionRepository.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,33 @@ | ||
| ο»Ώusing System.Data; | ||
| using Bit.Core.Dirt.Repositories; | ||
| using Bit.Core.Settings; | ||
| using Bit.Infrastructure.Dapper.Repositories; | ||
| using Dapper; | ||
| using Microsoft.Data.SqlClient; | ||
|
|
||
| namespace Bit.Infrastructure.Dapper.Dirt; | ||
|
|
||
| public class CipherBrowserHealthExclusionRepository : BaseRepository, ICipherBrowserHealthExclusionRepository | ||
| { | ||
| public CipherBrowserHealthExclusionRepository(GlobalSettings globalSettings) | ||
| : base(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString) | ||
| { | ||
| } | ||
|
|
||
| public async Task<int> ExcludeAsync(IEnumerable<Guid> cipherIds, Guid userId, string riskCategory) | ||
| { | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(riskCategory); | ||
|
|
||
| var ids = cipherIds.ToList(); | ||
| if (ids.Count == 0) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| await using var connection = new SqlConnection(ConnectionString); | ||
| return await connection.ExecuteScalarAsync<int>( | ||
| "[dbo].[Cipher_UpdateManyExclusions]", | ||
| new { Ids = ids.ToGuidIdArrayTVP(), UserId = userId, Exclude = true, RiskCategory = riskCategory }, | ||
| commandType: CommandType.StoredProcedure); | ||
| } | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
src/Infrastructure.EntityFramework/Dirt/CipherBrowserHealthExclusionRepository.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,59 @@ | ||
| ο»Ώusing AutoMapper; | ||
| using Bit.Core.Dirt.Repositories; | ||
| using Bit.Core.Utilities; | ||
| using Bit.Core.Vault.Enums; | ||
| using Bit.Infrastructure.EntityFramework.Repositories; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace Bit.Infrastructure.EntityFramework.Dirt; | ||
|
|
||
| public class CipherBrowserHealthExclusionRepository : BaseEntityFrameworkRepository, ICipherBrowserHealthExclusionRepository | ||
| { | ||
| public CipherBrowserHealthExclusionRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper) | ||
| : base(serviceScopeFactory, mapper) | ||
| { | ||
| } | ||
|
|
||
| public async Task<int> ExcludeAsync(IEnumerable<Guid> cipherIds, Guid userId, string riskCategory) | ||
| { | ||
|
lastbestdev marked this conversation as resolved.
Resolved
|
||
| ArgumentException.ThrowIfNullOrWhiteSpace(riskCategory); | ||
|
|
||
| var ids = cipherIds.ToList(); | ||
| if (ids.Count == 0) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| await using var scope = ServiceScopeFactory.CreateAsyncScope(); | ||
| var dbContext = GetDatabaseContext(scope); | ||
| var ciphers = await dbContext.Ciphers | ||
| .Where(c => ids.Contains(c.Id) && c.UserId == userId && c.OrganizationId == null | ||
| && c.Type == CipherType.Login && c.DeletedDate == null) | ||
| .ToListAsync(); | ||
|
|
||
| foreach (var cipher in ciphers) | ||
| { | ||
| var exclusions = string.IsNullOrWhiteSpace(cipher.Exclusions) | ||
|
AlexRubik marked this conversation as resolved.
Resolved
|
||
| ? new Dictionary<Guid, List<string>>() | ||
| : CoreHelpers.LoadClassFromJsonData<Dictionary<Guid, List<string>>>(cipher.Exclusions) | ||
| ?? new Dictionary<Guid, List<string>>(); | ||
| if (!exclusions.TryGetValue(userId, out var riskCategories) || riskCategories == null) | ||
| { | ||
| exclusions[userId] = [riskCategory]; | ||
| } | ||
| else if (!riskCategories.Contains(riskCategory)) | ||
| { | ||
| riskCategories.Add(riskCategory); | ||
| } | ||
| cipher.Exclusions = CoreHelpers.ClassToJsonData(exclusions); | ||
| } | ||
|
|
||
| if (ciphers.Count > 0) | ||
| { | ||
| await dbContext.UserBumpAccountRevisionDateAsync(userId); | ||
| } | ||
| await dbContext.SaveChangesAsync(); | ||
| return ciphers.Count; | ||
|
AlexRubik marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
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
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
51 changes: 51 additions & 0 deletions
51
src/Sql/dbo/Dirt/Stored Procedures/Cipher_UpdateManyExclusions.sql
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,51 @@ | ||
| CREATE PROCEDURE [dbo].[Cipher_UpdateManyExclusions] | ||
| @Ids AS [dbo].[GuidIdArray] READONLY, | ||
| @UserId AS UNIQUEIDENTIFIER, | ||
| @Exclude AS BIT, | ||
| @RiskCategory AS NVARCHAR(50) = NULL | ||
| AS | ||
| BEGIN | ||
| SET NOCOUNT ON | ||
|
|
||
| UPDATE | ||
| C | ||
| SET | ||
| [Exclusions] = CASE | ||
| -- First exclusion for this user: start the user's array | ||
| WHEN @Exclude = 1 AND JSON_QUERY(C.[Exclusions], CONCAT('$."', @UserId, '"')) IS NULL THEN JSON_MODIFY( | ||
| COALESCE(C.[Exclusions], N'{}'), | ||
| CONCAT('$."', @UserId, '"'), | ||
| JSON_QUERY(JSON_MODIFY(N'[]', 'append $', @RiskCategory)) | ||
| ) | ||
| -- Category already excluded: keep the value | ||
| WHEN @Exclude = 1 AND EXISTS ( | ||
| SELECT 1 | ||
| FROM OPENJSON(C.[Exclusions], CONCAT('$."', @UserId, '"')) | ||
| WHERE [value] COLLATE Latin1_General_BIN2 = @RiskCategory -- exact match, like EF | ||
| ) THEN C.[Exclusions] | ||
| WHEN @Exclude = 1 THEN JSON_MODIFY( | ||
| C.[Exclusions], | ||
| CONCAT('append $."', @UserId, '"'), | ||
| @RiskCategory | ||
| ) | ||
| -- NULL removes the user's key | ||
| ELSE JSON_MODIFY(COALESCE(C.[Exclusions], N'{}'), CONCAT('$."', @UserId, '"'), NULL) | ||
| END | ||
| FROM | ||
| [dbo].[Cipher] C | ||
| INNER JOIN @Ids I ON I.[Id] = C.[Id] | ||
| WHERE | ||
| C.[UserId] = @UserId | ||
| AND C.[OrganizationId] IS NULL | ||
| AND C.[Type] = 1 -- Login | ||
| AND C.[DeletedDate] IS NULL | ||
|
|
||
| DECLARE @Count INT = @@ROWCOUNT | ||
|
|
||
| IF @Count > 0 | ||
| BEGIN | ||
| EXEC [dbo].[User_BumpAccountRevisionDate] @UserId | ||
| END | ||
|
|
||
| SELECT @Count | ||
| END |
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
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
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.
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.