Files
AutopilotMonitor/src/Backend/AutopilotMonitor.Functions/Functions/Vulnerability/GetSoftwareInventoryFunction.cs
T
Oliver Kieselbach 7cef2ea361 backend: one typed error envelope (IApiErrorResponse) on every non-2xx body
Every error body now leads with { error, code, correlationId }: the generic
ApiErrorResponse (hint?, retryAfterSeconds?, operation?) plus the specialised
McpQuotaExceededResponse (reordered; `message` became `error`),
DelegatedSlotLimitReachedResponse (+correlationId) and the new
SessionDeletionRejectedResponse (replaces the anonymous delete arms). The
correlation id is stamped by the writers — ResponseHelper.ErrorAsync and the
new ApiErrorWriter for the middleware side (auth 401, policy 403/503, rate
limit 429, MCP quota 429, global exception 500) — never by call sites.
exceptionType and rateLimitInfo leave the wire; Constants.ApiErrorCodes is the
code vocabulary and is exported as the manifest section `apiErrorCodes`
(API_ERROR_CODES for web + MCP). CorrelationIdMiddleware names the readable
headers via Access-Control-Expose-Headers.

Guard: TypedResponseGuardTests no longer tolerates anonymous error shapes;
the 458 legacy sites in 149 files are frozen in WriteBaseline to melt down.
New facts: envelope-prefix reflection, ApiErrorWriter body/header pins,
exact-shape pins for the generic envelope and the changed specialised bodies.

Reverses the "error bodies stay anonymous" clause of D-043 (decision entry
follows with the docs stage).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-05 00:56:21 +02:00

76 lines
2.9 KiB
C#

using System;
using System.Linq;
using System.Net;
using System.Web;
using System.Threading.Tasks;
using AutopilotMonitor.Functions.Helpers;
using AutopilotMonitor.Functions.Services;
using AutopilotMonitor.Shared.DataAccess;
using AutopilotMonitor.Shared.Models;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
namespace AutopilotMonitor.Functions.Functions.Vulnerability
{
/// <summary>
/// Returns the full software inventory for a tenant from the SoftwareInventory table.
/// Global Admin only. Query param: ?tenantId=...
/// </summary>
public class GetSoftwareInventoryFunction
{
private readonly ILogger<GetSoftwareInventoryFunction> _logger;
private readonly IVulnerabilityRepository _vulnRepo;
private readonly GlobalAdminService _globalAdminService;
public GetSoftwareInventoryFunction(
ILogger<GetSoftwareInventoryFunction> logger,
IVulnerabilityRepository vulnRepo,
GlobalAdminService globalAdminService)
{
_logger = logger;
_vulnRepo = vulnRepo;
_globalAdminService = globalAdminService;
}
[Function("GetSoftwareInventory")]
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "vulnerability/software-inventory")] HttpRequestData req)
{
try
{
// Global Admin only — enforced by PolicyEnforcementMiddleware
var userIdentifier = TenantHelper.GetUserIdentifier(req);
var query = HttpUtility.ParseQueryString(req.Url.Query);
var tenantId = query["tenantId"];
if (string.IsNullOrEmpty(tenantId))
{
var badRequest = req.CreateResponse(HttpStatusCode.BadRequest);
await badRequest.WriteAsJsonAsync(new { success = false, message = "tenantId query parameter is required" });
return badRequest;
}
var inventory = await _vulnRepo.GetSoftwareInventoryAsync(tenantId);
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteAsJsonAsync(new SoftwareInventoryResponse
{
Success = true,
TenantId = tenantId,
Total = inventory.Count,
Matched = inventory.Count(i => !string.IsNullOrEmpty(i["cpeUri"]?.ToString())),
Unmatched = inventory.Count(i => string.IsNullOrEmpty(i["cpeUri"]?.ToString())),
Inventory = inventory
});
return response;
}
catch (Exception ex)
{
return await req.InternalServerErrorAsync(_logger, ex, "GetSoftwareInventory");
}
}
}
}