Skip to content

Commit

Permalink
Add: get_vts path for getting vts from redis cache (#1562)
Browse files Browse the repository at this point in the history
* Add: Implement get_vts for openvasd
* handle vt selection on get_vts
  • Loading branch information
jjnicola authored Feb 2, 2024
1 parent 7bfe3ee commit 9d1e74d
Show file tree
Hide file tree
Showing 28 changed files with 746 additions and 232 deletions.
20 changes: 17 additions & 3 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion rust/feed-verifier/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ fn main() {
};
let (ncd, nasl_cli) = run_get(
&mut kb,
&format!("{} feed update --vts-only", nasl_cli.to_str().unwrap_or_default()),
&format!(
"{} feed update --vts-only",
nasl_cli.to_str().unwrap_or_default()
),
)
.expect("results");
let mut errors = 0;
Expand Down
2 changes: 1 addition & 1 deletion rust/feed/src/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use nasl_interpreter::{
logger::DefaultLogger, AsBufReader, Context, ContextType, Interpreter, Loader, NaslValue,
Register,
};
use storage::{nvt::NVTField, Dispatcher, NoOpRetriever};
use storage::{item::NVTField, Dispatcher, NoOpRetriever};

use crate::verify::{self, HashSumFileItem, SignatureChecker};

Expand Down
41 changes: 21 additions & 20 deletions rust/json-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
sync::{Arc, Mutex},
};

use storage::{self, nvt::PerNVTDispatcher, Kb, StorageError};
use storage::{self, item::PerItemDispatcher, Kb, StorageError};

/// Wraps write calls of json elements to be as list.
///
Expand Down Expand Up @@ -66,14 +66,14 @@ where
}

/// It will transform a Nvt to json and write it into the given Writer.
pub struct NvtDispatcher<W>
pub struct ItemDispatcher<W>
where
W: Write,
{
w: Arc<Mutex<W>>,
kbs: Arc<Mutex<Vec<Kb>>>,
}
impl<S> NvtDispatcher<S>
impl<S> ItemDispatcher<S>
where
S: Write,
{
Expand All @@ -87,26 +87,26 @@ where
}

/// Returns a new instance as a Dispatcher
pub fn as_dispatcher<K>(w: S) -> PerNVTDispatcher<Self, K>
pub fn as_dispatcher<K>(w: S) -> PerItemDispatcher<Self, K>
where
K: AsRef<str>,
{
PerNVTDispatcher::new(Self::new(w))
PerItemDispatcher::new(Self::new(w))
}

fn as_json(&self, nvt: storage::nvt::Nvt) -> Result<(), storage::StorageError> {
fn as_json(&self, nvt: storage::item::Nvt) -> Result<(), storage::StorageError> {
let mut context = self.w.lock().map_err(StorageError::from)?;
serde_json::to_vec(&nvt)
.map_err(|e| StorageError::Dirty(format!("{e:?}")))
.and_then(|x| context.write_all(&x).map_err(StorageError::from))
}
}

impl<S, K> storage::nvt::NvtDispatcher<K> for NvtDispatcher<S>
impl<S, K> storage::item::ItemDispatcher<K> for ItemDispatcher<S>
where
S: Write,
{
fn dispatch_nvt(&self, nvt: storage::nvt::Nvt) -> Result<(), storage::StorageError> {
fn dispatch_nvt(&self, nvt: storage::item::Nvt) -> Result<(), storage::StorageError> {
self.as_json(nvt)
}

Expand All @@ -126,7 +126,7 @@ where
}
}

impl<S, K> storage::Retriever<K> for NvtDispatcher<S>
impl<S, K> storage::Retriever<K> for ItemDispatcher<S>
where
S: Write,
{
Expand All @@ -138,6 +138,7 @@ where
Ok(match scope {
// currently not supported
storage::Retrieve::NVT(_) => Vec::new(),
storage::Retrieve::NOTUS(_) => Vec::new(),
storage::Retrieve::KB(s) => {
let kbs = self.kbs.lock().map_err(StorageError::from)?;
kbs.iter()
Expand All @@ -162,7 +163,7 @@ where
mod tests {
use std::collections::BTreeMap;

use storage::nvt::{Nvt, ACT};
use storage::item::{Nvt, ACT};

use super::*;

Expand All @@ -174,9 +175,9 @@ mod tests {
.join(".")
}

fn generate_tags() -> BTreeMap<storage::nvt::TagKey, storage::nvt::TagValue> {
use storage::nvt::TagKey::*;
use storage::nvt::TagValue;
fn generate_tags() -> BTreeMap<storage::item::TagKey, storage::item::TagValue> {
use storage::item::TagKey::*;
use storage::item::TagValue;
let ts = "2012-09-23 02:15:34 -0400";
BTreeMap::from([
(Affected, TagValue::parse(Affected, "Affected").unwrap()),
Expand Down Expand Up @@ -221,9 +222,9 @@ mod tests {
(Vuldetect, TagValue::parse(Vuldetect, "Vuldetect").unwrap()),
])
}
fn generate_preferences() -> Vec<storage::nvt::NvtPreference> {
use storage::nvt::NvtPreference;
use storage::nvt::PreferenceType;
fn generate_preferences() -> Vec<storage::item::NvtPreference> {
use storage::item::NvtPreference;
use storage::item::PreferenceType;
[
PreferenceType::CheckBox,
PreferenceType::Entry,
Expand Down Expand Up @@ -261,8 +262,8 @@ mod tests {
}
}

fn generate_references() -> Vec<storage::nvt::NvtRef> {
use storage::nvt::NvtRef;
fn generate_references() -> Vec<storage::item::NvtRef> {
use storage::item::NvtRef;
vec![NvtRef {
class: "URL".to_owned(),
id: "unix:///var/lib/really.sock".to_owned(),
Expand All @@ -273,7 +274,7 @@ mod tests {
fn single_json() {
let nvt = generate_nvt("test", ACT::DestructiveAttack);
let mut buf = Vec::with_capacity(1208);
let dispatcher = super::NvtDispatcher::new(&mut buf);
let dispatcher = super::ItemDispatcher::new(&mut buf);
dispatcher.as_json(nvt.clone()).unwrap();
let single_json = String::from_utf8(buf).unwrap();
let result: Nvt = serde_json::from_str(&single_json).unwrap();
Expand All @@ -284,7 +285,7 @@ mod tests {
fn array_wrapper() {
let mut buf = Vec::with_capacity(1208 * 11);
let mut ja = ArrayWrapper::new(&mut buf);
let dispatcher = super::NvtDispatcher::new(&mut ja);
let dispatcher = super::ItemDispatcher::new(&mut ja);
for nvt in [
ACT::Init,
ACT::Scanner,
Expand Down
1 change: 0 additions & 1 deletion rust/models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ license = "GPL-2.0-or-later"
serde = {version = "1", features = ["derive"], optional = true}
bincode = {version = "2.0.0-rc.3", optional = true }


[features]
default = ["serde_support", "bincode_support"]
serde_support = ["serde"]
Expand Down
4 changes: 2 additions & 2 deletions rust/models/src/advisories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub struct Vulnerability {
/// Solution Type
pub solution_type: String,
/// Vuldetect
pub vuldeterct: String,
pub vuldetect: String,
/// Quality of detection
pub qod_type: String,
/// Severity vector
Expand Down Expand Up @@ -183,7 +183,7 @@ impl<'a> From<&VulnerabilityData<'a>> for Vulnerability {
insight: data.adv.insight.to_owned(),
solution: "Please install the updated package(s).".to_string(),
solution_type: "VendorFix".to_string(),
vuldeterct: "Checks if a vulnerable package version is present on the target host."
vuldetect: "Checks if a vulnerable package version is present on the target host."
.to_string(),
qod_type: data.adv.qod_type.to_owned(),
severity_vector: sv.to_string(),
Expand Down
2 changes: 1 addition & 1 deletion rust/nasl-builtin-description/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::str::FromStr;

use nasl_builtin_utils::{Context, FunctionErrorKind, Register};

use storage::nvt::{NVTField, NvtPreference, NvtRef, PreferenceType, TagKey, TagValue};
use storage::item::{NVTField, NvtPreference, NvtRef, PreferenceType, TagKey, TagValue};

use nasl_builtin_utils::{get_named_parameter, NaslFunction};
use nasl_syntax::NaslValue;
Expand Down
2 changes: 1 addition & 1 deletion rust/nasl-cli/src/feed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub fn run(root: &clap::ArgMatches) -> Option<Result<(), CliError>> {
let path = get_vts_path(args);

let mut o = json_storage::ArrayWrapper::new(io::stdout());
let dispatcher = json_storage::NvtDispatcher::as_dispatcher(&mut o);
let dispatcher = json_storage::ItemDispatcher::as_dispatcher(&mut o);
Some(match update::run(dispatcher, path, false) {
Ok(_) => o.end().map_err(StorageError::from).map_err(|se| CliError {
filename: "".to_string(),
Expand Down
8 changes: 4 additions & 4 deletions rust/nasl-interpreter/tests/description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ mod tests {
use nasl_syntax::logger::DefaultLogger;
use nasl_syntax::parse;
use nasl_syntax::NaslValue;
use storage::nvt::TagKey::*;
use storage::nvt::ACT::*;
use storage::nvt::{NVTField::*, NvtPreference, PreferenceType};
use storage::nvt::{NvtRef, TagValue};
use storage::item::TagKey::*;
use storage::item::ACT::*;
use storage::item::{NVTField::*, NvtPreference, PreferenceType};
use storage::item::{NvtRef, TagValue};
use storage::DefaultDispatcher;
use storage::Field::NVT;
use storage::Retriever;
Expand Down
2 changes: 1 addition & 1 deletion rust/nasl-syntax/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
operation::Operation,
prefix_extension::Prefix,
token::{Category, Token, Tokenizer},
unexpected_end, unexpected_statement, unexpected_token, AssignOrder, Statement, StatementKind,
unexpected_statement, unexpected_token, AssignOrder, Statement, StatementKind,
};

/// Is used to parse Token to Statement
Expand Down
2 changes: 1 addition & 1 deletion rust/nasl-syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub use lexer::Lexer;
pub use loader::*;
pub use naslvalue::*;
pub use statement::*;
pub use storage::nvt::ACT;
pub use storage::item::ACT;
pub use token::Base as NumberBase;
pub use token::Category as TokenCategory;
pub use token::IdentifierType;
Expand Down
1 change: 1 addition & 0 deletions rust/openvasd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ osp = { path = "../osp" }
nasl-interpreter = { path = "../nasl-interpreter" }
feed = { path = "../feed" }
storage = { path = "../storage" }
redis-storage = { path = "../redis-storage" }
infisto = { path = "../infisto" }
notus = { path = "../notus" }
hyper = { version = "1", features = ["full"] }
Expand Down
24 changes: 24 additions & 0 deletions rust/openvasd/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ pub struct Notus {
pub products_path: PathBuf,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct RedisSocket {
pub redis_socket: PathBuf,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct OspdWrapper {
pub result_check_interval: Duration,
Expand Down Expand Up @@ -59,6 +64,14 @@ impl Default for Notus {
}
}

impl Default for RedisSocket {
fn default() -> Self {
RedisSocket {
redis_socket: PathBuf::from("unix:///run/redis-openvas/redis.sock"),
}
}
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Listener {
pub address: SocketAddr,
Expand Down Expand Up @@ -174,6 +187,8 @@ pub struct Config {
pub log: Logging,
#[serde(default)]
pub storage: Storage,
#[serde(default)]
pub redis_socket: RedisSocket,
}

impl Display for Config {
Expand Down Expand Up @@ -248,6 +263,12 @@ impl Config {
.value_parser(clap::builder::PathBufValueParser::new())
.action(ArgAction::Set)
.help("Path containing the Notus products directory"))
.arg(
clap::Arg::new("redis-socket ")
.long("redis-socket")
.value_parser(clap::builder::PathBufValueParser::new())
.action(ArgAction::Set)
.help("Path to the redis socket"))
.arg(
clap::Arg::new("tls-certs")
.env("TLS_CERTS")
Expand Down Expand Up @@ -378,6 +399,9 @@ impl Config {
if let Some(path) = cmds.get_one::<PathBuf>("notus-products") {
config.notus.products_path = path.clone();
}
if let Some(path) = cmds.get_one::<PathBuf>("redis-socket") {
config.redis_socket.redis_socket = path.clone();
}
if let Some(path) = cmds.get_one::<PathBuf>("tls-certs") {
config.tls.certs = Some(path.clone());
}
Expand Down
Loading

0 comments on commit 9d1e74d

Please sign in to comment.