-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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-16812] Shortcut duplicate group patch requests #5354
Merged
eliykat
merged 28 commits into
main
from
ac/pm-16812/shortcut-duplicate-group-patch-requests
Feb 14, 2025
+1,437
โ250
Merged
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
7eb9e75
Refactor PatchGroupCommand
eliykat 574c1c8
Move changes to vnext patch command
eliykat 8132dd3
Detect duplicate add requests and return early
eliykat 97659af
Tweak wording
eliykat adb6331
tweak interface
eliykat 3e462c3
Add option to use HA replica
eliykat 42c58ca
Merge remote-tracking branch 'origin/main' into ac/pm-16812/shortcut-โฆ
eliykat 450bc40
Add new repository method
eliykat a17beef
Merge remote-tracking branch 'origin/main' into ac/pm-16812/shortcut-โฆ
eliykat cd88a87
Add db tests
eliykat 675bc7d
Merge remote-tracking branch 'origin/main' into ac/pm-16812/shortcut-โฆ
eliykat 840469c
Use HashSet to avoid duplicate guids
eliykat a1765a9
Update comment
eliykat 9f529af
Add null checks
eliykat d8036aa
Copy existing unit tests and make them pass
eliykat 36c8840
Add tests for new behavior and strengthen assertions
eliykat 26b1d6f
Move constants to scim application factory
eliykat b1e56ad
Copy tests and fix factory to use base implementation
eliykat 30a103e
Bump migration script date
eliykat 579032f
Address SonarQube feedback
eliykat 1f2528b
Address Robert feedback
eliykat 7401f74
Update comment
eliykat 9176519
Merge branch 'main' into ac/pm-16812/shortcut-duplicate-group-patch-rโฆ
eliykat 0360f7a
Merge remote-tracking branch 'origin/main' into ac/pm-16812/shortcut-โฆ
eliykat 6363f9a
Merge branch 'main' into ac/pm-16812/shortcut-duplicate-group-patch-rโฆ
eliykat a57257c
Merge commit 'ae9bb427a16ff2ff5d1ba3b62101ad7e55bec02e' into ac/pm-16โฆ
eliykat 0dde482
bump migration date
eliykat c01d0cb
Merge branch 'main' into ac/pm-16812/shortcut-duplicate-group-patch-rโฆ
eliykat 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
There are no files selected for viewing
This file contains 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
9 changes: 9 additions & 0 deletions
9
bitwarden_license/src/Scim/Groups/Interfaces/IPatchGroupCommandvNext.cs
This file contains 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,9 @@ | ||
๏ปฟusing Bit.Core.AdminConsole.Entities; | ||
using Bit.Scim.Models; | ||
|
||
namespace Bit.Scim.Groups.Interfaces; | ||
|
||
public interface IPatchGroupCommandvNext | ||
{ | ||
Task PatchGroupAsync(Group group, ScimPatchModel model); | ||
} |
170 changes: 170 additions & 0 deletions
170
bitwarden_license/src/Scim/Groups/PatchGroupCommandvNext.cs
This file contains 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,170 @@ | ||
๏ปฟusing System.Text.Json; | ||
using Bit.Core.AdminConsole.Entities; | ||
using Bit.Core.AdminConsole.OrganizationFeatures.Groups.Interfaces; | ||
using Bit.Core.AdminConsole.Repositories; | ||
using Bit.Core.AdminConsole.Services; | ||
using Bit.Core.Enums; | ||
using Bit.Core.Exceptions; | ||
using Bit.Core.Repositories; | ||
using Bit.Scim.Groups.Interfaces; | ||
using Bit.Scim.Models; | ||
using Bit.Scim.Utilities; | ||
|
||
namespace Bit.Scim.Groups; | ||
|
||
public class PatchGroupCommandvNext : IPatchGroupCommandvNext | ||
{ | ||
private readonly IGroupRepository _groupRepository; | ||
private readonly IGroupService _groupService; | ||
private readonly IUpdateGroupCommand _updateGroupCommand; | ||
private readonly ILogger<PatchGroupCommandvNext> _logger; | ||
private readonly IOrganizationRepository _organizationRepository; | ||
|
||
public PatchGroupCommandvNext( | ||
IGroupRepository groupRepository, | ||
IGroupService groupService, | ||
IUpdateGroupCommand updateGroupCommand, | ||
ILogger<PatchGroupCommandvNext> logger, | ||
IOrganizationRepository organizationRepository) | ||
{ | ||
_groupRepository = groupRepository; | ||
_groupService = groupService; | ||
_updateGroupCommand = updateGroupCommand; | ||
_logger = logger; | ||
_organizationRepository = organizationRepository; | ||
} | ||
|
||
public async Task PatchGroupAsync(Group group, ScimPatchModel model) | ||
{ | ||
foreach (var operation in model.Operations) | ||
{ | ||
await HandleOperationAsync(group, operation); | ||
} | ||
} | ||
|
||
private async Task HandleOperationAsync(Group group, ScimPatchModel.OperationModel operation) | ||
{ | ||
switch (operation.Op?.ToLowerInvariant()) | ||
{ | ||
// Replace a list of members | ||
case PatchOps.Replace when operation.Path?.ToLowerInvariant() == PatchPaths.Members: | ||
{ | ||
var ids = GetOperationValueIds(operation.Value); | ||
await _groupRepository.UpdateUsersAsync(group.Id, ids); | ||
break; | ||
} | ||
|
||
// Replace group name from path | ||
case PatchOps.Replace when operation.Path?.ToLowerInvariant() == PatchPaths.DisplayName: | ||
{ | ||
group.Name = operation.Value.GetString(); | ||
var organization = await _organizationRepository.GetByIdAsync(group.OrganizationId); | ||
if (organization == null) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
await _updateGroupCommand.UpdateGroupAsync(group, organization, EventSystemUser.SCIM); | ||
break; | ||
} | ||
|
||
// Replace group name from value object | ||
case PatchOps.Replace when | ||
string.IsNullOrWhiteSpace(operation.Path) && | ||
operation.Value.TryGetProperty("displayName", out var displayNameProperty): | ||
{ | ||
group.Name = displayNameProperty.GetString(); | ||
var organization = await _organizationRepository.GetByIdAsync(group.OrganizationId); | ||
if (organization == null) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
await _updateGroupCommand.UpdateGroupAsync(group, organization, EventSystemUser.SCIM); | ||
break; | ||
} | ||
|
||
// Add a single member | ||
case PatchOps.Add when | ||
!string.IsNullOrWhiteSpace(operation.Path) && | ||
operation.Path.StartsWith("members[value eq ", StringComparison.OrdinalIgnoreCase) && | ||
TryGetOperationPathId(operation.Path, out var addId): | ||
{ | ||
await AddMembersAsync(group, [addId]); | ||
break; | ||
} | ||
|
||
// Add a list of members | ||
case PatchOps.Add when | ||
operation.Path?.ToLowerInvariant() == PatchPaths.Members: | ||
{ | ||
await AddMembersAsync(group, GetOperationValueIds(operation.Value)); | ||
break; | ||
} | ||
|
||
// Remove a single member | ||
case PatchOps.Remove when | ||
!string.IsNullOrWhiteSpace(operation.Path) && | ||
operation.Path.StartsWith("members[value eq ", StringComparison.OrdinalIgnoreCase) && | ||
TryGetOperationPathId(operation.Path, out var removeId): | ||
{ | ||
await _groupService.DeleteUserAsync(group, removeId, EventSystemUser.SCIM); | ||
break; | ||
} | ||
|
||
// Remove a list of members | ||
case PatchOps.Remove when | ||
operation.Path?.ToLowerInvariant() == PatchPaths.Members: | ||
{ | ||
var orgUserIds = (await _groupRepository.GetManyUserIdsByIdAsync(group.Id)).ToHashSet(); | ||
foreach (var v in GetOperationValueIds(operation.Value)) | ||
{ | ||
orgUserIds.Remove(v); | ||
} | ||
await _groupRepository.UpdateUsersAsync(group.Id, orgUserIds); | ||
break; | ||
} | ||
|
||
default: | ||
{ | ||
_logger.LogWarning("Group patch operation not handled: {OperationOp}:{OperationPath}", operation.Op, operation.Path); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private async Task AddMembersAsync(Group group, HashSet<Guid> usersToAdd) | ||
{ | ||
// Azure Entra ID is known to send redundant "add" requests for each existing member every time any member | ||
// is removed. To avoid excessive load on the database, we check against the high availability replica and | ||
// return early if they already exist. | ||
var groupMembers = await _groupRepository.GetManyUserIdsByIdAsync(group.Id, useReadOnlyReplica: true); | ||
if (usersToAdd.IsSubsetOf(groupMembers)) | ||
{ | ||
_logger.LogDebug("Ignoring duplicate SCIM request to add members {Members} to group {Group}", usersToAdd, group.Id); | ||
return; | ||
} | ||
|
||
await _groupRepository.AddGroupUsersByIdAsync(group.Id, usersToAdd); | ||
} | ||
|
||
private static HashSet<Guid> GetOperationValueIds(JsonElement objArray) | ||
{ | ||
var ids = new HashSet<Guid>(); | ||
foreach (var obj in objArray.EnumerateArray()) | ||
{ | ||
if (obj.TryGetProperty("value", out var valueProperty)) | ||
{ | ||
if (valueProperty.TryGetGuid(out var guid)) | ||
{ | ||
ids.Add(guid); | ||
} | ||
} | ||
} | ||
return ids; | ||
} | ||
|
||
private static bool TryGetOperationPathId(string path, out Guid pathId) | ||
{ | ||
// Parse Guid from string like: members[value eq "{GUID}"}] | ||
return Guid.TryParse(path.Substring(18).Replace("\"]", string.Empty), out pathId); | ||
} | ||
} |
This file contains 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 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.
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.
The interesting part of the new command.