Skip to content

Commit d105547

Browse files
committed
fix: source document id must be non-null
(cherry picked from commit 877a6b8)
1 parent 9eb8c82 commit d105547

File tree

20 files changed

+128
-112
lines changed

20 files changed

+128
-112
lines changed

entity/src/advisory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct Model {
3131
pub withdrawn: Option<OffsetDateTime>,
3232
pub title: Option<String>,
3333
pub labels: Labels,
34-
pub source_document_id: Option<Uuid>,
34+
pub source_document_id: Uuid,
3535
}
3636

3737
#[cfg(feature = "async-graphql")]

entity/src/sbom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct Model {
1919
pub suppliers: Vec<String>,
2020
pub data_licenses: Vec<String>,
2121

22-
pub source_document_id: Option<Uuid>,
22+
pub source_document_id: Uuid,
2323

2424
#[cfg_attr(
2525
feature = "async-graphql",

migration/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod m0001130_gover_cmp;
2828
mod m0001140_expand_spdx_licenses_function;
2929
mod m0001150_case_license_text_sbom_id_function;
3030
mod m0001160_improve_expand_spdx_licenses_function;
31+
mod m0001170_non_null_source_document_id;
3132

3233
pub struct Migrator;
3334

@@ -63,6 +64,7 @@ impl MigratorTrait for Migrator {
6364
Box::new(m0001140_expand_spdx_licenses_function::Migration),
6465
Box::new(m0001150_case_license_text_sbom_id_function::Migration),
6566
Box::new(m0001160_improve_expand_spdx_licenses_function::Migration),
67+
Box::new(m0001170_non_null_source_document_id::Migration),
6668
]
6769
}
6870
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use sea_orm_migration::prelude::*;
2+
3+
#[derive(DeriveMigrationName)]
4+
pub struct Migration;
5+
6+
#[async_trait::async_trait]
7+
impl MigrationTrait for Migration {
8+
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
9+
manager
10+
.alter_table(
11+
Table::alter()
12+
.table(Advisory::Table)
13+
.modify_column(ColumnDef::new(Advisory::SourceDocumentId).not_null())
14+
.to_owned(),
15+
)
16+
.await?;
17+
18+
manager
19+
.alter_table(
20+
Table::alter()
21+
.table(Sbom::Table)
22+
.modify_column(ColumnDef::new(Sbom::SourceDocumentId).not_null())
23+
.to_owned(),
24+
)
25+
.await?;
26+
27+
Ok(())
28+
}
29+
30+
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
31+
manager
32+
.alter_table(
33+
Table::alter()
34+
.table(Sbom::Table)
35+
.modify_column(ColumnDef::new(Sbom::SourceDocumentId).null())
36+
.to_owned(),
37+
)
38+
.await?;
39+
40+
manager
41+
.alter_table(
42+
Table::alter()
43+
.table(Advisory::Table)
44+
.modify_column(ColumnDef::new(Advisory::SourceDocumentId).null())
45+
.to_owned(),
46+
)
47+
.await?;
48+
49+
Ok(())
50+
}
51+
}
52+
53+
#[derive(Iden)]
54+
enum Advisory {
55+
Table,
56+
SourceDocumentId,
57+
}
58+
59+
#[derive(Iden)]
60+
enum Sbom {
61+
Table,
62+
SourceDocumentId,
63+
}

modules/fundamental/src/advisory/endpoints/mod.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub async fn delete(
157157
false => Ok(HttpResponse::NotFound().finish()),
158158
true => {
159159
tx.commit().await?;
160-
if let Err(e) = delete_doc(v.source_document.as_ref(), i.storage()).await {
160+
if let Err(e) = delete_doc(&v.source_document, i.storage()).await {
161161
log::warn!("Ignoring {e}");
162162
}
163163
Ok(HttpResponse::Ok().json(v))
@@ -253,19 +253,15 @@ pub async fn download(
253253
return Ok(HttpResponse::NotFound().finish());
254254
};
255255

256-
if let Some(doc) = &advisory.source_document {
257-
let stream = ingestor
258-
.storage()
259-
.retrieve(doc.try_into()?)
260-
.await
261-
.map_err(Error::Storage)?
262-
.map(|stream| stream.map_err(Error::Storage));
256+
let stream = ingestor
257+
.storage()
258+
.retrieve(advisory.source_document.try_into()?)
259+
.await
260+
.map_err(Error::Storage)?
261+
.map(|stream| stream.map_err(Error::Storage));
263262

264-
Ok(match stream {
265-
Some(s) => HttpResponse::Ok().streaming(s),
266-
None => HttpResponse::NotFound().finish(),
267-
})
268-
} else {
269-
Ok(HttpResponse::NotFound().finish())
270-
}
263+
Ok(match stream {
264+
Some(s) => HttpResponse::Ok().streaming(s),
265+
None => HttpResponse::NotFound().finish(),
266+
})
271267
}

modules/fundamental/src/advisory/endpoints/test.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,7 @@ async fn delete_advisory(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
560560
.call_and_read_body_json(TestRequest::get().uri("/api/v2/advisory").to_request())
561561
.await;
562562
assert_eq!(advisory_list.total, 1);
563-
let key: StorageKey = advisory_list.items[0]
564-
.source_document
565-
.as_ref()
566-
.expect("valid document")
567-
.try_into()?;
563+
let key: StorageKey = advisory_list.items[0].source_document.clone().try_into()?;
568564
assert!(storage.retrieve(key.clone()).await?.is_some());
569565

570566
// first delete should succeed

modules/fundamental/src/advisory/model/details/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct AdvisoryDetails {
1717
pub head: AdvisoryHead,
1818

1919
#[serde(flatten)]
20-
pub source_document: Option<SourceDocument>,
20+
pub source_document: SourceDocument,
2121

2222
/// Vulnerabilities addressed within this advisory.
2323
pub vulnerabilities: Vec<AdvisoryVulnerabilitySummary>,
@@ -60,10 +60,7 @@ impl AdvisoryDetails {
6060
tx,
6161
)
6262
.await?,
63-
source_document: advisory
64-
.source_document
65-
.as_ref()
66-
.map(SourceDocument::from_entity),
63+
source_document: SourceDocument::from_entity(&advisory.source_document),
6764
vulnerabilities,
6865
average_severity: None,
6966
average_score: None,

modules/fundamental/src/advisory/model/summary.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct AdvisorySummary {
1717

1818
/// Information pertaning to the underlying source document, if any.
1919
#[serde(flatten)]
20-
pub source_document: Option<SourceDocument>,
20+
pub source_document: SourceDocument,
2121

2222
/// Average (arithmetic mean) severity of the advisory aggregated from *all* related vulnerability assertions.
2323
#[schema(required)]
@@ -69,10 +69,7 @@ impl AdvisorySummary {
6969
tx,
7070
)
7171
.await?,
72-
source_document: each
73-
.source_document
74-
.as_ref()
75-
.map(SourceDocument::from_entity),
72+
source_document: SourceDocument::from_entity(&each.source_document),
7673
average_severity: None,
7774
average_score: None,
7875
vulnerabilities,

modules/fundamental/src/advisory/service/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl AdvisoryService {
199199

200200
#[derive(Debug)]
201201
pub struct AdvisoryCatcher {
202-
pub source_document: Option<source_document::Model>,
202+
pub source_document: source_document::Model,
203203
pub advisory: advisory::Model,
204204
pub issuer: Option<organization::Model>,
205205
}
@@ -211,7 +211,8 @@ impl FromQueryResult for AdvisoryCatcher {
211211
res,
212212
"",
213213
source_document::Entity,
214-
)?,
214+
)?
215+
.ok_or_else(|| DbErr::Custom("Missing source document".to_string()))?,
215216
advisory: Self::from_query_result_multi_model(res, "", advisory::Entity)?,
216217
issuer: Self::from_query_result_multi_model_optional(res, "", organization::Entity)?,
217218
})

modules/fundamental/src/advisory/service/test.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ async fn single_advisory(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
152152
fetched,
153153
Some(AdvisoryDetails {
154154
head: AdvisoryHead { .. },
155-
source_document: Some(SourceDocument {
155+
source_document: SourceDocument {
156156
sha256,
157157
sha384,
158158
sha512,
159159
..
160-
}),
160+
},
161161
average_severity: None,
162162
..
163163
})
@@ -168,14 +168,13 @@ async fn single_advisory(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
168168
fetched,
169169
Some(AdvisoryDetails {
170170
head: AdvisoryHead { .. },
171-
source_document: Some(SourceDocument {
171+
source_document: SourceDocument {
172172
sha256,
173173
sha384,
174174
sha512,
175175
..
176-
}),
176+
},
177177
average_severity: None,
178-
179178
..
180179
})
181180
if sha256 == jenny256.to_string() && sha384 == jenny384.to_string() && sha512 == jenny512.to_string()));

0 commit comments

Comments
 (0)