mirror of
https://github.com/bitwarden/server
synced 2026-01-04 17:43:53 +00:00
[SM-1591] Adding SecretVersion table to server (#6406)
* Adding SecretVersion table to server * making the names singular not plural for new table * removing migration * fixing migration * Adding indexes for serviceacct and orguserId * indexes for sqllite * fixing migrations * adding indexes to secretVeriosn.sql * tests * removing tests * adding GO
This commit is contained in:
28
src/Core/SecretsManager/Entities/SecretVersion.cs
Normal file
28
src/Core/SecretsManager/Entities/SecretVersion.cs
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,6 +63,7 @@ public class DatabaseContext : DbContext
|
||||
public DbSet<Policy> Policies { get; set; }
|
||||
public DbSet<Provider> Providers { get; set; }
|
||||
public DbSet<Secret> Secret { get; set; }
|
||||
public DbSet<SecretVersion> SecretVersion { get; set; }
|
||||
public DbSet<ServiceAccount> ServiceAccount { get; set; }
|
||||
public DbSet<Project> Project { get; set; }
|
||||
public DbSet<ProviderUser> ProviderUsers { get; set; }
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ public class Secret : Core.SecretsManager.Entities.Secret
|
||||
public virtual ICollection<UserSecretAccessPolicy> UserAccessPolicies { get; set; }
|
||||
public virtual ICollection<GroupSecretAccessPolicy> GroupAccessPolicies { get; set; }
|
||||
public virtual ICollection<ServiceAccountSecretAccessPolicy> ServiceAccountAccessPolicies { get; set; }
|
||||
public virtual ICollection<SecretVersion> SecretVersions { get; set; }
|
||||
}
|
||||
|
||||
public class SecretMapperProfile : Profile
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
27
src/Sql/dbo/SecretsManager/Tables/SecretVersion.sql
Normal file
27
src/Sql/dbo/SecretsManager/Tables/SecretVersion.sql
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user