2019-07-11 15:03:17 -04:00
|
|
|
|
using System.Globalization;
|
2025-04-23 10:44:43 -04:00
|
|
|
|
using Bit.Core.AdminConsole.Services.NoopImplementations;
|
2025-02-11 10:20:06 -05:00
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
using Bit.Core.Services;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2019-07-11 16:19:38 -04:00
|
|
|
|
using Bit.Core.Utilities;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.SharedWeb.Utilities;
|
2019-03-02 12:15:28 -05:00
|
|
|
|
using Microsoft.IdentityModel.Logging;
|
2025-02-11 10:20:06 -05:00
|
|
|
|
using TableStorageRepos = Bit.Core.Repositories.TableStorage;
|
2019-03-02 12:15:28 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.EventsProcessor;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2019-03-02 12:15:28 -05:00
|
|
|
|
public class Startup
|
|
|
|
|
|
{
|
|
|
|
|
|
public Startup(IWebHostEnvironment env, IConfiguration configuration)
|
|
|
|
|
|
{
|
2019-07-11 15:03:17 -04:00
|
|
|
|
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
|
2019-03-02 12:15:28 -05:00
|
|
|
|
Configuration = configuration;
|
|
|
|
|
|
Environment = env;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
2020-01-10 08:33:13 -05:00
|
|
|
|
public IWebHostEnvironment Environment { get; set; }
|
2019-03-02 12:15:28 -05:00
|
|
|
|
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
|
{
|
2019-07-11 21:45:14 -04:00
|
|
|
|
// Options
|
2019-03-02 12:15:28 -05:00
|
|
|
|
services.AddOptions();
|
2019-07-11 21:45:14 -04:00
|
|
|
|
|
|
|
|
|
|
// Settings
|
2025-02-11 10:20:06 -05:00
|
|
|
|
var globalSettings = services.AddGlobalSettingsServices(Configuration, Environment);
|
2019-07-11 21:45:14 -04:00
|
|
|
|
|
2025-04-23 10:44:43 -04:00
|
|
|
|
// Data Protection
|
|
|
|
|
|
services.AddCustomDataProtectionServices(Environment, globalSettings);
|
|
|
|
|
|
|
|
|
|
|
|
// Repositories
|
|
|
|
|
|
services.AddDatabaseRepositories(globalSettings);
|
|
|
|
|
|
|
2019-07-11 21:45:14 -04:00
|
|
|
|
// Hosted Services
|
2025-02-11 10:20:06 -05:00
|
|
|
|
|
|
|
|
|
|
// Optional Azure Service Bus Listeners
|
|
|
|
|
|
if (CoreHelpers.SettingHasValue(globalSettings.EventLogging.AzureServiceBus.ConnectionString) &&
|
|
|
|
|
|
CoreHelpers.SettingHasValue(globalSettings.EventLogging.AzureServiceBus.TopicName))
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IEventRepository, TableStorageRepos.EventRepository>();
|
|
|
|
|
|
services.AddSingleton<AzureTableStorageEventHandler>();
|
|
|
|
|
|
services.AddKeyedSingleton<IEventWriteService, RepositoryEventWriteService>("persistent");
|
|
|
|
|
|
services.AddSingleton<IHostedService>(provider =>
|
|
|
|
|
|
new AzureServiceBusEventListenerService(
|
|
|
|
|
|
provider.GetRequiredService<AzureTableStorageEventHandler>(),
|
|
|
|
|
|
provider.GetRequiredService<ILogger<AzureServiceBusEventListenerService>>(),
|
|
|
|
|
|
globalSettings,
|
|
|
|
|
|
globalSettings.EventLogging.AzureServiceBus.EventRepositorySubscriptionName));
|
|
|
|
|
|
|
2025-04-23 10:44:43 -04:00
|
|
|
|
if (CoreHelpers.SettingHasValue(globalSettings.Slack.ClientId) &&
|
|
|
|
|
|
CoreHelpers.SettingHasValue(globalSettings.Slack.ClientSecret) &&
|
|
|
|
|
|
CoreHelpers.SettingHasValue(globalSettings.Slack.Scopes))
|
2025-02-11 10:20:06 -05:00
|
|
|
|
{
|
2025-04-23 10:44:43 -04:00
|
|
|
|
services.AddHttpClient(SlackService.HttpClientName);
|
|
|
|
|
|
services.AddSingleton<ISlackService, SlackService>();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<ISlackService, NoopSlackService>();
|
2025-02-11 10:20:06 -05:00
|
|
|
|
}
|
2025-04-23 10:44:43 -04:00
|
|
|
|
services.AddSingleton<SlackEventHandler>();
|
|
|
|
|
|
services.AddSingleton<IHostedService>(provider =>
|
|
|
|
|
|
new AzureServiceBusEventListenerService(
|
|
|
|
|
|
provider.GetRequiredService<SlackEventHandler>(),
|
|
|
|
|
|
provider.GetRequiredService<ILogger<AzureServiceBusEventListenerService>>(),
|
|
|
|
|
|
globalSettings,
|
|
|
|
|
|
globalSettings.EventLogging.AzureServiceBus.SlackSubscriptionName));
|
|
|
|
|
|
|
|
|
|
|
|
services.AddSingleton<WebhookEventHandler>();
|
|
|
|
|
|
services.AddHttpClient(WebhookEventHandler.HttpClientName);
|
|
|
|
|
|
|
|
|
|
|
|
services.AddSingleton<IHostedService>(provider =>
|
|
|
|
|
|
new AzureServiceBusEventListenerService(
|
|
|
|
|
|
provider.GetRequiredService<WebhookEventHandler>(),
|
|
|
|
|
|
provider.GetRequiredService<ILogger<AzureServiceBusEventListenerService>>(),
|
|
|
|
|
|
globalSettings,
|
|
|
|
|
|
globalSettings.EventLogging.AzureServiceBus.WebhookSubscriptionName));
|
2025-02-11 10:20:06 -05:00
|
|
|
|
}
|
2019-03-02 12:15:28 -05:00
|
|
|
|
services.AddHostedService<AzureQueueHostedService>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-11 16:19:38 -04:00
|
|
|
|
public void Configure(
|
|
|
|
|
|
IApplicationBuilder app,
|
2020-01-10 08:33:13 -05:00
|
|
|
|
IWebHostEnvironment env,
|
|
|
|
|
|
IHostApplicationLifetime appLifetime,
|
2019-07-11 16:19:38 -04:00
|
|
|
|
GlobalSettings globalSettings)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2019-07-11 16:19:38 -04:00
|
|
|
|
IdentityModelEventSource.ShowPII = true;
|
2019-07-23 16:38:49 -04:00
|
|
|
|
app.UseSerilog(env, appLifetime, globalSettings);
|
2021-11-09 11:37:14 -05:00
|
|
|
|
// Add general security headers
|
|
|
|
|
|
app.UseMiddleware<SecurityHeadersMiddleware>();
|
2020-01-10 08:33:13 -05:00
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
2019-03-02 12:15:28 -05:00
|
|
|
|
{
|
2020-01-10 08:33:13 -05:00
|
|
|
|
endpoints.MapGet("/alive",
|
2021-12-09 15:45:45 -05:00
|
|
|
|
async context => await context.Response.WriteAsJsonAsync(System.DateTime.UtcNow));
|
|
|
|
|
|
endpoints.MapGet("/now",
|
|
|
|
|
|
async context => await context.Response.WriteAsJsonAsync(System.DateTime.UtcNow));
|
|
|
|
|
|
endpoints.MapGet("/version",
|
2022-09-05 11:19:04 -04:00
|
|
|
|
async context => await context.Response.WriteAsJsonAsync(AssemblyHelpers.GetVersion()));
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2020-01-10 08:33:13 -05:00
|
|
|
|
});
|
2019-03-02 12:15:28 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|