-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PM-13706] Add repository + stored procedures for private key regener…
…ation (#4898) * Add stored procedure * Add repository
- Loading branch information
1 parent
fae8692
commit 718ff21
Showing
8 changed files
with
126 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#nullable enable | ||
namespace Bit.Core.KeyManagement.Models.Data; | ||
|
||
public class UserAsymmetricKeys | ||
{ | ||
public Guid UserId { get; set; } | ||
public required string PublicKey { get; set; } | ||
public required string UserKeyEncryptedPrivateKey { get; set; } | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Core/KeyManagement/Repositories/IUserAsymmetricKeysRepository.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 @@ | ||
#nullable enable | ||
using Bit.Core.KeyManagement.Models.Data; | ||
|
||
namespace Bit.Core.KeyManagement.Repositories; | ||
|
||
public interface IUserAsymmetricKeysRepository | ||
{ | ||
Task RegenerateUserAsymmetricKeysAsync(UserAsymmetricKeys userAsymmetricKeys); | ||
} |
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
36 changes: 36 additions & 0 deletions
36
src/Infrastructure.Dapper/KeyManagement/Repositories/UserAsymmetricKeysRepository.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,36 @@ | ||
#nullable enable | ||
using System.Data; | ||
using Bit.Core.KeyManagement.Models.Data; | ||
using Bit.Core.KeyManagement.Repositories; | ||
using Bit.Core.Settings; | ||
using Bit.Infrastructure.Dapper.Repositories; | ||
using Dapper; | ||
using Microsoft.Data.SqlClient; | ||
|
||
namespace Bit.Infrastructure.Dapper.KeyManagement.Repositories; | ||
|
||
public class UserAsymmetricKeysRepository : BaseRepository, IUserAsymmetricKeysRepository | ||
{ | ||
public UserAsymmetricKeysRepository(GlobalSettings globalSettings) | ||
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString) | ||
{ | ||
} | ||
|
||
public UserAsymmetricKeysRepository(string connectionString, string readOnlyConnectionString) : base( | ||
connectionString, readOnlyConnectionString) | ||
{ | ||
} | ||
|
||
public async Task RegenerateUserAsymmetricKeysAsync(UserAsymmetricKeys userAsymmetricKeys) | ||
{ | ||
await using var connection = new SqlConnection(ConnectionString); | ||
|
||
await connection.ExecuteAsync("[dbo].[UserAsymmetricKeys_Regenerate]", | ||
new | ||
{ | ||
userAsymmetricKeys.UserId, | ||
userAsymmetricKeys.PublicKey, | ||
PrivateKey = userAsymmetricKeys.UserKeyEncryptedPrivateKey | ||
}, commandType: CommandType.StoredProcedure); | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
...Infrastructure.EntityFramework/KeyManagement/Repositories/UserAsymmetricKeysRepository.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,34 @@ | ||
#nullable enable | ||
using AutoMapper; | ||
using Bit.Core.KeyManagement.Models.Data; | ||
using Bit.Core.KeyManagement.Repositories; | ||
using Bit.Infrastructure.EntityFramework.Repositories; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Bit.Infrastructure.EntityFramework.KeyManagement.Repositories; | ||
|
||
public class UserAsymmetricKeysRepository : BaseEntityFrameworkRepository, IUserAsymmetricKeysRepository | ||
{ | ||
public UserAsymmetricKeysRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper) : base( | ||
serviceScopeFactory, | ||
mapper) | ||
{ | ||
} | ||
|
||
public async Task RegenerateUserAsymmetricKeysAsync(UserAsymmetricKeys userAsymmetricKeys) | ||
{ | ||
await using var scope = ServiceScopeFactory.CreateAsyncScope(); | ||
var dbContext = GetDatabaseContext(scope); | ||
|
||
var entity = await dbContext.Users.FindAsync(userAsymmetricKeys.UserId); | ||
if (entity != null) | ||
{ | ||
var utcNow = DateTime.UtcNow; | ||
entity.PublicKey = userAsymmetricKeys.PublicKey; | ||
entity.PrivateKey = userAsymmetricKeys.UserKeyEncryptedPrivateKey; | ||
entity.RevisionDate = utcNow; | ||
entity.AccountRevisionDate = utcNow; | ||
await dbContext.SaveChangesAsync(); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Sql/KeyManagement/dbo/Stored Procedures/UserAsymmetricKeys_Regenerate.sql
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,16 @@ | ||
CREATE PROCEDURE [dbo].[UserAsymmetricKeys_Regenerate] | ||
@UserId UNIQUEIDENTIFIER, | ||
@PublicKey VARCHAR(MAX), | ||
@PrivateKey VARCHAR(MAX) | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
DECLARE @UtcNow DATETIME2(7) = GETUTCDATE(); | ||
|
||
UPDATE [dbo].[User] | ||
SET [PublicKey] = @PublicKey, | ||
[PrivateKey] = @PrivateKey, | ||
[RevisionDate] = @UtcNow, | ||
[AccountRevisionDate] = @UtcNow | ||
WHERE [Id] = @UserId | ||
END |
16 changes: 16 additions & 0 deletions
16
util/Migrator/DbScripts/2024-11-21_00_AddUserAsymmetricKeysRegenerate.sql
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,16 @@ | ||
CREATE OR ALTER PROCEDURE [dbo].[UserAsymmetricKeys_Regenerate] | ||
@UserId UNIQUEIDENTIFIER, | ||
@PublicKey VARCHAR(MAX), | ||
@PrivateKey VARCHAR(MAX) | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
DECLARE @UtcNow DATETIME2(7) = GETUTCDATE(); | ||
|
||
UPDATE [dbo].[User] | ||
SET [PublicKey] = @PublicKey, | ||
[PrivateKey] = @PrivateKey, | ||
[RevisionDate] = @UtcNow, | ||
[AccountRevisionDate] = @UtcNow | ||
WHERE [Id] = @UserId | ||
END |