mirror of
https://github.com/bitwarden/server.git
synced 2024-11-28 13:15:12 +01:00
6f04298e17
* ServerProtectedData for user entity * remove using statements * formatting * use data protection libs * no async * add data protection to ef user repo * switch to `SetApplicationName` per ASPNET docs * null checks * cleanup * value converter for EF * new line at eof * fix using * remove folder ref * restore ctor * fix lint * use global constant * UseApplicationServiceProvider for integration tests * implement constant for DatabaseFieldProtectedPrefix * Fix EF IntegrationTest * restore original values after protect and save * lint fixes * Use Constants Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>
54 lines
2.2 KiB
C#
54 lines
2.2 KiB
C#
using System.Text;
|
|
using Bit.Core;
|
|
using Bit.Core.Test.Helpers.Factories;
|
|
using Bit.Infrastructure.EntityFramework.Repositories;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Moq;
|
|
|
|
namespace Bit.Infrastructure.EFIntegration.Test.Helpers;
|
|
|
|
public static class DatabaseOptionsFactory
|
|
{
|
|
public static List<DbContextOptions<DatabaseContext>> Options { get; } = new();
|
|
|
|
static DatabaseOptionsFactory()
|
|
{
|
|
var services = new ServiceCollection()
|
|
.AddSingleton(sp =>
|
|
{
|
|
var dataProtector = new Mock<IDataProtector>();
|
|
dataProtector
|
|
.Setup(d => d.Unprotect(It.IsAny<byte[]>()))
|
|
.Returns<byte[]>(data => Encoding.UTF8.GetBytes(Constants.DatabaseFieldProtectedPrefix + Encoding.UTF8.GetString(data)));
|
|
|
|
var dataProtectionProvider = new Mock<IDataProtectionProvider>();
|
|
dataProtectionProvider
|
|
.Setup(x => x.CreateProtector(Constants.DatabaseFieldProtectorPurpose))
|
|
.Returns(dataProtector.Object);
|
|
|
|
return dataProtectionProvider.Object;
|
|
})
|
|
.BuildServiceProvider();
|
|
|
|
var globalSettings = GlobalSettingsFactory.GlobalSettings;
|
|
if (!string.IsNullOrWhiteSpace(GlobalSettingsFactory.GlobalSettings.PostgreSql?.ConnectionString))
|
|
{
|
|
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
|
Options.Add(new DbContextOptionsBuilder<DatabaseContext>()
|
|
.UseNpgsql(globalSettings.PostgreSql.ConnectionString)
|
|
.UseApplicationServiceProvider(services)
|
|
.Options);
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(GlobalSettingsFactory.GlobalSettings.MySql?.ConnectionString))
|
|
{
|
|
var mySqlConnectionString = globalSettings.MySql.ConnectionString;
|
|
Options.Add(new DbContextOptionsBuilder<DatabaseContext>()
|
|
.UseMySql(mySqlConnectionString, ServerVersion.AutoDetect(mySqlConnectionString))
|
|
.UseApplicationServiceProvider(services)
|
|
.Options);
|
|
}
|
|
}
|
|
}
|