Skip to content

Commit bd91205

Browse files
committed
treewide: collect patches from derivations and include in SBOM
1 parent 96bc5ca commit bd91205

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 0.4.0 (unreleased)
4+
5+
### Added
6+
7+
- Added the ability to extract patches from a derivation and include them in
8+
the SBOM.
9+
310
## 0.3.0
411

512
### Added

nix/buildtime-dependencies.nix

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ let
7171
] drv)
7272
// {
7373
path = drv.outPath;
74+
patches = lib.flatten (drv.patches or [ ]);
7475
}
7576
// lib.optionalAttrs (drv ? src && drv.src ? url) {
7677
src =

rust/transformer/src/cyclonedx.rs

+36
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use std::str::FromStr;
77
use anyhow::{Context, Result};
88
use cyclonedx_bom::external_models::normalized_string::NormalizedString;
99
use cyclonedx_bom::external_models::uri::{Purl, Uri};
10+
use cyclonedx_bom::models::attached_text::AttachedText;
1011
use cyclonedx_bom::models::bom::{Bom, UrnUuid};
12+
use cyclonedx_bom::models::code::{Diff, Patch, PatchClassification, Patches};
13+
use cyclonedx_bom::models::component::Pedigree;
1114
use cyclonedx_bom::models::component::{Classification, Component, Components, Scope};
1215
use cyclonedx_bom::models::external_reference::{
1316
self, ExternalReference, ExternalReferenceType, ExternalReferences,
@@ -170,6 +173,17 @@ impl CycloneDXComponent {
170173
component.external_references = Some(ExternalReferences(external_references));
171174
}
172175

176+
if !derivation.patches.is_empty() {
177+
component.pedigree = Some(Pedigree {
178+
ancestors: None,
179+
descendants: None,
180+
variants: None,
181+
commits: None,
182+
patches: Some(convert_patches(&derivation.patches)),
183+
notes: None,
184+
});
185+
}
186+
173187
Self(component)
174188
}
175189
}
@@ -277,3 +291,25 @@ fn metadata_tools() -> Tools {
277291
components: Some(Components(vec![component])),
278292
}
279293
}
294+
295+
fn convert_patches(patches: &[String]) -> Patches {
296+
let cyclonedx_patches = patches
297+
.iter()
298+
.filter_map(|patch| fs::read_to_string(patch).ok())
299+
.map(|diff| Patch {
300+
// As we know nothing about the patch at this level, the safest is to assume that it's
301+
// unofficial
302+
patch_type: PatchClassification::Unofficial,
303+
diff: Some(Diff {
304+
text: Some(AttachedText {
305+
content_type: Some(NormalizedString::new("text/plain")),
306+
encoding: None,
307+
content: diff,
308+
}),
309+
url: None,
310+
}),
311+
resolves: None,
312+
})
313+
.collect::<Vec<_>>();
314+
Patches(cyclonedx_patches)
315+
}

rust/transformer/src/derivation.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct Derivation {
1313
pub output_hash: Option<String>,
1414
pub src: Option<Src>,
1515
pub vendored_sbom: Option<String>,
16+
pub patches: Vec<String>,
1617
}
1718

1819
impl Derivation {

0 commit comments

Comments
 (0)