Skip to content

Commit

Permalink
Fix: load the feed metadata from an existing json file (#1708)
Browse files Browse the repository at this point in the history
* Fix: load the feed metadata from an existing json file

* 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
  • Loading branch information
jjnicola authored Sep 5, 2024
1 parent a18383e commit cfc22f8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
14 changes: 13 additions & 1 deletion rust/scannerctl/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

use std::{fmt::Display, path::PathBuf};
use std::{
fmt::Display,
path::{Path, PathBuf},
};

use feed::VerifyError;
use nasl_interpreter::{InterpretError, LoadError};
Expand Down Expand Up @@ -51,6 +54,15 @@ pub struct CliError {
pub kind: CliErrorKind,
}

impl CliError {
pub fn load_error(err: std::io::Error, path: &Path) -> Self {
Self {
filename: path.to_owned().to_string_lossy().to_string(),
kind: CliErrorKind::LoadError(LoadError::Dirty(err.to_string())),
}
}
}

impl Display for CliErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
44 changes: 35 additions & 9 deletions rust/scannerctl/src/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

use std::path::PathBuf;
use std::{
fs::{self},
path::PathBuf,
};

use nasl_interpreter::{
load_non_utf8_path, CodeInterpreter, FSPluginLoader, LoadError, NaslValue, NoOpLoader,
Expand Down Expand Up @@ -153,22 +156,35 @@ fn create_redis_storage(
redis_storage::CacheDispatcher::as_dispatcher(url, FEEDUPDATE_SELECTOR).unwrap()
}

fn create_fp_loader<S>(storage: &S, path: PathBuf) -> Result<FSPluginLoader<PathBuf>, CliError>
fn load_feed_by_exec<S>(storage: &S, pl: &FSPluginLoader<PathBuf>) -> 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 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<storage::item::Nvt> = serde_json::from_str(&buf)?;
let all_vts = vts.into_iter().map(|v| (v.filename.clone(), v)).collect();

store.set_vts(all_vts).map_err(|e| CliError {
filename: path.to_owned().to_string_lossy().to_string(),
kind: e.into(),
})?;
tracing::info!("loaded feed.");
Ok(())
}

pub fn run(
Expand All @@ -188,12 +204,22 @@ 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 builder = RunBuilder::default().loader(create_fp_loader(&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 {
load_feed_by_exec(&storage, &loader)?
}

let builder = RunBuilder::default().loader(loader);
builder.storage(storage).build().run(script)
}
};
Expand Down
8 changes: 7 additions & 1 deletion rust/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ type Kbs = HashMap<String, HashMap<String, Vec<Kb>>>;

/// Vts are using a relative file path as a key. This should make includes, script_dependency
/// lookups relative simple.
type Vts = HashMap<String, item::Nvt>;
pub type Vts = HashMap<String, item::Nvt>;

/// The results generated by log_, security_, error_message.
type Results = HashMap<String, Vec<models::Result>>;
Expand All @@ -324,6 +324,12 @@ impl DefaultDispatcher {
}
}

/// Stores an already existing Vts structure.
pub fn set_vts(&self, vts: Vts) -> Result<(), StorageError> {
let mut data = self.vts.as_ref().write()?;
*data = vts;
Ok(())
}
/// Cleanses stored data.
pub fn cleanse(&self) -> Result<(), StorageError> {
// TODO cleanse at least kbs, may rest?
Expand Down

0 comments on commit cfc22f8

Please sign in to comment.