Skip to content

Commit

Permalink
Fix: load the feed metadata from an existing json file
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola committed Sep 2, 2024
1 parent bad09b3 commit 19e798c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
44 changes: 41 additions & 3 deletions rust/scannerctl/src/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

use std::path::PathBuf;
use std::{collections::HashMap, fs::File, io::Read, path::PathBuf};

use nasl_interpreter::{
load_non_utf8_path, CodeInterpreter, FSPluginLoader, LoadError, NaslValue, NoOpLoader,
Expand Down Expand Up @@ -171,6 +171,37 @@ where
Ok(result)
}

fn create_loader_from_feed_json(store: &DefaultDispatcher, path: PathBuf) -> Result<FSPluginLoader<PathBuf>, CliError>
{
let result = FSPluginLoader::new(path.clone());
let mut file = match File::open(path.as_path()) {
Ok(file) => file,
Err(e) => {
return Err(CliError{
filename: path.to_owned().to_string_lossy().to_string(),
kind: CliErrorKind::LoadError(LoadError::Dirty(e.to_string()))
});

}
};

let mut buf = String::new();
if let Err(err) = file.read_to_string(&mut buf) {
return Err(CliError{
filename: path.to_owned().to_string_lossy().to_string(),
kind: CliErrorKind::LoadError(LoadError::Dirty(err.to_string()))
});
}

let vts: Vec<storage::item::Nvt> = serde_json::from_str(&buf)?;
let mut all_vts: HashMap<String, storage::item::Nvt> = HashMap::new();
let _ = vts.iter().map(|v| all_vts.insert(v.filename.clone(), v.clone()));
let _ = store.set_vts(all_vts);

Ok(result)
}


pub fn run(
db: &Db,
feed: Option<PathBuf>,
Expand All @@ -193,8 +224,15 @@ pub fn run(
}
(Db::InMemory, Some(path)) => {
let storage = DefaultDispatcher::new(true);
let builder = RunBuilder::default().loader(create_fp_loader(&storage, path)?);
builder.storage(storage).build().run(script)

if let Some(_jsonfile) = path.extension() {
let builder = RunBuilder::default().loader(create_loader_from_feed_json(&storage, path)?);
builder.storage(storage).build().run(script)

} else {
let builder = RunBuilder::default().loader(create_fp_loader(&storage, path)?);
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 @@ -290,7 +290,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 @@ -316,6 +316,12 @@ impl DefaultDispatcher {
}
}

/// Stores an already existing Vts struture.
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 19e798c

Please sign in to comment.