Skip to content
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-13706] Add repository + stored procedures for private key regeneration #11

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

lizard-boy
Copy link

@lizard-boy lizard-boy commented Oct 19, 2024

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-13706

📔 Objective

The purpose of this PR is to add the database stored procedure and repository layer for the private key regeneration project.

This initial phase will only target users not in organizations and without emergency access setup.
In future PRs, the database operations will expand to handle these cases.

📸 Screenshots

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

Greptile Summary

This pull request introduces a new repository and stored procedures for regenerating user asymmetric keys, focusing on users not in organizations and without emergency access setup.

  • Added UserAsymmetricKeys model in /src/Core/KeyManagement/Models/Data/UserAsymmetricKeys.cs with properties for UserId, PublicKey, and UserKeyEncryptedPrivateKey
  • Implemented IUserAsymmetricKeysRepository interface and its Dapper and Entity Framework implementations for key regeneration
  • Created stored procedure UserAsymmetricKeys_Regenerate in SQL and migration scripts to update user keys and revision dates
  • Updated service collection extensions in both Dapper and Entity Framework to register the new repository
  • Added RegenerateUserAsymmetricKeysAsync method in repositories to execute key regeneration logic

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8 file(s) reviewed, 7 comment(s)
Edit PR Review Bot Settings | Greptile


public class UserAsymmetricKeys
{
public Guid UserId { get; set; }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider making UserId a required property for consistency


public async Task RegenerateUserAsymmetricKeysAsync(UserAsymmetricKeys userAsymmetricKeys)
{
await using var connection = new SqlConnection(ConnectionString);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: consider using using instead of await using for better resource management

Comment on lines +28 to +34
await connection.ExecuteAsync("[dbo].[UserAsymmetricKeys_Regenerate]",
new
{
userAsymmetricKeys.UserId,
userAsymmetricKeys.PublicKey,
PrivateKey = userAsymmetricKeys.UserKeyEncryptedPrivateKey
}, commandType: CommandType.StoredProcedure);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: wrap in try-catch to handle potential SQL exceptions

await using var scope = ServiceScopeFactory.CreateAsyncScope();
var dbContext = GetDatabaseContext(scope);

var entity = await dbContext.Users.FindAsync(userAsymmetricKeys.UserId);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using FirstOrDefaultAsync with a predicate instead of FindAsync for better performance with large datasets

entity.PrivateKey = userAsymmetricKeys.UserKeyEncryptedPrivateKey;
entity.RevisionDate = utcNow;
entity.AccountRevisionDate = utcNow;
await dbContext.SaveChangesAsync();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Add error handling around SaveChangesAsync

Comment on lines +10 to +15
UPDATE [dbo].[User]
SET [PublicKey] = @PublicKey,
[PrivateKey] = @PrivateKey,
[RevisionDate] = @UtcNow,
[AccountRevisionDate] = @UtcNow
WHERE [Id] = @UserId
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Add error handling to check if the update was successful

Comment on lines +3 to +4
@PublicKey VARCHAR(MAX),
@PrivateKey VARCHAR(MAX)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using NVARCHAR(MAX) for @PublicKey and @privatekey to support Unicode characters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants