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

Finishing implementation of SendEvent #1765

Merged
merged 7 commits into from
Apr 11, 2022
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
6 changes: 3 additions & 3 deletions src/ApiService/ApiService/OneFuzzTypes/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public record NodeTasks
public enum NodeState
{
Init,
free,
Free,
SettingUp,
Rebooting,
Ready,
Expand All @@ -80,7 +80,7 @@ public partial record Node
NodeState State,
Guid? ScalesetId,
DateTimeOffset Heartbeat,
Version Version,
string Version,
bool ReimageRequested,
bool DeleteRequested,
bool DebugKeepNode
Expand All @@ -103,7 +103,7 @@ String InstanceName

//record AnyHttpUrl(AnyUrl):
// allowed_schemes = {'http', 'https
//
//



Expand Down
7 changes: 6 additions & 1 deletion src/ApiService/ApiService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
using ApiService.OneFuzzLib;

namespace Microsoft.OneFuzz.Service;

Expand Down Expand Up @@ -33,8 +34,12 @@ public static void Main()
.ConfigureServices((context, services) =>
services
.AddSingleton<ILogTracerFactory>(_ => new LogTracerFactory(GetLoggers()))
.AddScoped<ILogTracer>(s => s.GetService<LogTracerFactory>()?.MakeLogTracer(Guid.NewGuid()) ?? throw new InvalidOperationException("Unable to create a logger"))
chkeita marked this conversation as resolved.
Show resolved Hide resolved
.AddSingleton<IStorageProvider>(_ => new StorageProvider(EnvironmentVariables.OneFuzz.FuncStorage ?? throw new InvalidOperationException("Missing account id")))
.AddSingleton<INodeOperations, NodeOperations>()
.AddSingleton<IEvents, Events>()
.AddSingleton<IWebhookOperations, WebhookOperations>()
.AddSingleton<IWebhookMessageLogOperations, WebhookMessageLogOperations>()
.AddSingleton<IQueue, Queue>()
.AddSingleton<ICreds>(_ => new Creds())
.AddSingleton<IStorage, Storage>()
)
Expand Down
49 changes: 40 additions & 9 deletions src/ApiService/ApiService/onefuzzlib/Events.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using ApiService.OneFuzzLib;
using Microsoft.Extensions.Logging;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Microsoft.OneFuzz.Service
Expand All @@ -25,20 +29,20 @@ public interface IEvents
public class Events : IEvents
{
private readonly IQueue _queue;
private readonly ILogTracer _logger;
private readonly ILogger _logger;
private readonly IWebhookOperations _webhook;

public Events(IQueue queue, ILogTracer logger, IWebhookOperations webhook)
public Events(IQueue queue, ILoggerFactory loggerFactory, IWebhookOperations webhook)
{
_queue = queue;
_logger = logger;
_logger = loggerFactory.CreateLogger<Events>();
_webhook = webhook;
}

public async Task QueueSignalrEvent(EventMessage eventMessage)
{
var message = new SignalREvent("events", new List<EventMessage>() { eventMessage });
var encodedMessage = Encoding.UTF8.GetBytes(System.Text.Json.JsonSerializer.Serialize(message));
var encodedMessage = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message));
await _queue.SendMessage("signalr-events", encodedMessage, StorageType.Config);
}

Expand All @@ -60,15 +64,42 @@ public async Task SendEvent(BaseEvent anEvent)

public void LogEvent(BaseEvent anEvent, EventType eventType)
{
//todo
//var scrubedEvent = FilterEvent(anEvent);
//throw new NotImplementedException();
var options = EntityConverter.GetJsonSerializerOptions();
options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
options.Converters.Add(new RemoveUserInfo());
var serializedEvent = JsonSerializer.Serialize(anEvent, options);
_logger.LogInformation($"sending event: {eventType} - {serializedEvent}");
stishkin marked this conversation as resolved.
Show resolved Hide resolved
}
}



internal class RemoveUserInfo : JsonConverter<UserInfo>
{
public override UserInfo? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var newOptions = new JsonSerializerOptions(options);
RemoveUserInfo? self = null;
foreach (var converter in newOptions.Converters)
{
if (converter is RemoveUserInfo)
{
self = (RemoveUserInfo)converter;
break;
}
}

if (self != null)
{
newOptions.Converters.Remove(self);
}

return JsonSerializer.Deserialize<UserInfo>(ref reader, newOptions);
}

private object FilterEvent(BaseEvent anEvent)
public override void Write(Utf8JsonWriter writer, UserInfo value, JsonSerializerOptions options)
{
throw new NotImplementedException();
writer.WriteStringValue("{}");
}
}
}
10 changes: 5 additions & 5 deletions src/ApiService/ApiService/onefuzzlib/Queue.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Azure.Storage;
using Azure.Storage.Queues;
using Microsoft.Extensions.Logging;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
using System;
using System.Text.Json;
Expand All @@ -16,12 +17,12 @@ public interface IQueue
public class Queue : IQueue
{
IStorage _storage;
ILog _logger;
ILogger _logger;

public Queue(IStorage storage, ILog logger)
public Queue(IStorage storage, ILoggerFactory loggerFactory)
stishkin marked this conversation as resolved.
Show resolved Hide resolved
{
_storage = storage;
_logger = logger;
_logger = loggerFactory.CreateLogger<Queue>();
}


Expand All @@ -36,7 +37,6 @@ public async Task SendMessage(string name, byte[] message, StorageType storageTy
}
catch (Exception)
{

}
}
}
Expand All @@ -60,7 +60,7 @@ public QueueServiceClient GetQueueClient(StorageType storageType)
var accountId = _storage.GetPrimaryAccount(storageType);
//_logger.LogDEbug("getting blob container (account_id: %s)", account_id)
(var name, var key) = _storage.GetStorageAccountNameAndKey(accountId);
var accountUrl = new Uri($"https://%s.queue.core.windows.net{name}");
var accountUrl = new Uri($"https://{name}.queue.core.windows.net");
var client = new QueueServiceClient(accountUrl, new StorageSharedKeyCredential(name, key));
return client;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ApiService/ApiService/onefuzzlib/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using Azure.ResourceManager;
using Azure.ResourceManager.Storage;
using Azure.Core;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using System.Linq;
using Microsoft.Extensions.Logging;

namespace Microsoft.OneFuzz.Service;

Expand Down Expand Up @@ -33,7 +33,7 @@ public class Storage : IStorage
public Storage(ILoggerFactory loggerFactory, ICreds creds)
{
_creds = creds;
_logger = loggerFactory.CreateLogger<Storage>();
_logger = loggerFactory.CreateLogger<Storage>(); ;
}

// TODO: @cached
Expand Down
9 changes: 5 additions & 4 deletions src/ApiService/ApiService/onefuzzlib/WebhookOperations.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ApiService.OneFuzzLib.Orm;
using Microsoft.Extensions.Logging;
using Microsoft.OneFuzz.Service;
using System;
using System.Collections.Generic;
Expand All @@ -21,11 +22,11 @@ Guid EventId
);

private readonly IQueue _queue;
private readonly ILogTracer _log;
public WebhookMessageLogOperations(IStorage storage, IQueue queue, ILogTracer log) : base(storage)
private readonly ILogger _logger;
public WebhookMessageLogOperations(IStorage storage, IQueue queue, ILoggerFactory loggerFactory) : base(storage)
{
_queue = queue;
_log = log;
_logger = loggerFactory.CreateLogger<WebhookMessageLogOperations>(); ;
}


Expand All @@ -43,7 +44,7 @@ public async Task QueueWebhook(WebhookMessageLog webhookLog)

if (visibilityTimeout == null)
{
_log.Error($"invalid WebhookMessage queue state, not queuing. {webhookLog.WebhookId}:{webhookLog.EventId} - {webhookLog.State}");
_logger.LogError($"invalid WebhookMessage queue state, not queuing. {webhookLog.WebhookId}:{webhookLog.EventId} - {webhookLog.State}");

}
else
Expand Down
6 changes: 2 additions & 4 deletions src/ApiService/ApiService/onefuzzlib/orm/Orm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Azure.Core;
using Azure.Data.Tables;
using Azure.Data.Tables;
using Microsoft.OneFuzz.Service;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
using System;
Expand Down Expand Up @@ -50,8 +49,7 @@ public async Task<TableClient> GetTableClient(string table, string? accountId =
{
var account = accountId ?? EnvironmentVariables.OneFuzz.FuncStorage ?? throw new ArgumentNullException(nameof(accountId));
var (name, key) = _storage.GetStorageAccountNameAndKey(account);
var identifier = new ResourceIdentifier(account);
var tableClient = new TableServiceClient(new Uri($"https://{identifier.Name}.table.core.windows.net"), new TableSharedKeyCredential(name, key));
var tableClient = new TableServiceClient(new Uri($"https://{name}.table.core.windows.net"), new TableSharedKeyCredential(name, key));
await tableClient.CreateTableIfNotExistsAsync(table);
return tableClient.GetTableClient(table);
}
Expand Down