Closes #463. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
111 lines
4.5 KiB
Rust
111 lines
4.5 KiB
Rust
use compliance_agent::{
|
|
agent, api, config, database, finding_migrations, lifecycle, scheduler, secrets, webhooks,
|
|
};
|
|
use compliance_core::shutdown;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
match dotenvy::dotenv() {
|
|
Ok(path) => eprintln!("[dotenv] Loaded from: {}", path.display()),
|
|
Err(_) => eprintln!("[dotenv] No .env file found, using environment variables"),
|
|
}
|
|
|
|
let _telemetry_guard = compliance_core::telemetry::init_telemetry("compliance-agent");
|
|
|
|
// Log critical env vars at startup
|
|
tracing::info!(
|
|
chrome_ws_url = std::env::var("CHROME_WS_URL").ok().as_deref(),
|
|
pentest_email = std::env::var("PENTEST_VERIFICATION_EMAIL").ok().as_deref(),
|
|
secrets_key_set = std::env::var("SECRETS_ENCRYPTION_KEY").is_ok(),
|
|
"Loading configuration..."
|
|
);
|
|
let config = config::load_config()?;
|
|
|
|
tracing::info!("Connecting to MongoDB...");
|
|
// Per-tenant pool only — the agent has no shared "default" database
|
|
// after M7.2-D. `mongodb_database` is now the db-name prefix used
|
|
// for tenant databases (`<prefix>_<tenant_id>`).
|
|
let db_pool =
|
|
database::DatabasePool::connect(&config.mongodb_uri, &config.mongodb_database).await?;
|
|
|
|
let agent = agent::ComplianceAgent::new(config.clone(), db_pool);
|
|
|
|
// Stored findings in the form the scans now write them (#463), before a
|
|
// scan (a resumed one included) can compare against them.
|
|
match finding_migrations::migrate_stored_findings(&agent.db_pool).await {
|
|
Ok(m) if m == finding_migrations::Migrated::default() => {}
|
|
Ok(m) => tracing::info!(
|
|
rule_ids = m.rule_ids,
|
|
fingerprints = m.fingerprints,
|
|
titles = m.titles,
|
|
"migrated stored findings"
|
|
),
|
|
Err(e) => tracing::error!("migrating stored findings failed: {e}"),
|
|
}
|
|
|
|
// Before anything can start a new run, close the runs the previous agent
|
|
// process left open when it stopped.
|
|
let started_at = chrono::Utc::now();
|
|
match lifecycle::recover_orphaned_runs(&agent.db_pool, started_at).await {
|
|
Ok(0) => {}
|
|
Ok(closed) => {
|
|
tracing::warn!(closed, "closed runs left open by the previous agent");
|
|
// The code scans among them start again, in the background.
|
|
let resume_agent = agent.clone();
|
|
tokio::spawn(async move {
|
|
let resumed = lifecycle::resume_interrupted_scans(&resume_agent, started_at).await;
|
|
tracing::info!(resumed, "scans interrupted by the restart started again");
|
|
});
|
|
}
|
|
Err(e) => tracing::error!("closing runs left open by the previous agent failed: {e}"),
|
|
}
|
|
|
|
// Credentials stored before sealing existed are sealed now, before any
|
|
// handler can read one back.
|
|
match secrets::seal_stored_credentials(&agent.db_pool, &agent.secrets).await {
|
|
Ok(0) => {}
|
|
Ok(sealed) => tracing::info!(targets = sealed, "sealed stored credentials"),
|
|
Err(e) => tracing::error!("sealing stored credentials failed: {e}"),
|
|
}
|
|
|
|
// SIGTERM or Ctrl+C fires `stop`; each server then drains on its own.
|
|
let (trigger, stop) = shutdown::channel();
|
|
tokio::spawn(async move {
|
|
shutdown::signal().await;
|
|
trigger.fire();
|
|
});
|
|
|
|
tracing::info!("Starting scheduler...");
|
|
let scheduler_agent = agent.clone();
|
|
let scheduler_handle = tokio::spawn(async move {
|
|
if let Err(e) = scheduler::start_scheduler(&scheduler_agent).await {
|
|
tracing::error!("Scheduler error: {e}");
|
|
}
|
|
});
|
|
|
|
let sweeper_handle = tokio::spawn(lifecycle::run_werkbank_sweeper(
|
|
agent.db_pool.clone(),
|
|
stop.clone(),
|
|
));
|
|
|
|
tracing::info!("Starting webhook server on port {}...", config.webhook_port);
|
|
let webhook_agent = agent.clone();
|
|
let webhook_stop = stop.clone();
|
|
let webhook_handle = tokio::spawn(async move {
|
|
if let Err(e) = webhooks::start_webhook_server(&webhook_agent, webhook_stop).await {
|
|
tracing::error!("Webhook server error: {e}");
|
|
}
|
|
});
|
|
|
|
tracing::info!("Starting REST API on port {}...", config.agent_port);
|
|
api::start_api_server(agent, config.agent_port, stop).await?;
|
|
|
|
// The API has drained; the webhook server and sweeper saw the same signal
|
|
// and finish within the same grace period. Scans still running are cut
|
|
// off here and closed as `interrupted` at the next start.
|
|
let _ = tokio::join!(webhook_handle, sweeper_handle);
|
|
scheduler_handle.abort();
|
|
tracing::info!("Agent stopped");
|
|
Ok(())
|
|
}
|