Skip to content

Commit c2f7976

Browse files
committed
chore: add new field custom_document_license_refs into license
1 parent 531eb69 commit c2f7976

File tree

6 files changed

+453
-28
lines changed

6 files changed

+453
-28
lines changed

entity/src/license.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub struct Model {
99
pub spdx_licenses: Option<Vec<String>>,
1010
pub spdx_license_exceptions: Option<Vec<String>>,
1111
pub custom_license_refs: Option<Vec<String>>,
12+
pub custom_document_license_refs: Option<Vec<String>>,
1213
}
1314

1415
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

etc/test-data/spdx/license-sbom.json

Lines changed: 393 additions & 0 deletions
Large diffs are not rendered by default.

migration/src/m0001150_license_add_custom_license_refs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ impl MigrationTrait for Migration {
1111
Table::alter()
1212
.table(License::Table)
1313
.add_column(ColumnDef::new(License::CustomLicenseRefs).array(ColumnType::Text))
14+
.add_column(
15+
ColumnDef::new(License::CustomDocumentLicenseRefs).array(ColumnType::Text),
16+
)
1417
.to_owned(),
1518
)
1619
.await?;
@@ -24,6 +27,7 @@ impl MigrationTrait for Migration {
2427
Table::alter()
2528
.table(License::Table)
2629
.drop_column(License::CustomLicenseRefs)
30+
.drop_column(License::CustomDocumentLicenseRefs)
2731
.to_owned(),
2832
)
2933
.await?;
@@ -35,4 +39,5 @@ impl MigrationTrait for Migration {
3539
enum License {
3640
Table,
3741
CustomLicenseRefs,
42+
CustomDocumentLicenseRefs,
3843
}

modules/fundamental/tests/sbom/license.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
use flate2::read::GzDecoder;
2-
use sea_orm::{ColumnTrait, QuerySelect};
3-
use sea_orm::{EntityTrait, QueryFilter};
2+
use sea_orm::{ColumnTrait, QuerySelect, EntityTrait, QueryFilter};
3+
44
use sea_query::Cond;
55
use serde_json::{Value, json};
66
use std::io::Read;
77
use tar::Archive;
88
use test_context::test_context;
99
use test_log::test;
10-
use trustify_entity::sbom_package_license::LicenseCategory;
11-
use trustify_entity::{license, sbom_package, sbom_package_license};
10+
use trustify_entity::{sbom_package_license::LicenseCategory, license, sbom_package, sbom_package_license};
1211
use trustify_module_fundamental::license::{
1312
model::sbom_license::SbomNameId,
1413
service::{LicenseService, license_export::LicenseExporter},
1514
};
16-
use trustify_test_context::TrustifyContext;
17-
use trustify_test_context::subset::ContainsSubset;
15+
use trustify_test_context::{TrustifyContext, subset::ContainsSubset};
1816

1917
#[test_context(TrustifyContext)]
2018
#[test(tokio::test)]
@@ -46,27 +44,21 @@ async fn test_cyclonedx(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
4644
#[test_context(TrustifyContext)]
4745
#[test(tokio::test)]
4846
async fn test_custom_license_refs_spdx(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
49-
let result = ctx
50-
.ingest_document("spdx/SATELLITE-6.15-RHEL-8.json")
51-
.await?;
52-
53-
assert_eq!(
54-
Some("https://access.redhat.com/security/data/sbom/spdx/SATELLITE-6.15-RHEL-8"),
55-
result.clone().document_id.as_deref()
56-
);
47+
let _result = ctx.ingest_document("spdx/license-sbom.json").await?;
5748

5849
let license_result = license::Entity::find()
5950
.filter(
6051
Cond::any()
61-
.add(license::Column::Text.eq("LicenseRef-2 OR Ruby"))
62-
.add(license::Column::Text.eq("LicenseRef-GPLv3 AND LicenseRef-21")),
52+
.add(license::Column::Text.eq("(LicenseRef-1 AND MIT) OR DocumentRef-OCP-TOOLS-4.11-RHEL-8:LicenseRef-Netscape"))
53+
.add(license::Column::Text.eq("(LicenseRef-JasPer AND LicenseRef-1) OR DocumentRef-OCP-TOOLS-4.11-RHEL-8:LicenseRef-MPL")),
6354
)
6455
.select_only()
6556
.column(license::Column::Id)
6657
.column(license::Column::Text)
6758
.column(license::Column::SpdxLicenses)
6859
.column(license::Column::SpdxLicenseExceptions)
6960
.column(license::Column::CustomLicenseRefs)
61+
.column(license::Column::CustomDocumentLicenseRefs)
7062
.all(&ctx.db)
7163
.await?;
7264

@@ -76,20 +68,23 @@ async fn test_custom_license_refs_spdx(ctx: &TrustifyContext) -> Result<(), anyh
7668
);
7769
let expected_result = json!([
7870
{
79-
"id": "107c5a51-d315-56fb-9d4c-dd337f242d2e",
80-
"text": "LicenseRef-2 OR Ruby",
81-
"spdx_licenses": ["Ruby"],
71+
"id": "37551706-5849-5761-ab10-9fd29d317656",
72+
"text": "(LicenseRef-1 AND MIT) OR DocumentRef-OCP-TOOLS-4.11-RHEL-8:LicenseRef-Netscape",
73+
"spdx_licenses": ["MIT"],
8274
"spdx_license_exceptions": null,
83-
"custom_license_refs": ["LicenseRef-2:GPLv2+"]
75+
"custom_license_refs": ["LicenseRef-1:MIT/X License, GPL/CDDL, ASL2"],
76+
"custom_document_license_refs": ["DocumentRef-OCP-TOOLS-4.11-RHEL-8:LicenseRef-Netscape"]
8477
},
8578
{
86-
"id": "5bec012e-9891-5715-a550-09287ced2d54",
87-
"text": "LicenseRef-GPLv3 AND LicenseRef-21",
79+
"id": "119b7505-184b-5557-a729-dce9720718af",
80+
"text": "(LicenseRef-JasPer AND LicenseRef-1) OR DocumentRef-OCP-TOOLS-4.11-RHEL-8:LicenseRef-MPL",
8881
"spdx_licenses": null,
8982
"spdx_license_exceptions": null,
90-
"custom_license_refs": ["LicenseRef-GPLv3:GPLv3", "LicenseRef-21:Public domain"]
83+
"custom_license_refs": ["LicenseRef-JasPer:JasPer", "LicenseRef-1:MIT/X License, GPL/CDDL, ASL2"],
84+
"custom_document_license_refs": ["DocumentRef-OCP-TOOLS-4.11-RHEL-8:LicenseRef-MPL"]
9185
}
9286
]);
87+
9388
let license_result_value: Value =
9489
serde_json::to_value(&license_result).expect("Failed to serialize license_result to JSON");
9590
assert!(expected_result.contains_subset(license_result_value));

modules/ingestor/src/graph/sbom/common/license.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl LicenseInfo {
2222
Uuid::new_v5(&NAMESPACE, self.license.to_lowercase().as_bytes())
2323
}
2424

25-
pub fn spdx_info(&self) -> (Vec<String>, Vec<String>, Vec<String>) {
25+
pub fn spdx_info(&self) -> (Vec<String>, Vec<String>, Vec<String>, Vec<String>) {
2626
SpdxExpression::parse(&self.license)
2727
.map(|parsed| {
2828
let spdx_licenses = parsed
@@ -49,9 +49,27 @@ impl LicenseInfo {
4949
.map(|e| format!("LicenseRef-{}", e.identifier))
5050
.collect::<Vec<_>>();
5151

52-
(spdx_licenses, spdx_license_exceptions, custom_license_refs)
52+
let custom_document_license_refs = parsed
53+
.licenses()
54+
.iter()
55+
.filter(|e| e.license_ref && e.document_ref.is_some())
56+
.map(|e| {
57+
if let Some(doc_ref) = &e.document_ref {
58+
format!("DocumentRef-{}:LicenseRef-{}", doc_ref, e.identifier)
59+
} else {
60+
String::default()
61+
}
62+
})
63+
.collect::<Vec<_>>();
64+
65+
(
66+
spdx_licenses,
67+
spdx_license_exceptions,
68+
custom_license_refs,
69+
custom_document_license_refs,
70+
)
5371
})
54-
.unwrap_or((vec![], vec![], vec![]))
72+
.unwrap_or((vec![], vec![], vec![], vec![]))
5573
}
5674
}
5775

@@ -84,7 +102,8 @@ impl LicenseCreator {
84102
pub fn add(&mut self, info: &LicenseInfo) {
85103
let uuid = info.uuid();
86104

87-
let (spdx_licenses, spdx_exceptions, custom_license_refs) = info.spdx_info();
105+
let (spdx_licenses, spdx_exceptions, custom_license_refs, custom_document_license_refs) =
106+
info.spdx_info();
88107
let missing_custom_refs: Vec<_> = custom_license_refs
89108
.iter()
90109
.filter(|ref_id| {
@@ -121,6 +140,11 @@ impl LicenseCreator {
121140
Set(Some(spdx_exceptions))
122141
},
123142
custom_license_refs: Set(custom_license_refs_value),
143+
custom_document_license_refs: if custom_document_license_refs.is_empty() {
144+
Set(None)
145+
} else {
146+
Set(Some(custom_document_license_refs))
147+
},
124148
});
125149
}
126150

modules/ingestor/src/graph/sbom/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ impl SbomContext {
424424
license: license.to_string(),
425425
};
426426

427-
let (spdx_licenses, spdx_exceptions, custom_license_refs) = license_info.spdx_info();
427+
let (spdx_licenses, spdx_exceptions, custom_license_refs, custom_document_license_refs) =
428+
license_info.spdx_info();
428429

429430
let license = license::Entity::find_by_id(license_info.uuid())
430431
.one(connection)
@@ -451,6 +452,12 @@ impl SbomContext {
451452
} else {
452453
Set(Some(custom_license_refs))
453454
},
455+
456+
custom_document_license_refs: if custom_document_license_refs.is_empty() {
457+
Set(None)
458+
} else {
459+
Set(Some(custom_document_license_refs))
460+
},
454461
}
455462
.insert(connection)
456463
.await?

0 commit comments

Comments
 (0)