diff --git a/src/Api/Jobs/AliveJob.cs b/src/Api/Jobs/AliveJob.cs index 97f712b41..f9e2811a9 100644 --- a/src/Api/Jobs/AliveJob.cs +++ b/src/Api/Jobs/AliveJob.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using Bit.Core; using Bit.Core.Jobs; using Microsoft.Extensions.Logging; using Quartz; @@ -12,7 +13,7 @@ namespace Bit.Api.Jobs protected override Task ExecuteJobAsync(IJobExecutionContext context) { - _logger.LogInformation("It's alive!"); + _logger.LogInformation(Constants.BypassFiltersEventId, null, "It's alive!"); return Task.FromResult(0); } } diff --git a/src/Api/Startup.cs b/src/Api/Startup.cs index 46784a52d..d4eb7dd89 100644 --- a/src/Api/Startup.cs +++ b/src/Api/Startup.cs @@ -141,9 +141,7 @@ namespace Bit.Api return false; } - if(e.Level == LogEventLevel.Information && - (context.Contains(typeof(IpRateLimitMiddleware).FullName) || - context.StartsWith("\"Bit.Api.Jobs") || context.StartsWith("\"Bit.Core.Jobs"))) + if(e.Level == LogEventLevel.Information && context.Contains(typeof(IpRateLimitMiddleware).FullName)) { return true; } diff --git a/src/Core/Constants.cs b/src/Core/Constants.cs new file mode 100644 index 000000000..bb507baa7 --- /dev/null +++ b/src/Core/Constants.cs @@ -0,0 +1,7 @@ +namespace Bit.Core +{ + public static class Constants + { + public const int BypassFiltersEventId = 12482444; + } +} diff --git a/src/Core/Jobs/JobListener.cs b/src/Core/Jobs/JobListener.cs index 4d44d65e2..16c08e072 100644 --- a/src/Core/Jobs/JobListener.cs +++ b/src/Core/Jobs/JobListener.cs @@ -26,14 +26,16 @@ namespace Bit.Core.Jobs public Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken)) { - _logger.LogInformation("Starting job {0} at {1}.", context.JobDetail.JobType.Name, DateTime.UtcNow); + _logger.LogInformation(Constants.BypassFiltersEventId, null, "Starting job {0} at {1}.", + context.JobDetail.JobType.Name, DateTime.UtcNow); return Task.FromResult(0); } public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken)) { - _logger.LogInformation("Finished job {0} at {1}.", context.JobDetail.JobType.Name, DateTime.UtcNow); + _logger.LogInformation(Constants.BypassFiltersEventId, null, "Finished job {0} at {1}.", + context.JobDetail.JobType.Name, DateTime.UtcNow); return Task.FromResult(0); } } diff --git a/src/Core/Services/Implementations/LicensingService.cs b/src/Core/Services/Implementations/LicensingService.cs index 249c0eba4..d766fe3d5 100644 --- a/src/Core/Services/Implementations/LicensingService.cs +++ b/src/Core/Services/Implementations/LicensingService.cs @@ -64,7 +64,8 @@ namespace Bit.Core.Services } var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync(); - _logger.LogInformation("Validating licenses for {0} organizations.", enabledOrgs.Count); + _logger.LogInformation(Constants.BypassFiltersEventId, null, + "Validating licenses for {0} organizations.", enabledOrgs.Count); foreach(var org in enabledOrgs) { @@ -95,7 +96,8 @@ namespace Bit.Core.Services private async Task DisableOrganizationAsync(Organization org, ILicense license, string reason) { - _logger.LogInformation("Organization {0} ({1}) has an invalid license and is being disabled. Reason: {2}", + _logger.LogInformation(Constants.BypassFiltersEventId, null, + "Organization {0} ({1}) has an invalid license and is being disabled. Reason: {2}", org.Id, org.Name, reason); org.Enabled = false; org.ExpirationDate = license?.Expires ?? DateTime.UtcNow; @@ -111,7 +113,8 @@ namespace Bit.Core.Services } var premiumUsers = await _userRepository.GetManyByPremiumAsync(true); - _logger.LogInformation("Validating premium for {0} users.", premiumUsers.Count); + _logger.LogInformation(Constants.BypassFiltersEventId, null, + "Validating premium for {0} users.", premiumUsers.Count); foreach(var user in premiumUsers) { @@ -119,14 +122,16 @@ namespace Bit.Core.Services } var nonPremiumUsers = await _userRepository.GetManyByPremiumAsync(false); - _logger.LogInformation("Checking to restore premium for {0} users.", nonPremiumUsers.Count); + _logger.LogInformation(Constants.BypassFiltersEventId, null, + "Checking to restore premium for {0} users.", nonPremiumUsers.Count); foreach(var user in nonPremiumUsers) { var details = await _organizationUserRepository.GetManyDetailsByUserAsync(user.Id); if(details.Any(d => d.SelfHost && d.UsersGetPremium && d.Enabled)) { - _logger.LogInformation("Granting premium to user {0}({1}) because they are in an active organization " + + _logger.LogInformation(Constants.BypassFiltersEventId, null, + "Granting premium to user {0}({1}) because they are in an active organization " + "with premium features.", user.Id, user.Email); user.Premium = true; @@ -170,7 +175,8 @@ namespace Bit.Core.Services _userCheckCache.Add(user.Id, now); } - _logger.LogInformation("Validating premium license for user {0}({1}).", user.Id, user.Email); + _logger.LogInformation(Constants.BypassFiltersEventId, null, + "Validating premium license for user {0}({1}).", user.Id, user.Email); return await ProcessUserValidationAsync(user); } @@ -196,8 +202,8 @@ namespace Bit.Core.Services if(!valid) { - _logger.LogInformation("User {0}({1}) has an invalid license and premium is being disabled.", - user.Id, user.Email); + _logger.LogInformation(Constants.BypassFiltersEventId, null, + "User {0}({1}) has an invalid license and premium is being disabled.", user.Id, user.Email); user.Premium = false; user.PremiumExpirationDate = license?.Expires ?? DateTime.UtcNow; diff --git a/src/Core/Utilities/CustomIpRateLimitMiddleware.cs b/src/Core/Utilities/CustomIpRateLimitMiddleware.cs index 7083dfb87..4b547bef3 100644 --- a/src/Core/Utilities/CustomIpRateLimitMiddleware.cs +++ b/src/Core/Utilities/CustomIpRateLimitMiddleware.cs @@ -16,7 +16,7 @@ namespace Bit.Core.Utilities private readonly IpRateLimitOptions _options; private readonly IMemoryCache _memoryCache; private readonly IBlockIpService _blockIpService; - private readonly ILogger _logger; + private readonly ILogger _logger; public CustomIpRateLimitMiddleware( IMemoryCache memoryCache, @@ -25,7 +25,7 @@ namespace Bit.Core.Utilities IOptions options, IRateLimitCounterStore counterStore, IIpPolicyStore policyStore, - ILogger logger, + ILogger logger, IIpAddressParser ipParser = null) : base(next, options, counterStore, policyStore, logger, ipParser) { @@ -59,11 +59,13 @@ namespace Bit.Core.Utilities if(blockedCount > 10) { _blockIpService.BlockIpAsync(identity.ClientIp, false); - _logger.LogInformation($"Banned {identity.ClientIp}. \nInfo: \n{GetRequestInfo(httpContext)}"); + _logger.LogInformation(Constants.BypassFiltersEventId, null, + "Banned {0}. \nInfo: \n{1}", identity.ClientIp, GetRequestInfo(httpContext)); } else { - _logger.LogInformation($"Request blocked {identity.ClientIp}. \nInfo: \n{GetRequestInfo(httpContext)}"); + _logger.LogInformation(Constants.BypassFiltersEventId, null, + "Request blocked {0}. \nInfo: \n{1}", identity.ClientIp, GetRequestInfo(httpContext)); _memoryCache.Set(key, blockedCount, new MemoryCacheEntryOptions().SetSlidingExpiration(new TimeSpan(0, 5, 0))); } diff --git a/src/Core/Utilities/LoggerFactoryExtensions.cs b/src/Core/Utilities/LoggerFactoryExtensions.cs index aabb65d03..308f72e0a 100644 --- a/src/Core/Utilities/LoggerFactoryExtensions.cs +++ b/src/Core/Utilities/LoggerFactoryExtensions.cs @@ -22,14 +22,23 @@ namespace Bit.Core.Utilities return factory; } - if(filter == null) + bool inclusionPredicate(LogEvent e) { - filter = (e) => true; + if(filter == null) + { + return true; + } + var eventId = e.Properties.ContainsKey("EventId") ? e.Properties["EventId"].ToString() : null; + if(eventId?.Contains(Constants.BypassFiltersEventId.ToString()) ?? false) + { + return true; + } + return filter(e); } var config = new LoggerConfiguration() .Enrich.FromLogContext() - .Filter.ByIncludingOnly(filter); + .Filter.ByIncludingOnly(inclusionPredicate); if(CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Uri) && CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Key)) diff --git a/src/Identity/Startup.cs b/src/Identity/Startup.cs index 511ae6d5b..650c6d965 100644 --- a/src/Identity/Startup.cs +++ b/src/Identity/Startup.cs @@ -75,17 +75,17 @@ namespace Bit.Identity loggerFactory.AddSerilog(app, env, appLifetime, globalSettings, (e) => { 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(typeof(IpRateLimitMiddleware).FullName) && e.Level == LogEventLevel.Information) - { - return true; - } - return e.Level >= LogEventLevel.Error; });