mirror of
https://github.com/okieselbach/AutopilotMonitor.git
synced 2026-10-25 21:19:38 +00:00
1f796bf6e3
## Rules as GitHub single source of truth - Add 75 individual JSON rule files (2 gather, 18 analyze, 55 IME patterns) under rules/gather/, rules/analyze/, rules/ime-log-patterns/ - Add JSON Schemas for all three rule types with VS Code IntelliSense support - Add CI combine script (rules/scripts/combine.js) + GitHub Action that validates schemas with ajv-cli and generates rules/dist/ combined files - Convert BuiltIn*.cs from hardcoded C# objects to embedded JSON resource loaders - Add GitHubRuleRepository service to fetch dist/ files from raw.githubusercontent.com - Add ReseedFromGitHubFunction: POST /api/rules/reseed-from-github?type=all|gather|analyze|ime ## IME Log Patterns - Extend ImeLogPattern model with Description and IsBuiltIn properties - Add ImeLogPatterns table + API constants - Add TableStorageService CRUD methods for IME log patterns - Add ImeLogPatternService (CRUD, merge, auto-seed from embedded JSON) - Add ImeLogPatternsFunction: GET/PUT /api/rules/ime-log-patterns (Phase 1: read-only for tenants, Galactic Admin can enable/disable/edit globally) - Add IME Log Patterns management page (/ime-log-patterns) - Wire GetAgentConfigFunction to use ImeLogPatternService instead of static class ## API route consolidation - All rule endpoints now under /api/rules/* for single certificate exclusion path: /api/rules/gather, /api/rules/analyze, /api/rules/ime-log-patterns, /api/rules/reseed-from-github ## Export buttons - Add per-rule and export-all JSON download buttons to gather-rules, analyze-rules, and ime-log-patterns pages ## Admin configuration - Replace old individual reseed buttons with unified "Fetch & Reseed from GitHub" (calls new GitHub endpoint) plus compact individual-type reseed buttons - Add CDN cache hint: GitHub raw.githubusercontent.com caches up to 5 minutes ## Preview mode - ?preview=1 URL param seeds localStorage.previewMode (persists across navigation) - ?preview=0 clears it; toggle in Navbar settings dropdown (indigo, eye icon) - IME Log Patterns nav link gated behind galacticAdminMode OR previewMode - Preview badge shown on nav link when active for normal users ## Tests - Add xUnit test project (net8.0) with 5 tests for embedded resource loading, known IME actions validation, and valid categories check Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
132 lines
5.8 KiB
C#
132 lines
5.8 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using AutopilotMonitor.Functions.Middleware;
|
|
using AutopilotMonitor.Functions.Services;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.Azure.Functions.Worker;
|
|
using Microsoft.Azure.Functions.Worker.Builder;
|
|
using Microsoft.Azure.Functions.Worker.Middleware;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Identity.Web;
|
|
|
|
var builder = FunctionsApplication.CreateBuilder(args);
|
|
|
|
builder.ConfigureFunctionsWebApplication();
|
|
|
|
// Register authentication middleware in the pipeline (Azure Functions .NET 8 isolated worker pattern)
|
|
builder.UseMiddleware<AuthenticationMiddleware>();
|
|
|
|
// Configure JSON serialization to use camelCase
|
|
builder.Services.Configure<JsonSerializerOptions>(options =>
|
|
{
|
|
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
|
options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
|
// Serialize enums as strings for better readability and frontend compatibility
|
|
options.Converters.Add(new JsonStringEnumConverter());
|
|
});
|
|
|
|
builder.Services
|
|
.AddApplicationInsightsTelemetryWorkerService()
|
|
.ConfigureFunctionsApplicationInsights();
|
|
|
|
// Configure JWT Authentication for Multi-Tenant Azure AD
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddMicrosoftIdentityWebApi(options =>
|
|
{
|
|
// Configure JWT Bearer options if needed
|
|
options.Events = new JwtBearerEvents
|
|
{
|
|
OnAuthenticationFailed = context =>
|
|
{
|
|
var logger = context.HttpContext.RequestServices.GetRequiredService<ILoggerFactory>().CreateLogger("Auth");
|
|
logger.LogWarning("Authentication failed: {Error}", context.Exception?.Message);
|
|
return Task.CompletedTask;
|
|
},
|
|
OnTokenValidated = context =>
|
|
{
|
|
var logger = context.HttpContext.RequestServices.GetRequiredService<ILoggerFactory>().CreateLogger("Auth");
|
|
var claims = context.Principal?.Claims;
|
|
var tenantId = claims?.FirstOrDefault(c => c.Type == "tid")?.Value;
|
|
logger.LogDebug("Token validated for tenant: {TenantId}", tenantId);
|
|
return Task.CompletedTask;
|
|
}
|
|
};
|
|
},
|
|
options =>
|
|
{
|
|
// Multi-Tenant Configuration
|
|
options.Instance = "https://login.microsoftonline.com/";
|
|
options.TenantId = "organizations"; // Accept tokens from any Azure AD tenant
|
|
options.ClientId = builder.Configuration["EntraId:ClientId"];
|
|
|
|
// Token validation parameters
|
|
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuers = new[]
|
|
{
|
|
"https://login.microsoftonline.com/organizations/v2.0",
|
|
"https://sts.windows.net/{tenantid}/"
|
|
},
|
|
// Temporarily accept Microsoft Graph tokens (used by frontend with User.Read scope)
|
|
// TODO: Later expose custom API and use api://{clientId} scopes
|
|
ValidAudiences = new[]
|
|
{
|
|
builder.Configuration["EntraId:ClientId"] // Our app's client ID
|
|
//"https://graph.microsoft.com", // Microsoft Graph
|
|
//"00000003-0000-0000-c000-000000000000" // Microsoft Graph App ID
|
|
}
|
|
};
|
|
});
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
// Enable ASP.NET Core integration for authentication
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
// Register our services
|
|
builder.Services.AddMemoryCache();
|
|
builder.Services.AddHttpClient();
|
|
builder.Services.AddSingleton<TableStorageService>();
|
|
builder.Services.AddHostedService<TableInitializerService>(); // Initialize all tables at startup
|
|
builder.Services.AddSingleton<TenantConfigurationService>();
|
|
builder.Services.AddSingleton<AdminConfigurationService>();
|
|
builder.Services.AddSingleton<RateLimitService>();
|
|
builder.Services.AddSingleton<UsageMetricsService>();
|
|
builder.Services.AddSingleton<GalacticAdminService>();
|
|
builder.Services.AddSingleton<PreviewWhitelistService>();
|
|
builder.Services.AddSingleton<TenantAdminsService>();
|
|
builder.Services.AddSingleton<HealthCheckService>();
|
|
builder.Services.AddSingleton<GatherRuleService>();
|
|
builder.Services.AddSingleton<AnalyzeRuleService>();
|
|
builder.Services.AddSingleton<ImeLogPatternService>();
|
|
builder.Services.AddHttpClient<GitHubRuleRepository>();
|
|
builder.Services.AddSingleton<MaintenanceService>();
|
|
builder.Services.AddSingleton<BlockedDeviceService>();
|
|
builder.Services.AddSingleton<AutopilotMonitor.Functions.Security.AutopilotDeviceValidator>();
|
|
builder.Services.AddHttpClient<TeamsNotificationService>();
|
|
builder.Services.AddHttpClient<TelegramNotificationService>();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Log CORS configuration at startup so misconfigured origins are immediately visible
|
|
// in the log stream. CORS is enforced by Azure infrastructure, not by function code,
|
|
// so a blocked preflight never reaches the function worker and leaves no trace.
|
|
var startupLogger = app.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Startup");
|
|
var corsOrigins = builder.Configuration["Host:CORS"] // local.settings.json
|
|
?? builder.Configuration["WEBSITE_CORS_ALLOWED_ORIGINS"] // Azure App Settings
|
|
?? "(not configured - all cross-origin requests will be blocked!)";
|
|
var corsCredentials = builder.Configuration["Host:CORSCredentials"]
|
|
?? builder.Configuration["WEBSITE_CORS_SUPPORT_CREDENTIALS"]
|
|
?? "unknown";
|
|
startupLogger.LogInformation(
|
|
"=== CORS CONFIG: AllowedOrigins={CorsOrigins} | SupportCredentials={CorsCredentials} ===",
|
|
corsOrigins, corsCredentials);
|
|
|
|
app.Run();
|