1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 16:42:50 -05:00

[PM-3797 Part 3] Add vault domains to key rotation (#3436)

## Type of change

<!-- (mark with an `X`) -->

```
- [ ] Bug fix
- [ ] New feature development
- [x] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other
```

## Objective
<!--Describe what the purpose of this PR is. For example: what bug you're fixing or what new feature you're adding-->
Previous PR: #3434
Adds ciphers and folders to the new key rotation.


## Code changes
<!--Explain the changes you've made to each file or major component. This should help the reviewer understand your changes-->
<!--Also refer to any related changes or PRs in other repositories-->

* **file.ext:** Description of what was changed and why

## Before you submit

- Please check for formatting errors (`dotnet format --verify-no-changes`) (required)
- If making database changes - make sure you also update Entity Framework queries and/or migrations
- Please add **unit tests** where it makes sense to do so (encouraged but not required)
- If this change requires a **documentation update** - notify the documentation team
- If this change has particular **deployment requirements** - notify the DevOps team
This commit is contained in:
Jake Fink
2023-12-06 08:46:36 -05:00
committed by GitHub
parent dbf8907bfc
commit 4b2bd6cee6
17 changed files with 485 additions and 8 deletions

View File

@ -161,6 +161,8 @@ public class UserRepository : Repository<Core.Entities.User, User, Guid>, IUserR
entity.AccountRevisionDate = user.AccountRevisionDate;
entity.RevisionDate = user.RevisionDate;
await dbContext.SaveChangesAsync();
// Update re-encrypted data
foreach (var action in updateDataActions)
{

View File

@ -1,6 +1,7 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using AutoMapper;
using Bit.Core.Auth.UserFeatures.UserKey;
using Bit.Core.Enums;
using Bit.Core.Utilities;
using Bit.Core.Vault.Enums;
@ -13,6 +14,7 @@ using Bit.Infrastructure.EntityFramework.Repositories.Vault.Queries;
using Bit.Infrastructure.EntityFramework.Vault.Models;
using Bit.Infrastructure.EntityFramework.Vault.Repositories.Queries;
using LinqToDB.EntityFrameworkCore;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using NS = Newtonsoft.Json;
@ -825,6 +827,34 @@ public class CipherRepository : Repository<Core.Vault.Entities.Cipher, Cipher, G
}
}
/// <inheritdoc />
public UpdateEncryptedDataForKeyRotation UpdateForKeyRotation(
Guid userId, IEnumerable<Core.Vault.Entities.Cipher> ciphers)
{
return async (SqlConnection _, SqlTransaction _) =>
{
var newCiphers = ciphers.ToList();
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var userCiphers = await GetDbSet(dbContext)
.Where(c => c.UserId == userId)
.ToListAsync();
var validCiphers = userCiphers
.Where(cipher => newCiphers.Any(newCipher => newCipher.Id == cipher.Id));
foreach (var cipher in validCiphers)
{
var updateCipher = newCiphers.First(newCipher => newCipher.Id == cipher.Id);
cipher.Data = updateCipher.Data;
cipher.Attachments = updateCipher.Attachments;
cipher.RevisionDate = updateCipher.RevisionDate;
cipher.Key = updateCipher.Key;
}
await dbContext.SaveChangesAsync();
};
}
public async Task UpdateUserKeysAndCiphersAsync(User user, IEnumerable<Core.Vault.Entities.Cipher> ciphers, IEnumerable<Core.Vault.Entities.Folder> folders, IEnumerable<Core.Tools.Entities.Send> sends)
{
using (var scope = ServiceScopeFactory.CreateScope())

View File

@ -1,7 +1,9 @@
using AutoMapper;
using Bit.Core.Auth.UserFeatures.UserKey;
using Bit.Core.Vault.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Infrastructure.EntityFramework.Vault.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
@ -36,4 +38,28 @@ public class FolderRepository : Repository<Core.Vault.Entities.Folder, Folder, G
return Mapper.Map<List<Core.Vault.Entities.Folder>>(folders);
}
}
/// <inheritdoc />
public UpdateEncryptedDataForKeyRotation UpdateForKeyRotation(
Guid userId, IEnumerable<Core.Vault.Entities.Folder> folders)
{
return async (SqlConnection _, SqlTransaction _) =>
{
var newFolders = folders.ToList();
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var userFolders = await GetDbSet(dbContext)
.Where(f => f.UserId == userId)
.ToListAsync();
var validFolders = userFolders
.Where(folder => newFolders.Any(newFolder => newFolder.Id == folder.Id));
foreach (var folder in validFolders)
{
var updateFolder = newFolders.First(newFolder => newFolder.Id == folder.Id);
folder.Name = updateFolder.Name;
}
await dbContext.SaveChangesAsync();
};
}
}