Implementation of basic PatchComplianceTask. First real iteration, basic/raw asf.
92 lines
2.8 KiB
C#
92 lines
2.8 KiB
C#
using OversightService.Services;
|
|
|
|
namespace OversightService
|
|
{
|
|
public class Worker : BackgroundService
|
|
{
|
|
private readonly ILogger<Worker> _logger;
|
|
private OsqueryTaskScheduler? _taskScheduler;
|
|
private AppConfig? _config;
|
|
|
|
public Worker(ILogger<Worker> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
_logger.LogInformation("OversightService starting...");
|
|
|
|
try
|
|
{
|
|
// Load configuration
|
|
_config = LoadConfig();
|
|
_logger.LogInformation("Configuration loaded successfully.");
|
|
|
|
// Initialize osquery task scheduler
|
|
InitializeTaskScheduler();
|
|
_logger.LogInformation("Task scheduler initialized successfully.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to initialize OversightService");
|
|
throw;
|
|
}
|
|
|
|
// Keep service running
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
{
|
|
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
|
|
}
|
|
|
|
// Cleanup on shutdown
|
|
_logger.LogInformation("OversightService stopping...");
|
|
_taskScheduler?.Stop();
|
|
}
|
|
|
|
private AppConfig LoadConfig()
|
|
{
|
|
var configPath = Path.Combine(AppContext.BaseDirectory, "config.json");
|
|
|
|
if (!File.Exists(configPath))
|
|
{
|
|
throw new FileNotFoundException($"Config file not found at {configPath}");
|
|
}
|
|
|
|
var json = File.ReadAllText(configPath);
|
|
var config = Newtonsoft.Json.JsonConvert.DeserializeObject<AppConfig>(json);
|
|
|
|
if (config == null)
|
|
{
|
|
throw new InvalidOperationException("Failed to deserialize config.json");
|
|
}
|
|
|
|
return config;
|
|
}
|
|
|
|
private void InitializeTaskScheduler()
|
|
{
|
|
if (_config == null)
|
|
{
|
|
throw new InvalidOperationException("Config must be loaded before initializing scheduler");
|
|
}
|
|
|
|
_taskScheduler = new OsqueryTaskScheduler();
|
|
|
|
// Register patch compliance task if enabled
|
|
if (_config.PatchCompliance.Enabled)
|
|
{
|
|
var apiClient = new ApiClient(_config);
|
|
var patchTask = new PatchComplianceTask(_config, apiClient);
|
|
_taskScheduler.RegisterTask(patchTask);
|
|
|
|
_logger.LogInformation("Patch compliance task registered (interval: {hours} hours)",
|
|
_config.PatchCompliance.CheckIntervalHours);
|
|
}
|
|
|
|
// Start the scheduler
|
|
_taskScheduler.Start();
|
|
}
|
|
}
|
|
}
|