-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This module gets a scan config and prepare the data to be stored in redis as openvas-scanner expected. The data is stored in redis as well. For that, a new module openvas_redis was added as well, which adds an implements the RedisHelper struct. Depends on #1566
- Loading branch information
Showing
11 changed files
with
845 additions
and
81 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod cmd; | ||
pub mod ctl; | ||
pub mod error; | ||
pub mod openvas_redis; | ||
pub mod pref_handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// SPDX-FileCopyrightText: 2024 Greenbone AG | ||
// | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
use redis_storage::{ | ||
dberror::{DbError, RedisStorageResult}, | ||
NameSpaceSelector, RedisCtx, RedisGetNvt, RedisWrapper, | ||
}; | ||
use std::sync::{Arc, Mutex}; | ||
use storage::item::Nvt; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct RedisHelper<R> | ||
where | ||
R: RedisWrapper, | ||
{ | ||
cache: Arc<Mutex<R>>, | ||
task_kb: Arc<Mutex<R>>, | ||
} | ||
|
||
impl RedisHelper<RedisCtx> { | ||
/// Initialize a RedisHelper struct with the connection to access the NVT cache | ||
/// and a empty task knowledge base to store the scan configuration to be sent to openvas. | ||
pub fn init( | ||
redis_url: &str, | ||
selector: &[NameSpaceSelector], | ||
) -> RedisStorageResult<RedisHelper<RedisCtx>> { | ||
let mut rctx = RedisCtx::open(redis_url, selector)?; | ||
rctx.delete_namespace()?; | ||
let cache = RedisCtx::open(redis_url, selector)?; | ||
|
||
Ok(RedisHelper::<RedisCtx> { | ||
cache: Arc::new(Mutex::new(cache)), | ||
task_kb: Arc::new(Mutex::new(rctx)), | ||
}) | ||
} | ||
|
||
/// Provide access to the cache | ||
pub fn kb_id(&self) -> RedisStorageResult<u32> { | ||
let cache = &self | ||
.cache | ||
.lock() | ||
.map_err(|e| DbError::SystemError(format!("{e:?}")))?; | ||
Ok(cache.db) | ||
} | ||
} | ||
|
||
pub trait KbAccess { | ||
fn push_kb_item<T: redis::ToRedisArgs>(&self, key: &str, value: T) -> RedisStorageResult<()>; | ||
} | ||
|
||
impl KbAccess for RedisHelper<RedisCtx> { | ||
fn push_kb_item<T: redis::ToRedisArgs>(&self, key: &str, value: T) -> RedisStorageResult<()> { | ||
let mut kb = Arc::as_ref(&self.task_kb) | ||
.lock() | ||
.map_err(|e| DbError::SystemError(format!("{e:?}")))?; | ||
|
||
kb.lpush(key, value)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub trait VtHelper { | ||
fn get_vt(&self, oid: &str) -> RedisStorageResult<Option<Nvt>>; | ||
} | ||
|
||
impl VtHelper for RedisHelper<RedisCtx> { | ||
fn get_vt(&self, oid: &str) -> RedisStorageResult<Option<Nvt>> { | ||
let mut cache = Arc::as_ref(&self.cache) | ||
.lock() | ||
.map_err(|e| DbError::SystemError(format!("{e:?}")))?; | ||
|
||
cache.redis_get_vt(oid) | ||
} | ||
} |
Oops, something went wrong.