use cortex_client::apis::job_api;
use cortex_client::models::JobCreateRequest;
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 abuseipdb_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";
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 example for IP {}",
analyzer_name_to_run, analyzer_worker_id, ip_to_analyze
))),
parameters: None, label: Some(Some("abuseipdb_example_scan".to_string())),
force: Some(false), attributes: None, };
match job_api::create_analyzer_job(&config, &analyzer_worker_id, job_request).await {
Ok(job_response) => {
println!("\nSuccessfully created analyzer job:");
println!("{:#?}", job_response);
if let Some(job_id) = job_response._id {
println!("\nJob status is Success. Attempting to fetch report directly using get_job_report for job ID: {}", job_id);
match job_api::get_job_report(&config, &job_id).await {
Ok(report_response) => {
println!("\nSuccessfully fetched job report:");
match report_response {
cortex_client::models::JobReportResponse::Object(json_report) => {
println!("Report (JSON): {:#?}", json_report);
}
cortex_client::models::JobReportResponse::JobReportResponseOneOf(
status_enum,
) => {
println!("Job status from get_job_report: {:?}", status_enum);
println!("This is unexpected if the job was marked 'Success' previously.");
}
}
}
Err(e) => {
eprintln!("\nError fetching job report with get_job_report: {:?}", e);
}
}
} else {
eprintln!("\nJob created, but no job ID was returned in the response. Cannot fetch report.");
}
}
Err(e) => {
eprintln!("\nError creating analyzer job: {:?}", e);
eprintln!("Please check:");
eprintln!(
" 1. The analyzer ID '{}' is correct and the analyzer is enabled in Cortex.",
analyzer_worker_id
);
eprintln!(" 2. Cortex is running and accessible at the configured CORTEX_ENDPOINT.");
eprintln!(" 3. Your CORTEX_API_KEY has the necessary permissions.");
}
}
Ok(())
}