From 5d6788f47a8fdf5458ba56a9730b84463f924ca0 Mon Sep 17 00:00:00 2001 From: Kraemii Date: Wed, 3 Apr 2024 17:54:06 +0200 Subject: [PATCH] Change: use a json dict instead of an array of id-value pairs for scan_preferences --- rust/doc/openapi.yml | 16 ++++++-------- rust/doc/reverse-sensor-openapi.yml | 16 ++++++-------- rust/models/src/lib.rs | 19 +++++----------- rust/models/src/scan.rs | 6 +++-- rust/models/src/scan_preference.rs | 34 ++++++++++++++++++++++------- rust/openvas/src/pref_handler.rs | 2 +- rust/osp/src/commands.rs | 8 +++---- 7 files changed, 54 insertions(+), 47 deletions(-) diff --git a/rust/doc/openapi.yml b/rust/doc/openapi.yml index 01d43d47ac..a579b1440f 100644 --- a/rust/doc/openapi.yml +++ b/rust/doc/openapi.yml @@ -415,10 +415,8 @@ components: target: $ref: "#/components/schemas/Target" scan_preferences: - description: "Overwrite the default settings of the Scanner." - type: "array" - items: - $ref: "#/components/schemas/ScanPreference" + description: "Overwrite the default settings for a scan. Setting are id-value pairs." + type: "object" vts: type: "array" description: "A collection of VTs, which are run for the given target." @@ -947,11 +945,11 @@ components: "reverse_lookup_only": false, }, "scan_preferences": - [ - { "id": "target_port", "value": "443" }, - { "id": "use_https", "value": "1" }, - { "id": "profile", "value": "fast_scan" }, - ], + { + "target_port": 443, + "use_https": true, + "cgi_path": "/cgi-bin:/scripts", + }, "vts": [ { diff --git a/rust/doc/reverse-sensor-openapi.yml b/rust/doc/reverse-sensor-openapi.yml index 58a14b149d..4fe7aaf6e9 100644 --- a/rust/doc/reverse-sensor-openapi.yml +++ b/rust/doc/reverse-sensor-openapi.yml @@ -229,10 +229,8 @@ components: target: $ref: "#/components/schemas/Target" scan_preferences: - description: "Overwrite the default settings of the Scanner." - type: "array" - items: - $ref: "#/components/schemas/ScanPreference" + description: "Overwrite the default settings for a scan. Setting are id-value pairs." + type: "object" vts: type: "array" description: "A collection of VTs, which are run for the given target." @@ -653,11 +651,11 @@ components: "reverse_lookup_only": false, }, "scan_preferences": - [ - { "id": "target_port", "value": "443" }, - { "id": "use_https", "value": "1" }, - { "id": "profile", "value": "fast_scan" }, - ], + { + "target_port": 443, + "use_https": true, + "cgi_path": "/cgi-bin:/scripts", + }, "vts": [ { diff --git a/rust/models/src/lib.rs b/rust/models/src/lib.rs index 459ae981ff..f535a075b8 100644 --- a/rust/models/src/lib.rs +++ b/rust/models/src/lib.rs @@ -137,20 +137,11 @@ mod tests { "reverse_lookup_unify": true, "reverse_lookup_only": false }, - "scan_preferences": [ - { - "id": "target_port", - "value": "443" - }, - { - "id": "use_https", - "value": "1" - }, - { - "id": "profile", - "value": "fast_scan" - } - ], + "scan_preferences": { + "target_port": 443, + "use_https": true, + "cgi_path": "/cgi-bin:/scripts" + }, "vts": [ { "oid": "1.3.6.1.4.1.25623.1.0.10662", diff --git a/rust/models/src/scan.rs b/rust/models/src/scan.rs index e0e5fc2fe2..b2e628adab 100644 --- a/rust/models/src/scan.rs +++ b/rust/models/src/scan.rs @@ -2,7 +2,9 @@ // // SPDX-License-Identifier: GPL-2.0-or-later -use super::{scan_preference::ScanPreference, target::Target, vt::VT}; +use std::collections::HashMap; + +use super::{scan_preference::PreferenceValue, target::Target, vt::VT}; /// Struct for creating and getting a scan #[derive(Default, Debug, Clone, PartialEq, Eq)] @@ -22,7 +24,7 @@ pub struct Scan { serde(default, alias = "scanner_preferences") )] /// Configuration options for the scanner - pub scan_preferences: Vec, + pub scan_preferences: HashMap, /// List of VTs to execute for the target pub vts: Vec, } diff --git a/rust/models/src/scan_preference.rs b/rust/models/src/scan_preference.rs index a763049a0a..2599bb26f3 100644 --- a/rust/models/src/scan_preference.rs +++ b/rust/models/src/scan_preference.rs @@ -2,15 +2,33 @@ // // SPDX-License-Identifier: GPL-2.0-or-later -/// Configuration preference for the scanner -#[derive(Default, Debug, Clone, PartialEq, Eq)] +use std::fmt::Display; + +/// Preference value +#[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr( feature = "serde_support", - derive(serde::Serialize, serde::Deserialize) + derive(serde::Serialize, serde::Deserialize), + serde(untagged) )] -pub struct ScanPreference { - /// The ID of the scanner preference. - pub id: String, - /// The value of the scanner preference. - pub value: String, +pub enum PreferenceValue { + Bool(bool), + Int(i64), + String(String), +} + +impl Default for PreferenceValue { + fn default() -> Self { + Self::Int(0) + } +} + +impl Display for PreferenceValue { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + PreferenceValue::Bool(v) => write!(f, "{v}"), + PreferenceValue::Int(v) => write!(f, "{v}"), + PreferenceValue::String(v) => write!(f, "{v}"), + } + } } diff --git a/rust/openvas/src/pref_handler.rs b/rust/openvas/src/pref_handler.rs index 27534bd805..6ad3700b3a 100644 --- a/rust/openvas/src/pref_handler.rs +++ b/rust/openvas/src/pref_handler.rs @@ -342,7 +342,7 @@ where .scan_preferences .clone() .iter() - .map(|x| format!("{}|||{}", x.id, x.value)) + .map(|x| format!("{}|||{}", x.0, x.1)) .collect::>(); if options.is_empty() { diff --git a/rust/osp/src/commands.rs b/rust/osp/src/commands.rs index d9c09bcf4e..8123c955bd 100644 --- a/rust/osp/src/commands.rs +++ b/rust/osp/src/commands.rs @@ -181,14 +181,14 @@ fn write_vts(scan: &Scan, writer: &mut Writer) -> Result<()> { }) } -// In the openvasd API it is called scanner preferences while in the OSP side +// In the openvasd API it is called scan preferences while in the OSP side // it is called scanner parameters. fn write_scanner_prefs(scan: &Scan, writer: &mut Writer) -> Result<()> { writer.write_event(Event::Start(BytesStart::new("scanner_params")))?; for p in &scan.scan_preferences { - writer.write_event(Event::Start(BytesStart::new(&p.id)))?; - writer.write_event(Event::Text(BytesText::new(&p.value)))?; - writer.write_event(Event::End(BytesEnd::new(&p.id)))?; + writer.write_event(Event::Start(BytesStart::new(p.0)))?; + writer.write_event(Event::Text(BytesText::new(p.1.to_string().as_str())))?; + writer.write_event(Event::End(BytesEnd::new(p.0)))?; } writer.write_event(Event::End(BytesEnd::new("scanner_params")))?;