From 3511867bdc7645cc1f9a5ebe4d869155b662079f Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 4 Sep 2024 11:03:24 -0300 Subject: [PATCH] Fix: bug that the FSPluginLoader is incorrectly set is fixed We just use the feed.json when it is available and if not we execute like previously. Patch provided by @nichtsfrei --- rust/scannerctl/src/interpret/mod.rs | 38 +++++++++++++--------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/rust/scannerctl/src/interpret/mod.rs b/rust/scannerctl/src/interpret/mod.rs index 0b3035420..6bc27e653 100644 --- a/rust/scannerctl/src/interpret/mod.rs +++ b/rust/scannerctl/src/interpret/mod.rs @@ -156,29 +156,25 @@ fn create_redis_storage( redis_storage::CacheDispatcher::as_dispatcher(url, FEEDUPDATE_SELECTOR).unwrap() } -fn create_fp_loader(storage: &S, path: PathBuf) -> Result, CliError> +fn load_feed_by_exec(storage: &S, pl: &FSPluginLoader) -> Result<(), CliError> where S: storage::Dispatcher, { // update feed with storage tracing::info!("loading feed. This may take a while."); - let result = FSPluginLoader::new(path); - let verifier = feed::HashSumNameLoader::sha256(&result)?; - let updater = feed::Update::init("scannerctl", 5, &result, storage, verifier); + let verifier = feed::HashSumNameLoader::sha256(pl)?; + let updater = feed::Update::init("scannerctl", 5, pl, storage, verifier); for u in updater { - tracing::warn!(updated=?u); + tracing::trace!(updated=?u); u?; } tracing::info!("loaded feed."); - Ok(result) + Ok(()) } -fn create_loader_from_feed_json( - store: &DefaultDispatcher, - path: PathBuf, -) -> Result, CliError> { - let result = FSPluginLoader::new(path.clone()); +fn load_feed_by_json(store: &DefaultDispatcher, path: &PathBuf) -> Result<(), CliError> { + tracing::info!(path=?path, "loading feed via json. This may take a while."); let buf = fs::read_to_string(&path).map_err(|e| CliError::load_error(e, &path))?; let vts: Vec = serde_json::from_str(&buf)?; let all_vts = vts.into_iter().map(|v| (v.filename.clone(), v)).collect(); @@ -187,8 +183,8 @@ fn create_loader_from_feed_json( filename: path.to_owned().to_string_lossy().to_string(), kind: e.into(), })?; - - Ok(result) + tracing::info!("loaded feed."); + Ok(()) } pub fn run( @@ -208,18 +204,20 @@ pub fn run( (Db::InMemory, None) => builder.build().run(script), (Db::Redis(url), Some(path)) => { let storage = create_redis_storage(url); - let builder = RunBuilder::default().loader(create_fp_loader(&storage, path)?); + let loader = FSPluginLoader::new(path); + load_feed_by_exec(&storage, &loader)?; + let builder = RunBuilder::default().loader(loader); builder.storage(storage).build().run(script) } (Db::InMemory, Some(path)) => { let storage = DefaultDispatcher::new(true); - - let is_json = path.extension().map(|ext| ext == "json").unwrap_or(false); - let loader = if is_json { - create_loader_from_feed_json(&storage, path)? + let guessed_feed_json = path.join("feed.json"); + let loader = FSPluginLoader::new(path.clone()); + if guessed_feed_json.exists() { + load_feed_by_json(&storage, &guessed_feed_json)? } else { - create_fp_loader(&storage, path)? - }; + load_feed_by_exec(&storage, &loader)? + } let builder = RunBuilder::default().loader(loader); builder.storage(storage).build().run(script)