Skip to main content

csaf_walker/verification/check/
mod.rs

1use crate::verification::check::{
2    base::{check_csaf_base, check_csaf_document_tracking_revision_history},
3    informational_advisory::check_vulnerabilities_not_exits,
4    security_incident_response::{check_csaf_document_notes, check_csaf_document_references},
5    vex::{
6        check_all_products_v11ies_exits_in_product_tree,
7        check_branches_relationships_product_match, check_csaf_vex, check_history,
8        check_vulnerabilities_cve_ids, check_vulnerabilities_product_status,
9        check_vulnerabilities_size,
10    },
11};
12use async_trait::async_trait;
13use csaf::Csaf;
14use std::borrow::Cow;
15
16pub mod base;
17pub mod informational_advisory;
18pub mod security_advisory;
19pub mod security_incident_response;
20pub mod vex;
21
22pub type CheckError = Cow<'static, str>;
23
24#[async_trait(?Send)]
25pub trait Check {
26    /// Perform a check on a CSAF document
27    async fn check(&self, csaf: &Csaf) -> anyhow::Result<Vec<CheckError>>;
28}
29
30/// Implementation to allow a simple function style check
31#[async_trait(?Send)]
32impl<F> Check for F
33where
34    F: Fn(&Csaf) -> Vec<CheckError>,
35{
36    async fn check(&self, csaf: &Csaf) -> anyhow::Result<Vec<CheckError>> {
37        Ok((self)(csaf))
38    }
39}
40
41#[derive(Debug, Default)]
42pub struct Checking {
43    results: Vec<CheckError>,
44}
45
46impl Checking {
47    pub fn new() -> Self {
48        Default::default()
49    }
50
51    pub fn require(mut self, msg: impl Into<CheckError>, ok: bool) -> Self {
52        if !ok {
53            self.results.push(msg.into());
54        }
55        self
56    }
57
58    pub fn done(self) -> Vec<CheckError> {
59        self.results
60    }
61}
62
63pub fn init_verifying_visitor() -> Vec<(&'static str, Box<dyn Check>)> {
64    vec![
65        (
66            "check_vulnerabilities_not_exits",
67            Box::new(check_vulnerabilities_not_exits),
68        ),
69        (
70            "check_csaf_document_notes",
71            Box::new(check_csaf_document_notes),
72        ),
73        (
74            "check_csaf_document_references",
75            Box::new(check_csaf_document_references),
76        ),
77        ("check_csaf_base", Box::new(check_csaf_base)),
78        (
79            "check_csaf_document_tracking_revision_history",
80            Box::new(check_csaf_document_tracking_revision_history),
81        ),
82        (
83            "check_vulnerabilities_size",
84            Box::new(check_vulnerabilities_size),
85        ),
86        (
87            "check_vulnerabilities_product_status",
88            Box::new(check_vulnerabilities_product_status),
89        ),
90        (
91            "check_vulnerabilities_cve_ids",
92            Box::new(check_vulnerabilities_cve_ids),
93        ),
94        (
95            "check_all_products_v11ies_exits_in_product_tree",
96            Box::new(check_all_products_v11ies_exits_in_product_tree),
97        ),
98        ("check_history", Box::new(check_history)),
99        ("check_csaf_vex", Box::new(check_csaf_vex)),
100        (
101            "check_branches_relationships_product_match",
102            Box::new(check_branches_relationships_product_match),
103        ),
104    ]
105}