diff --git a/src/ApiService/ApiService/TestHooks.cs b/src/ApiService/ApiService/TestHooks.cs index 658d76ffb91..84e1642c72b 100644 --- a/src/ApiService/ApiService/TestHooks.cs +++ b/src/ApiService/ApiService/TestHooks.cs @@ -42,7 +42,6 @@ public async Task Info([HttpTrigger(AuthorizationLevel.Anonymo } - [Function("GetKeyvaultAddress")] public async Task GetKeyVaultAddress([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/secrets/keyvaultaddress")] HttpRequestData req) { _log.Info("Getting keyvault address"); @@ -87,24 +86,5 @@ from cs in queryComponents return resp; } - - [Function("GetWorkspaceId")] - public async Task 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 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 diff --git a/src/ApiService/ApiService/TestHooks/LogAnalyticsTestHooks.cs b/src/ApiService/ApiService/TestHooks/LogAnalyticsTestHooks.cs index 697e81e1bca..19964d24fb8 100644 --- a/src/ApiService/ApiService/TestHooks/LogAnalyticsTestHooks.cs +++ b/src/ApiService/ApiService/TestHooks/LogAnalyticsTestHooks.cs @@ -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; } diff --git a/src/ApiService/ApiService/TestHooks/NotificationOperationsTestHooks.cs b/src/ApiService/ApiService/TestHooks/NotificationOperationsTestHooks.cs index 58e5acf7ee4..91d2c908e61 100644 --- a/src/ApiService/ApiService/TestHooks/NotificationOperationsTestHooks.cs +++ b/src/ApiService/ApiService/TestHooks/NotificationOperationsTestHooks.cs @@ -70,7 +70,7 @@ public async Task GetRegressionReportTask([HttpTrigger(Authori var s = await req.ReadAsStringAsync(); var report = JsonSerializer.Deserialize(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); diff --git a/src/ApiService/ApiService/TestHooks/NsgOperationsTestHooks.cs b/src/ApiService/ApiService/TestHooks/NsgOperationsTestHooks.cs new file mode 100644 index 00000000000..c3011142ba1 --- /dev/null +++ b/src/ApiService/ApiService/TestHooks/NsgOperationsTestHooks.cs @@ -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 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 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 diff --git a/src/ApiService/ApiService/onefuzzlib/NsgOperations.cs b/src/ApiService/ApiService/onefuzzlib/NsgOperations.cs index cf9efb22787..33b120e7737 100644 --- a/src/ApiService/ApiService/onefuzzlib/NsgOperations.cs +++ b/src/ApiService/ApiService/onefuzzlib/NsgOperations.cs @@ -97,11 +97,18 @@ await _creds.GetResourceGroupResource() } public async Async.Task 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 ListNsgs() { @@ -118,9 +125,18 @@ public bool OkToDelete(HashSet active_regions, string nsg_region, string /// public async Async.Task 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) {