Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move scanner traits to model crate and introduce result struct #1577

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rust/Cargo.lock

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

1 change: 1 addition & 0 deletions rust/models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license = "GPL-2.0-or-later"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = "0.1.77"
serde = { version = "1", features = ["derive"], optional = true }

[features]
Expand Down
1 change: 1 addition & 0 deletions rust/models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod product;
mod result;
mod scan;
mod scan_action;
pub mod scanner;
mod scanner_preference;
mod status;
mod target;
Expand Down
75 changes: 75 additions & 0 deletions rust/models/src/scanner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use async_trait::async_trait;

use crate::{Scan, Status};

/// Contains results of a scan as well as identification factors and statuses.
///
/// It is usually returned on fetch_results which gets all results of all running scans for further
/// processing.
#[derive(Debug, Default, PartialEq, Eq)]
#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct ScanResults {
pub id: String,
pub status: Status,
pub results: Vec<crate::Result>,
}

/// Starts a scan
#[async_trait]
pub trait ScanStarter {
/// Starts a scan
async fn start_scan(&self, scan: Scan) -> Result<(), Error>;
}

/// Stops a scan
#[async_trait]
pub trait ScanStopper {
/// Stops a scan
async fn stop_scan<I>(&self, id: I) -> Result<(), Error>
where
I: AsRef<str> + Send + 'static;
}

/// Deletes a scan
#[async_trait]
pub trait ScanDeleter {
async fn delete_scan<I>(&self, id: I) -> Result<(), Error>
where
I: AsRef<str> + Send + 'static;
}

#[async_trait]
pub trait ScanResultFetcher {
/// Fetches the results of a scan and combines the results with response
async fn fetch_results<I>(&self, id: I) -> Result<ScanResults, Error>
where
I: AsRef<str> + Send + 'static;
}

/// Combines all traits needed for a scanner.
#[async_trait]
pub trait Scanner: ScanStarter + ScanStopper + ScanDeleter + ScanResultFetcher {}

impl<T> Scanner for T where T: ScanStarter + ScanStopper + ScanDeleter + ScanResultFetcher {}

#[derive(Debug)]
pub enum Error {
Unexpected(String),
Connection(String),
Poisoned,
}

impl std::error::Error for Error {}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Unexpected(x) => write!(f, "Unexpecdted issue: {x}"),
Self::Connection(x) => write!(f, "Connection issue: {x}"),
_ => write!(f, "{:?}", self),
}
}
}
16 changes: 10 additions & 6 deletions rust/openvasd/src/controller/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use std::{path::PathBuf, sync::RwLock};
use async_trait::async_trait;
use storage::DefaultDispatcher;

use crate::{
notus::NotusWrapper,
response,
scan::{Error, ScanDeleter, ScanResultFetcher, ScanStarter, ScanStopper},
use crate::{notus::NotusWrapper, response};

use models::scanner::{
Error, ScanDeleter, ScanResultFetcher, ScanResults, ScanStarter, ScanStopper,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -166,7 +166,11 @@ where
/// Sets the scanner. This is required.
pub fn scanner(self, scanner: S) -> ContextBuilder<S, DB, Scanner<S>>
where
S: super::Scanner + 'static + std::marker::Send + std::marker::Sync + std::fmt::Debug,
S: models::scanner::Scanner
+ 'static
+ std::marker::Send
+ std::marker::Sync
+ std::fmt::Debug,
{
let Self {
result_config,
Expand Down Expand Up @@ -270,7 +274,7 @@ impl ScanDeleter for NoOpScanner {

#[async_trait]
impl ScanResultFetcher for NoOpScanner {
async fn fetch_results<I>(&self, _: I) -> Result<crate::scan::FetchResult, Error>
async fn fetch_results<I>(&self, _: I) -> Result<ScanResults, Error>
where
I: AsRef<str> + Send,
{
Expand Down
10 changes: 4 additions & 6 deletions rust/openvasd/src/controller/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ use std::{fmt::Display, marker::PhantomData, sync::Arc};
use super::{context::Context, ClientIdentifier};

use hyper::{Method, Request};
use models::scanner::{ScanDeleter, ScanResultFetcher, ScanStarter, ScanStopper};

use crate::{
controller::ClientHash,
notus::NotusScanner,
scan::{self, Error, ScanDeleter, ScanStarter, ScanStopper},
};
use crate::{controller::ClientHash, notus::NotusScanner};
use models::scanner::*;

enum HealthOpts {
/// Ready
Expand Down Expand Up @@ -137,7 +135,7 @@ where
S: ScanStarter
+ ScanStopper
+ ScanDeleter
+ scan::ScanResultFetcher
+ ScanResultFetcher
+ std::marker::Send
+ std::marker::Sync
+ 'static,
Expand Down
4 changes: 3 additions & 1 deletion rust/openvasd/src/controller/feed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use std::sync::Arc;

use models::scanner::Scanner;

use crate::feed::FeedIdentifier;

use super::context::Context;

pub async fn fetch<S, DB>(ctx: Arc<Context<S, DB>>)
where
S: super::Scanner + 'static + std::marker::Send + std::marker::Sync,
S: Scanner + 'static + std::marker::Send + std::marker::Sync,
DB: crate::storage::Storage + 'static + std::marker::Send + std::marker::Sync,
{
tracing::debug!("Starting VTS synchronization loop");
Expand Down
Loading
Loading