mirror of
https://github.com/bitwarden/server.git
synced 2025-07-03 00:52:49 -05:00
Postgres & MySql Support For Self-Hosted Installations (#1386)
* EF Database Support Init (#1221) * scaffolding for ef support * deleted old postgres repos * added tables to oncreate * updated all the things to .NET 5 * Addition to #1221: Migrated DockerFiles from dotnet/3.1 to 5.0 (#1223) * Migrated DockerFiles from dotnet/3.1 to 5.0 * Migrated SSO/Dockerfile from dotnet 3.1 to 5.0 Co-authored-by: Daniel James Smith <djsmith85@users.noreply.github.com> * EFDatabaseSupport: Updated links and description in README.md and SETUP.md (#1232) * Updated requirements in README.md * Updated link to documentation of app-secrets * upgraded dotnet version to 5.0 * Ef database support implementation examples (#1265) * mostly finished testing the user repo * finished testing user repo * finished org, user, ssoconfig, and ssouser ef implementations * removed unused prop * fixed a sql file * fixed a spacing issue * fixed a spacing issue * removed extra database creation * refactoring * MsSql => SqlServer * refactoring * code review fixes * build fix * code review * continued attempts to fix the the build * skipped another test * finished all create test * initial pass at several repos * continued building out repos * initial pass at several repos * initial pass at device repo * initial pass at collection repo * initial run of all Entity Framework implementations * signup, signin, create/edit ciphers works * sync working * all web vault pages seem to load with 100% 200s * bulkcopy, folders, and favorites * group and collection management * sso, groups, emergency access, send * get basic creates matching on all repos * got everything building again post merge * removed some IDE config files * cleanup * no more notimplemented methods in the cipher repo * no more not implementeds everywhere * cleaned up schema/navigation properties and fixed tests * removed a sql comment that was written in c# style * fixed build issues from merge * removed unsupported db providers * formatting * code review refactors * naming cleanup for queries * added provider methods * cipher repo cleanup * implemented several missing procedures from the EF implementation surround account revision dates, keys, and storage * fixed the build * added a null check * consolidated some cipher repo methods * formatting fix * cleaned up indentation of queries * removed .idea file * generated postgres migrations * added mysql migrations * formatting * Bug Fixes & Formatting * Formatting * fixed a bug with bulk import when using MySql * code review fixes * fixed the build * implemented new methods * formatting * fixed the build * cleaned up select statements in ef queries * formatting * formatting * formatting Co-authored-by: Daniel James Smith <djsmith85@users.noreply.github.com>
This commit is contained in:
@ -18,6 +18,7 @@ using Bit.Core.Utilities;
|
||||
using IdentityModel;
|
||||
using IdentityServer4.AccessTokenValidation;
|
||||
using IdentityServer4.Configuration;
|
||||
using LinqToDB.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
@ -46,56 +47,108 @@ namespace Bit.Core.Utilities
|
||||
{
|
||||
public static void AddSqlServerRepositories(this IServiceCollection services, GlobalSettings globalSettings)
|
||||
{
|
||||
var usePostgreSql = CoreHelpers.SettingHasValue(globalSettings.PostgreSql?.ConnectionString);
|
||||
var useEf = usePostgreSql;
|
||||
var selectedDatabaseProvider = globalSettings.DatabaseProvider;
|
||||
var provider = SupportedDatabaseProviders.SqlServer;
|
||||
var connectionString = string.Empty;
|
||||
if (!string.IsNullOrWhiteSpace(selectedDatabaseProvider))
|
||||
{
|
||||
switch (selectedDatabaseProvider.ToLowerInvariant())
|
||||
{
|
||||
case "postgres":
|
||||
case "postgresql":
|
||||
provider = SupportedDatabaseProviders.Postgres;
|
||||
connectionString = globalSettings.PostgreSql.ConnectionString;
|
||||
break;
|
||||
case "mysql":
|
||||
case "mariadb":
|
||||
provider = SupportedDatabaseProviders.MySql;
|
||||
connectionString = globalSettings.MySql.ConnectionString;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var useEf = (provider != SupportedDatabaseProviders.SqlServer);
|
||||
|
||||
if (useEf)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(connectionString))
|
||||
{
|
||||
throw new Exception($"Database provider type {provider} was selected but no connection string was found.");
|
||||
}
|
||||
LinqToDBForEFTools.Initialize();
|
||||
services.AddAutoMapper(typeof(EntityFrameworkRepos.UserRepository));
|
||||
services.AddDbContext<EntityFrameworkRepos.DatabaseContext>(options =>
|
||||
{
|
||||
if (usePostgreSql)
|
||||
if (provider == SupportedDatabaseProviders.Postgres)
|
||||
{
|
||||
options.UseNpgsql(globalSettings.PostgreSql.ConnectionString);
|
||||
options.UseNpgsql(connectionString);
|
||||
}
|
||||
else if (provider == SupportedDatabaseProviders.MySql)
|
||||
{
|
||||
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
|
||||
}
|
||||
});
|
||||
services.AddSingleton<ICipherRepository, EntityFrameworkRepos.CipherRepository>();
|
||||
services.AddSingleton<ICollectionCipherRepository, EntityFrameworkRepos.CollectionCipherRepository>();
|
||||
services.AddSingleton<ICollectionRepository, EntityFrameworkRepos.CollectionRepository>();
|
||||
services.AddSingleton<IDeviceRepository, EntityFrameworkRepos.DeviceRepository>();
|
||||
services.AddSingleton<IEmergencyAccessRepository, EntityFrameworkRepos.EmergencyAccessRepository>();
|
||||
services.AddSingleton<IFolderRepository, EntityFrameworkRepos.FolderRepository>();
|
||||
services.AddSingleton<IGrantRepository, EntityFrameworkRepos.GrantRepository>();
|
||||
services.AddSingleton<IGroupRepository, EntityFrameworkRepos.GroupRepository>();
|
||||
services.AddSingleton<IInstallationRepository, EntityFrameworkRepos.InstallationRepository>();
|
||||
services.AddSingleton<IMaintenanceRepository, EntityFrameworkRepos.MaintenanceRepository>();
|
||||
services.AddSingleton<IOrganizationRepository, EntityFrameworkRepos.OrganizationRepository>();
|
||||
services.AddSingleton<IOrganizationUserRepository, EntityFrameworkRepos.OrganizationUserRepository>();
|
||||
services.AddSingleton<IPolicyRepository, EntityFrameworkRepos.PolicyRepository>();
|
||||
services.AddSingleton<ISendRepository, EntityFrameworkRepos.SendRepository>();
|
||||
services.AddSingleton<ISsoConfigRepository, EntityFrameworkRepos.SsoConfigRepository>();
|
||||
services.AddSingleton<ISsoUserRepository, EntityFrameworkRepos.SsoUserRepository>();
|
||||
services.AddSingleton<ITaxRateRepository, EntityFrameworkRepos.TaxRateRepository>();
|
||||
services.AddSingleton<ITransactionRepository, EntityFrameworkRepos.TransactionRepository>();
|
||||
services.AddSingleton<IU2fRepository, EntityFrameworkRepos.U2fRepository>();
|
||||
services.AddSingleton<IUserRepository, EntityFrameworkRepos.UserRepository>();
|
||||
//services.AddSingleton<ICipherRepository, EntityFrameworkRepos.CipherRepository>();
|
||||
//services.AddSingleton<IOrganizationRepository, EntityFrameworkRepos.OrganizationRepository>();
|
||||
services.AddSingleton<IProviderRepository, EntityFrameworkRepos.ProviderRepository>();
|
||||
services.AddSingleton<IProviderUserRepository, EntityFrameworkRepos.ProviderUserRepository>();
|
||||
services.AddSingleton<IProviderOrganizationRepository, EntityFrameworkRepos.ProviderOrganizationRepository>();
|
||||
services.AddSingleton<IProviderOrganizationProviderUserRepository, EntityFrameworkRepos.ProviderOrganizationProviderUserRepository>();
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddSingleton<IUserRepository, SqlServerRepos.UserRepository>();
|
||||
services.AddSingleton<ICipherRepository, SqlServerRepos.CipherRepository>();
|
||||
services.AddSingleton<IDeviceRepository, SqlServerRepos.DeviceRepository>();
|
||||
services.AddSingleton<IGrantRepository, SqlServerRepos.GrantRepository>();
|
||||
services.AddSingleton<IOrganizationRepository, SqlServerRepos.OrganizationRepository>();
|
||||
services.AddSingleton<IOrganizationUserRepository, SqlServerRepos.OrganizationUserRepository>();
|
||||
services.AddSingleton<ICollectionRepository, SqlServerRepos.CollectionRepository>();
|
||||
services.AddSingleton<IFolderRepository, SqlServerRepos.FolderRepository>();
|
||||
services.AddSingleton<ICollectionCipherRepository, SqlServerRepos.CollectionCipherRepository>();
|
||||
services.AddSingleton<ICollectionRepository, SqlServerRepos.CollectionRepository>();
|
||||
services.AddSingleton<IDeviceRepository, SqlServerRepos.DeviceRepository>();
|
||||
services.AddSingleton<IEmergencyAccessRepository, SqlServerRepos.EmergencyAccessRepository>();
|
||||
services.AddSingleton<IFolderRepository, SqlServerRepos.FolderRepository>();
|
||||
services.AddSingleton<IGrantRepository, SqlServerRepos.GrantRepository>();
|
||||
services.AddSingleton<IGroupRepository, SqlServerRepos.GroupRepository>();
|
||||
services.AddSingleton<IU2fRepository, SqlServerRepos.U2fRepository>();
|
||||
services.AddSingleton<IInstallationRepository, SqlServerRepos.InstallationRepository>();
|
||||
services.AddSingleton<IMaintenanceRepository, SqlServerRepos.MaintenanceRepository>();
|
||||
services.AddSingleton<ITransactionRepository, SqlServerRepos.TransactionRepository>();
|
||||
services.AddSingleton<IOrganizationRepository, SqlServerRepos.OrganizationRepository>();
|
||||
services.AddSingleton<IOrganizationUserRepository, SqlServerRepos.OrganizationUserRepository>();
|
||||
services.AddSingleton<IPolicyRepository, SqlServerRepos.PolicyRepository>();
|
||||
services.AddSingleton<ISendRepository, SqlServerRepos.SendRepository>();
|
||||
services.AddSingleton<ISsoConfigRepository, SqlServerRepos.SsoConfigRepository>();
|
||||
services.AddSingleton<ISsoUserRepository, SqlServerRepos.SsoUserRepository>();
|
||||
services.AddSingleton<ISendRepository, SqlServerRepos.SendRepository>();
|
||||
services.AddSingleton<ITaxRateRepository, SqlServerRepos.TaxRateRepository>();
|
||||
services.AddSingleton<IEmergencyAccessRepository, SqlServerRepos.EmergencyAccessRepository>();
|
||||
services.AddSingleton<IProviderRepository, SqlServerRepos.ProviderRepository>();
|
||||
services.AddSingleton<IProviderUserRepository, SqlServerRepos.ProviderUserRepository>();
|
||||
services.AddSingleton<IProviderOrganizationRepository, SqlServerRepos.ProviderOrganizationRepository>();
|
||||
services.AddSingleton<IProviderOrganizationProviderUserRepository, SqlServerRepos.ProviderOrganizationProviderUserRepository>();
|
||||
services.AddSingleton<ITransactionRepository, SqlServerRepos.TransactionRepository>();
|
||||
services.AddSingleton<IU2fRepository, SqlServerRepos.U2fRepository>();
|
||||
services.AddSingleton<IUserRepository, SqlServerRepos.UserRepository>();
|
||||
}
|
||||
|
||||
if (globalSettings.SelfHosted)
|
||||
{
|
||||
if (useEf)
|
||||
{
|
||||
// TODO
|
||||
services.AddSingleton<IEventRepository, EntityFrameworkRepos.EventRepository>();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Reference in New Issue
Block a user