Skip to content

Commit

Permalink
Add: store references in the redis cache.
Browse files Browse the repository at this point in the history
Also, some code improvements, like using a vector with the lpush redis command, instead of creating a custom Cmd::new()
  • Loading branch information
jjnicola committed Nov 23, 2022
1 parent 26d2287 commit 2affab7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 43 deletions.
24 changes: 11 additions & 13 deletions rust/nvtcache/src/nvt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,8 @@ impl Nvt {
// ^ TODO: write fn for refs, severities, prefs, which are LinkedLists

/// Set the NVT category. Check that category is a valid Category
pub fn set_category(&mut self, category: Category) -> Result<()> {
if category >= Category::ActInit && category <= Category::ActEnd {
self.category = category;
return Ok(());
}
return Err(DbError::CustomErr(
"Invalid category for an NVT".to_string(),
));
pub fn set_category(&mut self, category: Category) {
self.category = category;
}

/// Set the NVT family
Expand Down Expand Up @@ -520,9 +514,9 @@ impl Nvt {
Ok(self.qod.clone())
}

/// Get the NVT category. Check that category is a valid Category
pub fn get_category(&mut self) -> Result<i32> {
Ok(self.category as i32)
/// Get the NVT category.
pub fn get_category(&mut self) -> i32 {
self.category as i32
}

/// Get the NVT family
Expand Down Expand Up @@ -563,7 +557,11 @@ impl Nvt {
});

// TODO is the white space really necessary as indicated by the tests?
return (cves.iter().as_ref().join(", "), bids.iter().as_ref().join(", "), xrefs.iter().as_ref().join(", "));
return (
cves.iter().as_ref().join(", "),
bids.iter().as_ref().join(", "),
xrefs.iter().as_ref().join(", "),
);
}

/// Transforms prefs to string representatiosn {id}:{name}:{id}:{default} so that it can be stored into redis
Expand All @@ -575,7 +573,7 @@ impl Nvt {
"{}:{}:{}:{}",
pref.get_id(),
pref.get_name(),
pref.get_id(),
pref.get_type(),
pref.get_default()
)
})
Expand Down
51 changes: 25 additions & 26 deletions rust/nvtcache/src/redisconnector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,38 +198,37 @@ impl RedisCtx {
let required_ports = nvt.get_required_ports()?;
let dependencies = nvt.get_dependencies()?;
let tags = nvt.get_tag()?;
let category = nvt.get_category()?;
let category = nvt.get_category();
let family = nvt.get_family()?;

// Get the references
let (cves, bids, xrefs) = nvt.get_refs();

let key_name = ["nvt:".to_owned(), oid.to_owned()].join("");
Cmd::new()
.arg("RPUSH")
.arg(key_name)
.arg(filename)
.arg(required_keys)
.arg(mandatory_keys)
.arg(excluded_keys)
.arg(required_udp_ports)
.arg(required_ports)
.arg(dependencies)
.arg(tags)
.arg(cves)
.arg(bids)
.arg(xrefs)
.arg(category)
.arg(family)
.arg(name)
.query(&mut self.kb)?;

//TODO: Add preferences
//let key_name = ["oid:".to_owned(), oid.to_owned(), "prefs".to_owned()].join("");
//let prefs = nvt.get_prefs()?;
//for pref in prefs.iter_mut() {
//
//}
let values: Vec<String> = [
filename,
required_keys,
mandatory_keys,
excluded_keys,
required_udp_ports,
required_ports,
dependencies,
tags,
cves,
bids,
xrefs,
category.to_string(),
family,
name,
]
.to_vec();

self.kb.rpush(key_name, values)?;

// Add preferences
let key_name = ["oid:".to_owned(), oid.to_owned(), "prefs".to_owned()].join("");
let prefs = nvt.get_prefs();
self.kb.lpush(key_name, prefs)?;

Ok(())
}
Expand Down
16 changes: 12 additions & 4 deletions rust/nvtcache/tests/nvtcache_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::error::Error;
//test
#[cfg(test)]
mod test {
use ::nvtcache::nvt::NvtPref;

use super::*;

#[test]
Expand Down Expand Up @@ -81,10 +83,16 @@ mod test {
Ok(_) => (),
Err(_) => println!("Error"),
}
match fake_nvt.set_name("Custom Script for the vulnerability 1".to_owned()) {
Ok(_) => (),
Err(_) => println!("Error"),
}
fake_nvt.set_name("Custom Script for the vulnerability 1".to_owned())?;

let pref = NvtPref::new(
0,
"entry".to_string(),
"Timeout".to_string(),
"320".to_string(),
)?;
fake_nvt.add_pref(pref)?;

let filename = "custom.nasl".to_owned();
match nvtcache.add_nvt(fake_nvt, filename) {
Ok(_) => println!("Nvt successfully added"),
Expand Down

0 comments on commit 2affab7

Please sign in to comment.