-
Notifications
You must be signed in to change notification settings - Fork 33
Use cargo SBOM precursor files, if available #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
use std::collections::HashMap; | ||
|
||
use auditable_serde::{Package, Source, VersionInfo}; | ||
use cargo_metadata::{ | ||
semver::{self, Version}, | ||
DependencyKind, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// Cargo SBOM precursor format. | ||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct SbomPrecursor { | ||
/// Schema version | ||
pub version: u32, | ||
/// Index into the crates array for the root crate | ||
pub root: usize, | ||
/// Array of all crates | ||
pub crates: Vec<Crate>, | ||
/// Information about rustc used to perform the compilation | ||
pub rustc: RustcInfo, | ||
} | ||
|
||
impl From<SbomPrecursor> for VersionInfo { | ||
fn from(sbom: SbomPrecursor) -> Self { | ||
// cargo sbom data format has more nodes than the auditable info format - if a crate is both a build | ||
// and runtime dependency it will appear twice in the `crates` array. | ||
// The `VersionInfo` format lists each package only once, with a single `kind` field | ||
// (Runtime having precedence over other kinds). | ||
|
||
// Firstly, we deduplicate the (name, version) pairs and create a mapping from the | ||
// original indices in the cargo sbom array to the new index in the auditable info package array. | ||
let (_, mut packages, indices) = sbom.crates.iter().enumerate().fold( | ||
(HashMap::new(), Vec::new(), Vec::new()), | ||
|(mut id_to_index_map, mut packages, mut indices), (index, crate_)| { | ||
match id_to_index_map.entry(crate_.id.clone()) { | ||
std::collections::hash_map::Entry::Occupied(entry) => { | ||
// Just store the new index in the indices array | ||
indices.push(*entry.get()); | ||
} | ||
std::collections::hash_map::Entry::Vacant(entry) => { | ||
let (name, version, source) = parse_fully_qualified_package_id(&crate_.id); | ||
// If the entry does not exist, we create it | ||
packages.push(Package { | ||
name, | ||
version, | ||
source, | ||
// Assume build, if we determine this is a runtime dependency we'll update later | ||
kind: auditable_serde::DependencyKind::Build, | ||
// We will fill this in later | ||
dependencies: Vec::new(), | ||
root: index == sbom.root, | ||
}); | ||
entry.insert(packages.len() - 1); | ||
indices.push(packages.len() - 1); | ||
} | ||
} | ||
(id_to_index_map, packages, indices) | ||
}, | ||
); | ||
|
||
// Traverse the graph as given by the sbom to fill in the dependencies with the new indices. | ||
// | ||
// Keep track of whether the dependency is a runtime dependency. | ||
// If we ever encounter a non-runtime dependency, all deps in the remaining subtree | ||
// are not runtime dependencies, i.e a runtime dep of a build dep is not recognized as a runtime dep. | ||
let mut stack = Vec::new(); | ||
stack.push((sbom.root, true)); | ||
while let Some((old_index, is_runtime)) = stack.pop() { | ||
let crate_ = &sbom.crates[old_index]; | ||
for dep in &crate_.dependencies { | ||
stack.push((dep.index, dep.kind == DependencyKind::Normal && is_runtime)); | ||
} | ||
|
||
let package = &mut packages[indices[old_index]]; | ||
if is_runtime { | ||
package.kind = auditable_serde::DependencyKind::Runtime | ||
}; | ||
|
||
for dep in &crate_.dependencies { | ||
let new_dep_index = indices[dep.index]; | ||
if package.dependencies.contains(&new_dep_index) { | ||
continue; // Already added this dependency | ||
} else if new_dep_index == indices[old_index] { | ||
// If the dependency is the same as the package itself, skip it | ||
continue; | ||
} else { | ||
package.dependencies.push(new_dep_index); | ||
} | ||
} | ||
} | ||
|
||
VersionInfo { packages } | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct Crate { | ||
/// Package ID specification | ||
pub id: String, | ||
/// List of target kinds | ||
pub kind: Vec<String>, | ||
/// Enabled feature flags | ||
pub features: Vec<String>, | ||
/// Dependencies for this crate | ||
pub dependencies: Vec<Dependency>, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct Dependency { | ||
/// Index into the crates array | ||
pub index: usize, | ||
/// Dependency kind: "normal", "build", or "dev" | ||
pub kind: DependencyKind, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct RustcInfo { | ||
/// Compiler version | ||
pub version: String, | ||
/// Compiler wrapper | ||
pub wrapper: Option<String>, | ||
/// Compiler workspace wrapper | ||
pub workspace_wrapper: Option<String>, | ||
/// Commit hash for rustc | ||
pub commit_hash: String, | ||
/// Host target triple | ||
pub host: String, | ||
/// Verbose version string: `rustc -vV` | ||
pub verbose_version: String, | ||
} | ||
|
||
const CRATES_IO_INDEX: &str = "https://github.com/rust-lang/crates.io-index"; | ||
|
||
/// Parses a fully qualified package ID spec string into a tuple of (name, version, source). | ||
/// The package ID spec format is defined at https://doc.rust-lang.org/cargo/reference/pkgid-spec.html#package-id-specifications-1 | ||
/// | ||
/// The fully qualified form of a package ID spec is mentioned in the Cargo documentation, | ||
/// figuring it out is left as an exercise to the reader. | ||
/// | ||
/// Adapting the grammar in the cargo doc, the format appears to be : | ||
/// ```norust | ||
/// fully_qualified_spec := kind "+" proto "://" hostname-and-path [ "?" query] "#" [ name "@" ] semver | ||
/// query := ( "branch" | "tag" | "rev" ) "=" ref | ||
/// semver := digits "." digits "." digits [ "-" prerelease ] [ "+" build ] | ||
/// kind := "registry" | "git" | "path" | ||
/// proto := "http" | "git" | "file" | ... | ||
/// ``` | ||
/// where: | ||
/// - the name is always present except when the kind is `path` and the last segment of the path doesn't match the name | ||
/// - the query string is only present for git dependencies (which we can ignore since we don't record git information) | ||
fn parse_fully_qualified_package_id(id: &str) -> (String, Version, Source) { | ||
let (kind, rest) = id.split_once('+').expect("Package ID to have a kind"); | ||
let (url, rest) = rest | ||
.split_once('#') | ||
.expect("Package ID to have version information"); | ||
let source = match (kind, url) { | ||
("registry", CRATES_IO_INDEX) => Source::CratesIo, | ||
("registry", _) => Source::Registry, | ||
("git", _) => Source::Git, | ||
("path", _) => Source::Local, | ||
tofay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_ => Source::Other(kind.to_string()), | ||
}; | ||
|
||
if source == Source::Local { | ||
// For local packages, the name might be in the suffix after '#' if it has | ||
// a diferent name than the last segment of the path. | ||
if let Some((name, version)) = rest.split_once('@') { | ||
( | ||
name.to_string(), | ||
semver::Version::parse(version).expect("Version to be valid SemVer"), | ||
source, | ||
) | ||
} else { | ||
// If no name is specified, use the last segment of the path as the name | ||
let name = url | ||
.split('/') | ||
.next_back() | ||
.unwrap() | ||
.split('\\') | ||
.next_back() | ||
.unwrap(); | ||
( | ||
name.to_string(), | ||
semver::Version::parse(rest).expect("Version to be valid SemVer"), | ||
source, | ||
) | ||
} | ||
} else { | ||
// For other sources, the name and version are after the '#', separated by '@' | ||
let (name, version) = rest | ||
.split_once('@') | ||
.expect("Package ID to have a name and version"); | ||
( | ||
name.to_string(), | ||
semver::Version::parse(version).expect("Version to be valid SemVer"), | ||
source, | ||
) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Cargo.lock |
1 change: 1 addition & 0 deletions
1
cargo-auditable/tests/fixtures/build_then_runtime_dep/top_level_crate/build.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fn main() {} |
1 change: 1 addition & 0 deletions
1
cargo-auditable/tests/fixtures/custom_rustc_path/runtime_dep/build.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fn main() {} |
8 changes: 8 additions & 0 deletions
8
cargo-auditable/tests/fixtures/path_not_equal_name/bar/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "bar" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
||
[workspace] |
14 changes: 14 additions & 0 deletions
14
cargo-auditable/tests/fixtures/path_not_equal_name/bar/src/lib.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
pub fn add(left: u64, right: u64) -> u64 { | ||
left + right | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
assert_eq!(result, 4); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
cargo-auditable/tests/fixtures/path_not_equal_name/foo/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "foo" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
bar = { version = "0.1.0", path = "../bar" } | ||
baz = { version = "0.1.0", path = "../qux" } | ||
|
||
[workspace] |
3 changes: 3 additions & 0 deletions
3
cargo-auditable/tests/fixtures/path_not_equal_name/foo/src/main.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |
8 changes: 8 additions & 0 deletions
8
cargo-auditable/tests/fixtures/path_not_equal_name/qux/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "baz" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
||
[workspace] |
14 changes: 14 additions & 0 deletions
14
cargo-auditable/tests/fixtures/path_not_equal_name/qux/src/lib.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
pub fn add(left: u64, right: u64) -> u64 { | ||
left + right | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
assert_eq!(result, 4); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
cargo-auditable/tests/fixtures/runtime_then_build_dep/runtime_dep/build.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fn main() {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.