Files
AutopilotMonitor/src/Backend/AutopilotMonitor.Functions/Functions/Vulnerability/GetSoftwareInventoryFunction.cs
T
Oliver Kieselbach 6e54b52376 backend: migrate the 458 anonymous error bodies to the typed envelope helpers
Every non-2xx body in the Functions project now goes through ResponseHelper
(ErrorAsync / BadRequestAsync / NotFoundAsync / ForbiddenAsync /
UnauthorizedAsync / ConflictAsync / InternalServerErrorAsync): 408 sites by a
pattern-exact rewrite of the CreateResponse + WriteAsJsonAsync + return shape,
50 by hand. The eight private BadRequestAsync shadow helpers are gone; the raw
500 catch blocks use InternalServerErrorAsync (sanitized message, correlation
id, MCP hint). 429s keep Retry-After via retryAfterSeconds.

Specialised error bodies where a consumer reads more than the prefix:
AppHomingDeniedResponse (kebab reason codes now Constants.AppHomingReasonCodes,
probe result), TenantConfigPatchFailedResponse (backupId, drift),
QueryBackendLogsErrorResponse (upstreamCode, statusCode, source, hint,
upstream). Twenty-one message-first 200 bodies that the old error tolerance had
let through are typed too (MessageResponse, PreviewWhitelistActionResponse,
SuccessMessageResponse). New codes: PayloadTooLarge, UpstreamError,
UpstreamTimeout, the three trial-gate codes; the agent security path
(SecurityValidationExtensions) writes the envelope with the retry window.

TypedResponseGuardTests: WriteBaseline is empty again — the anonymous
WriteAsJsonAsync ratchet now covers error bodies with zero tolerated sites.
Manifest and generated TypeScript regenerated.

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

74 lines
2.7 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))
{
return await req.BadRequestAsync("tenantId query parameter is required");
}
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");
}
}
}
}