-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new example, Discover API Hosts Details
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use clap::Parser; | ||
|
||
use rusty_falcon::{ | ||
apis::discover_api::{get_hosts, query_hosts}, | ||
easy::client::FalconHandle, | ||
}; | ||
|
||
// We set API limits to a constant value as the `get_hosts` takes max 100 elements. | ||
const LIMIT: i32 = 100; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(author, version, about, long_about = None)] | ||
struct Args { | ||
#[arg(short, long)] | ||
filter: Option<String>, | ||
|
||
#[arg(short, long)] | ||
sort: String, | ||
|
||
#[arg(short, long)] | ||
query: Option<String>, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let args = Args::parse(); | ||
|
||
let falcon = FalconHandle::from_env() | ||
.await | ||
.expect("Could not authenticate with CrowdStrike API"); | ||
|
||
let mut details = vec![]; | ||
let mut offset = 0usize; | ||
|
||
loop { | ||
let response = query_hosts( | ||
&falcon.cfg, | ||
Some(offset as i32), | ||
Some(LIMIT), | ||
Some(args.sort.as_str()), | ||
args.filter.as_deref(), | ||
) | ||
.await | ||
.expect("Could not fetch CCID"); | ||
|
||
if response.errors.is_some() { | ||
eprintln!( | ||
"Errors occurred while getting Falcon CCID: {:?}", | ||
response.errors | ||
); | ||
std::process::exit(1); | ||
} | ||
|
||
if response.resources.is_empty() { | ||
eprintln!("No CCID returned"); | ||
break; | ||
} | ||
|
||
offset = offset + response.resources.len(); | ||
|
||
let batch_details = get_hosts(&falcon.cfg, response.resources) | ||
.await | ||
.map(|entities| entities.resources.into_iter().collect::<Vec<_>>()) | ||
.expect("Couldn't fetch hosts details."); | ||
details.extend(batch_details); | ||
|
||
match response.meta.pagination { | ||
Some(pagination) if offset < pagination.total as usize => {} | ||
_ => break, | ||
}; | ||
} | ||
|
||
println!( | ||
"{}", | ||
serde_json::to_string_pretty(&details).expect("Couldn't convert the data to json.") | ||
); | ||
} |