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

Event export serializer #3543

Merged
merged 49 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
c69deed
Release 8.7.1 (hotfix) (#3459)
AdamL-Microsoft Aug 29, 2023
c8986aa
Revert "Release 8.7.1 (hotfix) (#3459)" (#3468)
AdamL-Microsoft Aug 30, 2023
7b40402
Redo 8.7.1 (#3469)
AdamL-Microsoft Aug 30, 2023
d999603
Support custom ado fields that mark work items as duplicate (#3467)
kananb Aug 30, 2023
b2435b1
Update readme with archive message (#3408)
mgreisen Aug 31, 2023
b913074
Bump tokio from 1.30.0 to 1.32.0 in /src/proxy-manager (#3425)
dependabot[bot] Aug 31, 2023
14ab36e
Bump tokio from 1.30.0 to 1.32.0 in /src/agent (#3424)
dependabot[bot] Aug 31, 2023
f141050
Remove unnecessary method argument (#3473)
kananb Sep 1, 2023
d4319d2
Bump elsa from 1.8.1 to 1.9.0 in /src/agent (#3411)
dependabot[bot] Sep 4, 2023
93b16ec
Bump tempfile from 3.7.1 to 3.8.0 in /src/agent (#3437)
dependabot[bot] Sep 5, 2023
7f7ab37
Bump tempfile from 3.7.1 to 3.8.0 in /src/proxy-manager (#3436)
dependabot[bot] Sep 5, 2023
b2e6a07
Updating requirements.txt to accept >= onefuzztypes. (#3477)
nharper285 Sep 5, 2023
aa9c9ea
Bump notify from 6.0.1 to 6.1.1 in /src/agent (#3435)
dependabot[bot] Sep 5, 2023
74475cc
Bump azure_* crates (#3478)
Porges Sep 5, 2023
64699ed
Release 8.8.0 (#3466)
AdamL-Microsoft Sep 6, 2023
a3fb480
Bump clap from 4.3.21 to 4.4.2 in /src/agent (#3484)
dependabot[bot] Sep 6, 2023
59c52d6
Bump gimli from 0.27.3 to 0.28.0 in /src/agent (#3414)
dependabot[bot] Sep 6, 2023
dd9e266
Bump clap from 4.3.21 to 4.4.2 in /src/proxy-manager (#3474)
dependabot[bot] Sep 6, 2023
6e2cb14
Bump winreg from 0.50.0 to 0.51.0 in /src/agent (#3434)
dependabot[bot] Sep 6, 2023
d2d57a8
Starting integration tests (#3438)
tevoinea Sep 7, 2023
830b479
Fix sed checks for CLI versioning (#3486)
nharper285 Sep 7, 2023
896329d
Bump bytes from 1.4.0 to 1.5.0 in /src/agent (#3488)
dependabot[bot] Sep 10, 2023
d34138d
Improve area/iteration path validation (#3489)
kananb Sep 11, 2023
d009476
Improve handling of unexpected breakpoints (#3493)
tevoinea Sep 13, 2023
18f2b4a
Update azure_* crates (#3503)
Porges Sep 13, 2023
9ede0de
Fuzz coverage recording (#3322)
tevoinea Sep 14, 2023
cde6a19
Reporting coverage on task start up (#3502)
nharper285 Sep 14, 2023
1fb1563
Remove feature flag from heartbeat metrics. (#3505)
nharper285 Sep 14, 2023
c7a9827
Update archive notice. (#3507)
mgreisen Sep 15, 2023
58da7b4
Add onefuzz service version to job created events (#3504)
kananb Sep 20, 2023
60766e6
Tevoinea/add version checking in local tasks (#3517)
tevoinea Sep 21, 2023
e3c4a40
Create directories if they don't exist in the template (#3522)
tevoinea Sep 21, 2023
d1ccb1e
Support for retention policies on containers (#3501)
Porges Sep 26, 2023
7efea43
Bump rayon from 1.7.0 to 1.8.0 in /src/agent (#3520)
dependabot[bot] Sep 26, 2023
f3b7e20
Bump insta from 1.31.0 to 1.32.0 in /src/agent (#3521)
dependabot[bot] Sep 26, 2023
d2ba170
Disable `repro` and `debug` VM CLI commands. (#3494)
nharper285 Sep 27, 2023
2c8ecc9
Make modules case insenstive on windows (#3527)
tevoinea Sep 28, 2023
e12b41e
Update windows interceptor list (#3528)
tevoinea Sep 28, 2023
f8f4461
Starting to work on custom converter
tevoinea Sep 28, 2023
c1ee35b
Add slim serializer for events export
tevoinea Oct 3, 2023
252e795
.
tevoinea Oct 4, 2023
7387ada
Merge branch 'main' of https://github.com/microsoft/onefuzz into tevo…
tevoinea Oct 4, 2023
701025b
Cleanup and better tests
tevoinea Oct 5, 2023
9a214e3
Merge branch 'main' into tevoinea/CustomEventSerializer
tevoinea Oct 5, 2023
9899410
Merge branch 'main' into tevoinea/CustomEventSerializer
tevoinea Oct 6, 2023
5ea3ccc
Update feature-flags.bicep
tevoinea Oct 10, 2023
9cd0dfa
Merge branch 'main' into tevoinea/CustomEventSerializer
tevoinea Oct 10, 2023
bbdcb8d
Fix api.py
tevoinea Oct 10, 2023
49c0c54
Merge branch 'tevoinea/CustomEventSerializer' of https://github.com/t…
tevoinea Oct 10, 2023
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
1 change: 1 addition & 0 deletions src/ApiService/ApiService/FeatureFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public static class FeatureFlagConstants {
public const string EnableDryRunBlobRetention = "EnableDryRunBlobRetention";
public const string EnableWorkItemCreation = "EnableWorkItemCreation";
public const string EnableContainerRetentionPolicies = "EnableContainerRetentionPolicies";
public const string EnableSlimEventSerialization = "EnableSlimEventSerialization";
}
25 changes: 12 additions & 13 deletions src/ApiService/ApiService/onefuzzlib/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public class Events : IEvents {
private readonly IContainers _containers;
private readonly ICreds _creds;
private readonly JsonSerializerOptions _options;
private readonly JsonSerializerOptions _optionsSlim;
private readonly JsonSerializerOptions _deserializingFromBlobOptions;
private readonly IOnefuzzContext _context;

public Events(ILogger<Events> log, IOnefuzzContext context) {
_queue = context.Queue;
Expand All @@ -47,9 +49,12 @@ public class Events : IEvents {
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
_options.Converters.Add(new RemoveUserInfo());
_optionsSlim = new JsonSerializerOptions(_options);
_optionsSlim.Converters.Add(new EventExportConverter<DownloadableEventMessage>());
_deserializingFromBlobOptions = new JsonSerializerOptions(EntityConverter.GetJsonSerializerOptions()) {
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
_context = context;
}

public virtual async Async.Task QueueSignalrEvent(DownloadableEventMessage message) {
Expand All @@ -58,7 +63,13 @@ public class Events : IEvents {
("event_id", message.EventId.ToString())
};
var ev = new SignalREvent("events", new List<DownloadableEventMessage>() { message });
var queueResult = await _queue.QueueObject("signalr-events", ev, StorageType.Config, serializerOptions: _options);

var opts = await _context.FeatureManagerSnapshot.IsEnabledAsync(FeatureFlagConstants.EnableSlimEventSerialization) switch {
true => _optionsSlim,
false => _options
};

var queueResult = await _queue.QueueObject("signalr-events", ev, StorageType.Config, serializerOptions: opts);

if (!queueResult) {
_log.AddTags(tags);
Expand Down Expand Up @@ -155,16 +166,4 @@ public class Events : IEvents {
);
}
}


public class RemoveUserInfo : JsonConverter<UserInfo> {
public override UserInfo? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
throw new NotSupportedException("reading UserInfo is not supported");
}

public override void Write(Utf8JsonWriter writer, UserInfo value, JsonSerializerOptions options) {
writer.WriteStartObject();
writer.WriteEndObject();
}
}
}
13 changes: 11 additions & 2 deletions src/ApiService/ApiService/onefuzzlib/WebhookOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ public interface IWebhookOperations : IOrm<Webhook> {
public class WebhookOperations : Orm<Webhook>, IWebhookOperations {

private readonly IHttpClientFactory _httpFactory;
private readonly JsonSerializerOptions _options;
private readonly JsonSerializerOptions _optionsSlim;

public WebhookOperations(IHttpClientFactory httpFactory, ILogger<WebhookOperations> log, IOnefuzzContext context)
: base(log, context) {
_httpFactory = httpFactory;
_options = EntityConverter.GetJsonSerializerOptions();
_optionsSlim = new JsonSerializerOptions(_options);
_optionsSlim.Converters.Add(new EventExportConverter<WebhookMessage>());
}

public async Async.Task SendEvent(DownloadableEventMessage eventMessage) {
Expand Down Expand Up @@ -139,11 +144,15 @@ public WebhookOperations(IHttpClientFactory httpFactory, ILogger<WebhookOperatio
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);
var opts = await _context.FeatureManagerSnapshot.IsEnabledAsync(FeatureFlagConstants.EnableSlimEventSerialization) switch {
true => _optionsSlim,
false => _options
};
if (messageFormat != null && messageFormat == WebhookMessageFormat.EventGrid) {
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());
data = JsonSerializer.Serialize(eventGridMessage, options: opts);
} else {
data = JsonSerializer.Serialize(webhookMessage, options: EntityConverter.GetJsonSerializerOptions());
data = JsonSerializer.Serialize(webhookMessage, options: opts);
}

string? digest = null;
Expand Down
76 changes: 76 additions & 0 deletions src/ApiService/ApiService/onefuzzlib/events/Converters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Collections;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.OneFuzz.Service {
public class RemoveUserInfo : JsonConverter<UserInfo> {
public override UserInfo? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
throw new NotSupportedException("reading UserInfo is not supported");
}

public override void Write(Utf8JsonWriter writer, UserInfo value, JsonSerializerOptions options) {
writer.WriteStartObject();
writer.WriteEndObject();
}
}

/// <summary>
/// <b>THIS IS A WRITE ONLY JSON CONVERTER</b>
/// <br/>
/// It should only be used when serializing events to be sent outside of the service
/// </summary>
public class EventExportConverter<T> : JsonConverter<T>
where T : DownloadableEventMessage {
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
throw new NotSupportedException("This converter should only be used when serializing event messages to sent outside of the service");
}

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) {
BoundedSerializer.WriteInternal(writer, value, options);
}
}

public class BoundedSerializer {
private static HashSet<Type> boundedTypes = new HashSet<Type>{
typeof(Guid),
typeof(DateTime),
typeof(int),
typeof(bool),
typeof(float),
typeof(double),
typeof(long),
typeof(char),
typeof(Uri)
};

public static void WriteInternal(Utf8JsonWriter writer, object type, JsonSerializerOptions options) {
writer.WriteStartObject();
var properties = type.GetType().GetProperties();
foreach (var property in properties) {
if (property.GetValue(type, null) == null
|| typeof(IEnumerable).IsAssignableFrom(property.PropertyType)
|| type.GetType() == property.PropertyType) {
continue;
}
if (HasBoundedSerialization(property)) {
var serialized = JsonSerializer.Serialize(property.GetValue(type, null), property.PropertyType, options);
if (!string.IsNullOrEmpty(serialized)) {
writer.WritePropertyName(property.Name);
writer.WriteRawValue(serialized);
}
} else if (property.PropertyType.IsClass) {
writer.WritePropertyName(property.Name);
WriteInternal(writer, property.GetValue(type, null)!, options);
}
}
writer.WriteEndObject();
}

public static bool HasBoundedSerialization(PropertyInfo propertyInfo) {
return propertyInfo.PropertyType.IsEnum ||
boundedTypes.Contains(propertyInfo.PropertyType) ||
typeof(IValidatedString).IsAssignableFrom(propertyInfo.PropertyType);
}
}
}
219 changes: 219 additions & 0 deletions src/ApiService/Tests/EventExportConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using FluentAssertions;
using FsCheck;
using FsCheck.Xunit;
using Microsoft.OneFuzz.Service;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
using Xunit;


namespace Tests;

public class EventExportConverterTests {
enum Color {
Red,
Blue
}

[Fact]
public void BaseTypesAreBounded() {
var a = new {
guid = Guid.NewGuid(),
date = new DateTime(),
en = Color.Red,
b = 1,
boo = false,
flo = float.Pi,
doub = double.Tau,
lon = long.MinValue,
cha = 'a'
};

a.GetType().GetProperties().All(p => BoundedSerializer.HasBoundedSerialization(p)).Should().BeTrue();
}

[Fact]
public void StringIsNotBounded() {
var a = new {
bad = "this is not bounded"
};

BoundedSerializer.HasBoundedSerialization(a.GetType().GetProperty("bad")!).Should().BeFalse();
}

[Fact]
public void ValidatedStringIsBounded() {
var a = new {
scalesetid = ScalesetId.Parse("abc-123")
};

BoundedSerializer.HasBoundedSerialization(a.GetType().GetProperty("scalesetid")!).Should().BeTrue();
}

[Fact]
public void ComplexObjectsAreSerialized() {
var randomGuid = Guid.NewGuid();
var a = new DownloadableEventMessage(
randomGuid,
EventType.CrashReported,
new EventCrashReported(
new Report(
"https://example.com",
null,
"target.exe",
"crash",
string.Empty,
new List<string> { "this", "is", "a", "stacktrace" },
string.Empty,
string.Empty,
null,
Guid.NewGuid(),
Guid.NewGuid(),
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
),
Container.Parse("this-is-a-container"),
"crash-abc123",
null
),
Guid.NewGuid(),
"onefuzz",
DateTime.Now,
new Uri("https://example.com"),
null
);
var serializerOptions = new JsonSerializerOptions(EntityConverter.GetJsonSerializerOptions());
serializerOptions.Converters.Add(new EventExportConverter<DownloadableEventMessage>());

var serialized = JsonSerializer.Serialize(a, serializerOptions);

serialized.Should().NotBeNullOrEmpty();
serialized.Should().NotContain("stacktrace"); // List<string> is not serialized
serialized.Should().NotContain("crash-abc123"); // string is not serialized
serialized.Should().Contain("this-is-a-container"); // ValidatedString is serialized
serialized.Should().Contain("crash_reported"); // Enum is serialized
serialized.Should().Contain(DateTime.Now.Year.ToString()); // DateTime is serialized
serialized.Should().Contain(randomGuid.ToString()); // Guid id serialized
}

[Fact]
public void TestWebhookMessage() {
var a = new WebhookMessageEventGrid(
"2.0.0",
"eventsubject",
EventType.JobCreated,
DateTime.Now,
Guid.NewGuid(),
new WebhookMessage(
Guid.NewGuid(),
EventType.JobCreated,
new EventJobCreated(
Guid.NewGuid(),
new JobConfig("some project", "some name", "some build", 1, "some logs"),
null,
"8.0"),
Guid.NewGuid(),
"onefuzz",
Guid.NewGuid(),
DateTime.Now,
new Uri("https://example.com")
)
);

var serializerOptions = new JsonSerializerOptions(EntityConverter.GetJsonSerializerOptions());
serializerOptions.Converters.Add(new EventExportConverter<WebhookMessage>());

var serialized = JsonSerializer.Serialize(a, serializerOptions);

serialized.Should().Contain("eventsubject");
serialized.Should().NotContain("some project");
}

public class EventExportConverterSerializationTests {
private readonly JsonSerializerOptions _opts = new JsonSerializerOptions(EntityConverter.GetJsonSerializerOptions());
public EventExportConverterSerializationTests() {
_ = Arb.Register<Arbitraries>();
_opts.Converters.Add(new EventExportConverter<DownloadableEventMessage>());
}

void Test<T>(T v) {
// TODO: Try cloning/creating a new serializer options from the existing one?
var serialized = JsonSerializer.Serialize(v, _opts);
var _ = JsonSerializer.Deserialize<dynamic>(serialized);
}

[Property]
public void EventNodeHeartbeat(EventNodeHeartbeat e) => Test(e);


[Property]
public void EventTaskHeartbeat(EventTaskHeartbeat e) => Test(e);

[Property]
public void EventTaskStopped(EventTaskStopped e) => Test(e);

[Property]
public void EventInstanceConfigUpdated(EventInstanceConfigUpdated e) => Test(e);

[Property]
public void EventProxyCreated(EventProxyCreated e) => Test(e);

[Property]
public void EventProxyDeleted(EventProxyDeleted e) => Test(e);

[Property]
public void EventProxyFailed(EventProxyFailed e) => Test(e);

[Property]
public void EventProxyStateUpdated(EventProxyStateUpdated e) => Test(e);


[Property]
public void EventCrashReported(EventCrashReported e) => Test(e);


[Property]
public void EventRegressionReported(EventRegressionReported e) => Test(e);


[Property]
public void EventFileAdded(EventFileAdded e) => Test(e);

[Property]
public void EventTaskFailed(EventTaskFailed e) => Test(e);

[Property]
public void EventTaskStateUpdated(EventTaskStateUpdated e) => Test(e);

[Property]
public void EventScalesetFailed(EventScalesetFailed e) => Test(e);

[Property]
public void EventScalesetResizeScheduled(EventScalesetResizeScheduled e) => Test(e);

[Property]
public void EventScalesetStateUpdated(EventScalesetStateUpdated e) => Test(e);

[Property]
public void EventNodeDeleted(EventNodeDeleted e) => Test(e);

[Property]
public void EventNodeCreated(EventNodeCreated e) => Test(e);

[Property]
public void EventMessage(DownloadableEventMessage e) => Test(e);
}
}
Loading
Loading