11use crate :: {
2- data :: { MigrationTraitWithData , Sbom as SbomDoc , SchemaDataManager } ,
3- sbom ,
2+ advisory ,
3+ data :: { Advisory as AdvisoryDoc , MigrationTraitWithData , SchemaDataManager } ,
44} ;
5- use sea_orm:: { ActiveModelTrait , IntoActiveModel , Set } ;
5+ use sea_orm:: { EntityTrait , IntoActiveModel , sea_query :: extension :: postgres :: Type } ;
66use sea_orm_migration:: prelude:: * ;
7- use trustify_common:: advisory:: cyclonedx:: extract_properties_json;
7+ use strum:: VariantNames ;
8+ use trustify_entity:: { advisory, advisory_vulnerability_score} ;
9+ use trustify_module_ingestor:: { graph:: cvss:: ScoreCreator , service:: advisory:: osv:: extract_scores} ;
810
911#[ derive( DeriveMigrationName ) ]
1012pub struct Migration ;
@@ -13,47 +15,92 @@ pub struct Migration;
1315impl MigrationTraitWithData for Migration {
1416 async fn up ( & self , manager : & SchemaDataManager ) -> Result < ( ) , DbErr > {
1517 manager
16- . alter_table (
17- Table :: alter ( )
18- . table ( Sbom :: Table )
19- . add_column_if_not_exists (
20- ColumnDef :: new ( Sbom :: Properties )
21- . json ( )
22- . default ( serde_json:: Value :: Null )
23- . to_owned ( ) ,
24- )
18+ . create_type (
19+ Type :: create ( )
20+ . as_enum ( Severity :: Table )
21+ . values ( Severity :: VARIANTS . iter ( ) . skip ( 1 ) . copied ( ) )
2522 . to_owned ( ) ,
2623 )
2724 . await ?;
2825
2926 manager
30- . alter_table (
31- Table :: alter ( )
32- . table ( Sbom :: Table )
33- . modify_column ( ColumnDef :: new ( Sbom :: Properties ) . not_null ( ) . to_owned ( ) )
27+ . create_type (
28+ Type :: create ( )
29+ . as_enum ( ScoreType :: Table )
30+ . values ( ScoreType :: VARIANTS . iter ( ) . skip ( 1 ) . copied ( ) )
31+ . to_owned ( ) ,
32+ )
33+ . await ?;
34+
35+ manager
36+ . create_table (
37+ Table :: create ( )
38+ . table ( AdvisoryVulnerabilityScore :: Table )
39+ . if_not_exists ( )
40+ . col (
41+ ColumnDef :: new ( AdvisoryVulnerabilityScore :: Id )
42+ . uuid ( )
43+ . not_null ( )
44+ . primary_key ( )
45+ . to_owned ( ) ,
46+ )
47+ . col (
48+ ColumnDef :: new ( AdvisoryVulnerabilityScore :: AdvisoryId )
49+ . uuid ( )
50+ . not_null ( )
51+ . to_owned ( ) ,
52+ )
53+ . col (
54+ ColumnDef :: new ( AdvisoryVulnerabilityScore :: VulnerabilityId )
55+ . uuid ( )
56+ . not_null ( )
57+ . to_owned ( ) ,
58+ )
59+ . col (
60+ ColumnDef :: new ( AdvisoryVulnerabilityScore :: Type )
61+ . custom ( ScoreType :: Table )
62+ . not_null ( )
63+ . to_owned ( ) ,
64+ )
65+ . col (
66+ ColumnDef :: new ( AdvisoryVulnerabilityScore :: Vector )
67+ . string ( )
68+ . not_null ( )
69+ . to_owned ( ) ,
70+ )
71+ . col (
72+ ColumnDef :: new ( AdvisoryVulnerabilityScore :: Score )
73+ . float ( )
74+ . not_null ( )
75+ . to_owned ( ) ,
76+ )
77+ . col (
78+ ColumnDef :: new ( AdvisoryVulnerabilityScore :: Severity )
79+ . custom ( Severity :: Table )
80+ . not_null ( )
81+ . to_owned ( ) ,
82+ )
3483 . to_owned ( ) ,
3584 )
3685 . await ?;
3786
3887 manager
3988 . process (
4089 self ,
41- sbom ! ( async |sbom, model, tx| {
42- let mut model = model. into_active_model( ) ;
43- match sbom {
44- SbomDoc :: CycloneDx ( sbom) => {
45- model. properties = Set ( extract_properties_json( & sbom) ) ;
90+ advisory ! ( async |advisory, model, tx| {
91+ match advisory {
92+ AdvisoryDoc :: Cve ( advisory) => { }
93+ AdvisoryDoc :: Csaf ( advisory) => { }
94+ AdvisoryDoc :: Osv ( advisory) => {
95+ let mut creator = ScoreCreator :: new( model. id) ;
96+ extract_scores( & advisory, & mut creator) ;
97+ creator. create( tx) . await ?;
4698 }
47- SbomDoc :: Spdx ( _sbom) => {
48- model. properties = Set ( serde_json:: Value :: Object ( Default :: default ( ) ) ) ;
49- }
50- SbomDoc :: Other ( _) => {
99+ _ => {
51100 // we ignore others
52101 }
53102 }
54103
55- model. save( tx) . await ?;
56-
57104 Ok ( ( ) )
58105 } ) ,
59106 )
@@ -64,20 +111,60 @@ impl MigrationTraitWithData for Migration {
64111
65112 async fn down ( & self , manager : & SchemaDataManager ) -> Result < ( ) , DbErr > {
66113 manager
67- . alter_table (
68- Table :: alter ( )
69- . table ( Sbom :: Table )
70- . drop_column ( Sbom :: Properties )
114+ . drop_table (
115+ Table :: drop ( )
116+ . table ( AdvisoryVulnerabilityScore :: Table )
117+ . if_exists ( )
71118 . to_owned ( ) ,
72119 )
73120 . await ?;
74121
122+ manager
123+ . drop_type ( Type :: drop ( ) . if_exists ( ) . name ( "severity" ) . to_owned ( ) )
124+ . await ?;
125+
126+ manager
127+ . drop_type ( Type :: drop ( ) . if_exists ( ) . name ( "score_type" ) . to_owned ( ) )
128+ . await ?;
129+
75130 Ok ( ( ) )
76131 }
77132}
78133
79134#[ derive( DeriveIden ) ]
80- enum Sbom {
135+ enum AdvisoryVulnerabilityScore {
136+ Table ,
137+ Id ,
138+ AdvisoryId ,
139+ VulnerabilityId ,
140+ Type ,
141+ Vector ,
142+ Score ,
143+ Severity ,
144+ }
145+
146+ #[ derive( DeriveIden , strum:: VariantNames , strum:: Display ) ]
147+ #[ allow( unused) ]
148+ enum ScoreType {
149+ Table ,
150+ #[ strum( to_string = "2.0" ) ]
151+ V2_0 ,
152+ #[ strum( to_string = "3.0" ) ]
153+ V3_0 ,
154+ #[ strum( to_string = "3.1" ) ]
155+ V3_1 ,
156+ #[ strum( to_string = "4.0" ) ]
157+ V4_0 ,
158+ }
159+
160+ #[ derive( DeriveIden , strum:: VariantNames , strum:: Display ) ]
161+ #[ strum( serialize_all = "lowercase" ) ]
162+ #[ allow( unused) ]
163+ enum Severity {
81164 Table ,
82- Properties ,
165+ None ,
166+ Low ,
167+ Medium ,
168+ High ,
169+ Critical ,
83170}
0 commit comments