diff --git a/rust/doc/openapi.yml b/rust/doc/openapi.yml index 87c8e59070..a579b1440f 100644 --- a/rust/doc/openapi.yml +++ b/rust/doc/openapi.yml @@ -414,11 +414,9 @@ components: properties: target: $ref: "#/components/schemas/Target" - scanner_preferences: - description: "Overwrite the default settings of the Scanner." - type: "array" - items: - $ref: "#/components/schemas/ScannerPreference" + scan_preferences: + 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." @@ -445,11 +443,11 @@ components: $ref: "#/components/schemas/ScanID" target: $ref: "#/components/schemas/Target" - scanner_preferences: + scan_preferences: description: "Overwrite the default settings of the Scanner." type: "array" items: - $ref: "#/components/schemas/ScannerPreference" + $ref: "#/components/schemas/ScanPreference" vts: type: "array" description: "A collection of VTs, which are run for the given target." @@ -634,7 +632,7 @@ components: - aes - des - ScannerPreference: + ScanPreference: description: "Consists of a preference ID and its value." type: "object" properties: @@ -897,10 +895,7 @@ components: "2002::1234:abcd:ffff:c0a8:101/64", "examplehost", ], - "excluded_hosts": - [ - "192.168.0.14" - ], + "excluded_hosts": ["192.168.0.14"], "ports": [ { @@ -949,12 +944,12 @@ components: "reverse_lookup_unify": true, "reverse_lookup_only": false, }, - "scanner_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": [ { @@ -1030,7 +1025,7 @@ components: "reverse_lookup_unify": true, "reverse_lookup_only": false, }, - "scanner_preferences": + "scan_preferences": [ { "id": "target_port", "value": "443" }, { "id": "use_https", "value": "1" }, diff --git a/rust/doc/reverse-sensor-openapi.yml b/rust/doc/reverse-sensor-openapi.yml index 98b2245c94..4fe7aaf6e9 100644 --- a/rust/doc/reverse-sensor-openapi.yml +++ b/rust/doc/reverse-sensor-openapi.yml @@ -152,22 +152,22 @@ paths: parameters: - $ref: "#/components/parameters/ScanID" requestBody: - description: "The new status" - content: - application/json: + description: "The new status" + content: + application/json: + schema: + $ref: "#/components/schemas/Status" + examples: schema: - $ref: "#/components/schemas/Status" - examples: - schema: - description: "Schema of a status response." - status of a stored scan: - $ref: "#/components/examples/scan_status_stored" - status of a running scan: - $ref: "#/components/examples/scan_status_running" - status of a succeeded scan: - $ref: "#/components/examples/scan_status_success" - status of a failed scan: - $ref: "#/components/examples/scan_status_fail" + description: "Schema of a status response." + status of a stored scan: + $ref: "#/components/examples/scan_status_stored" + status of a running scan: + $ref: "#/components/examples/scan_status_running" + status of a succeeded scan: + $ref: "#/components/examples/scan_status_success" + status of a failed scan: + $ref: "#/components/examples/scan_status_fail" responses: "204": description: "Status received" @@ -228,11 +228,9 @@ components: $ref: "#/components/schemas/ScanID" target: $ref: "#/components/schemas/Target" - scanner_preferences: - description: "Overwrite the default settings of the Scanner." - type: "array" - items: - $ref: "#/components/schemas/ScannerPreference" + scan_preferences: + 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." @@ -396,7 +394,7 @@ components: - aes - des - ScannerPreference: + ScanPreference: description: "Consists of a preference ID and its value." type: "object" properties: @@ -573,14 +571,10 @@ components: items: $ref: "#/components/schemas/ScanAction" - examples: sensor_simple: description: "A simple example for sensor." - value: - { - "sensor_id": "6c591f83-8f7b-452a-8c78-ba35779e682f" - } + value: { "sensor_id": "6c591f83-8f7b-452a-8c78-ba35779e682f" } scan_simple: description: "A simple example for creating a scan." value: @@ -656,12 +650,12 @@ components: "reverse_lookup_unify": true, "reverse_lookup_only": false, }, - "scanner_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": [ { @@ -677,19 +671,20 @@ components: value: "6c591f83-8f7b-452a-8c78-ba35779e682f" scan_actions: description: "Actions to perform" - value: [ + value: + [ { "scan_id": "6c591f83-8f7b-452a-8c78-ba35779e682f", - "action": "start" + "action": "start", }, { "scan_id": "24591f83-8f7b-452a-8c78-ba35779e6816", - "action": "stop" + "action": "stop", }, { "scan_id": "13591f83-8f74-45da-8c7d-ba35779e682a", - "action": "delete" - } + "action": "delete", + }, ] scan_results: diff --git a/rust/models/src/lib.rs b/rust/models/src/lib.rs index 6c4eec8a64..f535a075b8 100644 --- a/rust/models/src/lib.rs +++ b/rust/models/src/lib.rs @@ -11,8 +11,8 @@ mod product; mod result; mod scan; mod scan_action; +mod scan_preference; pub mod scanner; -mod scanner_preference; mod status; mod target; mod vt; @@ -26,7 +26,7 @@ pub use product::*; pub use result::*; pub use scan::*; pub use scan_action::*; -pub use scanner_preference::*; +pub use scan_preference::*; pub use status::*; pub use target::*; pub use vt::*; @@ -137,20 +137,11 @@ mod tests { "reverse_lookup_unify": true, "reverse_lookup_only": false }, - "scanner_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 ce5283330c..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::{scanner_preference::ScannerPreference, 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)] @@ -17,9 +19,12 @@ pub struct Scan { pub scan_id: String, /// Information about the target to scan pub target: Target, - #[cfg_attr(feature = "serde_support", serde(default))] + #[cfg_attr( + feature = "serde_support", + serde(default, alias = "scanner_preferences") + )] /// Configuration options for the scanner - pub scanner_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 new file mode 100644 index 0000000000..2599bb26f3 --- /dev/null +++ b/rust/models/src/scan_preference.rs @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: 2023 Greenbone AG +// +// SPDX-License-Identifier: GPL-2.0-or-later + +use std::fmt::Display; + +/// Preference value +#[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr( + feature = "serde_support", + derive(serde::Serialize, serde::Deserialize), + serde(untagged) +)] +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/models/src/scanner_preference.rs b/rust/models/src/scanner_preference.rs deleted file mode 100644 index 5e1a461b28..0000000000 --- a/rust/models/src/scanner_preference.rs +++ /dev/null @@ -1,16 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Greenbone AG -// -// SPDX-License-Identifier: GPL-2.0-or-later - -/// Configuration preference for the scanner -#[derive(Default, Debug, Clone, PartialEq, Eq)] -#[cfg_attr( - feature = "serde_support", - derive(serde::Serialize, serde::Deserialize) -)] -pub struct ScannerPreference { - /// The ID of the scanner preference. - pub id: String, - /// The value of the scanner preference. - pub value: String, -} diff --git a/rust/openvas/src/pref_handler.rs b/rust/openvas/src/pref_handler.rs index 12a6cb2a9d..6ad3700b3a 100644 --- a/rust/openvas/src/pref_handler.rs +++ b/rust/openvas/src/pref_handler.rs @@ -339,10 +339,10 @@ where async fn prepare_scan_params_for_openvas(&mut self) -> RedisStorageResult<()> { let options = self .scan_config - .scanner_preferences + .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/openvasd/src/main.rs b/rust/openvasd/src/main.rs index 4e03064f01..b396acb6d3 100644 --- a/rust/openvasd/src/main.rs +++ b/rust/openvasd/src/main.rs @@ -13,6 +13,7 @@ pub mod controller; pub mod crypt; pub mod feed; pub mod notus; +pub mod preference; pub mod request; pub mod response; mod scheduling; diff --git a/rust/osp/src/commands.rs b/rust/osp/src/commands.rs index 9a7c9c7e56..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.scanner_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)))?; + for p in &scan.scan_preferences { + 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")))?;