mirror of
https://github.com/bitwarden/server
synced 2025-12-13 14:53:34 +00:00
Bitwarden Unified Self-Host project (#2410)
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\util\MySqlMigrations\MySqlMigrations.csproj" />
|
||||
<ProjectReference Include="..\..\util\PostgresMigrations\PostgresMigrations.csproj" />
|
||||
<ProjectReference Include="..\SharedWeb\SharedWeb.csproj" />
|
||||
<ProjectReference Include="..\..\util\Migrator\Migrator.csproj" />
|
||||
<ProjectReference Include="..\Core\Core.csproj" />
|
||||
|
||||
@@ -1,25 +1,19 @@
|
||||
using System.Data.SqlClient;
|
||||
using Bit.Core.Jobs;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Migrator;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Admin.HostedServices;
|
||||
|
||||
public class DatabaseMigrationHostedService : IHostedService, IDisposable
|
||||
{
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly ILogger<DatabaseMigrationHostedService> _logger;
|
||||
private readonly DbMigrator _dbMigrator;
|
||||
private readonly IDbMigrator _dbMigrator;
|
||||
|
||||
public DatabaseMigrationHostedService(
|
||||
GlobalSettings globalSettings,
|
||||
ILogger<DatabaseMigrationHostedService> logger,
|
||||
ILogger<DbMigrator> migratorLogger,
|
||||
ILogger<JobListener> listenerLogger)
|
||||
IDbMigrator dbMigrator,
|
||||
ILogger<DatabaseMigrationHostedService> logger)
|
||||
{
|
||||
_globalSettings = globalSettings;
|
||||
_logger = logger;
|
||||
_dbMigrator = new DbMigrator(globalSettings.SqlServer.ConnectionString, migratorLogger);
|
||||
_dbMigrator = dbMigrator;
|
||||
}
|
||||
|
||||
public virtual async Task StartAsync(CancellationToken cancellationToken)
|
||||
@@ -32,7 +26,7 @@ public class DatabaseMigrationHostedService : IHostedService, IDisposable
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbMigrator.MigrateMsSqlDatabase(true, cancellationToken);
|
||||
_dbMigrator.MigrateDatabase(true, cancellationToken);
|
||||
// TODO: Maybe flip a flag somewhere to indicate migration is complete??
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,21 @@ public class Startup
|
||||
StripeConfiguration.MaxNetworkRetries = globalSettings.Stripe.MaxNetworkRetries;
|
||||
|
||||
// Repositories
|
||||
services.AddSqlServerRepositories(globalSettings);
|
||||
var databaseProvider = services.AddDatabaseRepositories(globalSettings);
|
||||
switch (databaseProvider)
|
||||
{
|
||||
case Core.Enums.SupportedDatabaseProviders.SqlServer:
|
||||
services.AddSingleton<IDbMigrator, Migrator.SqlServerDbMigrator>();
|
||||
break;
|
||||
case Core.Enums.SupportedDatabaseProviders.MySql:
|
||||
services.AddSingleton<IDbMigrator, MySqlMigrations.MySqlDbMigrator>();
|
||||
break;
|
||||
case Core.Enums.SupportedDatabaseProviders.Postgres:
|
||||
services.AddSingleton<IDbMigrator, PostgresMigrations.PostgresDbMigrator>();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Context
|
||||
services.AddScoped<ICurrentContext, CurrentContext>();
|
||||
|
||||
@@ -3211,82 +3211,96 @@
|
||||
"commercial.core": {
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"Core": "2022.8.4"
|
||||
"Core": "[2022.10.0, )"
|
||||
}
|
||||
},
|
||||
"core": {
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"AWSSDK.SQS": "3.7.2.47",
|
||||
"AWSSDK.SimpleEmail": "3.7.0.150",
|
||||
"AspNetCoreRateLimit": "4.0.2",
|
||||
"AspNetCoreRateLimit.Redis": "1.0.1",
|
||||
"Azure.Extensions.AspNetCore.DataProtection.Blobs": "1.2.1",
|
||||
"Azure.Storage.Blobs": "12.11.0",
|
||||
"Azure.Storage.Queues": "12.9.0",
|
||||
"BitPay.Light": "1.0.1907",
|
||||
"Braintree": "5.12.0",
|
||||
"Fido2.AspNet": "3.0.0-beta2",
|
||||
"Handlebars.Net": "2.1.2",
|
||||
"IdentityServer4": "4.1.2",
|
||||
"IdentityServer4.AccessTokenValidation": "3.0.1",
|
||||
"MailKit": "3.2.0",
|
||||
"Microsoft.AspNetCore.Authentication.JwtBearer": "6.0.4",
|
||||
"Microsoft.Azure.Cosmos.Table": "1.0.8",
|
||||
"Microsoft.Azure.NotificationHubs": "4.1.0",
|
||||
"Microsoft.Azure.ServiceBus": "5.2.0",
|
||||
"Microsoft.Data.SqlClient": "4.1.0",
|
||||
"Microsoft.Extensions.Caching.StackExchangeRedis": "6.0.6",
|
||||
"Microsoft.Extensions.Configuration.EnvironmentVariables": "6.0.1",
|
||||
"Microsoft.Extensions.Configuration.UserSecrets": "6.0.1",
|
||||
"Microsoft.Extensions.Identity.Stores": "6.0.4",
|
||||
"Newtonsoft.Json": "13.0.1",
|
||||
"Otp.NET": "1.2.2",
|
||||
"Quartz": "3.4.0",
|
||||
"SendGrid": "9.27.0",
|
||||
"Sentry.Serilog": "3.16.0",
|
||||
"Serilog.AspNetCore": "5.0.0",
|
||||
"Serilog.Extensions.Logging": "3.1.0",
|
||||
"Serilog.Extensions.Logging.File": "2.0.0",
|
||||
"Serilog.Sinks.AzureCosmosDB": "2.0.0",
|
||||
"Serilog.Sinks.SyslogMessages": "2.0.6",
|
||||
"Stripe.net": "40.0.0",
|
||||
"YubicoDotNetClient": "1.2.0"
|
||||
"AWSSDK.SQS": "[3.7.2.47, )",
|
||||
"AWSSDK.SimpleEmail": "[3.7.0.150, )",
|
||||
"AspNetCoreRateLimit": "[4.0.2, )",
|
||||
"AspNetCoreRateLimit.Redis": "[1.0.1, )",
|
||||
"Azure.Extensions.AspNetCore.DataProtection.Blobs": "[1.2.1, )",
|
||||
"Azure.Storage.Blobs": "[12.11.0, )",
|
||||
"Azure.Storage.Queues": "[12.9.0, )",
|
||||
"BitPay.Light": "[1.0.1907, )",
|
||||
"Braintree": "[5.12.0, )",
|
||||
"Fido2.AspNet": "[3.0.0-beta2, )",
|
||||
"Handlebars.Net": "[2.1.2, )",
|
||||
"IdentityServer4": "[4.1.2, )",
|
||||
"IdentityServer4.AccessTokenValidation": "[3.0.1, )",
|
||||
"MailKit": "[3.2.0, )",
|
||||
"Microsoft.AspNetCore.Authentication.JwtBearer": "[6.0.4, )",
|
||||
"Microsoft.Azure.Cosmos.Table": "[1.0.8, )",
|
||||
"Microsoft.Azure.NotificationHubs": "[4.1.0, )",
|
||||
"Microsoft.Azure.ServiceBus": "[5.2.0, )",
|
||||
"Microsoft.Data.SqlClient": "[4.1.0, )",
|
||||
"Microsoft.Extensions.Caching.StackExchangeRedis": "[6.0.6, )",
|
||||
"Microsoft.Extensions.Configuration.EnvironmentVariables": "[6.0.1, )",
|
||||
"Microsoft.Extensions.Configuration.UserSecrets": "[6.0.1, )",
|
||||
"Microsoft.Extensions.Identity.Stores": "[6.0.4, )",
|
||||
"Newtonsoft.Json": "[13.0.1, )",
|
||||
"Otp.NET": "[1.2.2, )",
|
||||
"Quartz": "[3.4.0, )",
|
||||
"SendGrid": "[9.27.0, )",
|
||||
"Sentry.Serilog": "[3.16.0, )",
|
||||
"Serilog.AspNetCore": "[5.0.0, )",
|
||||
"Serilog.Extensions.Logging": "[3.1.0, )",
|
||||
"Serilog.Extensions.Logging.File": "[2.0.0, )",
|
||||
"Serilog.Sinks.AzureCosmosDB": "[2.0.0, )",
|
||||
"Serilog.Sinks.SyslogMessages": "[2.0.6, )",
|
||||
"Stripe.net": "[40.0.0, )",
|
||||
"YubicoDotNetClient": "[1.2.0, )"
|
||||
}
|
||||
},
|
||||
"infrastructure.dapper": {
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"Core": "2022.8.4",
|
||||
"Dapper": "2.0.123",
|
||||
"System.Data.SqlClient": "4.8.3"
|
||||
"Core": "[2022.10.0, )",
|
||||
"Dapper": "[2.0.123, )",
|
||||
"System.Data.SqlClient": "[4.8.3, )"
|
||||
}
|
||||
},
|
||||
"infrastructure.entityframework": {
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"AutoMapper.Extensions.Microsoft.DependencyInjection": "11.0.0",
|
||||
"Core": "2022.8.4",
|
||||
"Microsoft.EntityFrameworkCore.Relational": "6.0.4",
|
||||
"Npgsql.EntityFrameworkCore.PostgreSQL": "6.0.4",
|
||||
"Pomelo.EntityFrameworkCore.MySql": "6.0.1",
|
||||
"linq2db.EntityFrameworkCore": "6.7.1"
|
||||
"AutoMapper.Extensions.Microsoft.DependencyInjection": "[11.0.0, )",
|
||||
"Core": "[2022.10.0, )",
|
||||
"Microsoft.EntityFrameworkCore.Relational": "[6.0.4, )",
|
||||
"Npgsql.EntityFrameworkCore.PostgreSQL": "[6.0.4, )",
|
||||
"Pomelo.EntityFrameworkCore.MySql": "[6.0.1, )",
|
||||
"linq2db.EntityFrameworkCore": "[6.7.1, )"
|
||||
}
|
||||
},
|
||||
"migrator": {
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"Core": "2022.8.4",
|
||||
"Microsoft.Extensions.Logging": "6.0.0",
|
||||
"dbup-sqlserver": "4.5.0"
|
||||
"Core": "[2022.10.0, )",
|
||||
"Microsoft.Extensions.Logging": "[6.0.0, )",
|
||||
"dbup-sqlserver": "[4.5.0, )"
|
||||
}
|
||||
},
|
||||
"mysqlmigrations": {
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"Core": "[2022.10.0, )",
|
||||
"Infrastructure.EntityFramework": "[2022.10.0, )"
|
||||
}
|
||||
},
|
||||
"postgresmigrations": {
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"Core": "[2022.10.0, )",
|
||||
"Infrastructure.EntityFramework": "[2022.10.0, )"
|
||||
}
|
||||
},
|
||||
"sharedweb": {
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"Core": "2022.8.4",
|
||||
"Infrastructure.Dapper": "2022.8.4",
|
||||
"Infrastructure.EntityFramework": "2022.8.4"
|
||||
"Core": "[2022.10.0, )",
|
||||
"Infrastructure.Dapper": "[2022.10.0, )",
|
||||
"Infrastructure.EntityFramework": "[2022.10.0, )"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ public class Startup
|
||||
StripeConfiguration.MaxNetworkRetries = globalSettings.Stripe.MaxNetworkRetries;
|
||||
|
||||
// Repositories
|
||||
services.AddSqlServerRepositories(globalSettings);
|
||||
services.AddDatabaseRepositories(globalSettings);
|
||||
|
||||
// Context
|
||||
services.AddScoped<ICurrentContext, CurrentContext>();
|
||||
|
||||
@@ -34,7 +34,7 @@ public class Startup
|
||||
StripeConfiguration.MaxNetworkRetries = globalSettings.Stripe.MaxNetworkRetries;
|
||||
|
||||
// Repositories
|
||||
services.AddSqlServerRepositories(globalSettings);
|
||||
services.AddDatabaseRepositories(globalSettings);
|
||||
|
||||
// PayPal Client
|
||||
services.AddSingleton<Utilities.PayPalIpnClient>();
|
||||
|
||||
@@ -24,6 +24,7 @@ public class GlobalSettings : IGlobalSettings
|
||||
get => BuildDirectory(_logDirectory, "/logs");
|
||||
set => _logDirectory = value;
|
||||
}
|
||||
public virtual bool LogDirectoryByProject { get; set; } = true;
|
||||
public virtual long? LogRollBySizeLimit { get; set; }
|
||||
public virtual bool EnableDevLogging { get; set; } = false;
|
||||
public virtual string LicenseDirectory
|
||||
|
||||
7
src/Core/Utilities/IDbMigrator.cs
Normal file
7
src/Core/Utilities/IDbMigrator.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Bit.Core.Utilities;
|
||||
|
||||
public interface IDbMigrator
|
||||
{
|
||||
bool MigrateDatabase(bool enableLogging = true,
|
||||
CancellationToken cancellationToken = default(CancellationToken));
|
||||
}
|
||||
@@ -125,13 +125,22 @@ public static class LoggerFactoryExtensions
|
||||
{
|
||||
if (globalSettings.LogRollBySizeLimit.HasValue)
|
||||
{
|
||||
config.WriteTo.File($"{globalSettings.LogDirectory}/{globalSettings.ProjectName}/log.txt",
|
||||
rollOnFileSizeLimit: true, fileSizeLimitBytes: globalSettings.LogRollBySizeLimit);
|
||||
var pathFormat = Path.Combine(globalSettings.LogDirectory, $"{globalSettings.ProjectName.ToLowerInvariant()}.log");
|
||||
if (globalSettings.LogDirectoryByProject)
|
||||
{
|
||||
pathFormat = Path.Combine(globalSettings.LogDirectory, globalSettings.ProjectName, "log.txt");
|
||||
}
|
||||
config.WriteTo.File(pathFormat, rollOnFileSizeLimit: true,
|
||||
fileSizeLimitBytes: globalSettings.LogRollBySizeLimit);
|
||||
}
|
||||
else
|
||||
{
|
||||
config.WriteTo
|
||||
.RollingFile($"{globalSettings.LogDirectory}/{globalSettings.ProjectName}/{{Date}}.txt");
|
||||
var pathFormat = Path.Combine(globalSettings.LogDirectory, $"{globalSettings.ProjectName.ToLowerInvariant()}_{{Date}}.log");
|
||||
if (globalSettings.LogDirectoryByProject)
|
||||
{
|
||||
pathFormat = Path.Combine(globalSettings.LogDirectory, globalSettings.ProjectName, "{Date}.txt");
|
||||
}
|
||||
config.WriteTo.RollingFile(pathFormat);
|
||||
}
|
||||
config
|
||||
.Enrich.FromLogContext()
|
||||
|
||||
@@ -29,7 +29,7 @@ public class Startup
|
||||
var globalSettings = services.AddGlobalSettingsServices(Configuration, Environment);
|
||||
|
||||
// Repositories
|
||||
services.AddSqlServerRepositories(globalSettings);
|
||||
services.AddDatabaseRepositories(globalSettings);
|
||||
|
||||
// Context
|
||||
services.AddScoped<ICurrentContext, CurrentContext>();
|
||||
|
||||
@@ -44,7 +44,7 @@ public class Startup
|
||||
services.AddCustomDataProtectionServices(Environment, globalSettings);
|
||||
|
||||
// Repositories
|
||||
services.AddSqlServerRepositories(globalSettings);
|
||||
services.AddDatabaseRepositories(globalSettings);
|
||||
|
||||
// Context
|
||||
services.AddScoped<ICurrentContext, CurrentContext>();
|
||||
|
||||
@@ -22,13 +22,14 @@ public static class EntityFrameworkServiceCollectionExtensions
|
||||
{
|
||||
if (provider == SupportedDatabaseProviders.Postgres)
|
||||
{
|
||||
options.UseNpgsql(connectionString);
|
||||
options.UseNpgsql(connectionString, b => b.MigrationsAssembly("PostgresMigrations"));
|
||||
// Handle NpgSql Legacy Support for `timestamp without timezone` issue
|
||||
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
||||
}
|
||||
else if (provider == SupportedDatabaseProviders.MySql)
|
||||
{
|
||||
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
|
||||
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString),
|
||||
b => b.MigrationsAssembly("MySqlMigrations"));
|
||||
}
|
||||
});
|
||||
services.AddSingleton<ICipherRepository, CipherRepository>();
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace Bit.SharedWeb.Utilities;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static void AddSqlServerRepositories(this IServiceCollection services, GlobalSettings globalSettings)
|
||||
public static SupportedDatabaseProviders AddDatabaseRepositories(this IServiceCollection services, GlobalSettings globalSettings)
|
||||
{
|
||||
var selectedDatabaseProvider = globalSettings.DatabaseProvider;
|
||||
var provider = SupportedDatabaseProviders.SqlServer;
|
||||
@@ -93,6 +93,8 @@ public static class ServiceCollectionExtensions
|
||||
services.AddSingleton<IInstallationDeviceRepository, TableStorageRepos.InstallationDeviceRepository>();
|
||||
services.AddSingleton<IMetaDataRepository, TableStorageRepos.MetaDataRepository>();
|
||||
}
|
||||
|
||||
return provider;
|
||||
}
|
||||
|
||||
public static void AddBaseServices(this IServiceCollection services, IGlobalSettings globalSettings)
|
||||
|
||||
Reference in New Issue
Block a user