2026-07-28 14:52:52 +00:00
|
|
|
using Sentry.AspNetCore;
|
|
|
|
|
|
2026-07-18 14:11:47 +00:00
|
|
|
using Heimdall.Agent;
|
2026-08-11 18:51:52 +02:00
|
|
|
using Heimdall.Agent.Logging;
|
2026-07-16 20:53:56 +00:00
|
|
|
using Heimdall.Agent.Services;
|
2026-07-18 14:09:32 +00:00
|
|
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
|
|
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
2026-08-11 18:51:52 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2026-07-18 14:11:47 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2026-07-16 19:09:28 +00:00
|
|
|
|
2026-07-18 14:09:32 +00:00
|
|
|
var builder = WebApplication.CreateSlimBuilder(args);
|
2026-07-16 20:53:56 +00:00
|
|
|
|
2026-07-28 10:14:34 +00:00
|
|
|
// ── Sentry (GlitchTip) — DSN from config pipeline with legacy env var fallback ──
|
|
|
|
|
// Primary: Sentry:Dsn from appsettings.json or env var Sentry__Dsn
|
|
|
|
|
// Fallback: legacy SENTRY_DSN env var (Docker backward compat)
|
|
|
|
|
// Neither set → DSN is null → Sentry SDK no-ops gracefully
|
|
|
|
|
var sentryDsn = builder.Configuration["Sentry:Dsn"]
|
|
|
|
|
?? Environment.GetEnvironmentVariable("SENTRY_DSN");
|
|
|
|
|
if (!string.IsNullOrEmpty(sentryDsn))
|
2026-07-18 19:41:57 +00:00
|
|
|
{
|
2026-07-28 14:52:52 +00:00
|
|
|
builder.WebHost.UseSentry((SentryAspNetCoreOptions options) =>
|
2026-07-19 19:33:10 +00:00
|
|
|
{
|
|
|
|
|
options.Dsn = sentryDsn;
|
|
|
|
|
options.MinimumBreadcrumbLevel = LogLevel.Debug;
|
|
|
|
|
options.MinimumEventLevel = LogLevel.Error;
|
2026-07-28 10:14:34 +00:00
|
|
|
options.TracesSampleRate = 0.05;
|
|
|
|
|
options.MaxRequestBodySize = Sentry.Extensibility.RequestSize.Small;
|
|
|
|
|
options.ProfilesSampleRate = 0.05;
|
2026-07-19 19:33:10 +00:00
|
|
|
});
|
|
|
|
|
}
|
2026-07-18 19:41:57 +00:00
|
|
|
|
2026-07-18 14:11:47 +00:00
|
|
|
// Strongly-typed configuration with validation
|
|
|
|
|
builder.Services.AddOptions<AgentOptions>()
|
|
|
|
|
.BindConfiguration(AgentOptions.SectionName)
|
|
|
|
|
.ValidateDataAnnotations()
|
|
|
|
|
.ValidateOnStart();
|
|
|
|
|
|
2026-07-18 14:09:32 +00:00
|
|
|
// HTTP clients
|
2026-07-18 14:11:47 +00:00
|
|
|
builder.Services.AddHttpClient("Central", (sp, client) =>
|
2026-07-16 20:53:56 +00:00
|
|
|
{
|
2026-07-18 14:11:47 +00:00
|
|
|
var options = sp.GetRequiredService<IOptions<AgentOptions>>().Value;
|
2026-07-27 18:43:25 +00:00
|
|
|
var baseUrl = options.CentralUrl.EndsWith('/') ? options.CentralUrl : options.CentralUrl + "/";
|
|
|
|
|
client.BaseAddress = new Uri(baseUrl);
|
2026-07-16 20:53:56 +00:00
|
|
|
client.Timeout = TimeSpan.FromSeconds(10);
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-18 14:02:40 +00:00
|
|
|
builder.Services.AddHttpClient("IpResolver", client =>
|
|
|
|
|
{
|
|
|
|
|
client.Timeout = TimeSpan.FromSeconds(5);
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-18 14:09:32 +00:00
|
|
|
// Agent services
|
2026-07-18 14:11:47 +00:00
|
|
|
builder.Services.AddSingleton<AgentState>(sp =>
|
|
|
|
|
{
|
|
|
|
|
var state = new AgentState();
|
|
|
|
|
var options = sp.GetRequiredService<IOptions<AgentOptions>>().Value;
|
|
|
|
|
|
|
|
|
|
var stateDir = options.StateDirectory
|
|
|
|
|
?? (OperatingSystem.IsLinux() ? "/var/lib/heimdall" : Directory.GetCurrentDirectory());
|
|
|
|
|
state.ConfigureStateDirectory(stateDir);
|
|
|
|
|
|
|
|
|
|
return state;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSingleton<SystemMetricsProvider>();
|
2026-08-11 16:15:41 +02:00
|
|
|
builder.Services.AddSingleton<NetworkCameraScanner>();
|
|
|
|
|
builder.Services.AddSingleton<DiscoveryScanTrigger>();
|
|
|
|
|
builder.Services.AddSingleton<AgentCredentialStore>();
|
2026-08-11 17:34:22 +02:00
|
|
|
builder.Services.AddSingleton<RuntimeLogLevel>();
|
2026-07-16 20:53:56 +00:00
|
|
|
builder.Services.AddHostedService<AgentRegistrationService>();
|
2026-07-18 18:58:20 +00:00
|
|
|
builder.Services.AddHostedService<ConfigReceiverService>();
|
2026-08-11 16:15:41 +02:00
|
|
|
builder.Services.AddHostedService<AgentDiscoveryService>();
|
2026-07-16 19:09:28 +00:00
|
|
|
|
2026-08-11 18:51:52 +02:00
|
|
|
// Log capture & upload to Central (ticket #50)
|
|
|
|
|
builder.Services.AddSingleton<LogBufferService>();
|
|
|
|
|
builder.Services.AddSingleton<AgentLoggingProvider>();
|
|
|
|
|
builder.Services.AddSingleton<ILoggerProvider>(sp => sp.GetRequiredService<AgentLoggingProvider>());
|
|
|
|
|
builder.Services.AddHostedService<LogUploadService>();
|
|
|
|
|
|
2026-07-18 14:09:32 +00:00
|
|
|
// Health checks — used by Docker HEALTHCHECK
|
|
|
|
|
builder.Services.AddHealthChecks()
|
|
|
|
|
.AddCheck<AgentHealthCheck>("agent_health", tags: ["ready"]);
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
2026-07-18 14:11:47 +00:00
|
|
|
// Health endpoint
|
2026-07-18 14:09:32 +00:00
|
|
|
app.MapHealthChecks("/healthz", new HealthCheckOptions
|
|
|
|
|
{
|
|
|
|
|
Predicate = _ => true,
|
|
|
|
|
ResultStatusCodes =
|
|
|
|
|
{
|
|
|
|
|
[HealthStatus.Healthy] = StatusCodes.Status200OK,
|
|
|
|
|
[HealthStatus.Degraded] = StatusCodes.Status200OK,
|
|
|
|
|
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable,
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await app.RunAsync();
|