From 66dcb9f34d7d716ebbea7f83c0833ce34d41aa9a Mon Sep 17 00:00:00 2001 From: Paul Mcilreavy Date: Wed, 18 Nov 2020 07:47:14 +1000 Subject: [PATCH] Fix bug causing subscription to be always validated (#19) * - Add warning when event is dropped due to there being no subscribers - Reduce log level to DBG when an event is filtered out - Fix bug in validation logic * Add disabled topic example --- ...icationEventsToSubscriberCommandHandler.cs | 18 ++++++++++--- .../ValidateAllSubscriptionsCommandHandler.cs | 2 +- .../Middleware/EventGridMiddleware.cs | 4 +-- .../Middleware/SasKeyValidator.cs | 4 +-- .../example.appsettings.json | 27 ++++++++++--------- 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/AzureEventGridSimulator/Domain/Commands/SendNotificationEventsToSubscriberCommandHandler.cs b/src/AzureEventGridSimulator/Domain/Commands/SendNotificationEventsToSubscriberCommandHandler.cs index 8c4ca01..e3bb7da 100644 --- a/src/AzureEventGridSimulator/Domain/Commands/SendNotificationEventsToSubscriberCommandHandler.cs +++ b/src/AzureEventGridSimulator/Domain/Commands/SendNotificationEventsToSubscriberCommandHandler.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Net.Http; using System.Text; using System.Threading; @@ -34,11 +35,22 @@ protected override Task Handle(SendNotificationEventsToSubscriberCommand request eventGridEvent.MetadataVersion = "1"; } - foreach (var subscription in request.Topic.Subscribers) + if (!request.Topic.Subscribers.Any()) { + _logger.LogWarning("'{TopicName}' has no subscribers so {EventCount} event(s) could not be forwarded", request.Topic.Name, request.Events.Length); + } + else if (request.Topic.Subscribers.All(o => o.Disabled)) + { + _logger.LogWarning("'{TopicName}' has no enabled subscribers so {EventCount} event(s) could not be forwarded", request.Topic.Name, request.Events.Length); + } + else + { + foreach (var subscription in request.Topic.Subscribers) + { #pragma warning disable 4014 - SendToSubscriber(subscription, request.Events); + SendToSubscriber(subscription, request.Events); #pragma warning restore 4014 + } } return Task.CompletedTask; @@ -85,7 +97,7 @@ await httpClient.PostAsync(subscription.Endpoint, content) } else { - _logger.LogWarning("Event {EventId} filtered out for subscriber '{SubscriberName}'.", evt.Id, subscription.Name); + _logger.LogDebug("Event {EventId} filtered out for subscriber '{SubscriberName}'.", evt.Id, subscription.Name); } } } diff --git a/src/AzureEventGridSimulator/Domain/Commands/ValidateAllSubscriptionsCommandHandler.cs b/src/AzureEventGridSimulator/Domain/Commands/ValidateAllSubscriptionsCommandHandler.cs index 77063ee..3137fa1 100644 --- a/src/AzureEventGridSimulator/Domain/Commands/ValidateAllSubscriptionsCommandHandler.cs +++ b/src/AzureEventGridSimulator/Domain/Commands/ValidateAllSubscriptionsCommandHandler.cs @@ -38,7 +38,7 @@ public async Task Handle(ValidateAllSubscriptionsCommand request, Cancella .Where(o => !o.Disabled)) { foreach (var subscriber in enabledTopic.Subscribers - .Where(o => !o.Disabled)) + .Where(o => !o.DisableValidation && !o.Disabled)) { await ValidateSubscription(enabledTopic, subscriber); } diff --git a/src/AzureEventGridSimulator/Infrastructure/Middleware/EventGridMiddleware.cs b/src/AzureEventGridSimulator/Infrastructure/Middleware/EventGridMiddleware.cs index 553e261..5f73047 100644 --- a/src/AzureEventGridSimulator/Infrastructure/Middleware/EventGridMiddleware.cs +++ b/src/AzureEventGridSimulator/Infrastructure/Middleware/EventGridMiddleware.cs @@ -82,7 +82,7 @@ private async Task ValidateNotificationRequest(HttpContext context, const int maximumAllowedOverallMessageSizeInBytes = 1536000; const int maximumAllowedEventGridEventSizeInBytes = 66560; - logger.LogDebug("Message is {Bytes} in length.", requestBody.Length); + logger.LogTrace("Message is {Bytes} in length.", requestBody.Length); if (requestBody.Length > maximumAllowedOverallMessageSizeInBytes) { @@ -97,7 +97,7 @@ private async Task ValidateNotificationRequest(HttpContext context, // ReSharper disable once MethodHasAsyncOverload var eventSize = JsonConvert.SerializeObject(evt, Formatting.None).Length; - logger.LogDebug("Event is {Bytes} in length.", eventSize); + logger.LogTrace("Event is {Bytes} in length.", eventSize); if (eventSize > maximumAllowedEventGridEventSizeInBytes) { diff --git a/src/AzureEventGridSimulator/Infrastructure/Middleware/SasKeyValidator.cs b/src/AzureEventGridSimulator/Infrastructure/Middleware/SasKeyValidator.cs index a09bbed..70bf7e3 100644 --- a/src/AzureEventGridSimulator/Infrastructure/Middleware/SasKeyValidator.cs +++ b/src/AzureEventGridSimulator/Infrastructure/Middleware/SasKeyValidator.cs @@ -28,7 +28,7 @@ public bool IsValid(IHeaderDictionary requestHeaders, string topicKey) return false; } - _logger.LogDebug("'aeg-sas-key' header is valid"); + _logger.LogTrace("'aeg-sas-key' header is valid"); return true; } @@ -42,7 +42,7 @@ public bool IsValid(IHeaderDictionary requestHeaders, string topicKey) return false; } - _logger.LogDebug("'aeg-sas-token' header is valid"); + _logger.LogTrace("'aeg-sas-token' header is valid"); return true; } diff --git a/src/AzureEventGridSimulator/example.appsettings.json b/src/AzureEventGridSimulator/example.appsettings.json index a32427b..2954e55 100644 --- a/src/AzureEventGridSimulator/example.appsettings.json +++ b/src/AzureEventGridSimulator/example.appsettings.json @@ -1,19 +1,6 @@ { "topics": [ - { - "name": "SupplyChain-DEV", - "port": 60111, - "key": "TheLocal+DevelopmentKey=", - "disabled": true, // this topic is disabled it's endpoint won't be created at startup - "subscribers": [ - { - "name": "LocalWebApiSubscription", - "endpoint": "https://localhost:5050/api/SomeApi" - } - ] - }, - { "name": "MyAwesomeTopic", "port": 60101, @@ -35,7 +22,21 @@ "subscribers": [ // There are no subscribers defined yet, so you can send events to this topic but they won't be delivered to anyone ] + }, + + { + "name": "ADisabledTopic", + "port": 60103, + "key": "TheLocal+DevelopmentKey=", + "disabled": true, // this topic is disabled so it's endpoint won't be created at startup + "subscribers": [ + { + "name": "LocalWebApiSubscription", + "endpoint": "https://localhost:5050/api/SomeApi" + } + ] } + ], "Serilog": { "MinimumLevel": {