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

proxy forward test hooks #1924

Merged
merged 3 commits into from
May 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
10 changes: 5 additions & 5 deletions src/ApiService/ApiService/OneFuzzTypes/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,22 @@ public record Node

public record Forward
(
int SrcPort,
int DstPort,
long SrcPort,
long DstPort,
string DstIp
);


public record ProxyForward
(
[PartitionKey] Region Region,
int Port,
[RowKey] string Port,
Guid ScalesetId,
Guid MachineId,
Guid? ProxyId,
[RowKey] int DstPort,
long DstPort,
string DstIp,
DateTimeOffset EndTime
[property: JsonPropertyName("endtime")] DateTimeOffset EndTime
) : EntityBase();

public record ProxyConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public async Task<HttpResponseData> Patch([HttpTrigger(AuthorizationLevel.Anonym
} else {

var query = UriExtension.GetQueryComponents(req.Url);
bool isNew = UriExtension.GetBoolValue("isNew", query, false);
bool isNew = UriExtension.GetBool("isNew", query, false);
//requireEtag wont' work since our current schema does not return etag to the client when getting data form the table, so
// there is no way to know which etag to use
bool requireEtag = UriExtension.GetBoolValue("requireEtag", query, false);
bool requireEtag = UriExtension.GetBool("requireEtag", query, false);

await _configOps.Save(newInstanceConfig, isNew, requireEtag);

Expand Down
4 changes: 2 additions & 2 deletions src/ApiService/ApiService/TestHooks/IpOperationsTestHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task<HttpResponseData> DeletePublicNic([HttpTrigger(AuthorizationLe

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

if (yes) {
await _ipOps.DeleteNic(rg, name);
Expand All @@ -79,7 +79,7 @@ public async Task<HttpResponseData> DeleteIp([HttpTrigger(AuthorizationLevel.Ano

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

if (yes) {
await _ipOps.DeleteIp(rg, name);
Expand Down
8 changes: 4 additions & 4 deletions src/ApiService/ApiService/TestHooks/JobOperationsTestHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public async Task<HttpResponseData> SearchState([HttpTrigger(AuthorizationLevel.
_log.Info("Search jobs by state");

var query = UriExtension.GetQueryComponents(req.Url);
var init = UriExtension.GetBoolValue("init", query, false);
var enabled = UriExtension.GetBoolValue("enabled", query, false);
var stopping = UriExtension.GetBoolValue("stopping", query, false);
var stopped = UriExtension.GetBoolValue("stopped", query, false);
var init = UriExtension.GetBool("init", query, false);
var enabled = UriExtension.GetBool("enabled", query, false);
var stopping = UriExtension.GetBool("stopping", query, false);
var stopped = UriExtension.GetBool("stopped", query, false);

var states = new List<JobState>();
if (init) {
Expand Down
31 changes: 7 additions & 24 deletions src/ApiService/ApiService/TestHooks/NodeOperationsTestHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public async Task<HttpResponseData> ToReimage([HttpTrigger(AuthorizationLevel.An
_log.Info("to reimage");

var query = UriExtension.GetQueryComponents(req.Url);
var done = UriExtension.GetBoolValue("done", query, false);
var done = UriExtension.GetBool("done", query, false);

var s = await req.ReadAsStringAsync();
var node = JsonSerializer.Deserialize<Node>(s!, EntityConverter.GetJsonSerializerOptions());
Expand Down Expand Up @@ -158,35 +158,18 @@ public async Task<HttpResponseData> SearchStates([HttpTrigger(AuthorizationLevel
_log.Info("search states");

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

Guid? poolId = default;
if (query.ContainsKey("poolId")) {
poolId = Guid.Parse(query["poolId"]);
}

Guid? scaleSetId = default;
if (query.ContainsKey("scaleSetId")) {
scaleSetId = Guid.Parse(query["scaleSetId"]);
}
Guid? poolId = UriExtension.GetGuid("poolId", query);
Guid? scaleSetId = UriExtension.GetGuid("scaleSetId", query);

List<NodeState>? states = default;
if (query.ContainsKey("states")) {
states = query["states"].Split('-').Select(s => Enum.Parse<NodeState>(s)).ToList();
}
string? poolName = UriExtension.GetString("poolName", query);

string? poolName = default;
if (query.ContainsKey("poolName")) {
poolName = query["poolName"];
}

var excludeUpdateScheduled = UriExtension.GetBoolValue("excludeUpdateScheduled", query, false);
int? numResults = default;
if (query.ContainsKey("numResults")) {
numResults = int.Parse(query["numResults"]);
}
var excludeUpdateScheduled = UriExtension.GetBool("excludeUpdateScheduled", query, false);
int? numResults = UriExtension.GetInt("numResults", query);
var r = _nodeOps.SearchStates(poolId, scaleSetId, states, poolName, excludeUpdateScheduled, numResults);


var json = JsonSerializer.Serialize(await r.ToListAsync(), EntityConverter.GetJsonSerializerOptions());
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteStringAsync(json);
Expand Down Expand Up @@ -236,7 +219,7 @@ public async Task<HttpResponseData> CreateNode([HttpTrigger(AuthorizationLevel.A

string version = query["version"];

bool isNew = UriExtension.GetBoolValue("isNew", query, false);
bool isNew = UriExtension.GetBool("isNew", query, false);

var node = await _nodeOps.Create(poolId, poolName, machineId, scaleSetId, version, isNew);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task<HttpResponseData> NewFiles([HttpTrigger(AuthorizationLevel.Ano

var container = query["container"];
var fileName = query["fileName"];
var failTaskOnTransientError = UriExtension.GetBoolValue("failTaskOnTransientError", query, true);
var failTaskOnTransientError = UriExtension.GetBool("failTaskOnTransientError", query, true);

await _notificationOps.NewFiles(new Container(container), fileName, failTaskOnTransientError);
var resp = req.CreateResponse(HttpStatusCode.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class NsgOperationsTestHooks {
private readonly INsgOperations _nsgOperations;

public NsgOperationsTestHooks(ILogTracer log, IConfigOperations configOps, INsgOperations nsgOperations) {
_log = log.WithTag("TestHooks", nameof(NotificationOperationsTestHooks));
_log = log.WithTag("TestHooks", nameof(NsgOperationsTestHooks));
_configOps = configOps; ;
_nsgOperations = nsgOperations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public PoolOperationsTestHooks(ILogTracer log, IConfigOperations configOps, IPoo


[Function("GetPoolTestHook")]
public async Task<HttpResponseData> GetNsg([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/poolOperations/pool")] HttpRequestData req) {
public async Task<HttpResponseData> GetPool([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/poolOperations/pool")] HttpRequestData req) {
_log.Info("get pool");

var query = UriExtension.GetQueryComponents(req.Url);
Expand Down
43 changes: 43 additions & 0 deletions src/ApiService/ApiService/TestHooks/ProxyForwardTestHooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 ProxyForwardTestHooks {
private readonly ILogTracer _log;
private readonly IConfigOperations _configOps;
private readonly IProxyForwardOperations _proxyForward;

public ProxyForwardTestHooks(ILogTracer log, IConfigOperations configOps, IProxyForwardOperations proxyForward) {
_log = log.WithTag("TestHooks", nameof(ProxyForwardTestHooks));
_configOps = configOps; ;
_proxyForward = proxyForward; ;
}

[Function("SearchForwardTestHook")]
public async Task<HttpResponseData> SearchForward([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/proxyForwardOperations/search")] HttpRequestData req) {
_log.Info("search proxy forward");

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

var poolRes = _proxyForward.SearchForward(
UriExtension.GetGuid("scaleSetId", query),
UriExtension.GetString("region", query),
UriExtension.GetGuid("machineId", query),
UriExtension.GetGuid("proxyId", query),
UriExtension.GetInt("dstPort", query));

var json = JsonSerializer.Serialize(await poolRes.ToListAsync(), EntityConverter.GetJsonSerializerOptions());
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteStringAsync(json);
return resp;
}
}
}
#endif
33 changes: 29 additions & 4 deletions src/ApiService/ApiService/TestHooks/UriExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,43 @@ from cs in queryComponents
return new Dictionary<string, string>(q);
}

public static bool GetBoolValue(string key, IDictionary<string, string> query, bool defaultValue = false) {
public static bool GetBool(string key, IDictionary<string, string> query, bool defaultValue = false) {
bool v;
if (query.ContainsKey(key)) {
if (!bool.TryParse(query[key], out v)) {
v = defaultValue;
}
v = bool.Parse(query[key]);
} else {
v = defaultValue;
}
return v;
}

public static int? GetInt(string key, IDictionary<string, string> query, int? defaultValue = null) {
int? v;
if (query.ContainsKey(key)) {
v = int.Parse(query[key]);
} else {
v = defaultValue;
}
return v;
}


public static string? GetString(string key, IDictionary<string, string> query, string? defaultValue = null) {
if (query.ContainsKey(key)) {
return query[key];
} else {
return defaultValue;
}
}

public static Guid? GetGuid(string key, IDictionary<string, string> query, Guid? defaultValue = null) {
if (query.ContainsKey(key)) {
return Guid.Parse(query[key]);
} else {
return defaultValue;
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public IAsyncEnumerable<ProxyForward> SearchForward(Guid? scalesetId = null, str
var conditions =
new[] {
scalesetId != null ? $"scaleset_id eq '{scalesetId}'" : null,
region != null ? $"region eq '{region}'" : null ,
region != null ? $"PartitionKey eq '{region}'" : null ,
machineId != null ? $"machine_id eq '{machineId}'" : null ,
proxyId != null ? $"proxy_id eq '{proxyId}'" : null ,
dstPort != null ? $"dsp_port eq {dstPort }" : null ,
dstPort != null ? $"dst_port eq {dstPort}" : null ,
}.Where(x => x != null);

var filter = string.Join(" and ", conditions);
var filter = Query.And(conditions!);

return QueryAsync(filter);
}
Expand Down
3 changes: 1 addition & 2 deletions src/ApiService/ApiService/onefuzzlib/ProxyOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public async Async.Task SaveProxyConfig(Proxy proxy) {
}



public async Async.Task SetState(Proxy proxy, VmState state) {
if (proxy.State == state) {
return;
Expand All @@ -127,7 +126,7 @@ public async Async.Task<List<Forward>> GetForwards(Proxy proxy) {
if (entry.EndTime < DateTimeOffset.UtcNow) {
await _context.ProxyForwardOperations.Delete(entry);
} else {
forwards.Add(new Forward(entry.Port, entry.DstPort, entry.DstIp));
forwards.Add(new Forward(long.Parse(entry.Port), entry.DstPort, entry.DstIp));
}
}
return forwards;
Expand Down
4 changes: 2 additions & 2 deletions src/ApiService/Tests/OrmModelsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public static Gen<Node> Node() {
}

public static Gen<ProxyForward> ProxyForward() {
return Arb.Generate<Tuple<Tuple<string, int, Guid, Guid, Guid?, int>, Tuple<IPv4Address, DateTimeOffset>>>().Select(
return Arb.Generate<Tuple<Tuple<string, long, Guid, Guid, Guid?, long>, Tuple<IPv4Address, DateTimeOffset>>>().Select(
arg =>
new ProxyForward(
Region: arg.Item1.Item1,
Port: arg.Item1.Item2,
Port: arg.Item1.Item2.ToString(),
ScalesetId: arg.Item1.Item3,
MachineId: arg.Item1.Item4,
ProxyId: arg.Item1.Item5,
Expand Down