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

More testing #1884

Merged
merged 2 commits into from
May 3, 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
7 changes: 5 additions & 2 deletions src/ApiService/ApiService/TestHooks/ContainerTestHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using Microsoft.OneFuzz.Service;

#if DEBUG

namespace ApiService.TestHooks {
public class ContainerTestHooks {
Expand All @@ -14,12 +15,12 @@ public class ContainerTestHooks {
private readonly IContainers _containers;

public ContainerTestHooks(ILogTracer log, IConfigOperations configOps, IContainers containers) {
_log = log.WithTag("TestHooks", "ContainerTestHooks");
_log = log.WithTag("TestHooks", nameof(ContainerTestHooks));
_configOps = configOps;
_containers = containers;
}

[Function("GetInstanceId")]
[Function("GetInstanceIdTestHook")]
public async Task<HttpResponseData> GetInstanceId([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/containers/instanceId")] HttpRequestData req) {
_log.Info("Get instance ID");
var instanceId = await _containers.GetInstanceId();
Expand All @@ -33,3 +34,5 @@ public async Task<HttpResponseData> GetInstanceId([HttpTrigger(AuthorizationLeve

}
}

#endif
16 changes: 9 additions & 7 deletions src/ApiService/ApiService/TestHooks/CredsTestHookks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

using Microsoft.OneFuzz.Service;


#if DEBUG

namespace ApiService.TestHooks {
public class CredsTestHookks {
Expand All @@ -14,12 +14,12 @@ public class CredsTestHookks {
private readonly ICreds _creds;

public CredsTestHookks(ILogTracer log, IConfigOperations configOps, ICreds creds) {
_log = log.WithTag("TestHooks", "ContainerTestHooks");
_log = log.WithTag("TestHooks", nameof(CredsTestHookks));
_configOps = configOps;
_creds = creds;
}

[Function("GetSubscription")]
[Function("GetSubscriptionTestHook")]
public async Task<HttpResponseData> GetSubscription([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/creds/subscription")] HttpRequestData req) {
_log.Info("Get subscription");
var resp = req.CreateResponse(HttpStatusCode.OK);
Expand All @@ -28,23 +28,23 @@ public async Task<HttpResponseData> GetSubscription([HttpTrigger(AuthorizationLe
}


[Function("GetBaseResourceGroup")]
[Function("GetBaseResourceGroupTestHook")]
public async Task<HttpResponseData> GetBaseResourceGroup([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/creds/baseResourceGroup")] HttpRequestData req) {
_log.Info("Get base resource group");
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteStringAsync(_creds.GetBaseResourceGroup().ToString());
return resp;
}

[Function("GetInstanceName")]
[Function("GetInstanceNameTestHook")]
public async Task<HttpResponseData> GetInstanceName([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/creds/instanceName")] HttpRequestData req) {
_log.Info("Get instance name");
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteStringAsync(_creds.GetInstanceName().ToString());
return resp;
}

[Function("GetBaseRegion")]
[Function("GetBaseRegionTestHook")]
public async Task<HttpResponseData> GetBaseRegion([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/creds/baseRegion")] HttpRequestData req) {
_log.Info("Get base region");
var resp = req.CreateResponse(HttpStatusCode.OK);
Expand All @@ -53,7 +53,7 @@ public async Task<HttpResponseData> GetBaseRegion([HttpTrigger(AuthorizationLeve
return resp;
}

[Function("GetInstanceUrl")]
[Function("GetInstanceUrlTestHook")]
public async Task<HttpResponseData> GetInstanceUrl([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/creds/instanceUrl")] HttpRequestData req) {
_log.Info("Get instance url");
var resp = req.CreateResponse(HttpStatusCode.OK);
Expand All @@ -63,3 +63,5 @@ public async Task<HttpResponseData> GetInstanceUrl([HttpTrigger(AuthorizationLev

}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.OneFuzz.Service;



#if DEBUG
namespace ApiService.TestHooks {
public class DiskOperationsTestHooks {
private readonly ILogTracer _log;
Expand All @@ -15,13 +15,13 @@ public class DiskOperationsTestHooks {
private readonly ICreds _creds;

public DiskOperationsTestHooks(ILogTracer log, IConfigOperations configOps, IDiskOperations diskOps, ICreds creds) {
_log = log.WithTag("TestHooks", "ContainerTestHooks");
_log = log.WithTag("TestHooks", nameof(DiskOperationsTestHooks));
_configOps = configOps;
_diskOps = diskOps;
_creds = creds;
}

[Function("GetDiskNames")]
[Function("GetDiskNamesTestHook")]
public async Task<HttpResponseData> GetSubscription([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/disks")] HttpRequestData req) {
_log.Info("Get disk names");
var resp = req.CreateResponse(HttpStatusCode.OK);
Expand All @@ -34,3 +34,4 @@ public async Task<HttpResponseData> GetSubscription([HttpTrigger(AuthorizationLe

}
}
#endif
36 changes: 36 additions & 0 deletions src/ApiService/ApiService/TestHooks/EventsTestHooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.OneFuzz.Service;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;

#if DEBUG
namespace ApiService.TestHooks {

public class EventsTestHooks {
private readonly ILogTracer _log;
private readonly IConfigOperations _configOps;
private readonly IEvents _events;

public EventsTestHooks(ILogTracer log, IConfigOperations configOps, IEvents events) {
_log = log.WithTag("TestHooks", nameof(EventsTestHooks));
_configOps = configOps;
_events = events;
}

[Function("LogEventTestHook")]
public async Task<HttpResponseData> LogEvent([HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "testhooks/events/logEvent")] HttpRequestData req) {
_log.Info("Log event");

var s = await req.ReadAsStringAsync();
var msg = JsonSerializer.Deserialize<EventMessage>(s!, EntityConverter.GetJsonSerializerOptions());
_events.LogEvent(msg!.Event);
var resp = req.CreateResponse(HttpStatusCode.OK);
return resp;
}
}

}
#endif
53 changes: 53 additions & 0 deletions src/ApiService/ApiService/TestHooks/ExtensionsTestHooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.OneFuzz.Service;


#if DEBUG
namespace ApiService.TestHooks {

public class ExtensionsTestHooks {

private readonly ILogTracer _log;
private readonly IConfigOperations _configOps;
private readonly IExtensions _extensions;

public ExtensionsTestHooks(ILogTracer log, IConfigOperations configOps, IExtensions extensions) {
_log = log.WithTag("TestHooks", nameof(ExtensionsTestHooks));
_configOps = configOps;
_extensions = extensions;
}

[Function("GenericExtensionsHook")]
public async Task<HttpResponseData> GenericExtensions([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/extensions/genericExtensions")] HttpRequestData req) {
_log.Info("Get Generic extensions");

var query = UriExtension.GetQueryComponents(req.Url);

Os os;
if (query["os"].ToLowerInvariant() == "windows") {
os = Os.Windows;
} else if (query["os"].ToLowerInvariant() == "linux") {
os = Os.Linux;
} else {
var err = req.CreateResponse(HttpStatusCode.BadRequest);
await err.WriteAsJsonAsync(new { error = $"unsupported os {query["os"]}" });
return err;
}

var ext = await (_extensions as Extensions)!.GenericExtensions(query["region"], os);
var resp = req.CreateResponse(HttpStatusCode.OK);

await resp.WriteAsJsonAsync(ext);

return resp;
}



}
}

#endif
13 changes: 6 additions & 7 deletions src/ApiService/ApiService/TestHooks/InstanceConfigTestHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
using Microsoft.OneFuzz.Service;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;


#if DEBUG
namespace ApiService.TestHooks {
public class InstanceConfigTestHooks {

private readonly ILogTracer _log;
private readonly IConfigOperations _configOps;

public InstanceConfigTestHooks(ILogTracer log, IConfigOperations configOps) {
_log = log.WithTag("TestHooks", "InstanceConfigTestHooks");
_log = log.WithTag("TestHooks", nameof(InstanceConfigTestHooks));
_configOps = configOps;
}

[Function("GetInstanceConfig")]
[Function("GetInstanceConfigTestHook")]
public async Task<HttpResponseData> Get([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/instance-config")] HttpRequestData req) {
_log.Info("Fetching instance config");
var config = await _configOps.Fetch();
Expand All @@ -37,7 +39,7 @@ public async Task<HttpResponseData> Get([HttpTrigger(AuthorizationLevel.Anonymou
}
}

[Function("PatchInstanceConfig")]
[Function("PatchInstanceConfigTestHook")]
public async Task<HttpResponseData> Patch([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "testhooks/instance-config")] HttpRequestData req) {
_log.Info("Patch instance config");

Expand All @@ -64,9 +66,6 @@ public async Task<HttpResponseData> Patch([HttpTrigger(AuthorizationLevel.Anonym
return resp;
}
}




}
}
#endif
95 changes: 95 additions & 0 deletions src/ApiService/ApiService/TestHooks/IpOperationsTestHooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.OneFuzz.Service;


#if DEBUG
namespace ApiService.TestHooks {
public class IpOperationsTestHooks {
private readonly ILogTracer _log;
private readonly IConfigOperations _configOps;
private readonly IIpOperations _ipOps;

public IpOperationsTestHooks(ILogTracer log, IConfigOperations configOps, IIpOperations ipOps) {
_log = log.WithTag("TestHooks", nameof(IpOperationsTestHooks));
_configOps = configOps;
_ipOps = ipOps;
}

[Function("GetPublicNicTestHook")]
public async Task<HttpResponseData> GetPublicNic([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/ipOps/publicNic")] HttpRequestData req) {
_log.Info("Get public nic");

var query = UriExtension.GetQueryComponents(req.Url);

var rg = query["rg"];
var name = query["name"];

var nic = await _ipOps.GetPublicNic(rg, name);

var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteStringAsync(nic.Get().Value.Data.Name);
return resp;
}

[Function("GetIpTestHook")]
public async Task<HttpResponseData> GetIp([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/ipOps/ip")] HttpRequestData req) {
_log.Info("Get public nic");

var query = UriExtension.GetQueryComponents(req.Url);

var rg = query["rg"];
var name = query["name"];

var ip = await _ipOps.GetIp(rg, name);

var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteStringAsync(ip.Get().Value.Data.Name);
return resp;
}


[Function("DeletePublicNicTestHook")]
public async Task<HttpResponseData> DeletePublicNic([HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "testhooks/ipOps/publicNic")] HttpRequestData req) {
_log.Info("Get public nic");

var query = UriExtension.GetQueryComponents(req.Url);

var rg = query["rg"];
var name = query["name"];
var yes = UriExtension.GetBoolValue("force", query, false);

if (yes) {
await _ipOps.DeleteNic(rg, name);
var resp = req.CreateResponse(HttpStatusCode.OK);
return resp;
} else {
var resp = req.CreateResponse(HttpStatusCode.NotModified);
return resp;
}
}

[Function("DeleteIpTestHook")]
public async Task<HttpResponseData> DeleteIp([HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "testhooks/ipOps/ip")] HttpRequestData req) {
_log.Info("Get public nic");

var query = UriExtension.GetQueryComponents(req.Url);

var rg = query["rg"];
var name = query["name"];
var yes = UriExtension.GetBoolValue("force", query, false);

if (yes) {
await _ipOps.DeleteIp(rg, name);
var resp = req.CreateResponse(HttpStatusCode.OK);
return resp;
} else {
var resp = req.CreateResponse(HttpStatusCode.NotModified);
return resp;
}
}
}
}
#endif
12 changes: 7 additions & 5 deletions src/ApiService/ApiService/onefuzzlib/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ List<EventMessage> arguments


public interface IEvents {
public Async.Task SendEvent(BaseEvent anEvent);
Async.Task SendEvent(BaseEvent anEvent);

public Async.Task QueueSignalrEvent(EventMessage message);
Async.Task QueueSignalrEvent(EventMessage message);

void LogEvent(BaseEvent anEvent);
}

public class Events : IEvents {
Expand Down Expand Up @@ -52,15 +54,15 @@ public async Async.Task SendEvent(BaseEvent anEvent) {
);
await QueueSignalrEvent(eventMessage);
await _webhook.SendEvent(eventMessage);
LogEvent(anEvent, eventType);
LogEvent(anEvent);
}

public void LogEvent(BaseEvent anEvent, EventType eventType) {
public void LogEvent(BaseEvent anEvent) {
var options = EntityConverter.GetJsonSerializerOptions();
options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
options.Converters.Add(new RemoveUserInfo());
var serializedEvent = JsonSerializer.Serialize(anEvent, anEvent.GetType(), options);
_log.WithTag("Event Type", eventType.ToString()).Info($"sending event: {eventType} - {serializedEvent}");
_log.WithTag("Event Type", anEvent.GetEventType().ToString()).Info($"sending event: {anEvent.GetEventType()} - {serializedEvent}");
}
}

Expand Down
Loading