1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-02 13:53:23 +01:00
bitwarden-server/src/Admin/Startup.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

145 lines
4.7 KiB
C#
Raw Normal View History

2018-03-21 17:57:43 +01:00
using System;
2019-07-11 21:03:17 +02:00
using System.Globalization;
using Bit.Core.Context;
2018-03-21 19:26:49 +01:00
using Bit.Core.Identity;
using Bit.Core.Settings;
2018-03-21 19:26:49 +01:00
using Bit.Core.Utilities;
2018-03-21 17:57:43 +01:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
2018-03-21 17:57:43 +01:00
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
2020-01-10 14:33:13 +01:00
using Microsoft.Extensions.Hosting;
2018-03-21 19:26:49 +01:00
using Stripe;
2018-03-21 17:57:43 +01:00
2021-06-30 09:35:26 +02:00
#if !OSS
using Bit.CommCore.Utilities;
#endif
2018-03-21 17:57:43 +01:00
namespace Bit.Admin
{
public class Startup
{
2020-01-10 14:33:13 +01:00
public Startup(IWebHostEnvironment env, IConfiguration configuration)
2018-03-21 17:57:43 +01:00
{
2019-07-11 21:03:17 +02:00
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
2018-03-21 17:57:43 +01:00
Configuration = configuration;
Environment = env;
2018-03-21 17:57:43 +01:00
}
public IConfiguration Configuration { get; private set; }
2020-01-10 14:33:13 +01:00
public IWebHostEnvironment Environment { get; set; }
2018-03-21 17:57:43 +01:00
public void ConfigureServices(IServiceCollection services)
{
2018-03-21 19:26:49 +01:00
// Options
services.AddOptions();
// Settings
var globalSettings = services.AddGlobalSettingsServices(Configuration);
services.Configure<AdminSettings>(Configuration.GetSection("AdminSettings"));
// Data Protection
services.AddCustomDataProtectionServices(Environment, globalSettings);
2018-03-21 19:26:49 +01:00
// Stripe Billing
StripeConfiguration.ApiKey = globalSettings.Stripe.ApiKey;
StripeConfiguration.MaxNetworkRetries = globalSettings.Stripe.MaxNetworkRetries;
2018-03-21 19:26:49 +01:00
// Repositories
services.AddSqlServerRepositories(globalSettings);
// Context
services.AddScoped<ICurrentContext, CurrentContext>();
2018-03-21 19:26:49 +01:00
// Identity
services.AddPasswordlessIdentityServices<ReadOnlyEnvIdentityUserStore>(globalSettings);
services.Configure<SecurityStampValidatorOptions>(options =>
{
options.ValidationInterval = TimeSpan.FromMinutes(5);
});
if (globalSettings.SelfHosted)
2018-03-28 16:38:01 +02:00
{
services.ConfigureApplicationCookie(options =>
{
options.Cookie.Path = "/admin";
});
}
2018-03-21 19:26:49 +01:00
// Services
services.AddBaseServices();
services.AddDefaultServices(globalSettings);
2021-12-16 15:35:09 +01:00
2021-06-30 09:35:26 +02:00
#if OSS
services.AddOosServices();
#else
services.AddCommCoreServices();
#endif
2018-03-21 19:26:49 +01:00
// Mvc
services.AddMvc(config =>
{
config.Filters.Add(new LoggingExceptionHandlerFilterAttribute());
});
2018-03-21 17:57:43 +01:00
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
// Jobs service
2019-11-04 13:43:15 +01:00
Jobs.JobsHostedService.AddJobsServices(services, globalSettings.SelfHosted);
services.AddHostedService<Jobs.JobsHostedService>();
if (globalSettings.SelfHosted)
2019-03-25 18:21:05 +01:00
{
services.AddHostedService<HostedServices.DatabaseMigrationHostedService>();
}
else
2019-03-05 05:41:46 +01:00
{
if (CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString))
2019-03-18 21:23:37 +01:00
{
services.AddHostedService<HostedServices.AzureQueueBlockIpHostedService>();
}
else if (CoreHelpers.SettingHasValue(globalSettings.Amazon?.AccessKeySecret))
2019-03-18 21:23:37 +01:00
{
services.AddHostedService<HostedServices.AmazonSqsBlockIpHostedService>();
}
Support large organization sync (#1311) * Increase organization max seat size from 30k to 2b (#1274) * Increase organization max seat size from 30k to 2b * PR review. Do not modify unless state matches expected * Organization sync simultaneous event reporting (#1275) * Split up azure messages according to max size * Allow simultaneous login of organization user events * Early resolve small event lists * Clarify logic Co-authored-by: Chad Scharf <3904944+cscharf@users.noreply.github.com> * Improve readability This comes at the cost of multiple serializations, but the improvement in wire-time should more than make up for this on message where serialization time matters Co-authored-by: Chad Scharf <3904944+cscharf@users.noreply.github.com> * Queue emails (#1286) * Extract common Azure queue methods * Do not use internal entity framework namespace * Prefer IEnumerable to IList unless needed All of these implementations were just using `Count == 1`, which is easily replicated. This will be used when abstracting Azure queues * Add model for azure queue message * Abstract Azure queue for reuse * Creat service to enqueue mail messages for later processing Azure queue mail service uses Azure queues. Blocking just blocks until all the work is done -- This is how emailing works today * Provide mail queue service to DI * Queue organization invite emails for later processing All emails can later be added to this queue * Create Admin hosted service to process enqueued mail messages * Prefer constructors to static generators * Mass delete organization users (#1287) * Add delete many to Organization Users * Correct formatting * Remove erroneous migration * Clarify parameter name * Formatting fixes * Simplify bump account revision sproc * Formatting fixes * Match file names to objects * Indicate if large import is expected * Early pull all existing users we were planning on inviting (#1290) * Early pull all existing users we were planning on inviting * Improve sproc name * Batch upsert org users (#1289) * Add UpsertMany sprocs to OrganizationUser * Add method to create TVPs from any object. Uses DbOrder attribute to generate. Sproc will fail unless TVP column order matches that of the db type * Combine migrations * Correct formatting * Include sql objects in sql project * Keep consisten parameter names * Batch deletes for performance * Correct formatting * consolidate migrations * Use batch methods in OrganizationImport * Declare @BatchSize * Transaction names limited to 32 chars Drop sproc before creating it if it exists * Update import tests * Allow for more users in org upgrades * Fix formatting * Improve class hierarchy structure * Use name tuple types * Fix formatting * Front load all reflection * Format constructor * Simplify ToTvp as class-specific extension Co-authored-by: Chad Scharf <3904944+cscharf@users.noreply.github.com>
2021-05-17 16:43:02 +02:00
if (CoreHelpers.SettingHasValue(globalSettings.Mail.ConnectionString))
{
services.AddHostedService<HostedServices.AzureQueueMailHostedService>();
}
2019-03-05 05:41:46 +01:00
}
2018-03-21 17:57:43 +01:00
}
2018-03-21 19:26:49 +01:00
public void Configure(
IApplicationBuilder app,
2020-01-10 14:33:13 +01:00
IWebHostEnvironment env,
IHostApplicationLifetime appLifetime,
2019-07-23 22:38:49 +02:00
GlobalSettings globalSettings)
2018-03-21 17:57:43 +01:00
{
2019-07-23 22:38:49 +02:00
app.UseSerilog(env, appLifetime, globalSettings);
// Add general security headers
app.UseMiddleware<SecurityHeadersMiddleware>();
if (globalSettings.SelfHosted)
2018-03-21 17:57:43 +01:00
{
2018-03-24 13:39:55 +01:00
app.UsePathBase("/admin");
2019-04-26 15:52:54 +02:00
app.UseForwardedHeaders(globalSettings);
2018-03-21 17:57:43 +01:00
}
if (env.IsDevelopment())
2018-03-24 04:27:33 +01:00
{
2018-03-24 13:39:55 +01:00
app.UseDeveloperExceptionPage();
2018-03-24 04:27:33 +01:00
}
else
{
app.UseExceptionHandler("/error");
}
2018-03-24 04:27:33 +01:00
2018-03-21 17:57:43 +01:00
app.UseStaticFiles();
2020-01-10 14:33:13 +01:00
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute());
2018-03-21 17:57:43 +01:00
}
}
}