@@ -15,8 +15,8 @@ use crate::domain::scanresult::{
1515    severity:: Severity , 
1616} ; 
1717
18- impl  From < & JsonScanResultV1 >  for  ScanResult  { 
19-     fn  from ( report :  & JsonScanResultV1 )  -> Self  { 
18+ impl  From < JsonScanResultV1 >  for  ScanResult  { 
19+     fn  from ( report :  JsonScanResultV1 )  -> Self  { 
2020        let  mut  scan_result = ScanResult :: from ( & report. result . metadata ) ; 
2121
2222        add_layers ( & report. result ,  & mut  scan_result) ; 
@@ -225,20 +225,20 @@ fn arch_from_str(string: &str) -> Architecture {
225225    } 
226226} 
227227
228- #[ derive( Debug ,  Deserialize ) ]  
228+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
229229pub ( super )  struct  JsonScanResultV1  { 
230230    pub  info :  JsonInfo , 
231231    pub  scanner :  JsonScanner , 
232232    pub  result :  JsonResult , 
233233} 
234234
235- #[ derive( Debug ,  Deserialize ) ]  
235+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
236236pub ( super )  struct  JsonScanner  { 
237237    pub  name :  String , 
238238    pub  version :  String , 
239239} 
240240
241- #[ derive( Debug ,  Deserialize ) ]  
241+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
242242pub ( super )  struct  JsonInfo  { 
243243    #[ serde( rename = "scanTime" ) ]  
244244    pub  scan_time :  DateTime < Utc > , 
@@ -283,7 +283,7 @@ pub enum ImageMetadataArchitecture {
283283} 
284284
285285#[ derive( Debug ,  Deserialize ,  PartialEq ,  Eq ,  Hash ,  Clone ) ]  
286- #[ serde( rename_all = "camelCase " ) ]  
286+ #[ serde( rename_all = "lowercase " ) ]  
287287pub  enum  JsonSeverity  { 
288288    Critical , 
289289    High , 
@@ -304,7 +304,7 @@ impl From<JsonSeverity> for Severity {
304304    } 
305305} 
306306
307- #[ derive( Debug ,  Deserialize ) ]  
307+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
308308pub ( super )  struct  JsonBundle  { 
309309    #[ serde( rename = "identifier" ,  default ) ]  
310310    pub  identifier :  String , 
@@ -316,15 +316,15 @@ pub(super) struct JsonBundle {
316316    pub  bundle_type :  String , 
317317} 
318318
319- #[ derive( Debug ,  Deserialize ) ]  
319+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
320320pub ( super )  struct  JsonCvssScore  { 
321321    pub  score :  f32 , 
322322    #[ serde( default ) ]   // FIXME(fede): test this 
323323    pub  vector :  String , 
324324    pub  version :  String , 
325325} 
326326
327- #[ derive( Debug ,  Deserialize ) ]  
327+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
328328pub ( super )  struct  JsonLayer  { 
329329    #[ serde( rename = "command" ,  default ) ]  
330330    pub  command :  Option < String > , 
@@ -336,7 +336,7 @@ pub(super) struct JsonLayer {
336336    pub  size :  Option < u64 > , 
337337} 
338338
339- #[ derive( Debug ,  Deserialize ) ]  
339+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
340340pub ( super )  struct  JsonPackage  { 
341341    #[ serde( rename = "isRemoved" ,  default ) ]  
342342    pub  is_removed :  bool , 
@@ -400,15 +400,15 @@ impl From<JsonPackageType> for PackageType {
400400    } 
401401} 
402402
403- #[ derive( Debug ,  Deserialize ,  Default ) ]  
403+ #[ derive( Debug ,  Deserialize ,  Default ,   Clone ) ]  
404404pub ( super )  struct  JsonPolicies  { 
405405    #[ serde( rename = "globalEvaluation" ,  default ) ]  
406406    pub  global_evaluation :  String , 
407407    #[ serde( rename = "evaluations" ,  default ) ]  
408408    pub  evaluations :  Option < Vec < JsonPolicy > > , 
409409} 
410410
411- #[ derive( Debug ,  Deserialize ) ]  
411+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
412412pub ( super )  struct  JsonPolicy  { 
413413    #[ serde( rename = "bundles" ,  default ) ]  
414414    pub  bundles :  Option < Vec < JsonBundle > > , 
@@ -426,13 +426,13 @@ pub(super) struct JsonPolicy {
426426    pub  updated_at :  DateTime < Utc > , 
427427} 
428428
429- #[ derive( Debug ,  Deserialize ,  Default ) ]  
429+ #[ derive( Debug ,  Deserialize ,  Default ,   Clone ) ]  
430430pub ( super )  struct  JsonProducer  { 
431431    #[ serde( rename = "producedAt" ,  default ) ]  
432432    pub  produced_at :  DateTime < Utc > , 
433433} 
434434
435- #[ derive( Debug ,  Deserialize ) ]  
435+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
436436pub ( super )  struct  JsonRiskAccept  { 
437437    #[ serde( rename = "createdAt" ) ]  
438438    pub  created_at :  DateTime < Utc > , 
@@ -479,7 +479,7 @@ impl From<JsonRiskAcceptReason> for AcceptedRiskReason {
479479        } 
480480    } 
481481} 
482- #[ derive( Debug ,  Deserialize ) ]  
482+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
483483pub ( super )  struct  JsonRule  { 
484484    #[ serde( rename = "description" ) ]  
485485    pub  description :  String , 
@@ -495,7 +495,7 @@ pub(super) struct JsonRule {
495495    pub  rule_type :  String , 
496496} 
497497
498- #[ derive( Debug ,  Deserialize ) ]  
498+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
499499pub ( super )  struct  JsonFailure  { 
500500    #[ serde( rename = "remediation" ,  default ) ]  
501501    pub  remediation :  String , 
@@ -505,7 +505,7 @@ pub(super) struct JsonFailure {
505505    pub  vulnerability_ref :  String , 
506506} 
507507
508- #[ derive( Debug ,  Deserialize ) ]  
508+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
509509pub ( super )  struct  JsonResult  { 
510510    #[ serde( rename = "assetType" ) ]  
511511    pub  asset_type :  String , 
@@ -527,7 +527,7 @@ pub(super) struct JsonResult {
527527    pub  vulnerabilities :  HashMap < String ,  JsonVulnerability > , 
528528} 
529529
530- #[ derive( Debug ,  Deserialize ) ]  
530+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
531531pub ( super )  struct  JsonMetadata  { 
532532    #[ serde( rename = "architecture" ) ]  
533533    pub  architecture :  String , 
@@ -551,7 +551,7 @@ pub(super) struct JsonMetadata {
551551    pub  size :  u64 , 
552552} 
553553
554- #[ derive( Debug ,  Deserialize ) ]  
554+ #[ derive( Debug ,  Deserialize ,   Clone ) ]  
555555pub ( super )  struct  JsonVulnerability  { 
556556    #[ serde( rename = "cvssScore" ) ]  
557557    pub  cvss_score :  JsonCvssScore , 
@@ -574,3 +574,62 @@ pub(super) struct JsonVulnerability {
574574    #[ serde( rename = "solutionDate" ,  default ) ]  
575575    pub  solution_date :  Option < NaiveDate > , 
576576} 
577+ 
578+ #[ cfg( test) ]  
579+ mod  tests { 
580+     use  crate :: { 
581+         domain:: scanresult:: { scan_result:: ScanResult ,  severity:: Severity } , 
582+         infra:: sysdig_image_scanner_json_scan_result_v1:: JsonScanResultV1 , 
583+     } ; 
584+ 
585+     #[ test]  
586+     fn  it_loads_postgres13 ( )  { 
587+         let  postgres_13_json = include_bytes ! ( "../../tests/fixtures/scan-results/postgres_13.json" ) ; 
588+         let  json_scan_result:  JsonScanResultV1  = serde_json:: from_slice ( postgres_13_json) . unwrap ( ) ; 
589+ 
590+         let  scan_result:  ScanResult  = json_scan_result. clone ( ) . into ( ) ; 
591+ 
592+         assert_eq ! ( json_scan_result. result. vulnerabilities. len( ) ,  100 ) ; 
593+         assert_eq ! ( 
594+             scan_result
595+                 . vulnerabilities( ) 
596+                 . iter( ) 
597+                 . filter( |v| v. severity( )  == Severity :: Critical ) 
598+                 . count( ) , 
599+             2 
600+         ) ; 
601+         assert_eq ! ( 
602+             scan_result
603+                 . vulnerabilities( ) 
604+                 . iter( ) 
605+                 . filter( |v| v. severity( )  == Severity :: High ) 
606+                 . count( ) , 
607+             3 
608+         ) ; 
609+         assert_eq ! ( 
610+             scan_result
611+                 . vulnerabilities( ) 
612+                 . iter( ) 
613+                 . filter( |v| v. severity( )  == Severity :: Medium ) 
614+                 . count( ) , 
615+             1 
616+         ) ; 
617+         assert_eq ! ( 
618+             scan_result
619+                 . vulnerabilities( ) 
620+                 . iter( ) 
621+                 . filter( |v| v. severity( )  == Severity :: Low ) 
622+                 . count( ) , 
623+             2 
624+         ) ; 
625+         assert_eq ! ( 
626+             scan_result
627+                 . vulnerabilities( ) 
628+                 . iter( ) 
629+                 . filter( |v| v. severity( )  == Severity :: Negligible ) 
630+                 . count( ) , 
631+             32 
632+         ) ; 
633+         // assert_eq!(scan_result.vulnerabilities().len(), 97); 
634+     } 
635+ } 
0 commit comments