Skip to content

Commit

Permalink
Change: dont use constants. Use environment variables instead, with d…
Browse files Browse the repository at this point in the history
…efault fallback, instead
  • Loading branch information
jjnicola committed Nov 23, 2022
1 parent 4d263ff commit b8ba2ba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
5 changes: 5 additions & 0 deletions rust/nvtcache/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
/// Module to handle custom errors
pub mod dberror;
/// Module to handle Nvt metadata. The Nvt structure is defined here as well
/// as the methods to set and get the struct members.
pub mod nvt;
/// Module include objects and methods to upload an Nvt in redis
pub mod nvtcache;
/// Module with structures and methods to access redis.
pub mod redisconnector;
26 changes: 18 additions & 8 deletions rust/nvtcache/src/nvtcache.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
const NVTICACHE: &str = "nvticache";
const PLUGIN_PATH: &str = "/home/jnicola/install/var/lib/openvas/plugins/";

use crate::dberror::Result;
use crate::nvt::*;
use crate::redisconnector::*;
use std::env;
use std::path::Path;

pub struct NvtCache {
pub cache: RedisCtx,
pub init: bool,
cache_key: String,
plugin_path: String,
}

/// NvtCache implementation.
impl NvtCache {
/// Initialize and return an NVT Cache Object
pub fn init() -> Result<NvtCache> {
let rctx = RedisCtx::new()?;
let redis_default_socket = |_| "unix:///run/redis/redis-server.sock".to_string();
let redis_socket = env::var("REDIS_SOCKET").unwrap_or_else(redis_default_socket);
let rctx = RedisCtx::new(redis_socket.to_string())?;

let default_plugin_path = |_| "/var/lib/openvas/plugins/".to_string();
let plugin_path = env::var("PLUGIN_PATH").unwrap_or_else(default_plugin_path);

let cache_key = "nvticache".to_string();

Ok(NvtCache {
cache: rctx,
init: true,
cache_key,
plugin_path,
})
}

Expand All @@ -35,13 +45,13 @@ impl NvtCache {

/// Set the key nvtcache
pub fn set_version(&mut self, feed_version: &str) -> Result<()> {
let _ = self.cache.redis_set_key(NVTICACHE, feed_version)?;
let _ = self.cache.redis_set_key(&self.cache_key, feed_version)?;
Ok(())
}

/// Get the key nvtcache, which has the feed version
pub fn get_version(&mut self) -> Result<String> {
let version = self.cache.redis_get_key(NVTICACHE)?;
let version = self.cache.redis_get_key(&self.cache_key)?;
Ok(version)
}

Expand All @@ -50,7 +60,7 @@ impl NvtCache {
/// with the version in the cache
/// Return True if it is updated, False if outdated, Error otherwise.
pub fn check_feed(&mut self, current: &str) -> Result<bool> {
let cached = self.cache.redis_get_key(NVTICACHE)?;
let cached = self.cache.redis_get_key(&self.cache_key)?;
if cached == current {
return Ok(true);
}
Expand Down Expand Up @@ -79,7 +89,7 @@ impl NvtCache {
// If it is in the cache, and are not the same filename
// we check if it is still in the filesystem.
if !cached_nvt.is_empty() && cached_nvt != filename {
let mut src_path: String = PLUGIN_PATH.to_owned();
let mut src_path: String = self.plugin_path.to_owned();
src_path.push_str(&cached_nvt);

// If still exists, the oid is duplicated
Expand Down
24 changes: 14 additions & 10 deletions rust/nvtcache/src/redisconnector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ use crate::dberror::DbError;
use crate::dberror::Result;
use crate::nvt::Nvt;
use redis::*;
use std::collections::LinkedList;

const GLOBAL_DBINDEX_NAME: &str = "GVM.__GlobalDBIndex";
const REDIS_DEFAULT_PATH: &str = "unix:///run/redis/redis-server.sock";

pub enum KbNvtPos {
NvtFilenamePos,
Expand All @@ -31,6 +27,7 @@ pub struct RedisCtx {
kb: Connection, //a redis connection
db: u32, // the name space
maxdb: u32, // max db index
global_db_index: String,
}

#[derive(Debug, PartialEq)]
Expand All @@ -52,13 +49,15 @@ impl FromRedisValue for RedisValueHandler {

impl RedisCtx {
/// Connect to the redis server and return a redis context object
pub fn new() -> Result<RedisCtx> {
let client = redis::Client::open(REDIS_DEFAULT_PATH)?;
pub fn new(redis_socket: String) -> Result<RedisCtx> {
let client = redis::Client::open(redis_socket)?;
let kb = client.get_connection()?;
let global_db_index = "GVM.__GlobalDBIndex".to_string();
let mut redisctx = RedisCtx {
kb,
db: 0,
maxdb: 0,
global_db_index,
};
let _kbi = redisctx.select_database()?;
Ok(redisctx)
Expand Down Expand Up @@ -121,7 +120,7 @@ impl RedisCtx {
}

fn try_database(&mut self, dbi: u32) -> Result<u32> {
let ret = self.kb.hset_nx(GLOBAL_DBINDEX_NAME, dbi, 1)?;
let ret = self.kb.hset_nx(&self.global_db_index, dbi, 1)?;
Ok(ret)
}

Expand All @@ -130,7 +129,7 @@ impl RedisCtx {
let mut selected_db: u32 = 0;

// Start always from 1. Namespace 0 is reserved
//format GLOBAL_DBINDEX_NAME
//format self.global_db_index
for i in 1..maxdb {
let ret = self.try_database(i)?;
if ret == 1 {
Expand All @@ -153,7 +152,7 @@ impl RedisCtx {
let dbi = self.get_namespace()?;
// Remove the entry from the hash list
self.set_namespace(0)?;
self.kb.hdel(GLOBAL_DBINDEX_NAME, dbi)?;
self.kb.hdel(&self.global_db_index, dbi)?;
Ok(())
}

Expand Down Expand Up @@ -205,7 +204,7 @@ impl RedisCtx {
// Get the references
let (cves, bids, xrefs) = nvt.get_refs();

let key_name = ["nvt:".to_owned(), oid].join("");
let key_name = ["nvt:".to_owned(), oid.to_owned()].join("");
Cmd::new()
.arg("RPUSH")
.arg(key_name)
Expand All @@ -226,6 +225,11 @@ impl RedisCtx {
.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() {
//
//}

nvt.destroy();

Expand Down

0 comments on commit b8ba2ba

Please sign in to comment.