1+
12use crate :: {
23 graph:: { Graph , Outcome , sbom:: cyclonedx} ,
34 model:: IngestResult ,
45 service:: { Error , Warnings } ,
56} ;
67use sea_orm:: TransactionTrait ;
8+ use serde_cyclonedx:: cyclonedx:: v_1_6:: Component ;
79use tracing:: instrument;
810use trustify_common:: { hashing:: Digests , id:: Id } ;
911use trustify_entity:: labels:: Labels ;
@@ -17,6 +19,11 @@ impl<'g> CyclonedxLoader<'g> {
1719 Self { graph }
1820 }
1921
22+ /*pub fn aiType() -> bool{
23+
24+ false
25+ }*/
26+
2027 #[ instrument( skip( self , buffer) , err( level=tracing:: Level :: INFO ) ) ]
2128 pub async fn load (
2229 & self ,
@@ -29,7 +36,7 @@ impl<'g> CyclonedxLoader<'g> {
2936 let cdx: Box < serde_cyclonedx:: cyclonedx:: v_1_6:: CycloneDx > = serde_json:: from_slice ( buffer)
3037 . map_err ( |err| Error :: UnsupportedFormat ( format ! ( "Failed to parse: {err}" ) ) ) ?;
3138
32- let labels = labels . add ( "type" , "cyclonedx" ) ;
39+ let labels = labels_with_ai_type_check ( cdx . components . clone ( ) ) ;
3340
3441 log:: info!(
3542 "Storing - version: {:?}, serialNumber: {:?}" ,
@@ -75,6 +82,23 @@ impl<'g> CyclonedxLoader<'g> {
7582 }
7683}
7784
85+
86+ fn labels_with_ai_type_check ( components : Option < Vec < Component > > ) -> Labels {
87+ match components {
88+ Some ( vec) => {
89+ for component in vec {
90+ if component. type_ == "machine-learning-model" {
91+ return Labels :: new ( ) . add ( "type" , "cyclonedx" ) . add ( "ai" , "machine-learning-model" ) ;
92+ }
93+ }
94+ }
95+ None => {
96+ return Labels :: new ( ) . add ( "type" , "cyclonedx" ) ;
97+ }
98+ }
99+ Labels :: new ( ) . add ( "type" , "cyclonedx" )
100+ }
101+
78102#[ cfg( test) ]
79103mod test {
80104 use crate :: service:: { Cache , IngestorService } ;
@@ -105,4 +129,28 @@ mod test {
105129
106130 Ok ( ( ) )
107131 }
132+
133+
134+ #[ test_context( TrustifyContext ) ]
135+ #[ test( tokio:: test) ]
136+ async fn ingest_ai_cyclonedx ( ctx : & TrustifyContext ) -> Result < ( ) , anyhow:: Error > {
137+ let db = & ctx. db ;
138+ let graph = Graph :: new ( db. clone ( ) ) ;
139+ let data = document_bytes ( "cyclonedx/ai/nvidia_canary-1b-v2_aibom.json" ) . await ?;
140+
141+ let ingestor = IngestorService :: new ( graph, ctx. storage . clone ( ) , Default :: default ( ) ) ;
142+
143+ ingestor
144+ . ingest (
145+ & data,
146+ Format :: CycloneDX ,
147+ [ ( "type" , "cyclonedx" ) , ( "ai" , "machine-learning-model" ) ] ,
148+ None ,
149+ Cache :: Skip ,
150+ )
151+ . await
152+ . expect ( "must ingest" ) ;
153+
154+ Ok ( ( ) )
155+ }
108156}
0 commit comments