Skip to content

Commit

Permalink
Change: use a json dict instead of an array of id-value pairs for sca…
Browse files Browse the repository at this point in the history
…n_preferences
  • Loading branch information
Kraemii committed Apr 3, 2024
1 parent 093764d commit 5d6788f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 47 deletions.
16 changes: 7 additions & 9 deletions rust/doc/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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":
[
{
Expand Down
16 changes: 7 additions & 9 deletions rust/doc/reverse-sensor-openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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":
[
{
Expand Down
19 changes: 5 additions & 14 deletions rust/models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions rust/models/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -22,7 +24,7 @@ pub struct Scan {
serde(default, alias = "scanner_preferences")
)]
/// Configuration options for the scanner
pub scan_preferences: Vec<ScanPreference>,
pub scan_preferences: HashMap<String, PreferenceValue>,
/// List of VTs to execute for the target
pub vts: Vec<VT>,
}
34 changes: 26 additions & 8 deletions rust/models/src/scan_preference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"),
}
}
}
2 changes: 1 addition & 1 deletion rust/openvas/src/pref_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ where
.scan_preferences
.clone()
.iter()
.map(|x| format!("{}|||{}", x.id, x.value))
.map(|x| format!("{}|||{}", x.0, x.1))
.collect::<Vec<String>>();

if options.is_empty() {
Expand Down
8 changes: 4 additions & 4 deletions rust/osp/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))?;
Expand Down

0 comments on commit 5d6788f

Please sign in to comment.