Skip to content

Commit

Permalink
fix typo and apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola committed Feb 4, 2025
1 parent 03ebe0e commit cc684f2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
4 changes: 2 additions & 2 deletions rust/doc/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -910,12 +910,12 @@ components:
description: "Additional information about the scanned host",
properties: {
finished_tests: {
description: "The number of vulnerability test alredy run for the host",
description: "The number of vulnerability tests already run for the host",
type: "integer",
format: "int32"
},
total_tests: {
description: "The total amount of vulnerability test to be run for the host",
description: "The total amount of vulnerability tests to be run for the host",
type: "integer",
format: "int32"
}
Expand Down
48 changes: 34 additions & 14 deletions rust/src/models/host_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,49 @@ impl HostInfoBuilder {
derive(serde::Serialize, serde::Deserialize)
)]
pub struct SingleHostScanInfo {
finished_tests: i32,
total_tests: i32,
finished_tests: AmountOfTests,
total_tests: AmountOfTests,
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
enum AmountOfTests {
AmountOfTests(i32),
DeadHost,
}

impl Default for AmountOfTests {
fn default() -> Self {
AmountOfTests::AmountOfTests(0)
}
}

impl SingleHostScanInfo {
pub fn new(finished_tests: i32, total_tests: i32) -> Self {
Self {
finished_tests,
total_tests,
finished_tests: AmountOfTests::AmountOfTests(finished_tests),
total_tests: if total_tests == -1 {
AmountOfTests::DeadHost
} else {
AmountOfTests::AmountOfTests(total_tests)
},
}
}

pub fn finished_tests(&self) -> i32 {
self.finished_tests
pub fn is_finished(&self) -> bool {
if let AmountOfTests::AmountOfTests(f) = self.finished_tests {
if let AmountOfTests::AmountOfTests(t) = self.total_tests {
return f == t;
}
}
false
}

pub fn total_tests(&self) -> i32 {
self.total_tests
pub fn is_dead(&self) -> bool {
matches!(self.total_tests, AmountOfTests::DeadHost)
}
}

Expand Down Expand Up @@ -118,9 +143,6 @@ 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;
Expand All @@ -141,9 +163,7 @@ 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.finished_tests() == progress.total_tests()
|| progress.total_tests == ScanProgress::DeadHost as i32
{
if progress.is_finished() || progress.is_dead() {
hs.remove(host);
} else {
hs.insert(host.to_string(), progress.clone());
Expand Down
24 changes: 11 additions & 13 deletions rust/src/openvas/result_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ where
} else if result_type == "ALARM" {
push_result(OspResultType::Alarm);
} else if result_type == "DEADHOST" {
new_dead += i64::from_str(&value).expect("Valid amount of dead hosts");
new_dead += i64::from_str(&value).expect("Expected a valid amount of dead hosts");
} else if host_count {
count_total = i64::from_str(&value).expect("Valid amount of dead hosts");
count_total = i64::from_str(&value).expect("Expected a valid amount of dead hosts");
} else if excluded_hosts {
count_excluded = i64::from_str(&value).expect("Valid amount of excluded hosts");
count_excluded =
i64::from_str(&value).expect("Expected a valid amount of excluded hosts");
}
}
if let Ok(mut results) = Arc::as_ref(&self.results).lock() {
Expand All @@ -163,39 +164,36 @@ where
}

fn process_status(&self, redis_status: Vec<String>) -> RedisStorageResult<()> {
enum ScanProgress {
DeadHost = -1,
}
let mut new_dead = 0;
let mut new_alive = 0;
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 current_host = fields.next().expect("Expected a valid status value");
let launched = fields.next().expect("Expected a valid status value");
let total = fields.next().expect("Expected a valid status value");

let total = match i32::from_str(total) {
// No plugins
Ok(0) => {
continue;
}
// Host Dead
Ok(-1) => ScanProgress::DeadHost as i32,
Ok(-1) => -1,
Ok(n) => n,
_ => {
continue;
}
};

let launched = i32::from_str(launched).expect("Integer");

let launched = i32::from_str(launched).expect("Expected a valid integer value");
let host_progress = ((launched as f32 / total as f32) * 100.0) as i32;
if host_progress == -1 {
if total == -1 {
new_dead += 1;
} else if host_progress == 100 {
new_alive += 1;
}

all_hosts.insert(
current_host.to_string(),
SingleHostScanInfo::new(launched, total),
Expand Down

0 comments on commit cc684f2

Please sign in to comment.