Skip to content

Commit

Permalink
Fix: Feed is loaded many times in readis after openvasd restart (#1685)
Browse files Browse the repository at this point in the history
Check if the current version is already in the cache before uploading. Also remove the old entries before uploading, if the entry is present to avoid the amount if items inside keys (duplication).
Jira: SC-1122
  • Loading branch information
jjnicola authored Aug 8, 2024
1 parent 9d14839 commit 0edef24
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
12 changes: 12 additions & 0 deletions rust/feed/src/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ where
feed_version(self.loader, self.dispatcher)
}

/// Check if the current feed is outdated.
pub fn feed_is_outdated(&self, current_version: String) -> Result<bool, ErrorKind> {
self.verify_signature()?;
// the version in file
let v = self.feed_version()?;
if !current_version.is_empty() {
return Ok(v.as_str() != current_version.as_str());
};

Ok(true)
}

/// plugin_feed_info must be handled differently.
///
/// Usually a plugin_feed_info.inc is setup as a listing of keys.
Expand Down
5 changes: 5 additions & 0 deletions rust/openvasd/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ pub trait NVTStorer {

/// Returns the currently stored feed hash.
async fn feed_hash(&self) -> Vec<FeedHash>;

/// Returns the current feed version, if any.
async fn current_feed_version(&self) -> Result<String, Error> {
Ok("".to_string())
}
}

#[async_trait]
Expand Down
29 changes: 27 additions & 2 deletions rust/openvasd/src/storage/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<T> Storage<T> {
.unwrap()
}

async fn update_nasl(url: Arc<String>, p: PathBuf) -> Result<(), Error> {
async fn update_nasl(url: Arc<String>, p: PathBuf, current_feed: String) -> Result<(), Error> {
let nasl_feed_path = p;
tokio::task::spawn_blocking(move || {
tracing::debug!("starting nasl feed update");
Expand All @@ -79,8 +79,13 @@ impl<T> Storage<T> {

let redis_cache: CacheDispatcher<RedisCtx> =
redis_storage::CacheDispatcher::init(&url, FEEDUPDATE_SELECTOR)?;

let store = PerItemDispatcher::new(redis_cache);
let mut fu = feed::Update::init(oversion, 5, &loader, &store, verifier);
if !fu.feed_is_outdated(current_feed).unwrap() {
return Ok(());
}

if let Some(x) = fu.find_map(|x| x.err()) {
Err(Error::from(x))
} else {
Expand Down Expand Up @@ -181,7 +186,12 @@ where
for h in &hash {
match h.typus {
FeedType::NASL => {
_ = updates.spawn(Self::update_nasl(self.url.clone(), h.path.clone()))
let current_feed = self.current_feed_version().await?;
_ = updates.spawn(Self::update_nasl(
self.url.clone(),
h.path.clone(),
current_feed,
))
}
FeedType::Advisories => {
_ = updates.spawn(Self::update_advisories(self.url.clone(), h.path.clone()))
Expand Down Expand Up @@ -284,6 +294,21 @@ where
async fn feed_hash(&self) -> Vec<FeedHash> {
self.hash.read().await.to_vec()
}

async fn current_feed_version(&self) -> Result<String, Error> {
let url = self.url.to_string();
let cache_version = tokio::task::spawn_blocking(move || {
let mut cache = RedisCtx::open(&url, FEEDUPDATE_SELECTOR)?;
let version = cache.lindex("nvticache", 0)?;
if version.clone().is_empty() {
let _ = cache.delete_namespace();
}
Ok::<_, Error>(version)
})
.await
.unwrap()?;
Ok(cache_version)
}
}

#[async_trait]
Expand Down
18 changes: 18 additions & 0 deletions rust/redis-storage/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ pub const NOTUSUPDATE_SELECTOR: &[NameSpaceSelector] =
pub trait RedisWrapper {
fn rpush<T: ToRedisArgs>(&mut self, key: &str, val: T) -> RedisStorageResult<()>;
fn lpush<T: ToRedisArgs>(&mut self, key: &str, val: T) -> RedisStorageResult<()>;
fn del(&mut self, key: &str) -> RedisStorageResult<()>;
fn lindex(&mut self, key: &str, index: isize) -> RedisStorageResult<String>;
fn lrange(&mut self, key: &str, start: isize, end: isize) -> RedisStorageResult<Vec<String>>;
fn keys(&mut self, pattern: &str) -> RedisStorageResult<Vec<String>>;
Expand Down Expand Up @@ -221,6 +222,16 @@ impl RedisWrapper for RedisCtx {
.map_err(|e| e.into())
}

///Wrapper function to avoid accessing kb member directly.
#[inline(always)]
fn del(&mut self, key: &str) -> RedisStorageResult<()> {
self.kb
.as_mut()
.expect("Valid redis connection")
.del(key)
.map_err(|e| e.into())
}

///Wrapper function to avoid accessing kb member directly.
#[inline(always)]
fn lindex(&mut self, key: &str, index: isize) -> RedisStorageResult<String> {
Expand Down Expand Up @@ -551,12 +562,14 @@ pub trait RedisAddNvt: RedisWrapper {
&family,
&name,
];
self.del(&key_name)?;
self.rpush(&key_name, &values)?;

// Add preferences
let prefs = Self::prefs(&nvt.preferences);
if !prefs.is_empty() {
let key_name = format!("oid:{oid}:prefs");
self.del(&key_name)?;
self.lpush(&key_name, prefs)?;
//self.kb.lpush(&key_name, prefs)?;
}
Expand Down Expand Up @@ -721,6 +734,7 @@ where

fn dispatch_feed_version(&self, version: String) -> Result<(), StorageError> {
let mut cache = Arc::as_ref(&self.cache).lock()?;
cache.del(CACHE_KEY)?;
cache.rpush(CACHE_KEY, &[&version]).map_err(|e| e.into())
}

Expand Down Expand Up @@ -812,6 +826,10 @@ mod tests {
.unwrap();
Ok(())
}
fn del(&mut self, _: &str) -> crate::dberror::RedisStorageResult<()> {
Ok(())
}

fn lindex(&mut self, _: &str, _: isize) -> crate::dberror::RedisStorageResult<String> {
Ok(String::new())
}
Expand Down

0 comments on commit 0edef24

Please sign in to comment.