using System.Diagnostics; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using StellaOps.Auth.Client; using StellaOps.Configuration; using StellaOps.Scanner.Cache; using StellaOps.Scanner.Analyzers.OS.Plugin; using StellaOps.Scanner.Analyzers.Lang.Plugin; using StellaOps.Scanner.EntryTrace; using StellaOps.Scanner.Core.Contracts; using StellaOps.Scanner.Core.Security; using StellaOps.Scanner.Surface.Env; using StellaOps.Scanner.Surface.FS; using StellaOps.Scanner.Surface.Secrets; using StellaOps.Scanner.Surface.Validation; using StellaOps.Scanner.Worker.Diagnostics; using StellaOps.Scanner.Worker.Hosting; using StellaOps.Scanner.Worker.Options; using StellaOps.Scanner.Worker.Processing; using StellaOps.Scanner.Worker.Processing.Surface; using StellaOps.Scanner.Storage.Extensions; using StellaOps.Scanner.Storage; var builder = Host.CreateApplicationBuilder(args); builder.Services.AddOptions() .BindConfiguration(ScannerWorkerOptions.SectionName) .ValidateOnStart(); builder.Services.AddSingleton, ScannerWorkerOptionsValidator>(); builder.Services.AddSingleton(TimeProvider.System); builder.Services.AddScannerCache(builder.Configuration); builder.Services.AddSurfaceEnvironment(options => { options.ComponentName = "Scanner.Worker"; }); builder.Services.AddSurfaceValidation(); builder.Services.AddSurfaceFileCache(); builder.Services.AddSurfaceSecrets(); builder.Services.AddSingleton>(sp => new SurfaceCacheOptionsConfigurator(sp.GetRequiredService())); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddEntryTraceAnalyzer(); builder.Services.AddSingleton(); var storageSection = builder.Configuration.GetSection("ScannerStorage"); var connectionString = storageSection.GetValue("Mongo:ConnectionString"); if (!string.IsNullOrWhiteSpace(connectionString)) { builder.Services.AddScannerStorage(storageSection); builder.Services.AddSingleton, ScannerStorageSurfaceSecretConfigurator>(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); } else { builder.Services.TryAddSingleton(); } builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddHostedService(sp => sp.GetRequiredService()); var workerOptions = builder.Configuration.GetSection(ScannerWorkerOptions.SectionName).Get() ?? new ScannerWorkerOptions(); builder.Services.AddStellaOpsCrypto(workerOptions.Crypto); builder.Services.Configure(options => { options.ShutdownTimeout = workerOptions.Shutdown.Timeout; }); builder.ConfigureScannerWorkerTelemetry(workerOptions); if (workerOptions.Authority.Enabled) { builder.Services.AddStellaOpsAuthClient(clientOptions => { clientOptions.Authority = workerOptions.Authority.Issuer?.Trim() ?? string.Empty; clientOptions.ClientId = workerOptions.Authority.ClientId?.Trim() ?? string.Empty; clientOptions.ClientSecret = workerOptions.Authority.ClientSecret; clientOptions.EnableRetries = workerOptions.Authority.Resilience.EnableRetries ?? true; clientOptions.HttpTimeout = TimeSpan.FromSeconds(workerOptions.Authority.BackchannelTimeoutSeconds); clientOptions.DefaultScopes.Clear(); foreach (var scope in workerOptions.Authority.Scopes) { if (string.IsNullOrWhiteSpace(scope)) { continue; } clientOptions.DefaultScopes.Add(scope); } clientOptions.RetryDelays.Clear(); foreach (var delay in workerOptions.Authority.Resilience.RetryDelays) { if (delay <= TimeSpan.Zero) { continue; } clientOptions.RetryDelays.Add(delay); } if (workerOptions.Authority.Resilience.AllowOfflineCacheFallback is bool allowOffline) { clientOptions.AllowOfflineCacheFallback = allowOffline; } if (workerOptions.Authority.Resilience.OfflineCacheTolerance is { } tolerance && tolerance > TimeSpan.Zero) { clientOptions.OfflineCacheTolerance = tolerance; } }); } builder.Logging.Configure(options => { options.ActivityTrackingOptions = ActivityTrackingOptions.SpanId | ActivityTrackingOptions.TraceId | ActivityTrackingOptions.ParentId; }); var host = builder.Build(); await host.RunAsync(); public partial class Program; internal sealed class SurfaceCacheOptionsConfigurator : IConfigureOptions { private readonly ISurfaceEnvironment _surfaceEnvironment; public SurfaceCacheOptionsConfigurator(ISurfaceEnvironment surfaceEnvironment) { _surfaceEnvironment = surfaceEnvironment ?? throw new ArgumentNullException(nameof(surfaceEnvironment)); } public void Configure(SurfaceCacheOptions options) { ArgumentNullException.ThrowIfNull(options); var settings = _surfaceEnvironment.Settings; options.RootDirectory = settings.CacheRoot.FullName; } }