-
Notifications
You must be signed in to change notification settings - Fork 680
Change: progress information #1832
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
ec2d5e4
c510808
03ebe0e
cc684f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -895,10 +895,7 @@ components: | |||||
type: "integer" | ||||||
format: "int32" | ||||||
scanning: | ||||||
description: "The IP Addresses of the currently scanned hosts." | ||||||
type: "array" | ||||||
items: | ||||||
type: "string" | ||||||
$ref: "#/components/schemas/Scanning" | ||||||
|
||||||
required: | ||||||
- all | ||||||
|
@@ -908,6 +905,30 @@ components: | |||||
- queued | ||||||
- finished | ||||||
|
||||||
SingleHostProgress: { | ||||||
type: "object", | ||||||
description: "Additional information about the scanned host", | ||||||
properties: { | ||||||
finished_tests: { | ||||||
description: "The number of vulnerability test alredy run for the host", | ||||||
type: "integer", | ||||||
format: "int32" | ||||||
}, | ||||||
total_tests: { | ||||||
description: "The total amount of vulnerability test to be run for the host", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
type: "integer", | ||||||
format: "int32" | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
Scanning: | ||||||
description: "The IP Addresses of the currently scanned hosts." | ||||||
type: "object" | ||||||
additionalProperties: { | ||||||
$ref: "#/components/schemas/SingleHostProgress" | ||||||
} | ||||||
|
||||||
ScanAction: | ||||||
description: "An action to perform on a scan" | ||||||
type: "object" | ||||||
|
@@ -1228,16 +1249,24 @@ components: | |||||
{ | ||||||
"start_time": 1679649183, | ||||||
"status": "running", | ||||||
"host_info": | ||||||
{ | ||||||
"all": 14, | ||||||
"excluded": 0, | ||||||
"dead": 4, | ||||||
"alive": 6, | ||||||
"queued": 1, | ||||||
"finished": 1, | ||||||
"scanning": ["127.0.0.1", "10.0.5.1", "10.0.5.2", "10.0.5.3"], | ||||||
"host_info": { | ||||||
"all": 14, | ||||||
"excluded": 0, | ||||||
"dead": 4, | ||||||
"alive": 6, | ||||||
"queued": 1, | ||||||
"finished": 1, | ||||||
"scanning": { | ||||||
"192.168.0.1": { | ||||||
"finished_tests": 456, | ||||||
"total_tests": 1000 | ||||||
}, | ||||||
"192.168.0.2": { | ||||||
"finished_tests": 456, | ||||||
"total_tests": 1000 | ||||||
} | ||||||
}, | ||||||
}, | ||||||
} | ||||||
|
||||||
scan_status_success: | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ pub struct HostInfoBuilder { | |
pub alive: u64, | ||
pub queued: u64, | ||
pub finished: u64, | ||
pub scanning: Option<HashMap<String, i32>>, | ||
pub scanning: Option<HashMap<String, SingleHostScanInfo>>, | ||
} | ||
|
||
impl HostInfoBuilder { | ||
|
@@ -32,6 +32,33 @@ impl HostInfoBuilder { | |
} | ||
} | ||
|
||
#[derive(Default, Debug, Clone, Eq, PartialEq)] | ||
#[cfg_attr( | ||
feature = "serde_support", | ||
derive(serde::Serialize, serde::Deserialize) | ||
)] | ||
pub struct SingleHostScanInfo { | ||
finished_tests: i32, | ||
total_tests: i32, | ||
} | ||
|
||
impl SingleHostScanInfo { | ||
pub fn new(finished_tests: i32, total_tests: i32) -> Self { | ||
Self { | ||
finished_tests, | ||
total_tests, | ||
} | ||
} | ||
|
||
pub fn finished_tests(&self) -> i32 { | ||
self.finished_tests | ||
} | ||
|
||
pub fn total_tests(&self) -> i32 { | ||
self.total_tests | ||
} | ||
} | ||
|
||
/// Information about hosts of a running scan | ||
#[derive(Debug, Clone, Default, PartialEq, Eq)] | ||
#[cfg_attr( | ||
|
@@ -51,9 +78,8 @@ pub struct HostInfo { | |
feature = "serde_support", | ||
serde(skip_serializing_if = "Option::is_none") | ||
)] | ||
scanning: Option<HashMap<String, i32>>, | ||
// Hosts that are currently being scanned. The second entry is the number of | ||
// remaining VTs for this host. | ||
scanning: Option<HashMap<String, SingleHostScanInfo>>, | ||
#[cfg_attr(feature = "serde_support", serde(skip))] | ||
remaining_vts_per_host: HashMap<String, usize>, | ||
} | ||
|
||
|
@@ -92,6 +118,9 @@ impl HostInfo { | |
} | ||
|
||
pub fn update_with(mut self, other: &HostInfo) -> Self { | ||
enum ScanProgress { | ||
DeadHost = -1, | ||
} | ||
// total hosts value is sent once and only once must be updated | ||
if other.all != 0 { | ||
self.all = other.all; | ||
|
@@ -112,10 +141,12 @@ impl HostInfo { | |
// and never completely replaced. | ||
let mut hs = other.scanning.clone().unwrap_or_default(); | ||
for (host, progress) in self.scanning.clone().unwrap_or_default().iter() { | ||
if *progress == 100 || *progress == -1 { | ||
if progress.finished_tests() == progress.total_tests() | ||
|| progress.total_tests == ScanProgress::DeadHost as i32 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we already have the Also I'm not sure I fully understand the enum TotalTests {
Num(i32),
DeadHost,
} and to parse that from the given |
||
{ | ||
hs.remove(host); | ||
} else { | ||
hs.insert(host.to_string(), *progress); | ||
hs.insert(host.to_string(), progress.clone()); | ||
} | ||
} | ||
self.scanning = Some(hs); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,12 @@ use std::{ | |
sync::{Arc, Mutex}, | ||
}; | ||
|
||
use crate::openvas::openvas_redis::{KbAccess, VtHelper}; | ||
use crate::osp::{OspResultType, OspScanResult}; | ||
use crate::storage::redis::RedisStorageResult; | ||
use crate::{ | ||
models::SingleHostScanInfo, | ||
openvas::openvas_redis::{KbAccess, VtHelper}, | ||
}; | ||
|
||
/// Structure to hold the results retrieve from redis main kb | ||
#[derive(Default, Debug, Clone)] | ||
|
@@ -30,7 +33,7 @@ pub struct Results { | |
/// during the scan | ||
pub count_dead: i64, | ||
/// Current hosts status | ||
pub host_status: HashMap<String, i32>, | ||
pub host_status: HashMap<String, SingleHostScanInfo>, | ||
/// The scan status | ||
pub scan_status: String, | ||
} | ||
|
@@ -165,32 +168,38 @@ where | |
} | ||
let mut new_dead = 0; | ||
let mut new_alive = 0; | ||
let mut all_hosts: HashMap<String, i32> = HashMap::new(); | ||
let mut all_hosts: HashMap<String, SingleHostScanInfo> = HashMap::new(); | ||
for res in redis_status { | ||
let mut fields = res.splitn(3, '/'); | ||
let current_host = fields.next().expect("Valid status value"); | ||
let launched = fields.next().expect("Valid status value"); | ||
let total = fields.next().expect("Valid status value"); | ||
|
||
let host_progress: i32 = match i32::from_str(total) { | ||
let total = match i32::from_str(total) { | ||
// No plugins | ||
Ok(0) => { | ||
continue; | ||
} | ||
// Host Dead | ||
Ok(-1) => ScanProgress::DeadHost as i32, | ||
Ok(n) => ((f32::from_str(launched).expect("Integer") / n as f32) * 100.0) as i32, | ||
Ok(n) => n, | ||
_ => { | ||
continue; | ||
} | ||
}; | ||
|
||
let launched = i32::from_str(launched).expect("Integer"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that
If you unwrap, you get
which is almost more informative ... I think the better thing is to either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we even unwrap here? It is returning a |
||
|
||
let host_progress = ((launched as f32 / total as f32) * 100.0) as i32; | ||
if host_progress == -1 { | ||
new_dead += 1; | ||
} else if host_progress == 100 { | ||
new_alive += 1; | ||
} | ||
all_hosts.insert(current_host.to_string(), host_progress); | ||
all_hosts.insert( | ||
current_host.to_string(), | ||
SingleHostScanInfo::new(launched, total), | ||
); | ||
|
||
tracing::debug!("Host {} has progress: {}", current_host, host_progress); | ||
} | ||
|
@@ -225,6 +234,7 @@ mod tests { | |
|
||
use crate::models::{self, Protocol, Result, ResultType}; | ||
use crate::openvas::openvas_redis::FakeRedis; | ||
use crate::openvas::result_collector::SingleHostScanInfo; | ||
use std::collections::HashMap; | ||
|
||
use super::ResultHelper; | ||
|
@@ -346,11 +356,11 @@ mod tests { | |
resh.process_status(status).unwrap(); | ||
|
||
let mut r = HashMap::new(); | ||
r.insert("127.0.0.1".to_string(), 12); | ||
r.insert("127.0.0.3".to_string(), 75); | ||
r.insert("127.0.0.4".to_string(), 100); | ||
r.insert("127.0.0.2".to_string(), -1); | ||
r.insert("127.0.0.5".to_string(), -1); | ||
r.insert("127.0.0.1".to_string(), SingleHostScanInfo::new(128, 1000)); | ||
r.insert("127.0.0.3".to_string(), SingleHostScanInfo::new(750, 1000)); | ||
r.insert("127.0.0.4".to_string(), SingleHostScanInfo::new(1000, 1000)); | ||
r.insert("127.0.0.2".to_string(), SingleHostScanInfo::new(0, -1)); | ||
r.insert("127.0.0.5".to_string(), SingleHostScanInfo::new(0, -1)); | ||
|
||
assert_eq!(resh.results.as_ref().lock().unwrap().host_status, r); | ||
assert_eq!(resh.results.as_ref().lock().unwrap().count_alive, 1); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.