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

Commit

Permalink
nsg operations test hooks and some bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stas committed May 6, 2022
1 parent 777a7c6 commit f74ff22
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 29 deletions.
20 changes: 0 additions & 20 deletions src/ApiService/ApiService/TestHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public async Task<HttpResponseData> Info([HttpTrigger(AuthorizationLevel.Anonymo
}



[Function("GetKeyvaultAddress")]
public async Task<HttpResponseData> GetKeyVaultAddress([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/secrets/keyvaultaddress")] HttpRequestData req) {
_log.Info("Getting keyvault address");
Expand Down Expand Up @@ -87,24 +86,5 @@ from cs in queryComponents
return resp;
}


[Function("GetWorkspaceId")]
public async Task<HttpResponseData> GetWorkspaceId([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/logAnalytics/workspaceId")] HttpRequestData req) {
var id = _logAnalytics.GetWorkspaceId();
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteAsJsonAsync(id);
return resp;
}



[Function("GetMonitorSettings")]
public async Task<HttpResponseData> GetMonitorSettings([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/logAnalytics/monitorSettings")] HttpRequestData req) {
var settings = await _logAnalytics.GetMonitorSettings();
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteAsJsonAsync(settings);
return resp;
}

}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class LogAnalyticsTestHooks {


public LogAnalyticsTestHooks(ILogTracer log, IConfigOperations configOps, ILogAnalytics logAnalytics) {
_log = log.WithTag("TestHooks", nameof(JobOperationsTestHooks));
_log = log.WithTag("TestHooks", nameof(LogAnalyticsTestHooks));
_configOps = configOps;
_logAnalytics = logAnalytics;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task<HttpResponseData> GetRegressionReportTask([HttpTrigger(Authori

var s = await req.ReadAsStringAsync();
var report = JsonSerializer.Deserialize<RegressionReport>(s!, EntityConverter.GetJsonSerializerOptions());
var task = (_notificationOps as NotificationOperations)!.GetRegressionReportTask(report);
var task = (_notificationOps as NotificationOperations)!.GetRegressionReportTask(report!);

var json = JsonSerializer.Serialize(task, EntityConverter.GetJsonSerializerOptions());
var resp = req.CreateResponse(HttpStatusCode.OK);
Expand Down
55 changes: 55 additions & 0 deletions src/ApiService/ApiService/TestHooks/NsgOperationsTestHooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 NsgOperationsTestHooks {

private readonly ILogTracer _log;
private readonly IConfigOperations _configOps;
private readonly INsgOperations _nsgOperations;

public NsgOperationsTestHooks(ILogTracer log, IConfigOperations configOps, INsgOperations nsgOperations) {
_log = log.WithTag("TestHooks", nameof(NotificationOperationsTestHooks));
_configOps = configOps; ;
_nsgOperations = nsgOperations;
}


[Function("GetNsgTestHook")]
public async Task<HttpResponseData> GetNsg([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/nsgOperations/getNsg")] HttpRequestData req) {
_log.Info("get nsg");

var query = UriExtension.GetQueryComponents(req.Url);
var nsg = await _nsgOperations.GetNsg(query["name"]);

if (nsg is null) {
var resp = req.CreateResponse(HttpStatusCode.NotFound);
return resp;
} else {
var resp = req.CreateResponse(HttpStatusCode.OK);
var data = nsg!.Data;
await resp.WriteAsJsonAsync(new { ResourceId = data.ResourceGuid });
return resp;
}
}


[Function("ListNsgsTestHook")]
public async Task<HttpResponseData> ListNsgs([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/nsgOperations/listNsgs")] HttpRequestData req) {
_log.Info("list nsgs");

var nsgs = await _nsgOperations.ListNsgs().ToListAsync();

var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteAsJsonAsync(nsgs.Select(x => new { Name = x.Data.Name, ResourceId = x.Data.ResourceGuid }));
return resp;
}
}
}
#endif
30 changes: 23 additions & 7 deletions src/ApiService/ApiService/onefuzzlib/NsgOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,18 @@ await _creds.GetResourceGroupResource()
}

public async Async.Task<NetworkSecurityGroupResource?> GetNsg(string name) {
var response = await _creds.GetResourceGroupResource().GetNetworkSecurityGroupAsync(name);
if (response == null) {
//_logTracer.Debug($"nsg %s does not exist: {name}");
try {
var response = await _creds.GetResourceGroupResource().GetNetworkSecurityGroupAsync(name);
return response?.Value;
} catch (RequestFailedException ex) {
if (ex.ErrorCode == "ResourceNotFound") {
_logTracer.Verbose($"could not find nsg with name {name}");
return null;
} else {
_logTracer.Exception(ex, $"failed to get nsg {name}");
throw;
}
}
return response?.Value;
}

public IAsyncEnumerable<NetworkSecurityGroupResource> ListNsgs() {
Expand All @@ -118,9 +125,18 @@ public bool OkToDelete(HashSet<string> active_regions, string nsg_region, string
/// </summary>
public async Async.Task<bool> StartDeleteNsg(string name) {
_logTracer.Info($"deleting nsg: {name}");
var nsg = await _creds.GetResourceGroupResource().GetNetworkSecurityGroupAsync(name);
await nsg.Value.DeleteAsync(WaitUntil.Completed);
return true;
try {
var nsg = await _creds.GetResourceGroupResource().GetNetworkSecurityGroupAsync(name);
await nsg.Value.DeleteAsync(WaitUntil.Completed);
return true;
} catch (RequestFailedException ex) {
if (ex.ErrorCode == "ResourceNotFound") {
return true;
} else {
_logTracer.Exception(ex, $"failed to delete nsg {name}");
throw;
}
}
}

private static bool IsConcurrentRequestError(string err) {
Expand Down

0 comments on commit f74ff22

Please sign in to comment.