-
Notifications
You must be signed in to change notification settings - Fork 784
Expand file tree
/
Copy pathmain.rs
More file actions
159 lines (149 loc) · 5.35 KB
/
Copy pathmain.rs
File metadata and controls
159 lines (149 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// SPDX-FileCopyrightText: 2023 Greenbone AG
//
// SPDX-License-Identifier: GPL-2.0-or-later
use ::notus::{loader::hashsum::HashsumProductLoader, notus::Notus};
use models::scanner::{ScanDeleter, ScanResultFetcher, ScanStarter, ScanStopper};
use nasl_interpreter::FSPluginLoader;
use notus::NotusWrapper;
use crate::storage::FeedHash;
pub mod config;
pub mod controller;
pub mod crypt;
pub mod feed;
pub mod notus;
pub mod preference;
pub mod request;
pub mod response;
mod scheduling;
pub mod storage;
pub mod tls;
fn create_context<DB, ScanHandler>(
db: DB,
sh: ScanHandler,
config: &config::Config,
) -> controller::Context<ScanHandler, DB>
where
ScanHandler: ScanStarter
+ ScanStopper
+ ScanDeleter
+ ScanResultFetcher
+ std::fmt::Debug
+ std::marker::Sync
+ std::marker::Send
+ 'static,
{
let mut ctx_builder = controller::ContextBuilder::new();
let loader = FSPluginLoader::new(config.notus.products_path.to_string_lossy().to_string());
match HashsumProductLoader::new(loader) {
Ok(loader) => {
let notus = Notus::new(loader, config.feed.signature_check);
ctx_builder = ctx_builder.notus(NotusWrapper::new(notus));
}
Err(e) => tracing::warn!("Notus Scanner disabled: {e}"),
}
ctx_builder
.mode(config.mode.clone())
.scheduler_config(config.scheduler.clone())
.feed_config(config.feed.clone())
.scanner(sh)
.api_key(config.endpoints.key.clone())
.enable_get_scans(config.endpoints.enable_get_scans)
.storage(db)
.build()
}
async fn run<S>(
scanner: S,
config: &config::Config,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
where
S: ScanStarter
+ ScanStopper
+ ScanDeleter
+ ScanResultFetcher
+ std::fmt::Debug
+ std::marker::Sync
+ std::marker::Send
+ 'static,
{
tracing::info!(mode = ?config.mode, storage_type=?config.storage.storage_type, "configuring storage devices");
let feeds = match config.mode {
config::Mode::Service => vec![
FeedHash::nasl(&config.feed.path),
FeedHash::advisories(&config.notus.advisories_path),
],
config::Mode::ServiceNotus => vec![FeedHash::advisories(&config.notus.advisories_path)],
};
match config.storage.storage_type {
config::StorageType::Redis => {
tracing::info!(url = config.storage.redis.url, "using redis");
let ic = storage::inmemory::Storage::new(
crate::crypt::ChaCha20Crypt::default(),
feeds.clone(),
);
let ctx = create_context(
storage::redis::Storage::new(ic, config.storage.redis.url.clone(), feeds),
scanner,
config,
);
controller::run(ctx, config).await
}
config::StorageType::InMemory => {
tracing::info!("using in memory store. No sensitive data will be stored on disk.");
// Self::new(crate::crypt::ChaCha20Crypt::default(), "/var/lib/openvas/feed".to_string())
let ctx = create_context(
storage::inmemory::Storage::new(crate::crypt::ChaCha20Crypt::default(), feeds),
scanner,
config,
);
controller::run(ctx, config).await
}
config::StorageType::FileSystem => {
if let Some(key) = &config.storage.fs.key {
tracing::info!(
"using in file storage. Sensitive data will be encrypted stored on disk."
);
let ctx = create_context(
storage::file::encrypted(&config.storage.fs.path, key, feeds)?,
scanner,
config,
);
controller::run(ctx, config).await
} else {
tracing::warn!(
"using in file storage. Sensitive data will be stored on disk without any encryption."
);
let ctx = create_context(
storage::file::unencrypted(&config.storage.fs.path, feeds)?,
scanner,
config,
);
controller::run(ctx, config).await
}
}
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let config = config::Config::load();
let filter = tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing::metadata::LevelFilter::INFO.into())
.parse_lossy(format!("{},rustls=info,h2=info", &config.log.level));
tracing::debug!("config: {:?}", config);
tracing_subscriber::fmt().with_env_filter(filter).init();
if !config.scanner.ospd.socket.exists() {
tracing::warn!("OSPD socket {} does not exist. Some commands will not work until the socket is created!", config.scanner.ospd.socket.display());
}
match config.scanner.scanner_type {
config::ScannerType::OSPD => {
run(
osp::Scanner::new(
config.scanner.ospd.socket.clone(),
config.scanner.ospd.read_timeout,
),
&config,
)
.await
}
config::ScannerType::Openvas => run(openvas::Scanner::default(), &config).await,
}
}