1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-05 05:00:19 -05:00

Migrate deprecated Microsoft.Azure.EventGrid to Azure.Messaging.EventGrid (#1731)

* Migrate from deprecated Microsoft.Azure.EventGrid to Azure.Messaging.EventGrid

* Fixed retrieval/parsing of EventGridEvents

* Fixed an issue where the events where not handled by the registered event handlers
This commit is contained in:
Daniel James Smith 2021-11-30 19:47:56 +01:00 committed by GitHub
parent cc490a76de
commit ad18adf471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 22 deletions

View File

@ -28,10 +28,10 @@
</Choose> </Choose>
<ItemGroup> <ItemGroup>
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.7.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.9" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.9" />
<PackageReference Include="NewRelic.Agent" Version="8.41.0" /> <PackageReference Include="NewRelic.Agent" Version="8.41.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Microsoft.Azure.EventGrid" Version="3.2.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -14,7 +14,7 @@ using System.Collections.Generic;
using Bit.Core.Models.Table; using Bit.Core.Models.Table;
using Bit.Core.Settings; using Bit.Core.Settings;
using Core.Models.Data; using Core.Models.Data;
using Microsoft.Azure.EventGrid.Models; using Azure.Messaging.EventGrid;
using Bit.Core.Models.Data; using Bit.Core.Models.Data;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;

View File

@ -12,7 +12,7 @@ using Bit.Core.Settings;
using Bit.Core.Models.Api.Response; using Bit.Core.Models.Api.Response;
using Bit.Core.Enums; using Bit.Core.Enums;
using Bit.Core.Context; using Bit.Core.Context;
using Microsoft.Azure.EventGrid.Models; using Azure.Messaging.EventGrid;
using Bit.Api.Utilities; using Bit.Api.Utilities;
using System.Collections.Generic; using System.Collections.Generic;
using Bit.Core.Models.Table; using Bit.Core.Models.Table;

View File

@ -1,8 +1,8 @@
using Bit.Core.Utilities; using Bit.Core.Utilities;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.EventGrid; using Azure.Messaging.EventGrid;
using Microsoft.Azure.EventGrid.Models; using Azure.Messaging.EventGrid.SystemEvents;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -55,29 +55,25 @@ namespace Bit.Api.Utilities
} }
var response = string.Empty; var response = string.Empty;
var requestContent = await new StreamReader(request.Body).ReadToEndAsync(); var requestData = await BinaryData.FromStreamAsync(request.Body);
if (string.IsNullOrWhiteSpace(requestContent)) var eventGridEvents = EventGridEvent.ParseMany(requestData);
{
return new OkObjectResult(response);
}
var eventGridSubscriber = new EventGridSubscriber();
var eventGridEvents = eventGridSubscriber.DeserializeEventGridEvents(requestContent);
foreach (var eventGridEvent in eventGridEvents) foreach (var eventGridEvent in eventGridEvents)
{ {
if (eventGridEvent.Data is SubscriptionValidationEventData eventData) if (eventGridEvent.TryGetSystemEventData(out object systemEvent))
{ {
// Might want to enable additional validation: subject, topic etc. if (systemEvent is SubscriptionValidationEventData eventData)
var responseData = new SubscriptionValidationResponse()
{ {
ValidationResponse = eventData.ValidationCode // Might want to enable additional validation: subject, topic etc.
}; var responseData = new SubscriptionValidationResponse()
{
ValidationResponse = eventData.ValidationCode
};
return new OkObjectResult(responseData); return new OkObjectResult(responseData);
}
} }
else if (eventTypeHandlers.ContainsKey(eventGridEvent.EventType))
if (eventTypeHandlers.ContainsKey(eventGridEvent.EventType))
{ {
await eventTypeHandlers[eventGridEvent.EventType](eventGridEvent); await eventTypeHandlers[eventGridEvent.EventType](eventGridEvent);
} }