Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add three columns to advisory: #115

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions entity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ time = { workspace = true }
tokio = { workspace = true, features = ["full"] }
trustify-common = { path = "../common" }
trustify-cvss = { path = "../cvss" }
chrono = { workspace = true }

[dev-dependencies]
anyhow = { workspace = true }
3 changes: 3 additions & 0 deletions entity/src/advisory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub struct Model {
pub identifier: String,
pub location: String,
pub sha256: String,
pub published: Option<DateTimeUtc>,
pub modified: Option<DateTimeUtc>,
pub withdrawn: Option<DateTimeUtc>,
pub title: Option<String>,
}

Expand Down
6 changes: 6 additions & 0 deletions migration/src/m0000060_create_advisory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ impl MigrationTrait for Migration {
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Advisory::Published).timestamp_with_time_zone())
.col(ColumnDef::new(Advisory::Modified).timestamp_with_time_zone())
.col(ColumnDef::new(Advisory::Withdrawn).timestamp_with_time_zone())
.col(ColumnDef::new(Advisory::Identifier).string().not_null())
.col(ColumnDef::new(Advisory::Location).string().not_null())
.col(ColumnDef::new(Advisory::Sha256).string().not_null())
Expand All @@ -39,6 +42,9 @@ impl MigrationTrait for Migration {
pub enum Advisory {
Table,
Id,
Published,
Modified,
Withdrawn,
Identifier,
Location,
Sha256,
Expand Down
48 changes: 47 additions & 1 deletion modules/graph/src/graph/advisory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
use crate::graph::advisory::advisory_vulnerability::AdvisoryVulnerabilityContext;
use crate::graph::error::Error;
use crate::graph::Graph;
use sea_orm::prelude::DateTimeUtc;
use sea_orm::ActiveValue::Set;
use sea_orm::{ActiveModelTrait, EntityTrait, FromQueryResult, QueryFilter};
use sea_orm::{ActiveModelTrait, EntityTrait, FromQueryResult, IntoActiveModel, QueryFilter};
use sea_orm::{ColumnTrait, QuerySelect, RelationTrait};
use sea_query::{Condition, JoinType};
use std::cmp::min;
Expand Down Expand Up @@ -102,6 +103,51 @@ impl<'g> From<(&'g Graph, entity::advisory::Model)> for AdvisoryContext<'g> {
}

impl<'g> AdvisoryContext<'g> {
pub async fn set_published_at<TX: AsRef<Transactional>>(
&self,
published_at: DateTimeUtc,
tx: TX,
) -> Result<(), Error> {
let mut entity = self.advisory.clone().into_active_model();
entity.published = Set(Some(published_at));
entity.save(&self.graph.connection(&tx)).await?;
Ok(())
}

pub fn published_at(&self) -> Option<DateTimeUtc> {
self.advisory.published
}

pub async fn set_modified_at<TX: AsRef<Transactional>>(
&self,
modified_at: DateTimeUtc,
tx: TX,
) -> Result<(), Error> {
let mut entity = self.advisory.clone().into_active_model();
entity.modified = Set(Some(modified_at));
entity.save(&self.graph.connection(&tx)).await?;
Ok(())
}

pub fn modified_at(&self) -> Option<DateTimeUtc> {
self.advisory.modified
}

pub async fn set_withdrawn_at<TX: AsRef<Transactional>>(
&self,
withdrawn_at: DateTimeUtc,
tx: TX,
) -> Result<(), Error> {
let mut entity = self.advisory.clone().into_active_model();
entity.withdrawn = Set(Some(withdrawn_at));
entity.save(&self.graph.connection(&tx)).await?;
Ok(())
}

pub fn withdrawn_at(&self) -> Option<DateTimeUtc> {
self.advisory.withdrawn
}

pub async fn get_vulnerability<TX: AsRef<Transactional>>(
&self,
identifier: &str,
Expand Down
6 changes: 6 additions & 0 deletions modules/ingestor/src/service/advisory/osv/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ impl<'g> OsvLoader<'g> {
.ingest_advisory(osv.id, location, sha256, &tx)
.await?;

advisory.set_published_at(osv.published, &tx).await?;
advisory.set_modified_at(osv.modified, &tx).await?;
if let Some(withdrawn) = osv.withdrawn {
advisory.set_withdrawn_at(withdrawn, &tx).await?;
}

for cve_id in cve_ids {
let advisory_vuln = advisory.link_to_vulnerability(cve_id, &tx).await?;

Expand Down
Loading