Skip to content

Commit

Permalink
Rename ospd to osp, minor refactoring for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtsfrei committed Jan 9, 2025
1 parent ebec2a5 commit fdb2ab0
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 342 deletions.
6 changes: 3 additions & 3 deletions rust/src/scannerctl/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod execute;
mod feed;
mod interpret;
mod notusupdate;
mod ospd;
mod osp;
mod scanconfig;
mod syntax;

Expand Down Expand Up @@ -62,7 +62,7 @@ async fn main() {
);
let matches = syntax::extend_args(matches);
let matches = scanconfig::extend_args(matches);
let matches = ospd::extend_args(matches);
let matches = osp::extend_args(matches);
let matches = execute::extend_args(matches);
let matches = notusupdate::scanner::extend_args(matches);
let matches = feed::extend_args(matches).get_matches();
Expand Down Expand Up @@ -100,7 +100,7 @@ async fn run(matches: &ArgMatches) -> Result<(), CliError> {
if let Some(result) = notusupdate::scanner::run(matches).await {
return result;
}
if let Some(result) = ospd::run(matches).await {
if let Some(result) = osp::run(matches).await {
return result;
}
Err(CliError {
Expand Down
74 changes: 38 additions & 36 deletions rust/src/scannerctl/ospd/mod.rs → rust/src/scannerctl/osp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::io::BufRead;
use std::{io::BufReader, path::PathBuf, sync::Arc};

use clap::{arg, value_parser, Arg, ArgAction, Command};
use scannerlib::models::{Parameter, Scan, VT};
use scannerlib::models::{self, Parameter, Scan, VT};
use scannerlib::storage::{self, DefaultDispatcher, StorageError};
use start_scan::StartScan;
use start_scan::{StartScan, VtSelection};

use crate::{CliError, CliErrorKind};
use scannerlib::storage::item::{NVTField, NVTKey};
Expand All @@ -18,8 +18,8 @@ mod start_scan;

pub fn extend_args(cmd: Command) -> Command {
cmd.subcommand(crate::add_verbose(
Command::new("ospd")
.about("Transforms a ospd-start-scan xml to a scan json for openvasd. ")
Command::new("osp")
.about("Transforms a osp start-scan xml to a scan json for openvasd. ")
.arg(
arg!(-p --path <FILE> "Path to the feed.")
.required(false)
Expand All @@ -37,7 +37,7 @@ pub fn extend_args(cmd: Command) -> Command {
))
}

pub async fn may_start_scan<R, S>(
pub async fn may_transform_start_scan<R, S>(
print_back: bool,
feed: Option<S>,
reader: R,
Expand All @@ -48,22 +48,19 @@ where
{
match quick_xml::de::from_reader(reader) {
Ok(x) if print_back => Some(Ok(format!("{x}"))),
Ok(x) if feed.is_some() => Some(start_scan(feed.unwrap(), x).await),
Ok(x) if feed.is_some() => Some(transform_start_scan(feed.unwrap(), x).await),
Ok(_) => Some(Err(CliErrorKind::MissingArguments(
vec!["path".to_string()],
))),
Err(_) => None,
}
}

async fn start_scan<S>(feed: S, sc: StartScan) -> Result<String, CliErrorKind>
async fn transform_vts<S>(feed: S, vts: VtSelection) -> Result<Vec<models::VT>, CliErrorKind>
where
S: storage::Retriever,
{
// currently we ignore the previous order as the scanner will reorder
// when scheduling internally anyway.
let svts = sc
.vt_selection
let mut result: Vec<_> = vts
.vt_single
.into_iter()
.flatten()
Expand All @@ -78,26 +75,16 @@ where
.collect(),
})
.collect();
let gvts = sc
.vt_selection
.vt_group
.into_iter()
.flatten()
.filter_map(
|x| match x.filter.split_once('=').map(|(k, v)| (k.trim(), v.trim())) {
Some(("family", v)) => Some(v.to_string()),
filter => {
tracing::warn!(?filter, "only family is supported, ignoring entry");
None
}
},
);
let mut scan = Scan {
scan_id: sc.id.unwrap_or_default(),
scan_preferences: sc.scanner_params.values,
target: sc.targets.target.into(),
vts: svts,
};
let gvts = vts.vt_group.into_iter().flatten().filter_map(|x| {
match x.filter.split_once('=').map(|(k, v)| (k.trim(), v.trim())) {
Some(("family", v)) => Some(v.to_string()),
filter => {
tracing::warn!(?filter, "only family is supported, ignoring entry");
None
}
}
});

// we iterate here to return an error when storage is behaving in an unexpected fashion
for family in gvts {
let fvts: Vec<VT> = match feed.retry_retrieve_by_field(
Expand All @@ -120,9 +107,24 @@ where
}
Err(e) => return Err(e.into()),
};
scan.vts.extend(fvts);
result.extend(fvts);
}
scan.vts.sort();
result.sort();
Ok(result)
}

async fn transform_start_scan<S>(feed: S, sc: StartScan) -> Result<String, CliErrorKind>
where
S: storage::Retriever,
{
// currently we ignore the previous order as the scanner will reorder
// when scheduling internally anyway.
let scan = Scan {
scan_id: sc.id.unwrap_or_default(),
scan_preferences: sc.scanner_params.values,
target: sc.targets.target.into(),
vts: transform_vts(feed, sc.vt_selection).await?,
};
let scan_json = match serde_json::to_string_pretty(&scan) {
Ok(s) => s,
Err(e) => return Err(e.into()),
Expand Down Expand Up @@ -160,7 +162,7 @@ pub async fn run(root: &clap::ArgMatches) -> Option<Result<(), CliError>> {
};
let print_back = args.get_one::<bool>("back").cloned().unwrap_or_default();
// currently we just support start scan if that changes chain the options.
let output = may_start_scan(print_back, feed, &mut bufreader).await;
let output = may_transform_start_scan(print_back, feed, &mut bufreader).await;
let result = match output {
Some(Ok(x)) => {
println!("{x}");
Expand Down Expand Up @@ -241,7 +243,7 @@ mod tests {
dispatch("2", "A");
dispatch("3", "A");

let output = may_start_scan(false, Some(d), reader)
let output = may_transform_start_scan(false, Some(d), reader)
.await
.unwrap()
.unwrap();
Expand Down Expand Up @@ -287,7 +289,7 @@ mod tests {
</start_scan>
"#;
let reader = BufReader::new(Cursor::new(input));
let output = may_start_scan::<_, DefaultDispatcher>(true, None, reader)
let output = may_transform_start_scan::<_, DefaultDispatcher>(true, None, reader)
.await
.unwrap()
.unwrap();
Expand Down
File renamed without changes.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit fdb2ab0

Please sign in to comment.