-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Continue the -api/-graph more completely.
Ingest a CVE record as a vuln and an advisory.
- Loading branch information
Bob McWhirter
committed
Mar 14, 2024
1 parent
c39386a
commit e1cedcc
Showing
12 changed files
with
210 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
|
||
pub mod v5; | ||
pub mod v5; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use crate::cve::cve_record::v5::CveRecord; | ||
use crate::hashing::HashingRead; | ||
use crate::Error; | ||
use std::io::Read; | ||
use trustify_graph::db::Transactional; | ||
use trustify_graph::graph::Graph; | ||
|
||
/// Loader capable of parsing a CVE Record JSON file | ||
/// and manipulating the Graph to integrate it into | ||
/// the knowledge base. | ||
/// | ||
/// Should result in ensuring that a *vulnerability* | ||
/// related to the CVE Record exists in the graph, _along with_ | ||
/// also ensuring that the CVE *advisory* ends up also | ||
/// in the graph. | ||
pub struct CveLoader<'g> { | ||
graph: &'g Graph, | ||
} | ||
|
||
impl<'g> CveLoader<'g> { | ||
pub fn new(graph: &'g Graph) -> Self { | ||
Self { graph } | ||
} | ||
|
||
pub async fn load<L: Into<String>, R: Read>( | ||
&self, | ||
location: L, | ||
record: R, | ||
) -> Result<(), Error> { | ||
let mut reader = HashingRead::new(record); | ||
let cve: CveRecord = serde_json::from_reader(&mut reader)?; | ||
|
||
self.graph | ||
.ingest_vulnerability(cve.cve_metadata.cve_id(), Transactional::None) | ||
.await?; | ||
|
||
let hashes = reader.hashes(); | ||
|
||
let sha256 = hex::encode(hashes.sha256.as_ref()); | ||
|
||
self.graph | ||
.ingest_advisory( | ||
cve.cve_metadata.cve_id(), | ||
location, | ||
sha256, | ||
Transactional::None, | ||
) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::cve::loader::CveLoader; | ||
use std::fs::File; | ||
use std::path::PathBuf; | ||
use std::str::FromStr; | ||
use test_log::test; | ||
use trustify_graph::db::Transactional; | ||
use trustify_graph::graph::Graph; | ||
|
||
#[test(tokio::test)] | ||
async fn cve_loader() -> Result<(), anyhow::Error> { | ||
let graph = Graph::for_test("ingestors_cve_loader").await?; | ||
|
||
let pwd = PathBuf::from_str(env!("CARGO_MANIFEST_DIR"))?; | ||
let test_data = pwd.join("../etc/test-data/mitre"); | ||
|
||
let cve_json = test_data.join("CVE-2024-28111.json"); | ||
let cve_file = File::open(cve_json)?; | ||
|
||
let loaded_vulnerability = graph | ||
.get_vulnerability("CVE-2024-28111", Transactional::None) | ||
.await?; | ||
|
||
assert!(loaded_vulnerability.is_none()); | ||
|
||
let loaded_advisory = graph | ||
.get_advisory( | ||
"CVE-2024-28111", | ||
"CVE-2024-28111.json", | ||
"06908108e8097f2a56e628e7814a7bd54a5fc95f645b7c9fab02c1eb8dd9cc0c", | ||
) | ||
.await?; | ||
|
||
assert!(loaded_advisory.is_none()); | ||
|
||
let loader = CveLoader::new(&graph); | ||
|
||
loader.load("CVE-2024-28111.json", cve_file).await?; | ||
|
||
let loaded_vulnerability = graph | ||
.get_vulnerability("CVE-2024-28111", Transactional::None) | ||
.await?; | ||
|
||
assert!(loaded_vulnerability.is_some()); | ||
|
||
let loaded_advisory = graph | ||
.get_advisory( | ||
"CVE-2024-28111", | ||
"CVE-2024-28111.json", | ||
"06908108e8097f2a56e628e7814a7bd54a5fc95f645b7c9fab02c1eb8dd9cc0c", | ||
) | ||
.await?; | ||
|
||
assert!(loaded_advisory.is_some()); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,4 @@ | ||
use trustify_graph::graph::Graph; | ||
|
||
pub mod cve_record; | ||
pub mod loader; | ||
|
||
pub struct CveIngestor<'g> { | ||
graph: &'g Graph, | ||
|
||
} | ||
|
||
impl<'g> CveIngestor<'g> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use ring::digest::Context; | ||
use ring::digest::{Digest, SHA256}; | ||
use std::io::Read; | ||
|
||
pub struct HashingRead<R: Read> { | ||
inner: R, | ||
sha256: Context, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Hashes { | ||
pub sha256: Digest, | ||
} | ||
|
||
impl<R: Read> HashingRead<R> { | ||
pub fn new(inner: R) -> Self { | ||
Self { | ||
inner, | ||
sha256: Context::new(&SHA256), | ||
} | ||
} | ||
|
||
pub fn hashes(&self) -> Hashes { | ||
Hashes { | ||
sha256: self.sha256.clone().finish(), | ||
} | ||
} | ||
} | ||
|
||
impl<R: Read> Read for &mut HashingRead<R> { | ||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | ||
let len = self.inner.read(buf)?; | ||
self.sha256.update(&buf[0..len]); | ||
Ok(len) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,23 @@ | ||
pub mod cve; | ||
|
||
pub mod hashing; | ||
|
||
pub mod cve; | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
Json(serde_json::Error), | ||
#[error(transparent)] | ||
Graph(trustify_graph::graph::error::Error), | ||
} | ||
|
||
impl From<serde_json::Error> for Error { | ||
fn from(value: serde_json::Error) -> Self { | ||
Self::Json(value) | ||
} | ||
} | ||
|
||
impl From<trustify_graph::graph::error::Error> for Error { | ||
fn from(value: trustify_graph::graph::error::Error) -> Self { | ||
Self::Graph(value) | ||
} | ||
} |
Oops, something went wrong.