Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Make WebhookMessageEventGrid compatible with the event grid format #3286

Merged
merged 4 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/ApiService/ApiService/OneFuzzTypes/Webhooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ public record WebhookMessage(Guid EventId,
DateOnly? ExpiresOn = null) : DownloadableEventMessage(EventId, EventType, Event, InstanceId, InstanceName, CreatedAt, SasUrl, ExpiresOn);


// the schema for the event grid message is defined here:
// https://learn.microsoft.com/en-us/azure/event-grid/event-schema#event-schema
public record WebhookMessageEventGrid(
[property: JsonPropertyName("dataVersion")] string DataVersion,
string Subject,
[property: JsonPropertyName("EventType")] EventType EventType,
[property: JsonPropertyName("eventTime")] DateTimeOffset EventTime,
Guid Id,
[property: TypeDiscrimnatorAttribute("EventType", typeof(EventTypeProvider))]
[property: JsonConverter(typeof(BaseEventConverter))]
BaseEvent Data,
Uri SasUrl,
String Version = "1.0");
WebhookMessage Data
);

public record WebhookMessageLog(
[RowKey] Guid EventId,
Expand Down
7 changes: 3 additions & 4 deletions src/ApiService/ApiService/onefuzzlib/WebhookOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,12 @@ public async Task<EventPing> Ping(Webhook webhook) {
var eventData = eventDataResult.OkV;

string data;
var instanceId = await _context.Containers.GetInstanceId();
var webhookMessage = new WebhookMessage(WebhookId: webhookId, EventId: eventId, EventType: eventType, Event: webhookEvent, InstanceId: instanceId, InstanceName: _context.Creds.GetInstanceName(), CreatedAt: eventData.CreatedAt, SasUrl: eventData.SasUrl);
if (messageFormat != null && messageFormat == WebhookMessageFormat.EventGrid) {
var eventGridMessage = new[] { new WebhookMessageEventGrid(Id: eventId, Data: webhookEvent, DataVersion: "1.0.0", Subject: _context.Creds.GetInstanceName(), EventType: eventType, EventTime: DateTimeOffset.UtcNow, SasUrl: eventData.SasUrl) };
var eventGridMessage = new[] { new WebhookMessageEventGrid(Id: eventId, Data: webhookMessage, DataVersion: "2.0.0", Subject: _context.Creds.GetInstanceName(), EventType: eventType, EventTime: DateTimeOffset.UtcNow) };
data = JsonSerializer.Serialize(eventGridMessage, options: EntityConverter.GetJsonSerializerOptions());
} else {
var instanceId = await _context.Containers.GetInstanceId();
var webhookMessage = new WebhookMessage(WebhookId: webhookId, EventId: eventId, EventType: eventType, Event: webhookEvent, InstanceId: instanceId, InstanceName: _context.Creds.GetInstanceName(), CreatedAt: eventData.CreatedAt, SasUrl: eventData.SasUrl);

data = JsonSerializer.Serialize(webhookMessage, options: EntityConverter.GetJsonSerializerOptions());
}

Expand Down
19 changes: 11 additions & 8 deletions src/ApiService/Tests/OrmModelsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,19 @@ public static Gen<WebhookMessage> WebhookMessage() {
}

public static Gen<WebhookMessageEventGrid> WebhookMessageEventGrid() {
return Arb.Generate<Tuple<string, string, BaseEvent, Guid, DateTimeOffset, Uri>>().Select(
var gen1 = Arb.Generate<Tuple<string, string, BaseEvent, Guid, DateTimeOffset, Uri>>();
var gen2 = WebhookMessage();
return gen1.Zip(gen2)

.Select(
arg =>
new WebhookMessageEventGrid(
DataVersion: arg.Item1,
Subject: arg.Item2,
EventType: arg.Item3.GetEventType(),
Data: arg.Item3,
Id: arg.Item4,
EventTime: arg.Item5,
SasUrl: arg.Item6
DataVersion: arg.Item1.Item1,
Subject: arg.Item1.Item2,
EventType: arg.Item1.Item3.GetEventType(),
Data: arg.Item2,
Id: arg.Item1.Item4,
EventTime: arg.Item1.Item5
)
);
}
Expand Down