diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 6e43954749..c11c8d31f6 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -1990,10 +1990,12 @@ dependencies = [ name = "osp" version = "0.1.0" dependencies = [ + "async-trait", "models", "quick-xml", "serde", "serde_json", + "tokio", "urlencoding", ] diff --git a/rust/openvasd/src/main.rs b/rust/openvasd/src/main.rs index 5606a76409..26e3067039 100644 --- a/rust/openvasd/src/main.rs +++ b/rust/openvasd/src/main.rs @@ -12,15 +12,14 @@ pub mod feed; pub mod notus; pub mod request; pub mod response; -pub mod scan; pub mod storage; pub mod tls; fn create_context( db: DB, config: &config::Config, -) -> controller::Context { - let scanner = scan::OSPDWrapper::new(config.ospd.socket.clone(), config.ospd.read_timeout); +) -> controller::Context { + let scanner = osp::Scanner::new(config.ospd.socket.clone(), config.ospd.read_timeout); let rc = config.ospd.result_check_interval; let fc = ( config.feed.path.clone(), diff --git a/rust/openvasd/src/storage/mod.rs b/rust/openvasd/src/storage/mod.rs index 3f15937512..a06e1fb2de 100644 --- a/rust/openvasd/src/storage/mod.rs +++ b/rust/openvasd/src/storage/mod.rs @@ -45,6 +45,11 @@ impl From for Error { } } +impl From for models::scanner::Error { + fn from(value: crate::storage::Error) -> Self { + Self::Unexpected(format!("{value:?}")) + } +} #[async_trait] pub trait ScanIDClientMapper { async fn add_scan_client_id(&self, scan_id: String, client_id: ClientHash) diff --git a/rust/osp/Cargo.toml b/rust/osp/Cargo.toml index 59fcb32f17..cfc835f70a 100644 --- a/rust/osp/Cargo.toml +++ b/rust/osp/Cargo.toml @@ -12,3 +12,5 @@ serde = { version = "1.0", features = ["derive"]} quick-xml = { version = "0.28.1", features = ["serialize"] } serde_json = "1.0" urlencoding = "2.1.2" +async-trait = "0.1.77" +tokio = { version = "1.36.0", features = [ "full" ] } diff --git a/rust/osp/src/lib.rs b/rust/osp/src/lib.rs index 0fe0b1755b..83161b6a77 100644 --- a/rust/osp/src/lib.rs +++ b/rust/osp/src/lib.rs @@ -6,10 +6,12 @@ mod commands; mod connection; mod response; +mod scanner; pub use commands::Error; pub use commands::ScanCommand; pub use connection::*; pub use response::*; +pub use scanner::Scanner; /// The id of a scan pub type ScanID = String; diff --git a/rust/openvasd/src/scan/mod.rs b/rust/osp/src/scanner.rs similarity index 83% rename from rust/openvasd/src/scan/mod.rs rename to rust/osp/src/scanner.rs index 2579d39105..5b8824ea22 100644 --- a/rust/openvasd/src/scan/mod.rs +++ b/rust/osp/src/scanner.rs @@ -1,7 +1,9 @@ // SPDX-FileCopyrightText: 2023 Greenbone AG // // SPDX-License-Identifier: GPL-2.0-or-later - +//!Contains the scanner implementation for ospd. +//! +//!The scanner is used in openvasd to control scans. use std::{path::PathBuf, time::Duration}; use async_trait::async_trait; @@ -9,20 +11,14 @@ use models::scanner::{ScanDeleter, ScanResultFetcher, ScanResults, ScanStarter, #[derive(Debug, Clone)] /// OSPD wrapper, is used to utilize ospd -pub struct OSPDWrapper { +pub struct Scanner { /// Path to the socket socket: PathBuf, /// Read timeout in seconds r_timeout: Option, } -impl From for models::scanner::Error { - fn from(value: crate::storage::Error) -> Self { - Self::Unexpected(format!("{value:?}")) - } -} - -impl OSPDWrapper { +impl Scanner { /// Creates a new instance of OSPDWrapper pub fn new(socket: PathBuf, r_timeout: Option) -> Self { Self { socket, r_timeout } @@ -51,11 +47,11 @@ impl OSPDWrapper { } #[async_trait] -impl ScanStarter for OSPDWrapper { +impl ScanStarter for Scanner { async fn start_scan(&self, scan: models::Scan) -> Result<(), models::scanner::Error> { let rtimeout = self.r_timeout; self.spawn_blocking(move |socket| { - osp::start_scan(socket, rtimeout, &scan) + crate::start_scan(socket, rtimeout, &scan) .map(|_| ()) .map_err(models::scanner::Error::from) }) @@ -64,14 +60,14 @@ impl ScanStarter for OSPDWrapper { } #[async_trait] -impl ScanStopper for OSPDWrapper { +impl ScanStopper for Scanner { async fn stop_scan(&self, id: I) -> Result<(), models::scanner::Error> where I: AsRef + Send + 'static, { let rtimeout = self.r_timeout; self.spawn_blocking(move |socket| { - osp::stop_scan(socket, rtimeout, id) + crate::stop_scan(socket, rtimeout, id) .map(|_| ()) .map_err(models::scanner::Error::from) }) @@ -80,14 +76,14 @@ impl ScanStopper for OSPDWrapper { } #[async_trait] -impl ScanDeleter for OSPDWrapper { +impl ScanDeleter for Scanner { async fn delete_scan(&self, id: I) -> Result<(), models::scanner::Error> where I: AsRef + Send + 'static, { let rtimeout = self.r_timeout; self.spawn_blocking(move |socket| { - osp::delete_scan(socket, rtimeout, id) + crate::delete_scan(socket, rtimeout, id) .map(|_| ()) .map_err(models::scanner::Error::from) }) @@ -96,14 +92,14 @@ impl ScanDeleter for OSPDWrapper { } #[async_trait] -impl ScanResultFetcher for OSPDWrapper { +impl ScanResultFetcher for Scanner { async fn fetch_results(&self, id: I) -> Result where I: AsRef + Send + 'static, { let rtimeout = self.r_timeout; self.spawn_blocking(move |socket| { - osp::get_delete_scan_results(socket, rtimeout, id) + crate::get_delete_scan_results(socket, rtimeout, id) .map(|r| ScanResults { id: r.clone().id, status: r.clone().into(),