feat(provenance): emit deterministic SPDX and CycloneDX SBOMs
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 4s
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 4s
- New Provenance::Sbom module generating SPDX 2.3 / CycloneDX 1.5 from lockfile packages + BuildGraph outputs - Wire vbs build|test --sbom FILE --sbom-format spdx|cyclonedx - Register the module in CMake, build.cpp and the self-host recipe - Ed25519 signing requested upstream as Void/Library#36 (VL-014) - Verified in Docker GCC 16: 8/8 CTest suites; deterministic JSON unit test + CLI SBOM assertion
This commit is contained in:
1 parent
2970fbe25c
commit
55ea305528
13 files changed
+403
-8
No files matched your search
@@ -14,6 +14,7 @@
|
||||
#include <Void/Build/Package/Toml.hpp>
|
||||
#include <Void/Build/Platform/FileOps.hpp>
|
||||
#include <Void/Build/Platform/ProcessSupervisor.hpp>
|
||||
#include <Void/Build/Provenance/Sbom.hpp>
|
||||
#include <Void/Build/Provider/Provider.hpp>
|
||||
#include <Void/Build/Provider/Translator.hpp>
|
||||
#include <Void/Build/SelfHost/TestRegistryGen.hpp>
|
||||
@@ -507,6 +508,8 @@ struct BuildCmdOptions {
|
||||
std::optional<std::string> remote_cache_token_env;
|
||||
std::vector<std::string> members;
|
||||
std::optional<std::string> changed_since;
|
||||
std::optional<std::string> sbom_arg;
|
||||
std::optional<std::string> sbom_format_arg;
|
||||
std::optional<std::filesystem::path> lock_root;
|
||||
std::optional<std::filesystem::path> state_dir;
|
||||
bool publish_editor_selection{true};
|
||||
@@ -856,6 +859,78 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
|
||||
std::cout << "vbs: [" << prof_name << "] built (" << rep->executed_count << " executed, "
|
||||
<< rep->cached_count << " cached)\n";
|
||||
|
||||
if (opts.sbom_arg) {
|
||||
Provenance::SbomInput sbom;
|
||||
sbom.project_name = project_root.filename().string();
|
||||
sbom.tool_version = std::string{Bootstrap::version};
|
||||
sbom.created_epoch = ctx.reproducibility_epoch;
|
||||
const auto sbom_ctx_key = Package::make_lock_context_key(ctx, "target");
|
||||
if (pinned_lock && pinned_lock->has_value()) {
|
||||
const auto &document = (*pinned_lock)->document;
|
||||
if (const auto it = document.contexts.find(sbom_ctx_key);
|
||||
it != document.contexts.end()) {
|
||||
for (const auto &entry : it->second.entries) {
|
||||
if (entry.version.empty()) {
|
||||
continue;
|
||||
}
|
||||
Provenance::SbomPackage package;
|
||||
package.name =
|
||||
entry.package_name.empty() ? entry.alias : entry.package_name;
|
||||
package.version = entry.version;
|
||||
package.download_location = entry.registry_authority.empty()
|
||||
? entry.location
|
||||
: entry.registry_authority;
|
||||
package.sha256 = entry.sha256;
|
||||
package.purl = "pkg:generic/" + package.name + "@" + package.version;
|
||||
sbom.packages.push_back(std::move(package));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const auto &target : graph->targets) {
|
||||
for (const auto &output : target.primary_outputs) {
|
||||
auto digest = Util::sha256_file_hex(output);
|
||||
if (!digest) {
|
||||
continue;
|
||||
}
|
||||
Provenance::SbomFile file;
|
||||
file.path = Platform::path_is_within(project_root, output)
|
||||
? output.lexically_relative(project_root).generic_string()
|
||||
: output.filename().string();
|
||||
file.sha256 = std::move(*digest);
|
||||
sbom.files.push_back(std::move(file));
|
||||
}
|
||||
}
|
||||
try {
|
||||
const auto format = opts.sbom_format_arg
|
||||
? Provenance::parse_sbom_format(*opts.sbom_format_arg)
|
||||
: Provenance::SbomFormat::Spdx;
|
||||
auto generated = Provenance::generate_sbom(sbom, format);
|
||||
if (!generated) {
|
||||
std::cerr << "vbs: SBOM generation failed: " << generated.error().message()
|
||||
<< '\n';
|
||||
all_ok = false;
|
||||
continue;
|
||||
}
|
||||
std::filesystem::path sbom_path{*opts.sbom_arg};
|
||||
if (opts.profiles.size() > 1) {
|
||||
sbom_path =
|
||||
sbom_path.parent_path() / (sbom_path.stem().string() + "." + prof_name +
|
||||
sbom_path.extension().string());
|
||||
}
|
||||
if (auto wrote = Platform::atomic_write_file(sbom_path, *generated, true); !wrote) {
|
||||
std::cerr << "vbs: failed writing SBOM: " << wrote.error().message() << '\n';
|
||||
all_ok = false;
|
||||
continue;
|
||||
}
|
||||
Void::Log::info("Sbom", "[{}] Wrote {} SBOM to {}", prof_name,
|
||||
Provenance::sbom_format_name(format), sbom_path.string());
|
||||
} catch (const std::exception &ex) {
|
||||
std::cerr << "vbs: " << ex.what() << '\n';
|
||||
all_ok = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (run_tests) {
|
||||
for (const auto &t : graph->targets) {
|
||||
if (t.kind != TargetKind::Test || t.primary_outputs.empty()) {
|
||||
@@ -2131,6 +2206,9 @@ int main(int argc, char **argv) {
|
||||
.option_many("--member", "", "Workspace member to build/test", build_opts.members)
|
||||
.option("--changed-since", "", "Build/test workspace members changed since a git ref",
|
||||
build_opts.changed_since)
|
||||
.option("--sbom", "", "Write an SBOM after a successful build", build_opts.sbom_arg)
|
||||
.option("--sbom-format", "", "SBOM format: spdx (default) or cyclonedx",
|
||||
build_opts.sbom_format_arg)
|
||||
.option("--remote-cache", "", "Shared HTTP action cache URL (or VBS_REMOTE_CACHE)",
|
||||
build_opts.remote_cache_arg)
|
||||
.option("--remote-cache-token-env", "",
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
#include <Void/Build/Provenance/Sbom.hpp>
|
||||
|
||||
#include <Void/Build/Util/Json.hpp>
|
||||
#include <Void/Build/Util/Sha256.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <ctime>
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <tuple>
|
||||
|
||||
namespace Void::Build::Provenance {
|
||||
namespace {
|
||||
|
||||
using Util::json_array;
|
||||
using Util::json_quote;
|
||||
|
||||
std::string iso8601(std::int64_t epoch) {
|
||||
const auto time = static_cast<std::time_t>(epoch);
|
||||
std::tm tm{};
|
||||
::gmtime_r(&time, &tm);
|
||||
char buffer[32];
|
||||
(void)std::strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%SZ", &tm);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::string purl_for(const SbomPackage &package) {
|
||||
if (!package.purl.empty()) {
|
||||
return package.purl;
|
||||
}
|
||||
return "pkg:generic/" + package.name + "@" + package.version;
|
||||
}
|
||||
|
||||
// Deterministic UUID-shaped serial derived from a hex digest (version 4 nibble).
|
||||
std::string uuid_from_hex(std::string_view hex) {
|
||||
const std::string h{hex};
|
||||
if (h.size() < 32) {
|
||||
return "00000000-0000-4000-8000-000000000000";
|
||||
}
|
||||
return std::format("{}-{}-4{}-8{}-{}", h.substr(0, 8), h.substr(8, 4), h.substr(13, 3),
|
||||
h.substr(17, 3), h.substr(20, 12));
|
||||
}
|
||||
|
||||
std::string document_identity(const SbomInput &input) {
|
||||
Util::Sha256Hasher hasher;
|
||||
hasher.update_length_prefixed("vbs-sbom-v1");
|
||||
hasher.update_length_prefixed(input.project_name);
|
||||
hasher.update_length_prefixed(input.project_version);
|
||||
hasher.update_length_prefixed(input.tool_version);
|
||||
hasher.update_u64(input.packages.size());
|
||||
for (const auto &package : input.packages) {
|
||||
hasher.update_length_prefixed(package.name);
|
||||
hasher.update_length_prefixed(package.version);
|
||||
hasher.update_length_prefixed(package.sha256);
|
||||
hasher.update_length_prefixed(purl_for(package));
|
||||
}
|
||||
hasher.update_u64(input.files.size());
|
||||
for (const auto &file : input.files) {
|
||||
hasher.update_length_prefixed(file.path);
|
||||
hasher.update_length_prefixed(file.sha256);
|
||||
}
|
||||
return hasher.finalize_hex();
|
||||
}
|
||||
|
||||
std::string checksum_json(std::string_view sha256, std::string_view algorithm_key,
|
||||
std::string_view value_key) {
|
||||
return "{\"" + std::string{algorithm_key} + "\":" + json_quote("SHA256") + ",\"" +
|
||||
std::string{value_key} + "\":" + json_quote(sha256) + '}';
|
||||
}
|
||||
|
||||
std::string spdx(const SbomInput &input, std::string_view identity) {
|
||||
std::string out =
|
||||
"{\n \"spdxVersion\": \"SPDX-2.3\",\n \"dataLicense\": \"CC0-1.0\",\n"
|
||||
" \"SPDXID\": \"SPDXRef-DOCUMENT\",\n \"name\": " +
|
||||
json_quote(input.project_name + "-sbom") + ",\n \"documentNamespace\": " +
|
||||
json_quote("https://voidbuild.dev/spdx/" + std::string{identity}) +
|
||||
",\n \"creationInfo\": {\"created\": " + json_quote(iso8601(input.created_epoch)) +
|
||||
", \"creators\": [" + json_quote("Tool: vbs-" + input.tool_version) +
|
||||
"]},\n \"packages\": [\n";
|
||||
|
||||
out +=
|
||||
" {\"SPDXID\": \"SPDXRef-Package-Root\", \"name\": " + json_quote(input.project_name) +
|
||||
", \"versionInfo\": " + json_quote(input.project_version) +
|
||||
", \"downloadLocation\": \"NOASSERTION\", \"filesAnalyzed\": false, "
|
||||
"\"supplier\": \"NOASSERTION\", \"licenseConcluded\": \"NOASSERTION\"}";
|
||||
|
||||
for (std::size_t i = 0; i < input.packages.size(); ++i) {
|
||||
const auto &package = input.packages[i];
|
||||
out += ",\n {\"SPDXID\": " + json_quote(std::format("SPDXRef-Package-{}", i)) +
|
||||
", \"name\": " + json_quote(package.name) +
|
||||
", \"versionInfo\": " + json_quote(package.version) + ", \"downloadLocation\": " +
|
||||
json_quote(package.download_location.empty() ? "NOASSERTION"
|
||||
: package.download_location) +
|
||||
", \"filesAnalyzed\": false, \"supplier\": " +
|
||||
json_quote(package.supplier.empty() ? "NOASSERTION" : package.supplier) +
|
||||
", \"licenseConcluded\": \"NOASSERTION\", \"checksums\": [" +
|
||||
checksum_json(package.sha256, "algorithm", "checksumValue") +
|
||||
"], \"externalRefs\": [{\"referenceCategory\": \"PACKAGE-MANAGER\", "
|
||||
"\"referenceType\": \"purl\", \"referenceLocator\": " +
|
||||
json_quote(purl_for(package)) + "}]}";
|
||||
}
|
||||
out += "\n ],\n \"files\": [\n";
|
||||
for (std::size_t i = 0; i < input.files.size(); ++i) {
|
||||
const auto &file = input.files[i];
|
||||
out += " {\"SPDXID\": " + json_quote(std::format("SPDXRef-File-{}", i)) +
|
||||
", \"fileName\": " + json_quote(file.path) + ", \"checksums\": [" +
|
||||
checksum_json(file.sha256, "algorithm", "checksumValue") + "]}";
|
||||
if (i + 1 != input.files.size()) {
|
||||
out += ',';
|
||||
}
|
||||
out += '\n';
|
||||
}
|
||||
out += " ],\n \"relationships\": [\n"
|
||||
" {\"spdxElementId\": \"SPDXRef-DOCUMENT\", \"relationshipType\": \"DESCRIBES\", "
|
||||
"\"relatedSpdxElement\": \"SPDXRef-Package-Root\"}";
|
||||
for (std::size_t i = 0; i < input.packages.size(); ++i) {
|
||||
out += ",\n {\"spdxElementId\": \"SPDXRef-Package-Root\", \"relationshipType\": "
|
||||
"\"DEPENDS_ON\", \"relatedSpdxElement\": " +
|
||||
json_quote(std::format("SPDXRef-Package-{}", i)) + '}';
|
||||
}
|
||||
for (std::size_t i = 0; i < input.files.size(); ++i) {
|
||||
out += ",\n {\"spdxElementId\": \"SPDXRef-Package-Root\", \"relationshipType\": "
|
||||
"\"CONTAINS\", \"relatedSpdxElement\": " +
|
||||
json_quote(std::format("SPDXRef-File-{}", i)) + '}';
|
||||
}
|
||||
out += "\n ]\n}\n";
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string cyclonedx(const SbomInput &input, std::string_view identity) {
|
||||
std::string out =
|
||||
"{\n \"bomFormat\": \"CycloneDX\",\n \"specVersion\": \"1.5\",\n \"serialNumber\": " +
|
||||
json_quote("urn:uuid:" + uuid_from_hex(identity)) + ",\n \"version\": 1,\n" +
|
||||
" \"metadata\": {\"timestamp\": " + json_quote(iso8601(input.created_epoch)) +
|
||||
", \"tools\": {\"components\": [{\"type\": \"application\", \"name\": \"vbs\", "
|
||||
"\"version\": " +
|
||||
json_quote(input.tool_version) +
|
||||
"}]}, \"component\": {\"type\": \"application\", \"name\": " +
|
||||
json_quote(input.project_name) + ", \"version\": " + json_quote(input.project_version) +
|
||||
"}},\n \"components\": [\n";
|
||||
|
||||
for (std::size_t i = 0; i < input.packages.size(); ++i) {
|
||||
const auto &package = input.packages[i];
|
||||
out += " {\"type\": \"library\", \"bom-ref\": " + json_quote(purl_for(package)) +
|
||||
", \"name\": " + json_quote(package.name) +
|
||||
", \"version\": " + json_quote(package.version) +
|
||||
", \"purl\": " + json_quote(purl_for(package)) + ", \"hashes\": [" +
|
||||
checksum_json(package.sha256, "alg", "content") + "]}";
|
||||
if (i + 1 != input.packages.size()) {
|
||||
out += ',';
|
||||
}
|
||||
out += '\n';
|
||||
}
|
||||
out += " ],\n \"dependencies\": [{\"ref\": " +
|
||||
json_quote(
|
||||
purl_for(SbomPackage{input.project_name, input.project_version, {}, {}, {}, {}})) +
|
||||
", \"dependsOn\": [";
|
||||
for (std::size_t i = 0; i < input.packages.size(); ++i) {
|
||||
if (i != 0) {
|
||||
out += ", ";
|
||||
}
|
||||
out += json_quote(purl_for(input.packages[i]));
|
||||
}
|
||||
out += "]}]\n}\n";
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string_view sbom_format_name(SbomFormat format) noexcept {
|
||||
switch (format) {
|
||||
case SbomFormat::Spdx:
|
||||
return "spdx";
|
||||
case SbomFormat::CycloneDX:
|
||||
return "cyclonedx";
|
||||
}
|
||||
return "spdx";
|
||||
}
|
||||
|
||||
SbomFormat parse_sbom_format(std::string_view name) {
|
||||
if (name == "spdx") {
|
||||
return SbomFormat::Spdx;
|
||||
}
|
||||
if (name == "cyclonedx" || name == "cdx") {
|
||||
return SbomFormat::CycloneDX;
|
||||
}
|
||||
throw std::invalid_argument{"unknown SBOM format '" + std::string{name} +
|
||||
"' (expected spdx or cyclonedx)"};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string, Void::Result::Error> generate_sbom(const SbomInput &raw_input,
|
||||
SbomFormat format) {
|
||||
SbomInput input = raw_input;
|
||||
std::ranges::sort(input.packages, [](const SbomPackage &a, const SbomPackage &b) {
|
||||
return std::tie(a.name, a.version, a.sha256) < std::tie(b.name, b.version, b.sha256);
|
||||
});
|
||||
std::ranges::sort(input.files, [](const SbomFile &a, const SbomFile &b) {
|
||||
return std::tie(a.path, a.sha256) < std::tie(b.path, b.sha256);
|
||||
});
|
||||
const std::string identity = document_identity(input);
|
||||
return format == SbomFormat::Spdx ? spdx(input, identity) : cyclonedx(input, identity);
|
||||
}
|
||||
|
||||
} // namespace Void::Build::Provenance
|
||||
Reference in new issue
Block a user