Skip to content

Commit

Permalink
fix: use the ingested column in the sbom query
Browse files Browse the repository at this point in the history
Relates #1077

Signed-off-by: Jim Crossley <[email protected]>
  • Loading branch information
jcrossley3 authored and ctron committed Dec 9, 2024
1 parent 7518ec7 commit 606f596
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
34 changes: 33 additions & 1 deletion modules/fundamental/src/sbom/endpoints/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use actix_http::StatusCode;
use actix_web::test::TestRequest;
use serde_json::Value;
use serde_json::{json, Value};
use test_context::test_context;
use test_log::test;
use trustify_common::{id::Id, model::PaginatedResults};
Expand Down Expand Up @@ -232,3 +232,35 @@ async fn get_advisories(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {

Ok(())
}

#[test_context(TrustifyContext)]
#[test(actix_web::test)]
async fn query_sboms_by_ingested_time(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
async fn query(app: &impl CallService, q: &str) -> Value {
let uri = format!("/api/v1/sbom?q={}", urlencoding::encode(q));
let req = TestRequest::get().uri(&uri).to_request();
app.call_and_read_body_json(req).await
}
let app = caller(ctx).await?;

// Ingest 2 sbom's, capturing the time between each ingestion
ctx.ingest_document("ubi9-9.2-755.1697625012.json").await?;
let t = chrono::Local::now().to_rfc3339();
ctx.ingest_document("zookeeper-3.9.2-cyclonedx.json")
.await?;

let all = query(&app, "ingested>yesterday").await;
let ubi = query(&app, &format!("ingested<{t}")).await;
let zoo = query(&app, &format!("ingested>{t}")).await;

log::debug!("{all:#?}");

// assert expected fields
assert_eq!(all["total"], 2);
assert_eq!(ubi["total"], 1);
assert_eq!(ubi["items"][0]["name"], json!("ubi9-container"));
assert_eq!(zoo["total"], 1);
assert_eq!(zoo["items"][0]["name"], json!("zookeeper"));

Ok(())
}
6 changes: 4 additions & 2 deletions modules/fundamental/src/sbom/service/sbom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use trustify_entity::{
qualified_purl::{self, CanonicalPurl, Qualifiers},
relationship::Relationship,
sbom::{self, SbomNodeLink},
sbom_node, sbom_package, sbom_package_cpe_ref, sbom_package_purl_ref, status, versioned_purl,
vulnerability,
sbom_node, sbom_package, sbom_package_cpe_ref, sbom_package_purl_ref, source_document, status,
versioned_purl, vulnerability,
};

impl SbomService {
Expand Down Expand Up @@ -111,11 +111,13 @@ impl SbomService {
sbom::Entity::find().filter(Expr::col(sbom::Column::Labels).contains(labels))
};
let limiter = query
.join(JoinType::Join, sbom::Relation::SourceDocument.def())
.find_also_linked(SbomNodeLink)
.filtering_with(
search,
Columns::from_entity::<sbom::Entity>()
.add_columns(sbom_node::Entity)
.add_columns(source_document::Entity)
.alias("sbom_node", "r0"),
)?
.limiting(connection, paginated.offset, paginated.limit);
Expand Down

0 comments on commit 606f596

Please sign in to comment.