Skip to content

Commit

Permalink
Fix: update feed_version response's field after feed update
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola committed Aug 7, 2024
1 parent 043403b commit 4c74263
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 11 deletions.
1 change: 0 additions & 1 deletion rust/feed/src/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ where

/// 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() {
Expand Down
5 changes: 3 additions & 2 deletions rust/openvasd/src/controller/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

use std::sync::RwLock;

use async_trait::async_trait;
use std::sync::{Arc, RwLock};
use storage::DefaultDispatcher;

use crate::{config, notus::NotusWrapper, response, scheduling, tls::TlsConfig};
Expand Down Expand Up @@ -205,6 +204,8 @@ impl<S, DB> ContextBuilder<S, DB, Scanner<S>> {
self.scanner.0,
self.storage,
);
let shared_feed = Arc::clone(&scheduler.feed_version());
self.response.add_feed_version(shared_feed);
Context {
response: self.response,
scheduler,
Expand Down
27 changes: 22 additions & 5 deletions rust/openvasd/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

use std::{convert::Infallible, error::Error, pin::Pin, sync::mpsc::Receiver, task::Poll, thread};
use std::{
convert::Infallible,
error::Error,
pin::Pin,
sync::{mpsc::Receiver, Arc},
task::Poll,
thread,
};

use http_body::Body;
use hyper::body::Bytes;
Expand All @@ -13,15 +20,15 @@ pub type Result = hyper::Response<BodyKind>;
pub struct Response {
authentication: String,
version: String,
feed_version: String,
feed_version: Arc<std::sync::RwLock<String>>,
}

impl Default for Response {
fn default() -> Self {
Self {
authentication: String::new(),
version: "1".to_string(),
feed_version: String::new(),
feed_version: Arc::new(std::sync::RwLock::new(String::new())),
}
}
}
Expand Down Expand Up @@ -133,7 +140,11 @@ impl Body for BodyKind {
impl Response {
/// Sets the version of the response header.
pub fn set_feed_version(&mut self, feed_version: &str) {
self.feed_version = feed_version.to_string();
*self.feed_version.write().expect("Invalid feed version") = feed_version.to_string();
}

pub fn add_feed_version(&mut self, feed_version: Arc<std::sync::RwLock<String>>) {
self.feed_version = feed_version;
}

/// Appends authentication to the response header.
Expand All @@ -150,7 +161,13 @@ impl Response {
hyper::Response::builder()
.header("authentication", &self.authentication)
.header("api-version", &self.version)
.header("feed-version", &self.feed_version)
.header(
"feed-version",
&*self
.feed_version
.read()
.expect("Not possible to read feed version"),
)
}

#[inline]
Expand Down
14 changes: 14 additions & 0 deletions rust/openvasd/src/scheduling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

use std::fmt::Display;
use std::sync::Arc;
use std::time::SystemTime;

use crate::storage::{Error as StorageError, FeedHash};
Expand Down Expand Up @@ -103,6 +104,8 @@ pub struct Scheduler<DB, Scanner> {
/// Is used to start, stop, ... scan.
scanner: Scanner,
config: config::Scheduler,
/// Feed version shared with response.
feed_version: Arc<std::sync::RwLock<String>>,
}

impl<DB, Scanner> Scheduler<DB, Scanner> {
Expand All @@ -116,12 +119,17 @@ impl<DB, Scanner> Scheduler<DB, Scanner> {
scanner,
config,
is_synchronizing_feed: RwLock::new(false),
feed_version: Arc::new(std::sync::RwLock::new(String::from("UNDEFINED"))),
}
}

pub fn config(&self) -> &config::Scheduler {
&self.config
}

pub fn feed_version(&self) -> Arc<std::sync::RwLock<String>> {
self.feed_version.clone()
}
}

impl<DB, Scanner> Scheduler<DB, Scanner>
Expand Down Expand Up @@ -475,6 +483,8 @@ where
Ok(_) => Ok(()),
Err(e) => Err(e),
};
let fv = self.db.current_feed_version().await.unwrap();
*self.feed_version.write().unwrap() = fv;
*sync_feed = false;
tracing::debug!(?result, "feed update finished, scans can be started again");
result
Expand All @@ -497,6 +507,10 @@ where
async fn feed_hash(&self) -> Vec<FeedHash> {
self.db.feed_hash().await.to_vec()
}

async fn current_feed_version(&self) -> Result<String, StorageError> {
self.db.current_feed_version().await
}
}

#[async_trait]
Expand Down
5 changes: 5 additions & 0 deletions rust/openvasd/src/storage/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ where
async fn feed_hash(&self) -> Vec<FeedHash> {
self.hash.read().await.to_vec()
}

async fn current_feed_version(&self) -> Result<String, Error> {
let v = self.feed_version.read().unwrap().clone();
Ok(v)
}
}

#[cfg(test)]
Expand Down
5 changes: 5 additions & 0 deletions rust/openvasd/src/storage/inmemory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ where
async fn feed_hash(&self) -> Vec<FeedHash> {
self.hash.read().await.to_vec()
}

async fn current_feed_version(&self) -> Result<String, Error> {
let v = self.feed_version.read().await.clone();
Ok(v)
}
}

#[cfg(test)]
Expand Down
4 changes: 1 addition & 3 deletions rust/openvasd/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,7 @@ pub trait NVTStorer {
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 fn current_feed_version(&self) -> Result<String, Error>;
}

#[async_trait]
Expand Down

0 comments on commit 4c74263

Please sign in to comment.