From 29580684a3ad931cecb3702d6cebdee2460f92c7 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 10 Jan 2020 08:33:13 -0500 Subject: [PATCH] upgrade to aspnet core 3.1 --- src/Admin/Admin.csproj | 6 +- src/Admin/Dockerfile | 2 +- .../AzureQueueBlockIpHostedService.cs | 35 +++++------ src/Admin/Program.cs | 19 +++--- src/Admin/Startup.cs | 15 +++-- src/Api/Api.csproj | 5 +- src/Api/Dockerfile | 2 +- src/Api/Program.cs | 51 +++++++++------- src/Api/Startup.cs | 61 ++++++++++--------- src/Billing/Billing.csproj | 11 +--- src/Billing/Program.cs | 43 +++++++------ src/Billing/Startup.cs | 11 ++-- src/Core/Core.csproj | 28 ++++----- ...stomIdentityServiceCollectionExtensions.cs | 10 +-- .../LowerInvariantLookupNormalizer.cs | 12 +++- .../Identity/PasswordlessSignInManager.cs | 3 +- .../TwoFactorRememberTokenProvider.cs | 6 +- src/Core/Models/Data/DictionaryEntity.cs | 3 +- src/Core/Models/Data/EventTableEntity.cs | 3 +- .../Models/Data/InstallationDeviceEntity.cs | 2 +- .../TableStorage/EventRepository.cs | 3 +- .../InstallationDeviceRepository.cs | 3 +- .../TableStorage/MetaDataRepository.cs | 3 +- .../AzureAttachmentStorageService.cs | 4 +- .../AzureQueueBlockIpService.cs | 34 +++-------- .../AzureQueueEventWriteService.cs | 20 ++---- .../AzureQueuePushNotificationService.cs | 12 ++-- .../Implementations/LicensingService.cs | 2 +- src/Core/Utilities/CoreHelpers.cs | 3 +- src/Core/Utilities/LoggerFactoryExtensions.cs | 5 +- .../Utilities/ServiceCollectionExtensions.cs | 36 ++++++++--- src/Events/Dockerfile | 2 +- src/Events/Events.csproj | 7 +-- src/Events/Program.cs | 40 ++++++------ src/Events/Startup.cs | 17 ++++-- .../AzureQueueHostedService.cs | 21 +++---- src/EventsProcessor/EventsProcessor.csproj | 7 +-- src/EventsProcessor/Program.cs | 15 +++-- src/EventsProcessor/Startup.cs | 21 ++++--- src/Icons/Dockerfile | 2 +- src/Icons/Icons.csproj | 3 +- src/Icons/Program.cs | 15 +++-- src/Icons/Startup.cs | 8 ++- src/Identity/Dockerfile | 2 +- src/Identity/Identity.csproj | 7 +-- src/Identity/Program.cs | 41 +++++++------ src/Identity/Startup.cs | 9 +-- src/Notifications/AzureQueueHostedService.cs | 25 +++----- src/Notifications/Dockerfile | 2 +- src/Notifications/Notifications.csproj | 6 +- src/Notifications/Program.cs | 61 ++++++++++--------- src/Notifications/Startup.cs | 40 +++++++----- test/Core.Test/Core.Test.csproj | 4 +- .../AzureQueueEventWriteServiceTests.cs | 3 +- util/Migrator/Migrator.csproj | 4 +- util/Server/Dockerfile | 2 +- util/Server/Server.csproj | 7 +-- util/Server/Startup.cs | 19 +++--- util/Setup/Dockerfile | 2 +- util/Setup/Setup.csproj | 4 +- 60 files changed, 429 insertions(+), 420 deletions(-) diff --git a/src/Admin/Admin.csproj b/src/Admin/Admin.csproj index 156f955966..bac2704d8d 100644 --- a/src/Admin/Admin.csproj +++ b/src/Admin/Admin.csproj @@ -2,10 +2,9 @@ 1.33.0 - netcoreapp2.2 + netcoreapp3.1 Bit.Admin bitwarden-Admin - InProcess @@ -14,8 +13,7 @@ - - + diff --git a/src/Admin/Dockerfile b/src/Admin/Dockerfile index e11c278162..95d13c8b8f 100644 --- a/src/Admin/Dockerfile +++ b/src/Admin/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 +FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 LABEL com.bitwarden.product="bitwarden" diff --git a/src/Admin/HostedServices/AzureQueueBlockIpHostedService.cs b/src/Admin/HostedServices/AzureQueueBlockIpHostedService.cs index c0ba0f0d66..fcd71b81eb 100644 --- a/src/Admin/HostedServices/AzureQueueBlockIpHostedService.cs +++ b/src/Admin/HostedServices/AzureQueueBlockIpHostedService.cs @@ -5,15 +5,14 @@ using System.Threading.Tasks; using Bit.Core; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Queue; +using Azure.Storage.Queues; namespace Bit.Admin.HostedServices { public class AzureQueueBlockIpHostedService : BlockIpHostedService { - private CloudQueue _blockQueue; - private CloudQueue _unblockQueue; + private QueueClient _blockIpQueueClient; + private QueueClient _unblockIpQueueClient; public AzureQueueBlockIpHostedService( ILogger logger, @@ -24,46 +23,42 @@ namespace Bit.Admin.HostedServices protected override async Task ExecuteAsync(CancellationToken cancellationToken) { - var storageAccount = CloudStorageAccount.Parse(_globalSettings.Storage.ConnectionString); - var queueClient = storageAccount.CreateCloudQueueClient(); - _blockQueue = queueClient.GetQueueReference("blockip"); - _unblockQueue = queueClient.GetQueueReference("unblockip"); + _blockIpQueueClient = new QueueClient(_globalSettings.Storage.ConnectionString, "blockip"); + _unblockIpQueueClient = new QueueClient(_globalSettings.Storage.ConnectionString, "unblockip"); while(!cancellationToken.IsCancellationRequested) { - var blockMessages = await _blockQueue.GetMessagesAsync(32, TimeSpan.FromSeconds(15), - null, null, cancellationToken); - if(blockMessages.Any()) + var blockMessages = await _blockIpQueueClient.ReceiveMessagesAsync(maxMessages: 32); + if(blockMessages.Value?.Any() ?? false) { - foreach(var message in blockMessages) + foreach(var message in blockMessages.Value) { try { - await BlockIpAsync(message.AsString, cancellationToken); + await BlockIpAsync(message.MessageText, cancellationToken); } catch(Exception e) { _logger.LogError(e, "Failed to block IP."); } - await _blockQueue.DeleteMessageAsync(message); + await _blockIpQueueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt); } } - var unblockMessages = await _unblockQueue.GetMessagesAsync(32, TimeSpan.FromSeconds(15), - null, null, cancellationToken); - if(unblockMessages.Any()) + var unblockMessages = await _unblockIpQueueClient.ReceiveMessagesAsync(maxMessages: 32); + if(unblockMessages.Value?.Any() ?? false) { - foreach(var message in unblockMessages) + foreach(var message in unblockMessages.Value) { try { - await UnblockIpAsync(message.AsString, cancellationToken); + await UnblockIpAsync(message.MessageText, cancellationToken); } catch(Exception e) { _logger.LogError(e, "Failed to unblock IP."); } - await _unblockQueue.DeleteMessageAsync(message); + await _unblockIpQueueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt); } } diff --git a/src/Admin/Program.cs b/src/Admin/Program.cs index 94dcdb8974..69a5dba44c 100644 --- a/src/Admin/Program.cs +++ b/src/Admin/Program.cs @@ -1,6 +1,6 @@ using Bit.Core.Utilities; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; using Serilog.Events; namespace Bit.Admin @@ -9,14 +9,16 @@ namespace Bit.Admin { public static void Main(string[] args) { - WebHost + Host .CreateDefaultBuilder(args) - .ConfigureKestrel(o => + .ConfigureWebHostDefaults(webBuilder => { - o.Limits.MaxRequestLineSize = 20_000; - }) - .UseStartup() - .ConfigureLogging((hostingContext, logging) => + webBuilder.ConfigureKestrel(o => + { + o.Limits.MaxRequestLineSize = 20_000; + }); + webBuilder.UseStartup(); + webBuilder.ConfigureLogging((hostingContext, logging) => logging.AddSerilog(hostingContext, e => { var context = e.Properties["SourceContext"].ToString(); @@ -27,7 +29,8 @@ namespace Bit.Admin return false; } return e.Level >= LogEventLevel.Error; - })) + })); + }) .Build() .Run(); } diff --git a/src/Admin/Startup.cs b/src/Admin/Startup.cs index 919b7b8710..c154ecc9fd 100644 --- a/src/Admin/Startup.cs +++ b/src/Admin/Startup.cs @@ -9,13 +9,14 @@ using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Stripe; namespace Bit.Admin { public class Startup { - public Startup(IHostingEnvironment env, IConfiguration configuration) + public Startup(IWebHostEnvironment env, IConfiguration configuration) { CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); Configuration = configuration; @@ -23,7 +24,7 @@ namespace Bit.Admin } public IConfiguration Configuration { get; private set; } - public IHostingEnvironment Environment { get; set; } + public IWebHostEnvironment Environment { get; set; } public void ConfigureServices(IServiceCollection services) { @@ -93,8 +94,8 @@ namespace Bit.Admin public void Configure( IApplicationBuilder app, - IHostingEnvironment env, - IApplicationLifetime appLifetime, + IWebHostEnvironment env, + IHostApplicationLifetime appLifetime, GlobalSettings globalSettings) { app.UseSerilog(env, appLifetime, globalSettings); @@ -110,9 +111,11 @@ namespace Bit.Admin app.UseDeveloperExceptionPage(); } - app.UseAuthentication(); app.UseStaticFiles(); - app.UseMvcWithDefaultRoute(); + app.UseRouting(); + app.UseAuthentication(); + app.UseAuthorization(); + app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); } } } diff --git a/src/Api/Api.csproj b/src/Api/Api.csproj index 47c574d227..3204b3a496 100644 --- a/src/Api/Api.csproj +++ b/src/Api/Api.csproj @@ -2,11 +2,10 @@ 1.33.0 - netcoreapp2.2 + netcoreapp3.1 Bit.Api bitwarden-Api false - InProcess bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml true @@ -24,7 +23,7 @@ - + diff --git a/src/Api/Dockerfile b/src/Api/Dockerfile index f5ceb37400..800b34367b 100644 --- a/src/Api/Dockerfile +++ b/src/Api/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 +FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 LABEL com.bitwarden.product="bitwarden" diff --git a/src/Api/Program.cs b/src/Api/Program.cs index 9542b7ece1..94aa38e8c6 100644 --- a/src/Api/Program.cs +++ b/src/Api/Program.cs @@ -1,5 +1,5 @@ -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; using Bit.Core.Utilities; using Serilog.Events; using Microsoft.IdentityModel.Tokens; @@ -11,32 +11,37 @@ namespace Bit.Api { public static void Main(string[] args) { - WebHost + Host .CreateDefaultBuilder(args) - .UseStartup() - .ConfigureLogging((hostingContext, logging) => - logging.AddSerilog(hostingContext, e => - { - var context = e.Properties["SourceContext"].ToString(); - if(e.Exception != null && (e.Exception.GetType() == typeof(SecurityTokenValidationException) || - e.Exception.Message == "Bad security stamp.")) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + webBuilder.ConfigureLogging((hostingContext, logging) => + logging.AddSerilog(hostingContext, e => { - return false; - } + var context = e.Properties["SourceContext"].ToString(); + if(e.Exception != null && + (e.Exception.GetType() == typeof(SecurityTokenValidationException) || + e.Exception.Message == "Bad security stamp.")) + { + return false; + } - if(e.Level == LogEventLevel.Information && context.Contains(typeof(IpRateLimitMiddleware).FullName)) - { - return true; - } + if(e.Level == LogEventLevel.Information && + context.Contains(typeof(IpRateLimitMiddleware).FullName)) + { + return true; + } - if(context.Contains("IdentityServer4.Validation.TokenValidator") || - context.Contains("IdentityServer4.Validation.TokenRequestValidator")) - { - return e.Level > LogEventLevel.Error; - } + if(context.Contains("IdentityServer4.Validation.TokenValidator") || + context.Contains("IdentityServer4.Validation.TokenRequestValidator")) + { + return e.Level > LogEventLevel.Error; + } - return e.Level >= LogEventLevel.Error; - })) + return e.Level >= LogEventLevel.Error; + })); + }) .Build() .Run(); } diff --git a/src/Api/Startup.cs b/src/Api/Startup.cs index ddc13992a6..f730219c63 100644 --- a/src/Api/Startup.cs +++ b/src/Api/Startup.cs @@ -13,12 +13,13 @@ using Bit.Core.Utilities; using IdentityModel; using System.Globalization; using Microsoft.IdentityModel.Logging; +using Microsoft.Extensions.Hosting; namespace Bit.Api { public class Startup { - public Startup(IHostingEnvironment env, IConfiguration configuration) + public Startup(IWebHostEnvironment env, IConfiguration configuration) { CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); Configuration = configuration; @@ -26,7 +27,7 @@ namespace Bit.Api } public IConfiguration Configuration { get; private set; } - public IHostingEnvironment Environment { get; set; } + public IWebHostEnvironment Environment { get; set; } public void ConfigureServices(IServiceCollection services) { @@ -113,7 +114,7 @@ namespace Bit.Api { config.Conventions.Add(new ApiExplorerGroupConvention()); config.Conventions.Add(new PublicApiControllersModelConvention()); - }).AddJsonOptions(options => + }).AddNewtonsoftJson(options => { if(Environment.IsProduction() && Configuration["swaggerGen"] != "true") { @@ -121,7 +122,7 @@ namespace Bit.Api } }); - services.AddSwagger(globalSettings); + //services.AddSwagger(globalSettings); if(globalSettings.SelfHosted) { @@ -138,8 +139,8 @@ namespace Bit.Api public void Configure( IApplicationBuilder app, - IHostingEnvironment env, - IApplicationLifetime appLifetime, + IWebHostEnvironment env, + IHostApplicationLifetime appLifetime, GlobalSettings globalSettings, ILogger logger) { @@ -162,39 +163,43 @@ namespace Bit.Api // Add static files to the request pipeline. app.UseStaticFiles(); + // Add routing + app.UseRouting(); + // Add Cors app.UseCors(policy => policy.SetIsOriginAllowed(h => true) .AllowAnyMethod().AllowAnyHeader().AllowCredentials()); - // Add authentication to the request pipeline. + // Add authentication and authorization to the request pipeline. app.UseAuthentication(); + app.UseAuthorization(); // Add current context app.UseMiddleware(); - // Add MVC to the request pipeline. - app.UseMvc(); + // Add endpoints to the request pipeline. + app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); // Add Swagger - if(Environment.IsDevelopment() || globalSettings.SelfHosted) - { - app.UseSwagger(config => - { - config.RouteTemplate = "specs/{documentName}/swagger.json"; - var host = globalSettings.BaseServiceUri.Api.Replace("https://", string.Empty) - .Replace("http://", string.Empty); - config.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.Host = host); - }); - app.UseSwaggerUI(config => - { - config.DocumentTitle = "Bitwarden API Documentation"; - config.RoutePrefix = "docs"; - config.SwaggerEndpoint($"{globalSettings.BaseServiceUri.Api}/specs/public/swagger.json", - "Bitwarden Public API"); - config.OAuthClientId("accountType.id"); - config.OAuthClientSecret("secretKey"); - }); - } + //if(Environment.IsDevelopment() || globalSettings.SelfHosted) + //{ + // app.UseSwagger(config => + // { + // config.RouteTemplate = "specs/{documentName}/swagger.json"; + // var host = globalSettings.BaseServiceUri.Api.Replace("https://", string.Empty) + // .Replace("http://", string.Empty); + // config.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.Host = host); + // }); + // app.UseSwaggerUI(config => + // { + // config.DocumentTitle = "Bitwarden API Documentation"; + // config.RoutePrefix = "docs"; + // config.SwaggerEndpoint($"{globalSettings.BaseServiceUri.Api}/specs/public/swagger.json", + // "Bitwarden Public API"); + // config.OAuthClientId("accountType.id"); + // config.OAuthClientSecret("secretKey"); + // }); + //} // Log startup logger.LogInformation(Constants.BypassFiltersEventId, globalSettings.ProjectName + " started."); diff --git a/src/Billing/Billing.csproj b/src/Billing/Billing.csproj index 5287fc3da2..a8a349ec18 100644 --- a/src/Billing/Billing.csproj +++ b/src/Billing/Billing.csproj @@ -2,11 +2,10 @@ 1.33.0 - netcoreapp2.2 + netcoreapp3.1 Bit.Billing bitwarden-Billing false - InProcess @@ -14,13 +13,7 @@ - - - - - - - + diff --git a/src/Billing/Program.cs b/src/Billing/Program.cs index 204dab7032..13a0bc3d01 100644 --- a/src/Billing/Program.cs +++ b/src/Billing/Program.cs @@ -1,7 +1,7 @@ -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting; using Bit.Core.Utilities; using Serilog.Events; +using Microsoft.Extensions.Hosting; namespace Bit.Billing { @@ -9,28 +9,31 @@ namespace Bit.Billing { public static void Main(string[] args) { - WebHost + Host .CreateDefaultBuilder(args) - .UseStartup() - .ConfigureLogging((hostingContext, logging) => - logging.AddSerilog(hostingContext, e => - { - var context = e.Properties["SourceContext"].ToString(); - if(e.Level == LogEventLevel.Information && - (context.StartsWith("\"Bit.Billing.Jobs") || context.StartsWith("\"Bit.Core.Jobs"))) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + webBuilder.ConfigureLogging((hostingContext, logging) => + logging.AddSerilog(hostingContext, e => { - return true; - } + var context = e.Properties["SourceContext"].ToString(); + if(e.Level == LogEventLevel.Information && + (context.StartsWith("\"Bit.Billing.Jobs") || context.StartsWith("\"Bit.Core.Jobs"))) + { + return true; + } - if(e.Properties.ContainsKey("RequestPath") && - !string.IsNullOrWhiteSpace(e.Properties["RequestPath"]?.ToString()) && - (context.Contains(".Server.Kestrel") || context.Contains(".Core.IISHttpServer"))) - { - return false; - } + if(e.Properties.ContainsKey("RequestPath") && + !string.IsNullOrWhiteSpace(e.Properties["RequestPath"]?.ToString()) && + (context.Contains(".Server.Kestrel") || context.Contains(".Core.IISHttpServer"))) + { + return false; + } - return e.Level >= LogEventLevel.Warning; - })) + return e.Level >= LogEventLevel.Warning; + })); + }) .Build() .Run(); } diff --git a/src/Billing/Startup.cs b/src/Billing/Startup.cs index 839c596dc0..c45a88191a 100644 --- a/src/Billing/Startup.cs +++ b/src/Billing/Startup.cs @@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.AspNetCore.Routing; using System.Globalization; +using Microsoft.Extensions.Hosting; namespace Bit.Billing { @@ -71,8 +72,8 @@ namespace Bit.Billing public void Configure( IApplicationBuilder app, - IHostingEnvironment env, - IApplicationLifetime appLifetime, + IWebHostEnvironment env, + IHostApplicationLifetime appLifetime, GlobalSettings globalSettings) { app.UseSerilog(env, appLifetime, globalSettings); @@ -82,9 +83,11 @@ namespace Bit.Billing app.UseDeveloperExceptionPage(); } - app.UseAuthentication(); app.UseStaticFiles(); - app.UseMvcWithDefaultRoute(); + app.UseRouting(); + app.UseAuthentication(); + app.UseAuthorization(); + app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); } } } diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 1ecfd3ec1e..bb72cd9b8f 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -1,7 +1,7 @@  - netcoreapp2.2 + netcoreapp3.1 Bit.Core false bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml @@ -24,34 +24,32 @@ + - + - - - - - - + + + - - - + + + + - + - + - @@ -59,7 +57,7 @@ - + diff --git a/src/Core/Identity/CustomIdentityServiceCollectionExtensions.cs b/src/Core/Identity/CustomIdentityServiceCollectionExtensions.cs index c703e96ac1..103d263098 100644 --- a/src/Core/Identity/CustomIdentityServiceCollectionExtensions.cs +++ b/src/Core/Identity/CustomIdentityServiceCollectionExtensions.cs @@ -24,7 +24,7 @@ namespace Microsoft.Extensions.DependencyInjection where TRole : class { // Hosting doesn't add IHttpContextAccessor by default - services.TryAddSingleton(); + services.AddHttpContextAccessor(); // Identity services services.TryAddScoped, UserValidator>(); services.TryAddScoped, PasswordValidator>(); @@ -34,10 +34,12 @@ namespace Microsoft.Extensions.DependencyInjection // No interface for the error describer so we can add errors without rev'ing the interface services.TryAddScoped(); services.TryAddScoped>(); + services.TryAddScoped>(); services.TryAddScoped, UserClaimsPrincipalFactory>(); - services.TryAddScoped, AspNetUserManager>(); - services.TryAddScoped, SignInManager>(); - services.TryAddScoped, AspNetRoleManager>(); + services.TryAddScoped, DefaultUserConfirmation>(); + services.TryAddScoped>(); + services.TryAddScoped>(); + services.TryAddScoped>(); if(setupAction != null) { diff --git a/src/Core/Identity/LowerInvariantLookupNormalizer.cs b/src/Core/Identity/LowerInvariantLookupNormalizer.cs index 98189b1b5a..591b840a47 100644 --- a/src/Core/Identity/LowerInvariantLookupNormalizer.cs +++ b/src/Core/Identity/LowerInvariantLookupNormalizer.cs @@ -4,7 +4,17 @@ namespace Bit.Core.Identity { public class LowerInvariantLookupNormalizer : ILookupNormalizer { - public string Normalize(string key) + public string NormalizeEmail(string email) + { + return Normalize(email); + } + + public string NormalizeName(string name) + { + return Normalize(name); + } + + private string Normalize(string key) { return key?.Normalize().ToLowerInvariant(); } diff --git a/src/Core/Identity/PasswordlessSignInManager.cs b/src/Core/Identity/PasswordlessSignInManager.cs index c54a3ddf55..defcc37dcc 100644 --- a/src/Core/Identity/PasswordlessSignInManager.cs +++ b/src/Core/Identity/PasswordlessSignInManager.cs @@ -22,8 +22,9 @@ namespace Bit.Core.Identity IOptions optionsAccessor, ILogger> logger, IAuthenticationSchemeProvider schemes, + IUserConfirmation confirmation, IMailService mailService) - : base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes) + : base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes, confirmation) { _mailService = mailService; } diff --git a/src/Core/Identity/TwoFactorRememberTokenProvider.cs b/src/Core/Identity/TwoFactorRememberTokenProvider.cs index 450bc938aa..2a7a1c23a9 100644 --- a/src/Core/Identity/TwoFactorRememberTokenProvider.cs +++ b/src/Core/Identity/TwoFactorRememberTokenProvider.cs @@ -2,6 +2,7 @@ using Bit.Core.Models.Table; using Microsoft.Extensions.Options; using Microsoft.AspNetCore.DataProtection; +using Microsoft.Extensions.Logging; namespace Bit.Core.Identity { @@ -9,8 +10,9 @@ namespace Bit.Core.Identity { public TwoFactorRememberTokenProvider( IDataProtectionProvider dataProtectionProvider, - IOptions options) - : base(dataProtectionProvider, options) + IOptions options, + ILogger> logger) + : base(dataProtectionProvider, options, logger) { } } diff --git a/src/Core/Models/Data/DictionaryEntity.cs b/src/Core/Models/Data/DictionaryEntity.cs index 90c3b156ed..7e7cf97fae 100644 --- a/src/Core/Models/Data/DictionaryEntity.cs +++ b/src/Core/Models/Data/DictionaryEntity.cs @@ -1,8 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Table; +using Microsoft.Azure.Cosmos.Table; namespace Bit.Core.Models.Data { diff --git a/src/Core/Models/Data/EventTableEntity.cs b/src/Core/Models/Data/EventTableEntity.cs index 052b08765a..56c753d260 100644 --- a/src/Core/Models/Data/EventTableEntity.cs +++ b/src/Core/Models/Data/EventTableEntity.cs @@ -2,8 +2,7 @@ using System.Collections.Generic; using Bit.Core.Enums; using Bit.Core.Utilities; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Table; +using Microsoft.Azure.Cosmos.Table; namespace Bit.Core.Models.Data { diff --git a/src/Core/Models/Data/InstallationDeviceEntity.cs b/src/Core/Models/Data/InstallationDeviceEntity.cs index d955409512..973c5c897c 100644 --- a/src/Core/Models/Data/InstallationDeviceEntity.cs +++ b/src/Core/Models/Data/InstallationDeviceEntity.cs @@ -1,5 +1,5 @@ using System; -using Microsoft.WindowsAzure.Storage.Table; +using Microsoft.Azure.Cosmos.Table; namespace Bit.Core.Models.Data { diff --git a/src/Core/Repositories/TableStorage/EventRepository.cs b/src/Core/Repositories/TableStorage/EventRepository.cs index 62c58596b7..12ebf57b42 100644 --- a/src/Core/Repositories/TableStorage/EventRepository.cs +++ b/src/Core/Repositories/TableStorage/EventRepository.cs @@ -5,8 +5,7 @@ using System.Threading.Tasks; using Bit.Core.Models.Data; using Bit.Core.Models.Table; using Bit.Core.Utilities; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Table; +using Microsoft.Azure.Cosmos.Table; namespace Bit.Core.Repositories.TableStorage { diff --git a/src/Core/Repositories/TableStorage/InstallationDeviceRepository.cs b/src/Core/Repositories/TableStorage/InstallationDeviceRepository.cs index 355a278044..71f95f67b5 100644 --- a/src/Core/Repositories/TableStorage/InstallationDeviceRepository.cs +++ b/src/Core/Repositories/TableStorage/InstallationDeviceRepository.cs @@ -3,8 +3,7 @@ using System.Linq; using System.Net; using System.Threading.Tasks; using Bit.Core.Models.Data; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Table; +using Microsoft.Azure.Cosmos.Table; namespace Bit.Core.Repositories.TableStorage { diff --git a/src/Core/Repositories/TableStorage/MetaDataRepository.cs b/src/Core/Repositories/TableStorage/MetaDataRepository.cs index 15b9576937..6762fbbd13 100644 --- a/src/Core/Repositories/TableStorage/MetaDataRepository.cs +++ b/src/Core/Repositories/TableStorage/MetaDataRepository.cs @@ -3,8 +3,7 @@ using System.Linq; using System.Net; using System.Threading.Tasks; using Bit.Core.Models.Data; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Table; +using Microsoft.Azure.Cosmos.Table; namespace Bit.Core.Repositories.TableStorage { diff --git a/src/Core/Services/Implementations/AzureAttachmentStorageService.cs b/src/Core/Services/Implementations/AzureAttachmentStorageService.cs index 1c20cf4351..21e15a9b2f 100644 --- a/src/Core/Services/Implementations/AzureAttachmentStorageService.cs +++ b/src/Core/Services/Implementations/AzureAttachmentStorageService.cs @@ -1,6 +1,6 @@ using System.Threading.Tasks; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Blob; +using Microsoft.Azure.Storage; +using Microsoft.Azure.Storage.Blob; using System.IO; using System; using Bit.Core.Models.Table; diff --git a/src/Core/Services/Implementations/AzureQueueBlockIpService.cs b/src/Core/Services/Implementations/AzureQueueBlockIpService.cs index a7843e8ba4..c51d1f6790 100644 --- a/src/Core/Services/Implementations/AzureQueueBlockIpService.cs +++ b/src/Core/Services/Implementations/AzureQueueBlockIpService.cs @@ -1,31 +1,24 @@ using System.Threading.Tasks; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Queue; using System; -using Bit.Core.Utilities; +using Azure.Storage.Queues; namespace Bit.Core.Services { public class AzureQueueBlockIpService : IBlockIpService { - private readonly CloudQueue _blockIpQueue; - private readonly CloudQueue _unblockIpQueue; - private bool _didInit = false; + private readonly QueueClient _blockIpQueueClient; + private readonly QueueClient _unblockIpQueueClient; private Tuple _lastBlock; public AzureQueueBlockIpService( GlobalSettings globalSettings) { - var storageAccount = CloudStorageAccount.Parse(globalSettings.Storage.ConnectionString); - var queueClient = storageAccount.CreateCloudQueueClient(); - - _blockIpQueue = queueClient.GetQueueReference("blockip"); - _unblockIpQueue = queueClient.GetQueueReference("unblockip"); + _blockIpQueueClient = new QueueClient(globalSettings.Storage.ConnectionString, "blockip"); + _unblockIpQueueClient = new QueueClient(globalSettings.Storage.ConnectionString, "unblockip"); } public async Task BlockIpAsync(string ipAddress, bool permanentBlock) { - await InitAsync(); var now = DateTime.UtcNow; if(_lastBlock != null && _lastBlock.Item1 == ipAddress && _lastBlock.Item2 == permanentBlock && (now - _lastBlock.Item3) < TimeSpan.FromMinutes(1)) @@ -35,24 +28,11 @@ namespace Bit.Core.Services } _lastBlock = new Tuple(ipAddress, permanentBlock, now); - var message = new CloudQueueMessage(ipAddress); - await _blockIpQueue.AddMessageAsync(message); + await _blockIpQueueClient.SendMessageAsync(ipAddress); if(!permanentBlock) { - await _unblockIpQueue.AddMessageAsync(message, null, new TimeSpan(0, 15, 0), null, null); + await _unblockIpQueueClient.SendMessageAsync(ipAddress, new TimeSpan(0, 15, 0)); } } - - private async Task InitAsync() - { - if(_didInit) - { - return; - } - - await _blockIpQueue.CreateIfNotExistsAsync(); - await _unblockIpQueue.CreateIfNotExistsAsync(); - _didInit = true; - } } } diff --git a/src/Core/Services/Implementations/AzureQueueEventWriteService.cs b/src/Core/Services/Implementations/AzureQueueEventWriteService.cs index 8f687475ce..51be0c7571 100644 --- a/src/Core/Services/Implementations/AzureQueueEventWriteService.cs +++ b/src/Core/Services/Implementations/AzureQueueEventWriteService.cs @@ -1,8 +1,6 @@ using System.Threading.Tasks; -using Bit.Core.Repositories; using System.Collections.Generic; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Queue; +using Azure.Storage.Queues; using Newtonsoft.Json; using Bit.Core.Models.Data; @@ -10,8 +8,7 @@ namespace Bit.Core.Services { public class AzureQueueEventWriteService : IEventWriteService { - private readonly CloudQueue _queue; - private readonly GlobalSettings _globalSettings; + private readonly QueueClient _queueClient; private JsonSerializerSettings _jsonSettings = new JsonSerializerSettings { @@ -19,28 +16,21 @@ namespace Bit.Core.Services }; public AzureQueueEventWriteService( - IEventRepository eventRepository, GlobalSettings globalSettings) { - var storageAccount = CloudStorageAccount.Parse(globalSettings.Events.ConnectionString); - var queueClient = storageAccount.CreateCloudQueueClient(); - - _queue = queueClient.GetQueueReference("event"); - _globalSettings = globalSettings; + _queueClient = new QueueClient(globalSettings.Events.ConnectionString, "event"); } public async Task CreateAsync(IEvent e) { var json = JsonConvert.SerializeObject(e, _jsonSettings); - var message = new CloudQueueMessage(json); - await _queue.AddMessageAsync(message); + await _queueClient.SendMessageAsync(json); } public async Task CreateManyAsync(IList e) { var json = JsonConvert.SerializeObject(e, _jsonSettings); - var message = new CloudQueueMessage(json); - await _queue.AddMessageAsync(message); + await _queueClient.SendMessageAsync(json); } } } diff --git a/src/Core/Services/Implementations/AzureQueuePushNotificationService.cs b/src/Core/Services/Implementations/AzureQueuePushNotificationService.cs index 6022d45bae..28ef9120f1 100644 --- a/src/Core/Services/Implementations/AzureQueuePushNotificationService.cs +++ b/src/Core/Services/Implementations/AzureQueuePushNotificationService.cs @@ -4,8 +4,7 @@ using Bit.Core.Models.Table; using Bit.Core.Enums; using Newtonsoft.Json; using Bit.Core.Models; -using Microsoft.WindowsAzure.Storage.Queue; -using Microsoft.WindowsAzure.Storage; +using Azure.Storage.Queues; using Microsoft.AspNetCore.Http; using System.Collections.Generic; @@ -13,7 +12,7 @@ namespace Bit.Core.Services { public class AzureQueuePushNotificationService : IPushNotificationService { - private readonly CloudQueue _queue; + private readonly QueueClient _queueClient; private readonly GlobalSettings _globalSettings; private readonly IHttpContextAccessor _httpContextAccessor; @@ -26,9 +25,7 @@ namespace Bit.Core.Services GlobalSettings globalSettings, IHttpContextAccessor httpContextAccessor) { - var storageAccount = CloudStorageAccount.Parse(globalSettings.Notifications.ConnectionString); - var queueClient = storageAccount.CreateCloudQueueClient(); - _queue = queueClient.GetQueueReference("notifications"); + _queueClient = new QueueClient(globalSettings.Notifications.ConnectionString, "notifications"); _globalSettings = globalSettings; _httpContextAccessor = httpContextAccessor; } @@ -143,8 +140,7 @@ namespace Bit.Core.Services var contextId = GetContextIdentifier(excludeCurrentContext); var message = JsonConvert.SerializeObject(new PushNotificationData(type, payload, contextId), _jsonSettings); - var queueMessage = new CloudQueueMessage(message); - await _queue.AddMessageAsync(queueMessage); + await _queueClient.SendMessageAsync(message); } private string GetContextIdentifier(bool excludeCurrentContext) diff --git a/src/Core/Services/Implementations/LicensingService.cs b/src/Core/Services/Implementations/LicensingService.cs index 3dff230cf3..aa904da944 100644 --- a/src/Core/Services/Implementations/LicensingService.cs +++ b/src/Core/Services/Implementations/LicensingService.cs @@ -4,7 +4,7 @@ using Bit.Core.Repositories; using Bit.Core.Utilities; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Logging; -using Microsoft.WindowsAzure.Storage; +using Microsoft.Azure.Storage; using Newtonsoft.Json; using System; using System.Collections.Generic; diff --git a/src/Core/Utilities/CoreHelpers.cs b/src/Core/Utilities/CoreHelpers.cs index b1b28858a5..a9652c9f53 100644 --- a/src/Core/Utilities/CoreHelpers.cs +++ b/src/Core/Utilities/CoreHelpers.cs @@ -16,7 +16,8 @@ using System.Web; using Microsoft.AspNetCore.DataProtection; using Bit.Core.Enums; using System.Threading.Tasks; -using Microsoft.WindowsAzure.Storage; +using Microsoft.Azure.Storage; +using Microsoft.Azure.Storage.Blob; namespace Bit.Core.Utilities { diff --git a/src/Core/Utilities/LoggerFactoryExtensions.cs b/src/Core/Utilities/LoggerFactoryExtensions.cs index f207d23bb8..4992347876 100644 --- a/src/Core/Utilities/LoggerFactoryExtensions.cs +++ b/src/Core/Utilities/LoggerFactoryExtensions.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Serilog; using Serilog.Events; @@ -12,8 +13,8 @@ namespace Bit.Core.Utilities { public static void UseSerilog( this IApplicationBuilder appBuilder, - IHostingEnvironment env, - IApplicationLifetime applicationLifetime, + IWebHostEnvironment env, + IHostApplicationLifetime applicationLifetime, GlobalSettings globalSettings) { if(env.IsDevelopment()) diff --git a/src/Core/Utilities/ServiceCollectionExtensions.cs b/src/Core/Utilities/ServiceCollectionExtensions.cs index 14b6db8c35..741cc56dca 100644 --- a/src/Core/Utilities/ServiceCollectionExtensions.cs +++ b/src/Core/Utilities/ServiceCollectionExtensions.cs @@ -15,7 +15,6 @@ using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore.Http; -using Microsoft.WindowsAzure.Storage; using System; using System.IO; using SqlServerRepos = Bit.Core.Repositories.SqlServer; @@ -34,6 +33,9 @@ using System.Security.Cryptography.X509Certificates; using Bit.Core.Utilities; using Serilog.Context; using AutoMapper; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Hosting; +using Microsoft.Azure.Storage; namespace Bit.Core.Utilities { @@ -41,11 +43,22 @@ namespace Bit.Core.Utilities { public static void AddSqlServerRepositories(this IServiceCollection services, GlobalSettings globalSettings) { - if(!string.IsNullOrWhiteSpace(globalSettings.PostgreSql?.ConnectionString)) + var usePostgreSql = !string.IsNullOrWhiteSpace(globalSettings.PostgreSql?.ConnectionString); + var useEf = usePostgreSql; + + if(useEf) { services.AddAutoMapper(typeof(EntityFrameworkRepos.UserRepository)); - services.AddDbContext(); + services.AddDbContext(options => + { + if(usePostgreSql) + { + options.UseNpgsql(globalSettings.PostgreSql.ConnectionString); + } + }); services.AddSingleton(); + //services.AddSingleton(); + //services.AddSingleton(); } else { @@ -67,7 +80,14 @@ namespace Bit.Core.Utilities if(globalSettings.SelfHosted) { - services.AddSingleton(); + if(useEf) + { + // TODO + } + else + { + services.AddSingleton(); + } services.AddSingleton(); services.AddSingleton(); } @@ -283,7 +303,7 @@ namespace Bit.Core.Utilities } public static void AddIdentityAuthenticationServices( - this IServiceCollection services, GlobalSettings globalSettings, IHostingEnvironment environment, + this IServiceCollection services, GlobalSettings globalSettings, IWebHostEnvironment environment, Action addAuthorization) { services @@ -313,7 +333,7 @@ namespace Bit.Core.Utilities } public static IIdentityServerBuilder AddCustomIdentityServerServices( - this IServiceCollection services, IHostingEnvironment env, GlobalSettings globalSettings) + this IServiceCollection services, IWebHostEnvironment env, GlobalSettings globalSettings) { var issuerUri = new Uri(globalSettings.BaseServiceUri.InternalIdentity); var identityServerBuilder = services @@ -373,7 +393,7 @@ namespace Bit.Core.Utilities } public static void AddCustomDataProtectionServices( - this IServiceCollection services, IHostingEnvironment env, GlobalSettings globalSettings) + this IServiceCollection services, IWebHostEnvironment env, GlobalSettings globalSettings) { if(env.IsDevelopment()) { @@ -417,7 +437,7 @@ namespace Bit.Core.Utilities } public static void UseDefaultMiddleware(this IApplicationBuilder app, - IHostingEnvironment env, GlobalSettings globalSettings) + IWebHostEnvironment env, GlobalSettings globalSettings) { string GetHeaderValue(HttpContext httpContext, string header) { diff --git a/src/Events/Dockerfile b/src/Events/Dockerfile index 055b362333..84740e3c34 100644 --- a/src/Events/Dockerfile +++ b/src/Events/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 +FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 LABEL com.bitwarden.product="bitwarden" diff --git a/src/Events/Events.csproj b/src/Events/Events.csproj index ea60495821..86710180b2 100644 --- a/src/Events/Events.csproj +++ b/src/Events/Events.csproj @@ -2,17 +2,12 @@ 1.33.0 - netcoreapp2.2 + netcoreapp3.1 Bit.Events bitwarden-Events false - InProcess - - - - diff --git a/src/Events/Program.cs b/src/Events/Program.cs index a3df4648ff..91c52af225 100644 --- a/src/Events/Program.cs +++ b/src/Events/Program.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Hosting; using Bit.Core.Utilities; using Serilog.Events; +using Microsoft.Extensions.Hosting; namespace Bit.Events { @@ -9,28 +10,31 @@ namespace Bit.Events { public static void Main(string[] args) { - WebHost + Host .CreateDefaultBuilder(args) - .UseStartup() - .ConfigureLogging((hostingContext, logging) => - logging.AddSerilog(hostingContext, e => - { - var context = e.Properties["SourceContext"].ToString(); - if(context.Contains("IdentityServer4.Validation.TokenValidator") || - context.Contains("IdentityServer4.Validation.TokenRequestValidator")) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + webBuilder.ConfigureLogging((hostingContext, logging) => + logging.AddSerilog(hostingContext, e => { - return e.Level > LogEventLevel.Error; - } + var context = e.Properties["SourceContext"].ToString(); + if(context.Contains("IdentityServer4.Validation.TokenValidator") || + context.Contains("IdentityServer4.Validation.TokenRequestValidator")) + { + return e.Level > LogEventLevel.Error; + } - if(e.Properties.ContainsKey("RequestPath") && - !string.IsNullOrWhiteSpace(e.Properties["RequestPath"]?.ToString()) && - (context.Contains(".Server.Kestrel") || context.Contains(".Core.IISHttpServer"))) - { - return false; - } + if(e.Properties.ContainsKey("RequestPath") && + !string.IsNullOrWhiteSpace(e.Properties["RequestPath"]?.ToString()) && + (context.Contains(".Server.Kestrel") || context.Contains(".Core.IISHttpServer"))) + { + return false; + } - return e.Level >= LogEventLevel.Error; - })) + return e.Level >= LogEventLevel.Error; + })); + }) .Build() .Run(); } diff --git a/src/Events/Startup.cs b/src/Events/Startup.cs index 5f3020d524..c4ac34b094 100644 --- a/src/Events/Startup.cs +++ b/src/Events/Startup.cs @@ -7,12 +7,13 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace Bit.Events { public class Startup { - public Startup(IHostingEnvironment env, IConfiguration configuration) + public Startup(IWebHostEnvironment env, IConfiguration configuration) { CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); Configuration = configuration; @@ -20,7 +21,7 @@ namespace Bit.Events } public IConfiguration Configuration { get; } - public IHostingEnvironment Environment { get; set; } + public IWebHostEnvironment Environment { get; set; } public void ConfigureServices(IServiceCollection services) { @@ -82,8 +83,8 @@ namespace Bit.Events public void Configure( IApplicationBuilder app, - IHostingEnvironment env, - IApplicationLifetime appLifetime, + IWebHostEnvironment env, + IHostApplicationLifetime appLifetime, GlobalSettings globalSettings) { app.UseSerilog(env, appLifetime, globalSettings); @@ -96,18 +97,22 @@ namespace Bit.Events // Default Middleware app.UseDefaultMiddleware(env, globalSettings); + // Add routing + app.UseRouting(); + // Add Cors app.UseCors(policy => policy.SetIsOriginAllowed(h => true) .AllowAnyMethod().AllowAnyHeader().AllowCredentials()); - // Add authentication to the request pipeline. + // Add authentication and authorization to the request pipeline. app.UseAuthentication(); + app.UseAuthorization(); // Add current context app.UseMiddleware(); // Add MVC to the request pipeline. - app.UseMvc(); + app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); } } } diff --git a/src/EventsProcessor/AzureQueueHostedService.cs b/src/EventsProcessor/AzureQueueHostedService.cs index 48eda65312..f672bc39cd 100644 --- a/src/EventsProcessor/AzureQueueHostedService.cs +++ b/src/EventsProcessor/AzureQueueHostedService.cs @@ -9,8 +9,7 @@ using Bit.Core.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Queue; +using Azure.Storage.Queues; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -23,7 +22,7 @@ namespace Bit.EventsProcessor private Task _executingTask; private CancellationTokenSource _cts; - private CloudQueue _queue; + private QueueClient _queueClient; private IEventWriteService _eventWriteService; public AzureQueueHostedService( @@ -67,23 +66,19 @@ namespace Bit.EventsProcessor var repo = new Core.Repositories.TableStorage.EventRepository(storageConnectionString); _eventWriteService = new RepositoryEventWriteService(repo); - - var storageAccount = CloudStorageAccount.Parse(storageConnectionString); - var queueClient = storageAccount.CreateCloudQueueClient(); - _queue = queueClient.GetQueueReference("event"); + _queueClient = new QueueClient(storageConnectionString, "event"); while(!cancellationToken.IsCancellationRequested) { try { - var messages = await _queue.GetMessagesAsync(32, TimeSpan.FromMinutes(1), - null, null, cancellationToken); - if(messages.Any()) + var messages = await _queueClient.ReceiveMessagesAsync(32); + if(messages.Value?.Any() ?? false) { - foreach(var message in messages) + foreach(var message in messages.Value) { - await ProcessQueueMessageAsync(message.AsString, cancellationToken); - await _queue.DeleteMessageAsync(message); + await ProcessQueueMessageAsync(message.MessageText, cancellationToken); + await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt); } } else diff --git a/src/EventsProcessor/EventsProcessor.csproj b/src/EventsProcessor/EventsProcessor.csproj index 0a48640a69..f5fd9f51c6 100644 --- a/src/EventsProcessor/EventsProcessor.csproj +++ b/src/EventsProcessor/EventsProcessor.csproj @@ -2,16 +2,11 @@ 1.33.0 - netcoreapp2.2 + netcoreapp3.1 Bit.EventsProcessor bitwarden-EventsProcessor - InProcess - - - - diff --git a/src/EventsProcessor/Program.cs b/src/EventsProcessor/Program.cs index de0d1aba01..0f4176e101 100644 --- a/src/EventsProcessor/Program.cs +++ b/src/EventsProcessor/Program.cs @@ -1,7 +1,7 @@ -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting; using Bit.Core.Utilities; using Serilog.Events; +using Microsoft.Extensions.Hosting; namespace Bit.EventsProcessor { @@ -9,11 +9,14 @@ namespace Bit.EventsProcessor { public static void Main(string[] args) { - WebHost + Host .CreateDefaultBuilder(args) - .UseStartup() - .ConfigureLogging((hostingContext, logging) => - logging.AddSerilog(hostingContext, e => e.Level >= LogEventLevel.Warning)) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + webBuilder.ConfigureLogging((hostingContext, logging) => + logging.AddSerilog(hostingContext, e => e.Level >= LogEventLevel.Warning)); + }) .Build() .Run(); } diff --git a/src/EventsProcessor/Startup.cs b/src/EventsProcessor/Startup.cs index ce1c8c5e9e..1fc7d5d13b 100644 --- a/src/EventsProcessor/Startup.cs +++ b/src/EventsProcessor/Startup.cs @@ -6,13 +6,14 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.IdentityModel.Logging; namespace Bit.EventsProcessor { public class Startup { - public Startup(IHostingEnvironment env, IConfiguration configuration) + public Startup(IWebHostEnvironment env, IConfiguration configuration) { CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); Configuration = configuration; @@ -20,7 +21,7 @@ namespace Bit.EventsProcessor } public IConfiguration Configuration { get; } - public IHostingEnvironment Environment { get; set; } + public IWebHostEnvironment Environment { get; set; } public void ConfigureServices(IServiceCollection services) { @@ -36,18 +37,18 @@ namespace Bit.EventsProcessor public void Configure( IApplicationBuilder app, - IHostingEnvironment env, - IApplicationLifetime appLifetime, + IWebHostEnvironment env, + IHostApplicationLifetime appLifetime, GlobalSettings globalSettings) { IdentityModelEventSource.ShowPII = true; app.UseSerilog(env, appLifetime, globalSettings); - app.Map("/alive", HandleMapAlive); - } - - private static void HandleMapAlive(IApplicationBuilder app) - { - app.Run(async context => await context.Response.WriteAsync(System.DateTime.UtcNow.ToString())); + app.UseRouting(); + app.UseEndpoints(endpoints => + { + endpoints.MapGet("/alive", + async context => await context.Response.WriteAsync(System.DateTime.UtcNow.ToString())); + }); } } } diff --git a/src/Icons/Dockerfile b/src/Icons/Dockerfile index 7e7235cb0a..6f9a6ff825 100644 --- a/src/Icons/Dockerfile +++ b/src/Icons/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 +FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 LABEL com.bitwarden.product="bitwarden" diff --git a/src/Icons/Icons.csproj b/src/Icons/Icons.csproj index b4400e010e..7ada3e9db2 100644 --- a/src/Icons/Icons.csproj +++ b/src/Icons/Icons.csproj @@ -2,7 +2,7 @@ 1.33.0 - netcoreapp2.2 + netcoreapp3.1 Bit.Icons bitwarden-Icons false @@ -10,7 +10,6 @@ - diff --git a/src/Icons/Program.cs b/src/Icons/Program.cs index 0282b48e5a..e5ffb8179c 100644 --- a/src/Icons/Program.cs +++ b/src/Icons/Program.cs @@ -1,7 +1,7 @@ -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting; using Bit.Core.Utilities; using Serilog.Events; +using Microsoft.Extensions.Hosting; namespace Bit.Icons { @@ -9,11 +9,14 @@ namespace Bit.Icons { public static void Main(string[] args) { - WebHost + Host .CreateDefaultBuilder(args) - .UseStartup() - .ConfigureLogging((hostingContext, logging) => - logging.AddSerilog(hostingContext, e => e.Level >= LogEventLevel.Error)) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + webBuilder.ConfigureLogging((hostingContext, logging) => + logging.AddSerilog(hostingContext, e => e.Level >= LogEventLevel.Error)); + }) .Build() .Run(); } diff --git a/src/Icons/Startup.cs b/src/Icons/Startup.cs index fc49dec4e7..8a50b7c44e 100644 --- a/src/Icons/Startup.cs +++ b/src/Icons/Startup.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.Net.Http.Headers; namespace Bit.Icons @@ -49,8 +50,8 @@ namespace Bit.Icons public void Configure( IApplicationBuilder app, - IHostingEnvironment env, - IApplicationLifetime appLifetime, + IWebHostEnvironment env, + IHostApplicationLifetime appLifetime, GlobalSettings globalSettings) { app.UseSerilog(env, appLifetime, globalSettings); @@ -70,7 +71,8 @@ namespace Bit.Icons await next(); }); - app.UseMvc(); + app.UseRouting(); + app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); } } } diff --git a/src/Identity/Dockerfile b/src/Identity/Dockerfile index 06d255f6a6..e2228a2577 100644 --- a/src/Identity/Dockerfile +++ b/src/Identity/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 +FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 LABEL com.bitwarden.product="bitwarden" diff --git a/src/Identity/Identity.csproj b/src/Identity/Identity.csproj index bfb69f0656..c7ccc903bf 100644 --- a/src/Identity/Identity.csproj +++ b/src/Identity/Identity.csproj @@ -2,19 +2,14 @@ 1.33.0 - netcoreapp2.2 + netcoreapp3.1 Bit.Identity bitwarden-Identity false - InProcess - - - - diff --git a/src/Identity/Program.cs b/src/Identity/Program.cs index c890d0c8ff..626fd87cc1 100644 --- a/src/Identity/Program.cs +++ b/src/Identity/Program.cs @@ -1,8 +1,8 @@ -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting; using Bit.Core.Utilities; using Serilog.Events; using AspNetCoreRateLimit; +using Microsoft.Extensions.Hosting; namespace Bit.Identity { @@ -10,27 +10,30 @@ namespace Bit.Identity { public static void Main(string[] args) { - WebHost + Host .CreateDefaultBuilder(args) - .UseStartup() - .ConfigureLogging((hostingContext, logging) => - logging.AddSerilog(hostingContext, e => - { - var context = e.Properties["SourceContext"].ToString(); - if(context.Contains(typeof(IpRateLimitMiddleware).FullName) && - e.Level == LogEventLevel.Information) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + webBuilder.ConfigureLogging((hostingContext, logging) => + logging.AddSerilog(hostingContext, e => { - return true; - } + var context = e.Properties["SourceContext"].ToString(); + if(context.Contains(typeof(IpRateLimitMiddleware).FullName) && + e.Level == LogEventLevel.Information) + { + return true; + } - if(context.Contains("IdentityServer4.Validation.TokenValidator") || - context.Contains("IdentityServer4.Validation.TokenRequestValidator")) - { - return e.Level > LogEventLevel.Error; - } + if(context.Contains("IdentityServer4.Validation.TokenValidator") || + context.Contains("IdentityServer4.Validation.TokenRequestValidator")) + { + return e.Level > LogEventLevel.Error; + } - return e.Level >= LogEventLevel.Error; - })) + return e.Level >= LogEventLevel.Error; + })); + }) .Build() .Run(); } diff --git a/src/Identity/Startup.cs b/src/Identity/Startup.cs index c4d513893b..b2b4612db0 100644 --- a/src/Identity/Startup.cs +++ b/src/Identity/Startup.cs @@ -8,12 +8,13 @@ using Bit.Core.Utilities; using AspNetCoreRateLimit; using System.Globalization; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Hosting; namespace Bit.Identity { public class Startup { - public Startup(IHostingEnvironment env, IConfiguration configuration) + public Startup(IWebHostEnvironment env, IConfiguration configuration) { CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); Configuration = configuration; @@ -21,7 +22,7 @@ namespace Bit.Identity } public IConfiguration Configuration { get; private set; } - public IHostingEnvironment Environment { get; set; } + public IWebHostEnvironment Environment { get; set; } public void ConfigureServices(IServiceCollection services) { @@ -74,8 +75,8 @@ namespace Bit.Identity public void Configure( IApplicationBuilder app, - IHostingEnvironment env, - IApplicationLifetime appLifetime, + IWebHostEnvironment env, + IHostApplicationLifetime appLifetime, GlobalSettings globalSettings, ILogger logger) { diff --git a/src/Notifications/AzureQueueHostedService.cs b/src/Notifications/AzureQueueHostedService.cs index ae4ba2e2da..f27696e2d5 100644 --- a/src/Notifications/AzureQueueHostedService.cs +++ b/src/Notifications/AzureQueueHostedService.cs @@ -6,8 +6,7 @@ using Bit.Core; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Queue; +using Azure.Storage.Queues; namespace Bit.Notifications { @@ -19,7 +18,7 @@ namespace Bit.Notifications private Task _executingTask; private CancellationTokenSource _cts; - private CloudQueue _queue; + private QueueClient _queueClient; public AzureQueueHostedService( ILogger logger, @@ -55,33 +54,29 @@ namespace Bit.Notifications private async Task ExecuteAsync(CancellationToken cancellationToken) { - var storageAccount = CloudStorageAccount.Parse(_globalSettings.Notifications.ConnectionString); - var queueClient = storageAccount.CreateCloudQueueClient(); - _queue = queueClient.GetQueueReference("notifications"); - + _queueClient = new QueueClient(_globalSettings.Notifications.ConnectionString, "notifications"); while(!cancellationToken.IsCancellationRequested) { try { - var messages = await _queue.GetMessagesAsync(32, TimeSpan.FromMinutes(1), - null, null, cancellationToken); - if(messages.Any()) + var messages = await _queueClient.ReceiveMessagesAsync(32); + if(messages.Value?.Any() ?? false) { - foreach(var message in messages) + foreach(var message in messages.Value) { try { await HubHelpers.SendNotificationToHubAsync( - message.AsString, _hubContext, cancellationToken); - await _queue.DeleteMessageAsync(message); + message.MessageText, _hubContext, cancellationToken); + await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt); } catch(Exception e) { _logger.LogError("Error processing dequeued message: " + - $"{message.Id} x{message.DequeueCount}.", e); + $"{message.MessageId} x{message.DequeueCount}.", e); if(message.DequeueCount > 2) { - await _queue.DeleteMessageAsync(message); + await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt); } } } diff --git a/src/Notifications/Dockerfile b/src/Notifications/Dockerfile index 60eb13d828..e78ea95113 100644 --- a/src/Notifications/Dockerfile +++ b/src/Notifications/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 +FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 LABEL com.bitwarden.product="bitwarden" diff --git a/src/Notifications/Notifications.csproj b/src/Notifications/Notifications.csproj index 9eec591d79..485988ab4f 100644 --- a/src/Notifications/Notifications.csproj +++ b/src/Notifications/Notifications.csproj @@ -2,15 +2,13 @@ 1.33.0 - netcoreapp2.2 + netcoreapp3.1 Bit.Notifications bitwarden-Notifications - InProcess - - + diff --git a/src/Notifications/Program.cs b/src/Notifications/Program.cs index ea923f412f..a5780e101a 100644 --- a/src/Notifications/Program.cs +++ b/src/Notifications/Program.cs @@ -1,7 +1,7 @@ -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting; using Bit.Core.Utilities; using Serilog.Events; +using Microsoft.Extensions.Hosting; namespace Bit.Notifications { @@ -9,39 +9,42 @@ namespace Bit.Notifications { public static void Main(string[] args) { - WebHost + Host .CreateDefaultBuilder(args) - .UseStartup() - .ConfigureLogging((hostingContext, logging) => - logging.AddSerilog(hostingContext, e => - { - var context = e.Properties["SourceContext"].ToString(); - if(context.Contains("IdentityServer4.Validation.TokenValidator") || - context.Contains("IdentityServer4.Validation.TokenRequestValidator")) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + webBuilder.ConfigureLogging((hostingContext, logging) => + logging.AddSerilog(hostingContext, e => { - return e.Level > LogEventLevel.Error; - } + var context = e.Properties["SourceContext"].ToString(); + if(context.Contains("IdentityServer4.Validation.TokenValidator") || + context.Contains("IdentityServer4.Validation.TokenRequestValidator")) + { + return e.Level > LogEventLevel.Error; + } - if(e.Level == LogEventLevel.Error && - e.MessageTemplate.Text == "Failed connection handshake.") - { - return false; - } + if(e.Level == LogEventLevel.Error && + e.MessageTemplate.Text == "Failed connection handshake.") + { + return false; + } - if(e.Level == LogEventLevel.Error && - e.MessageTemplate.Text.StartsWith("Failed writing message.")) - { - return false; - } + if(e.Level == LogEventLevel.Error && + e.MessageTemplate.Text.StartsWith("Failed writing message.")) + { + return false; + } - if(e.Level == LogEventLevel.Warning && - e.MessageTemplate.Text.StartsWith("Heartbeat took longer")) - { - return false; - } + if(e.Level == LogEventLevel.Warning && + e.MessageTemplate.Text.StartsWith("Heartbeat took longer")) + { + return false; + } - return e.Level >= LogEventLevel.Warning; - })) + return e.Level >= LogEventLevel.Warning; + })); + }) .Build() .Run(); } diff --git a/src/Notifications/Startup.cs b/src/Notifications/Startup.cs index 168ff9c4be..f906578c1e 100644 --- a/src/Notifications/Startup.cs +++ b/src/Notifications/Startup.cs @@ -8,13 +8,14 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.IdentityModel.Logging; namespace Bit.Notifications { public class Startup { - public Startup(IHostingEnvironment env, IConfiguration configuration) + public Startup(IWebHostEnvironment env, IConfiguration configuration) { CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); Configuration = configuration; @@ -22,7 +23,7 @@ namespace Bit.Notifications } public IConfiguration Configuration { get; } - public IHostingEnvironment Environment { get; set; } + public IWebHostEnvironment Environment { get; set; } public void ConfigureServices(IServiceCollection services) { @@ -81,8 +82,8 @@ namespace Bit.Notifications public void Configure( IApplicationBuilder app, - IHostingEnvironment env, - IApplicationLifetime appLifetime, + IWebHostEnvironment env, + IHostApplicationLifetime appLifetime, GlobalSettings globalSettings) { IdentityModelEventSource.ShowPII = true; @@ -93,29 +94,38 @@ namespace Bit.Notifications app.UseDeveloperExceptionPage(); } + // Add routing + app.UseRouting(); + // Add Cors app.UseCors(policy => policy.SetIsOriginAllowed(h => true) .AllowAnyMethod().AllowAnyHeader().AllowCredentials()); // Add authentication to the request pipeline. app.UseAuthentication(); + app.UseAuthorization(); // Add SignlarR - if(!string.IsNullOrWhiteSpace(globalSettings.Notifications?.AzureSignalRConnectionString)) + var useAzureSignalR = !string.IsNullOrWhiteSpace( + globalSettings.Notifications?.AzureSignalRConnectionString); + if(useAzureSignalR) { app.UseAzureSignalR(routes => routes.MapHub("/hub")); } - else - { - app.UseSignalR(routes => routes.MapHub("/hub", options => - { - options.ApplicationMaxBufferSize = 2048; // client => server messages are not even used - options.TransportMaxBufferSize = 4096; - })); - } - // Add MVC to the request pipeline. - app.UseMvc(); + // Add endpoints to the request pipeline. + app.UseEndpoints(endpoints => + { + if(!useAzureSignalR) + { + endpoints.MapHub("/hub", options => + { + options.ApplicationMaxBufferSize = 2048; // client => server messages are not even used + options.TransportMaxBufferSize = 4096; + }); + } + endpoints.MapDefaultControllerRoute(); + }); } } } diff --git a/test/Core.Test/Core.Test.csproj b/test/Core.Test/Core.Test.csproj index b04daac175..fc27843124 100644 --- a/test/Core.Test/Core.Test.csproj +++ b/test/Core.Test/Core.Test.csproj @@ -1,13 +1,13 @@  - netcoreapp2.2 + netcoreapp3.1 false Bit.Core.Test - + diff --git a/test/Core.Test/Services/AzureQueueEventWriteServiceTests.cs b/test/Core.Test/Services/AzureQueueEventWriteServiceTests.cs index c5921989ef..e479f30347 100644 --- a/test/Core.Test/Services/AzureQueueEventWriteServiceTests.cs +++ b/test/Core.Test/Services/AzureQueueEventWriteServiceTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using Bit.Core.Repositories; using Bit.Core.Services; using NSubstitute; @@ -19,7 +19,6 @@ namespace Bit.Core.Test.Services _eventRepository = Substitute.For(); _sut = new AzureQueueEventWriteService( - _eventRepository, _globalSettings ); } diff --git a/util/Migrator/Migrator.csproj b/util/Migrator/Migrator.csproj index e7d5d98e60..39c69112b2 100644 --- a/util/Migrator/Migrator.csproj +++ b/util/Migrator/Migrator.csproj @@ -1,7 +1,7 @@  - netcoreapp2.2 + netcoreapp3.1 Bit.Migrator @@ -11,7 +11,7 @@ - + diff --git a/util/Server/Dockerfile b/util/Server/Dockerfile index 9fb35fcf13..8715a88d14 100644 --- a/util/Server/Dockerfile +++ b/util/Server/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 +FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 LABEL com.bitwarden.product="bitwarden" diff --git a/util/Server/Server.csproj b/util/Server/Server.csproj index 96b99c6a6a..9c415cba45 100644 --- a/util/Server/Server.csproj +++ b/util/Server/Server.csproj @@ -1,14 +1,9 @@  - netcoreapp2.2 + netcoreapp3.1 false Bit.Server - InProcess - - - - diff --git a/util/Server/Startup.cs b/util/Server/Startup.cs index c27ce1ec24..9f98009325 100644 --- a/util/Server/Startup.cs +++ b/util/Server/Startup.cs @@ -33,12 +33,17 @@ namespace Bit.Server { if(configuration.GetValue("serveUnknown") ?? false) { - app.Map("/alive", HandleMapAlive); app.UseStaticFiles(new StaticFileOptions { ServeUnknownFileTypes = true, DefaultContentType = "application/octet-stream" }); + app.UseRouting(); + app.UseEndpoints(endpoints => + { + endpoints.MapGet("/alive", + async context => await context.Response.WriteAsync(System.DateTime.UtcNow.ToString())); + }); } else if(configuration.GetValue("webVault") ?? false) { @@ -71,14 +76,14 @@ namespace Bit.Server } else { - app.Map("/alive", HandleMapAlive); app.UseFileServer(); + app.UseRouting(); + app.UseEndpoints(endpoints => + { + endpoints.MapGet("/alive", + async context => await context.Response.WriteAsync(System.DateTime.UtcNow.ToString())); + }); } } - - private static void HandleMapAlive(IApplicationBuilder app) - { - app.Run(async context => await context.Response.WriteAsync(System.DateTime.UtcNow.ToString())); - } } } diff --git a/util/Setup/Dockerfile b/util/Setup/Dockerfile index d724debdab..c0c4fe6260 100644 --- a/util/Setup/Dockerfile +++ b/util/Setup/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/core/runtime:2.2 +FROM mcr.microsoft.com/dotnet/core/runtime:3.1 LABEL com.bitwarden.product="bitwarden" com.bitwarden.project="setup" diff --git a/util/Setup/Setup.csproj b/util/Setup/Setup.csproj index 43a98a4a5a..19e237bd72 100644 --- a/util/Setup/Setup.csproj +++ b/util/Setup/Setup.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp2.2 + netcoreapp3.1 1701;1702;1705;NU1701 Bit.Setup @@ -13,7 +13,7 @@ - +