Repository navigation
[PM-39454] feat(km): expose the user key id on sync and add the backfill endpoint #8112
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
Closed
quexten
wants to merge
4
commits into
km/user-key-id-06-validate-key-id
from
km/user-key-id-07-sync-and-backfill
Closed
Changes from all commits
Commits
Show all changes
4 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
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
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
19 changes: 19 additions & 0 deletions
19
src/Core/KeyManagement/Commands/Interfaces/ISetUserKeyIdCommand.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,19 @@ | ||
| namespace Bit.Core.KeyManagement.Commands.Interfaces; | ||
|
|
||
| public interface ISetUserKeyIdCommand | ||
| { | ||
| /// <summary> | ||
| /// Stores the hex-encoded key id of a user's current user key. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This is a backfill primitive for accounts that pre-date the key id being reported alongside | ||
| /// key material. It therefore only accepts a value when the account does not already have one — | ||
| /// changing an existing key id must happen through a key rotation. | ||
| /// </remarks> | ||
| /// <param name="userId">The user identifier.</param> | ||
| /// <param name="userKeyId">Hex-encoded key id of the user's current user key.</param> | ||
| /// <exception cref="Bit.Core.Exceptions.BadRequestException"> | ||
| /// Thrown when the account already has a key id. | ||
| /// </exception> | ||
| Task SetUserKeyIdAsync(Guid userId, Models.Data.KeyId userKeyId); | ||
| } |
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,26 @@ | ||
| using Bit.Core.Exceptions; | ||
| using Bit.Core.KeyManagement.Commands.Interfaces; | ||
| using Bit.Core.Repositories; | ||
|
|
||
| namespace Bit.Core.KeyManagement.Commands; | ||
|
|
||
| public class SetUserKeyIdCommand : ISetUserKeyIdCommand | ||
| { | ||
| private readonly IUserRepository _userRepository; | ||
|
|
||
| public SetUserKeyIdCommand(IUserRepository userRepository) | ||
| { | ||
| _userRepository = userRepository; | ||
| } | ||
|
|
||
| public async Task SetUserKeyIdAsync(Guid userId, Models.Data.KeyId userKeyId) | ||
| { | ||
| // The repository performs the "not already set" check and the write as one conditional | ||
| // statement, so two clients racing to backfill cannot both succeed. | ||
| var stored = await _userRepository.TrySetUserKeyIdAsync(userId, userKeyId); | ||
| if (!stored) | ||
| { | ||
| throw new BadRequestException("User key id is already set."); | ||
| } | ||
|
quexten 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
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
48 changes: 48 additions & 0 deletions
48
test/Core.Test/KeyManagement/Commands/SetUserKeyIdCommandTests.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,48 @@ | ||
| using Bit.Core.Exceptions; | ||
| using Bit.Core.KeyManagement.Commands; | ||
| using Bit.Core.KeyManagement.Models.Data; | ||
| using Bit.Core.Repositories; | ||
| using Bit.Test.Common.AutoFixture; | ||
| using Bit.Test.Common.AutoFixture.Attributes; | ||
| using NSubstitute; | ||
| using Xunit; | ||
|
|
||
| namespace Bit.Core.Test.KeyManagement.Commands; | ||
|
|
||
| [SutProviderCustomize] | ||
| public class SetUserKeyIdCommandTests | ||
| { | ||
| private static readonly KeyId _userKeyId = | ||
| KeyId.FromHexEncodedString("0123456789abcdef0123456789abcdef"); | ||
|
|
||
| [Theory, BitAutoData] | ||
| public async Task SetUserKeyIdAsync_WhenNoKeyIdStored_StoresIt( | ||
| Guid userId, | ||
| SutProvider<SetUserKeyIdCommand> sutProvider) | ||
| { | ||
| sutProvider.GetDependency<IUserRepository>() | ||
| .TrySetUserKeyIdAsync(userId, _userKeyId) | ||
| .Returns(true); | ||
|
|
||
| await sutProvider.Sut.SetUserKeyIdAsync(userId, _userKeyId); | ||
|
|
||
| await sutProvider.GetDependency<IUserRepository>() | ||
| .Received(1) | ||
| .TrySetUserKeyIdAsync(userId, _userKeyId); | ||
| } | ||
|
|
||
| [Theory, BitAutoData] | ||
| public async Task SetUserKeyIdAsync_WhenKeyIdAlreadyStored_ThrowsBadRequest( | ||
| Guid userId, | ||
| SutProvider<SetUserKeyIdCommand> sutProvider) | ||
| { | ||
| sutProvider.GetDependency<IUserRepository>() | ||
| .TrySetUserKeyIdAsync(userId, _userKeyId) | ||
| .Returns(false); | ||
|
|
||
| var exception = await Assert.ThrowsAsync<BadRequestException>( | ||
| () => sutProvider.Sut.SetUserKeyIdAsync(userId, _userKeyId)); | ||
|
|
||
| Assert.Equal("User key id is already set.", exception.Message); | ||
| } | ||
| } |
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.
❌ CRITICAL:
SetUserKeyIdRequestModeldoes not exist anywhere in the tree, sosrc/Apidoes not compile.Details and fix
git grep "class SetUserKeyIdRequestModel"returns nothing at this head, and no file namedSetUserKeyIdRequestModel.csexists undersrc/Api/KeyManagement/Models/Requests/in either this branch or the base branch (km/user-key-id-06-validate-key-id). The type is referenced by the controller (here and at line 107 viarequest.ToKeyId()), bytest/Api.Test/KeyManagement/AutoFixture/KeyIdRequestFixtures.cs:34,test/Api.IntegrationTest/KeyManagement/KeyIdRequestFixtures.cs:25, and by both new controller test files — all of which areCS0246.The model was authored earlier in the stack and then deleted by commit
62bce1e8f("remove unused request model") onkm/user-key-id-05-write-path; the deletion landed but the re-introduction that this PR depends on did not. Re-adding it here restores the endpoint's input validation, which the new integration tests (PostUserKeyIdAsync_MalformedKeyId_BadRequestcoveringnot-hex, uppercase, and empty string) rely on:Without the
[UserKeyId]attribute,KeyId.FromHexEncodedStringthrowsArgumentExceptionon malformed input, which surfaces as a 500 rather than the 400 those tests assert — so the model (not just any placeholder) is what keeps the malformed-input path correct.