Skip to content

Commit

Permalink
Change: move osp specific Scanner implementation from openvasd into osp
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtsfrei committed Feb 27, 2024
1 parent ab901d6 commit b869030
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
2 changes: 2 additions & 0 deletions rust/Cargo.lock

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

5 changes: 2 additions & 3 deletions rust/openvasd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: DB,
config: &config::Config,
) -> controller::Context<scan::OSPDWrapper, DB> {
let scanner = scan::OSPDWrapper::new(config.ospd.socket.clone(), config.ospd.read_timeout);
) -> controller::Context<osp::Scanner, DB> {
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(),
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 @@ -45,6 +45,11 @@ impl From<std::string::FromUtf8Error> for Error {
}
}

impl From<crate::storage::Error> 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)
Expand Down
2 changes: 2 additions & 0 deletions rust/osp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ] }
2 changes: 2 additions & 0 deletions rust/osp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
30 changes: 13 additions & 17 deletions rust/openvasd/src/scan/mod.rs → rust/osp/src/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
// 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;
use models::scanner::{ScanDeleter, ScanResultFetcher, ScanResults, ScanStarter, ScanStopper};

#[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<Duration>,
}

impl From<crate::storage::Error> 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<Duration>) -> Self {
Self { socket, r_timeout }
Expand Down Expand Up @@ -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)
})
Expand All @@ -64,14 +60,14 @@ impl ScanStarter for OSPDWrapper {
}

#[async_trait]
impl ScanStopper for OSPDWrapper {
impl ScanStopper for Scanner {
async fn stop_scan<I>(&self, id: I) -> Result<(), models::scanner::Error>
where
I: AsRef<str> + 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)
})
Expand All @@ -80,14 +76,14 @@ impl ScanStopper for OSPDWrapper {
}

#[async_trait]
impl ScanDeleter for OSPDWrapper {
impl ScanDeleter for Scanner {
async fn delete_scan<I>(&self, id: I) -> Result<(), models::scanner::Error>
where
I: AsRef<str> + 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)
})
Expand All @@ -96,14 +92,14 @@ impl ScanDeleter for OSPDWrapper {
}

#[async_trait]
impl ScanResultFetcher for OSPDWrapper {
impl ScanResultFetcher for Scanner {
async fn fetch_results<I>(&self, id: I) -> Result<ScanResults, models::scanner::Error>
where
I: AsRef<str> + 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(),
Expand Down

0 comments on commit b869030

Please sign in to comment.