using System; using System.Net; using System.Threading.Tasks; using AutopilotMonitor.Functions.Helpers; using AutopilotMonitor.Functions.Services; using Microsoft.Azure.Functions.Worker; using Microsoft.Azure.Functions.Worker.Http; using Microsoft.Extensions.Logging; namespace AutopilotMonitor.Functions.Functions.Config { public class GetTenantConfigurationFunction { private readonly ILogger _logger; private readonly TenantConfigurationService _configService; public GetTenantConfigurationFunction( ILogger logger, TenantConfigurationService configService) { _logger = logger; _configService = configService; } [Function("GetTenantConfiguration")] public async Task Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "config/{tenantId}")] HttpRequestData req, string tenantId) { try { // Authentication + TenantAdminOrGA authorization enforced by PolicyEnforcementMiddleware var requestCtx = req.GetRequestContext(); var userIdentifier = requestCtx.UserPrincipalName; _logger.LogInformation($"GetTenantConfiguration: {requestCtx.TargetTenantId} by user {userIdentifier}"); var config = await _configService.GetConfigurationAsync(requestCtx.TargetTenantId); // NOTE: Returns full TenantConfiguration including DiagnosticsBlobSasUrl. // Intentional — tenant admins need to view/manage the SAS URL in the Settings UI. // Access restricted to TenantAdminOrGA by PolicyEnforcementMiddleware. var response = req.CreateResponse(HttpStatusCode.OK); await response.WriteAsJsonAsync(config); return response; } catch (Exception ex) { _logger.LogError(ex, "Error getting configuration for tenant {TenantId}", tenantId); var response = req.CreateResponse(HttpStatusCode.InternalServerError); await response.WriteAsJsonAsync(new { error = "Internal server error" }); return response; } } } }