use cortex_client::apis::job_api;
use cortex_client::models::JobCreateRequest;
use std::time::Duration;
mod common;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = match common::setup_configuration() {
Ok(cfg) => cfg,
Err(e) => {
eprintln!("Configuration error: {}", e);
eprintln!(
"Please ensure CORTEX_ENDPOINT and CORTEX_API_KEY environment variables are set."
);
eprintln!("Example usage:");
eprintln!(" export CORTEX_ENDPOINT=\"http://localhost:9000/api\"");
eprintln!(" export CORTEX_API_KEY=\"your_api_key_here\"");
eprintln!(" cargo run --example wait_report_example");
return Err(e.into());
}
};
let analyzer_name_to_run = "AbuseIPDB_1_0";
let ip_to_analyze = "8.8.8.8";
let data_type = "ip";
println!("Looking for analyzer: {}", analyzer_name_to_run);
let analyzer_worker_id = match common::get_analyzer_id_by_name(&config, analyzer_name_to_run)
.await
{
Ok(Some(id)) => id,
Ok(None) => {
eprintln!("Could not find an analyzer instance named '{}'. Please check the name and ensure the analyzer is enabled in Cortex.", analyzer_name_to_run);
eprintln!("You can use the 'list_analyzers' example to see available analyzer names and their instance IDs.");
return Ok(()); }
Err(e) => {
eprintln!(
"Error trying to get analyzer ID for '{}': {}",
analyzer_name_to_run, e
);
return Err(e);
}
};
println!(
"Attempting to run analyzer instance ID '{}' (resolved from name '{}') on IP: {}",
analyzer_worker_id, analyzer_name_to_run, ip_to_analyze
);
let job_request = JobCreateRequest {
data: Some(ip_to_analyze.to_string()),
data_type: Some(data_type.to_string()),
tlp: Some(2), pap: Some(2), message: Some(Some(format!(
"Running {} (instance ID {}) scan from wait_report example for IP {}",
analyzer_name_to_run, analyzer_worker_id, ip_to_analyze
))),
parameters: None,
label: Some(Some("wait_report_example_scan".to_string())),
force: Some(true), attributes: None,
};
match common::run_job_and_wait_for_report(
&config,
&analyzer_worker_id,
job_request,
analyzer_name_to_run,
ip_to_analyze,
)
.await
{
Ok(report_response) => {
println!("\nReport details from wait_report_example:");
match report_response {
cortex_client::models::JobReportResponse::Object(json_report) => {
if let Some(summary) = json_report.get("summary") {
println!("Summary: {}", summary);
}
if let Some(taxonomies) = json_report.get("taxonomies") {
if let Some(tax_array) = taxonomies.as_array() {
println!("\nTaxonomies:");
for taxonomy in tax_array {
if let (
Some(level),
Some(namespace),
Some(predicate),
Some(value),
) = (
taxonomy.get("level").and_then(|v| v.as_str()),
taxonomy.get("namespace").and_then(|v| v.as_str()),
taxonomy.get("predicate").and_then(|v| v.as_str()),
taxonomy.get("value").and_then(|v| v.as_str()),
) {
println!(
" - {}: {}:{}={}",
level, namespace, predicate, value
);
}
}
}
}
println!("\nFull report (JSON):");
println!(
"{}",
serde_json::to_string_pretty(&json_report)
.unwrap_or_else(|_| json_report.to_string())
);
}
cortex_client::models::JobReportResponse::JobReportResponseOneOf(
status_enum,
) => {
eprintln!(
"Received non-object report status from common function: {:?}",
status_enum
);
eprintln!("This might indicate the job did not complete successfully or the report is not in the expected format.");
}
}
}
Err(e) => {
eprintln!(
"\nError in run_job_and_wait_for_report for analyzer '{}' on IP '{}': {:?}",
analyzer_name_to_run, ip_to_analyze, e
);
eprintln!("Please check the logs from the common function for more details.");
eprintln!("Also, ensure:");
eprintln!(
" 1. The analyzer ID '{}' (for '{}') is correct and the analyzer is enabled.",
analyzer_worker_id, analyzer_name_to_run
);
eprintln!(" 2. Cortex is running and accessible at the configured CORTEX_ENDPOINT.");
eprintln!(" 3. Your CORTEX_API_KEY has the necessary permissions.");
}
}
Ok(())
}