Skip to content

Commit

Permalink
Add: notus executable
Browse files Browse the repository at this point in the history
  • Loading branch information
Kraemii committed Nov 10, 2023
1 parent dcf07bc commit f37c0c8
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 14 deletions.
1 change: 1 addition & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion rust/notus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ edition = "2021"
[dependencies]
regex = "1.10.2"
serde_json = "1.0.96"
serde = { version = "1.0.163", features = ["derive"] }
serde = { version = "1.0.163" }
clap = { version = "~4" }

models = { path = "../models" }
18 changes: 9 additions & 9 deletions rust/notus/src/loader/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use crate::error::Error;
use super::AdvisoriesLoader;

#[derive(Debug)]
pub struct JSONAdvisoriesLoader<P>
pub struct JSONAdvisoryLoader<P>
where
P: AsRef<Path>,
{
path: P,
}

impl<P> JSONAdvisoriesLoader<P>
impl<P> JSONAdvisoryLoader<P>
where
P: AsRef<Path>,
{
Expand All @@ -43,7 +43,7 @@ where
}
}

impl<P> AdvisoriesLoader for JSONAdvisoriesLoader<P>
impl<P> AdvisoriesLoader for JSONAdvisoryLoader<P>
where
P: AsRef<Path>,
{
Expand Down Expand Up @@ -75,13 +75,13 @@ mod tests {

use crate::{error::Error, loader::AdvisoriesLoader};

use super::JSONAdvisoriesLoader;
use super::JSONAdvisoryLoader;

#[test]
fn test_load_advisories() {
let mut path = env!("CARGO_MANIFEST_DIR").to_string();
path.push_str("/data");
let loader = JSONAdvisoriesLoader::new(path).unwrap();
let loader = JSONAdvisoryLoader::new(path).unwrap();
let _ = loader.load_package_advisories("debian_10").unwrap();
}

Expand All @@ -90,7 +90,7 @@ mod tests {
let mut path = env!("CARGO_MANIFEST_DIR").to_string();
path.push_str("/data_foo");
assert!(
matches!(JSONAdvisoriesLoader::new(path.clone()).expect_err("Should fail"), Error::MissingAdvisoryDir(p) if p == path)
matches!(JSONAdvisoryLoader::new(path.clone()).expect_err("Should fail"), Error::MissingAdvisoryDir(p) if p == path)
);
}

Expand All @@ -99,15 +99,15 @@ mod tests {
let mut path = env!("CARGO_MANIFEST_DIR").to_string();
path.push_str("/data/debian_10.notus");
assert!(
matches!(JSONAdvisoriesLoader::new(path.clone()).expect_err("Should fail"), Error::AdvisoryDirIsFile(p) if p == path)
matches!(JSONAdvisoryLoader::new(path.clone()).expect_err("Should fail"), Error::AdvisoryDirIsFile(p) if p == path)
);
}

#[test]
fn test_err_unknown_os() {
let mut path = env!("CARGO_MANIFEST_DIR").to_string();
path.push_str("/data");
let loader = JSONAdvisoriesLoader::new(path).unwrap();
let loader = JSONAdvisoryLoader::new(path).unwrap();

let os = "foo";
assert!(
Expand All @@ -119,7 +119,7 @@ mod tests {
fn test_err_json_parse() {
let mut path = env!("CARGO_MANIFEST_DIR").to_string();
path.push_str("/data");
let loader = JSONAdvisoriesLoader::new(path.clone()).unwrap();
let loader = JSONAdvisoryLoader::new(path.clone()).unwrap();

let os = "debian_10_json_parse_err";
assert!(
Expand Down
70 changes: 70 additions & 0 deletions rust/notus/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-FileCopyrightText: 2023 Greenbone AG
//
// SPDX-License-Identifier: GPL-2.0-or-later

use std::{fs::File, io::Read, path::PathBuf};

use clap::{arg, value_parser, ArgAction, Command};
use notus::{loader::json::JSONAdvisoryLoader, notus::Notus};

fn main() {
let matches = Command::new("nasl-cli")
.version("1.0")
.about("Is a CLI tool around Notus.")
.arg(
arg!(-p --path <FILE> "Path to the notus advisories.")
.required(true)
.value_parser(value_parser!(PathBuf)),
)
.arg(arg!(-s --os <STRING> "To the packages corresponding operating system").required(true))
.arg(
arg!(-f --"pkg-file" <FILE> "Path to the notus packages to check for vulnerabilities, the file should contain a comma separated list of packages")
.required_unless_present("pkg-list")
.conflicts_with("pkg-list")
.value_parser(value_parser!(PathBuf)),
)
.arg(
arg!(-l --"pkg-list" <STRING> "A comma separated list of packages to check for vulnerabilities")
.required_unless_present("pkg-file"),
)
.arg(
arg!(-n --pretty "Enables pretty printing for the result").action(ArgAction::SetTrue)
)
.get_matches();

let advisory_path = matches.get_one::<PathBuf>("path").unwrap();
let loader = match JSONAdvisoryLoader::new(advisory_path) {
Ok(loader) => loader,
Err(err) => {
eprintln!("{err}");
return;
}
};

let packages = match matches.get_one::<PathBuf>("pkg-file") {
Some(path) => {
let mut buf = String::new();
File::open(path).unwrap().read_to_string(&mut buf).unwrap();
buf.split(",").map(str::to_string).collect::<Vec<String>>()
}
None => {
let list = matches.get_one::<String>("pkg-list").unwrap();
list.split(",").map(str::to_string).collect::<Vec<String>>()
}
};

let os = matches.get_one::<String>("os").unwrap();

let mut notus = Notus::new(loader);
match notus.scan(os, &packages) {
Ok(results) => {
let json = match matches.contains_id("pretty") {
true => serde_json::to_string_pretty(&results).unwrap(),
false => serde_json::to_string(&results).unwrap(),
};

println!("{json}");
}
Err(err) => eprintln!("{err}"),
}
}
8 changes: 4 additions & 4 deletions rust/notus/tests/notus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
mod tests {

use models::{FixedPackage, FixedVersion, Specifier};
use notus::{error::Error, loader::json::JSONAdvisoriesLoader, notus::Notus};
use notus::{error::Error, loader::json::JSONAdvisoryLoader, notus::Notus};

#[test]
fn test_notus() {
let mut path = env!("CARGO_MANIFEST_DIR").to_string();
path.push_str("/data");
let loader = JSONAdvisoriesLoader::new(path.clone()).unwrap();
let loader = JSONAdvisoryLoader::new(path.clone()).unwrap();
let mut notus = Notus::new(loader);

let packages = vec![
Expand Down Expand Up @@ -65,7 +65,7 @@ mod tests {
fn test_err_package_parse_error() {
let mut path = env!("CARGO_MANIFEST_DIR").to_string();
path.push_str("/data");
let loader = JSONAdvisoriesLoader::new(path.clone()).unwrap();
let loader = JSONAdvisoryLoader::new(path.clone()).unwrap();
let mut notus = Notus::new(loader);

let pkg_name = "wepofkewf~.124.sdefpo3-_~s#";
Expand All @@ -82,7 +82,7 @@ mod tests {
fn test_err_advisory_parse_error() {
let mut path = env!("CARGO_MANIFEST_DIR").to_string();
path.push_str("/data");
let loader = JSONAdvisoriesLoader::new(path.clone()).unwrap();
let loader = JSONAdvisoryLoader::new(path.clone()).unwrap();
let mut notus = Notus::new(loader);

let packages = vec![];
Expand Down

0 comments on commit f37c0c8

Please sign in to comment.