-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[SM-1591] Adding SecretVersion table to server #6406
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
Open
cd-bitwarden
wants to merge
17
commits into
main
Choose a base branch
from
SM-1591-HistoryOfSecrets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9daaa15
Adding SecretVersion table to server
cd-bitwarden 768eeb0
Merge branch 'main' into SM-1591-HistoryOfSecrets
cd-bitwarden 989f594
Merge branch 'main' into SM-1591-HistoryOfSecrets
cd-bitwarden 4fa76c9
making the names singular not plural for new table
cd-bitwarden 140724c
removing migration
cd-bitwarden 74e32db
fixing migration
cd-bitwarden 46d456e
Merge branch 'main' into SM-1591-HistoryOfSecrets
cd-bitwarden cf2b260
Merge branch 'main' into SM-1591-HistoryOfSecrets
cd-bitwarden 4405818
Adding indexes for serviceacct and orguserId
cd-bitwarden 95d2f96
indexes for sqllite
cd-bitwarden 7272a77
fixing migrations
cd-bitwarden 0dc1396
adding indexes to secretVeriosn.sql
cd-bitwarden e71b6ed
tests
cd-bitwarden 39576d7
removing tests
cd-bitwarden a7384e2
Merge branch 'main' into SM-1591-HistoryOfSecrets
cd-bitwarden 2d91b0c
adding GO
cd-bitwarden 58b5066
Merge branch 'SM-1591-HistoryOfSecrets' of https://github.com/bitwardโฆ
cd-bitwarden 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
๏ปฟ#nullable enable | ||
using Bit.Core.Entities; | ||
using Bit.Core.Utilities; | ||
|
||
namespace Bit.Core.SecretsManager.Entities; | ||
|
||
public class SecretVersion : ITableObject<Guid> | ||
{ | ||
public Guid Id { get; set; } | ||
|
||
public Guid SecretId { get; set; } | ||
|
||
public string Value { get; set; } = string.Empty; | ||
|
||
public DateTime VersionDate { get; set; } | ||
|
||
public Guid? EditorServiceAccountId { get; set; } | ||
|
||
public Guid? EditorOrganizationUserId { get; set; } | ||
|
||
public void SetNewId() | ||
{ | ||
if (Id == default(Guid)) | ||
{ | ||
Id = CoreHelpers.GenerateComb(); | ||
} | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
...ure.EntityFramework/SecretsManager/Configurations/SecretVersionEntityTypeConfiguration.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,42 @@ | ||
๏ปฟusing Bit.Infrastructure.EntityFramework.SecretsManager.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace Bit.Infrastructure.EntityFramework.SecretsManager.Configurations; | ||
|
||
public class SecretVersionEntityTypeConfiguration : IEntityTypeConfiguration<SecretVersion> | ||
{ | ||
public void Configure(EntityTypeBuilder<SecretVersion> builder) | ||
{ | ||
builder.Property(sv => sv.Id) | ||
.ValueGeneratedNever(); | ||
|
||
builder.HasKey(sv => sv.Id) | ||
.IsClustered(); | ||
|
||
builder.Property(sv => sv.Value) | ||
.IsRequired(); | ||
|
||
builder.Property(sv => sv.VersionDate) | ||
.IsRequired(); | ||
|
||
builder.HasOne(sv => sv.EditorServiceAccount) | ||
.WithMany() | ||
.HasForeignKey(sv => sv.EditorServiceAccountId) | ||
.OnDelete(DeleteBehavior.SetNull); | ||
|
||
builder.HasOne(sv => sv.EditorOrganizationUser) | ||
.WithMany() | ||
.HasForeignKey(sv => sv.EditorOrganizationUserId) | ||
.OnDelete(DeleteBehavior.SetNull); | ||
|
||
builder.HasIndex(sv => sv.SecretId) | ||
.HasDatabaseName("IX_SecretVersion_SecretId"); | ||
|
||
builder.HasIndex(sv => sv.EditorServiceAccountId) | ||
.HasDatabaseName("IX_SecretVersion_EditorServiceAccountId"); | ||
|
||
builder.HasIndex(sv => sv.EditorOrganizationUserId) | ||
.HasDatabaseName("IX_SecretVersion_EditorOrganizationUserId"); | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
src/Infrastructure.EntityFramework/SecretsManager/Models/SecretVersion.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,24 @@ | ||
๏ปฟ#nullable enable | ||
|
||
using AutoMapper; | ||
|
||
namespace Bit.Infrastructure.EntityFramework.SecretsManager.Models; | ||
|
||
public class SecretVersion : Core.SecretsManager.Entities.SecretVersion | ||
{ | ||
public Secret? Secret { get; set; } | ||
|
||
public ServiceAccount? EditorServiceAccount { get; set; } | ||
|
||
public Bit.Infrastructure.EntityFramework.Models.OrganizationUser? EditorOrganizationUser { get; set; } | ||
} | ||
|
||
public class SecretVersionMapperProfile : Profile | ||
{ | ||
public SecretVersionMapperProfile() | ||
{ | ||
CreateMap<Core.SecretsManager.Entities.SecretVersion, SecretVersion>() | ||
.PreserveReferences() | ||
.ReverseMap(); | ||
} | ||
} |
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,27 @@ | ||
CREATE TABLE [dbo].[SecretVersion] ( | ||
[Id] UNIQUEIDENTIFIER NOT NULL, | ||
[SecretId] UNIQUEIDENTIFIER NOT NULL, | ||
[Value] NVARCHAR (MAX) NOT NULL, | ||
[VersionDate] DATETIME2 (7) NOT NULL, | ||
[EditorServiceAccountId] UNIQUEIDENTIFIER NULL, | ||
[EditorOrganizationUserId] UNIQUEIDENTIFIER NULL, | ||
CONSTRAINT [PK_SecretVersion] PRIMARY KEY CLUSTERED ([Id] ASC), | ||
CONSTRAINT [FK_SecretVersion_OrganizationUser] FOREIGN KEY ([EditorOrganizationUserId]) REFERENCES [dbo].[OrganizationUser] ([Id]) ON DELETE SET NULL, | ||
CONSTRAINT [FK_SecretVersion_Secret] FOREIGN KEY ([SecretId]) REFERENCES [dbo].[Secret] ([Id]) ON DELETE CASCADE, | ||
CONSTRAINT [FK_SecretVersion_ServiceAccount] FOREIGN KEY ([EditorServiceAccountId]) REFERENCES [dbo].[ServiceAccount] ([Id]) ON DELETE SET NULL | ||
); | ||
|
||
GO | ||
CREATE NONCLUSTERED INDEX [IX_SecretVersion_SecretId] | ||
ON [dbo].[SecretVersion]([SecretId] ASC); | ||
|
||
GO | ||
CREATE NONCLUSTERED INDEX [IX_SecretVersion_EditorServiceAccountId] | ||
ON [dbo].[SecretVersion]([EditorServiceAccountId] ASC) | ||
WHERE [EditorServiceAccountId] IS NOT NULL; | ||
|
||
GO | ||
CREATE NONCLUSTERED INDEX [IX_SecretVersion_EditorOrganizationUserId] | ||
ON [dbo].[SecretVersion]([EditorOrganizationUserId] ASC) | ||
WHERE [EditorOrganizationUserId] IS NOT NULL; | ||
GO |
83 changes: 83 additions & 0 deletions
83
util/Migrator/DbScripts/2025-09-26_00_SM_AddSecretVersioningTable.sql
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,83 @@ | ||
SET ANSI_NULLS ON; | ||
SET QUOTED_IDENTIFIER ON; | ||
GO | ||
|
||
IF NOT EXISTS (SELECT 1 FROM sys.tables WHERE name = 'SecretVersion' AND schema_id = SCHEMA_ID('dbo')) | ||
BEGIN | ||
CREATE TABLE [dbo].[SecretVersion] ( | ||
[Id] UNIQUEIDENTIFIER NOT NULL, | ||
[SecretId] UNIQUEIDENTIFIER NOT NULL, | ||
[Value] NVARCHAR (MAX) NOT NULL, | ||
[VersionDate] DATETIME2 (7) NOT NULL, | ||
[EditorServiceAccountId] UNIQUEIDENTIFIER NULL, | ||
[EditorOrganizationUserId] UNIQUEIDENTIFIER NULL, | ||
CONSTRAINT [PK_SecretVersion] PRIMARY KEY CLUSTERED ([Id] ASC) | ||
); | ||
END | ||
GO | ||
|
||
-- Ensure foreign keys exist | ||
IF NOT EXISTS ( | ||
SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_SecretVersion_OrganizationUser' | ||
) | ||
BEGIN | ||
ALTER TABLE [dbo].[SecretVersion] | ||
ADD CONSTRAINT [FK_SecretVersion_OrganizationUser] | ||
FOREIGN KEY ([EditorOrganizationUserId]) | ||
REFERENCES [dbo].[OrganizationUser] ([Id]) | ||
ON DELETE SET NULL; | ||
END | ||
GO | ||
|
||
IF NOT EXISTS ( | ||
SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_SecretVersion_Secret' | ||
) | ||
BEGIN | ||
ALTER TABLE [dbo].[SecretVersion] | ||
ADD CONSTRAINT [FK_SecretVersion_Secret] | ||
FOREIGN KEY ([SecretId]) | ||
REFERENCES [dbo].[Secret] ([Id]) | ||
ON DELETE CASCADE; | ||
END | ||
GO | ||
|
||
IF NOT EXISTS ( | ||
SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_SecretVersion_ServiceAccount' | ||
) | ||
BEGIN | ||
ALTER TABLE [dbo].[SecretVersion] | ||
ADD CONSTRAINT [FK_SecretVersion_ServiceAccount] | ||
FOREIGN KEY ([EditorServiceAccountId]) | ||
REFERENCES [dbo].[ServiceAccount] ([Id]) | ||
ON DELETE SET NULL; | ||
END | ||
GO | ||
|
||
IF NOT EXISTS ( | ||
SELECT 1 FROM sys.indexes WHERE name = 'IX_SecretVersion_SecretId' AND object_id = OBJECT_ID('[dbo].[SecretVersion]') | ||
) | ||
BEGIN | ||
CREATE NONCLUSTERED INDEX [IX_SecretVersion_SecretId] | ||
cd-bitwarden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ON [dbo].[SecretVersion]([SecretId] ASC); | ||
END | ||
GO | ||
|
||
IF NOT EXISTS ( | ||
SELECT 1 FROM sys.indexes WHERE name = 'IX_SecretVersion_EditorServiceAccountId' AND object_id = OBJECT_ID('[dbo].[SecretVersion]') | ||
) | ||
BEGIN | ||
CREATE NONCLUSTERED INDEX [IX_SecretVersion_EditorServiceAccountId] | ||
ON [dbo].[SecretVersion]([EditorServiceAccountId] ASC) | ||
WHERE [EditorServiceAccountId] IS NOT NULL; | ||
END | ||
GO | ||
|
||
IF NOT EXISTS ( | ||
SELECT 1 FROM sys.indexes WHERE name = 'IX_SecretVersion_EditorOrganizationUserId' AND object_id = OBJECT_ID('[dbo].[SecretVersion]') | ||
) | ||
BEGIN | ||
CREATE NONCLUSTERED INDEX [IX_SecretVersion_EditorOrganizationUserId] | ||
ON [dbo].[SecretVersion]([EditorOrganizationUserId] ASC) | ||
WHERE [EditorOrganizationUserId] IS NOT NULL; | ||
END | ||
GO |
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.
๐ฑ Just wanted to point out that SQL Server is using filtered (partial) indexes while the EF migrations are using non-filtered indexes. Not really a big issue but something to keep in mind on whether we want to keep things as consistent as possible between environments. Mysql/Mariadb does not currently support filtered indexes (without a workaround) so we wouldn't be able to have all database platforms aligned 100% anyway.