feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"advisories": [
|
||||
{
|
||||
"cvss": 7.5,
|
||||
"fixed": "5.7.1",
|
||||
"id": "GHSA-test-0001",
|
||||
"package": "wolfssl",
|
||||
"summary": "test advisory with fix below critical"
|
||||
},
|
||||
{
|
||||
"cvss": 4.2,
|
||||
"fixed": "",
|
||||
"id": "GHSA-test-0002",
|
||||
"package": "moloapp",
|
||||
"summary": "low test advisory without fix"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
{
|
||||
"_type": "https://in-toto.io/Statement/v1",
|
||||
"predicate": {
|
||||
"buildDefinition": {
|
||||
"buildType": "https://moloapp.dev/build/v1",
|
||||
"externalParameters": {
|
||||
"commit": "9f3c1a2b4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8",
|
||||
"invocation": "molo release --test",
|
||||
"materials": [
|
||||
"gcc@15.2.0",
|
||||
"cmake@3.30.0"
|
||||
]
|
||||
},
|
||||
"resolvedDependencies": [
|
||||
{
|
||||
"digest": {
|
||||
"sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
},
|
||||
"uri": "sbom.cdx.json"
|
||||
},
|
||||
{
|
||||
"digest": {
|
||||
"sha256": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
},
|
||||
"uri": "sbom.spdx.json"
|
||||
}
|
||||
]
|
||||
},
|
||||
"runDetails": {
|
||||
"builder": {
|
||||
"id": "https://moloapp.dev/builder/test"
|
||||
}
|
||||
}
|
||||
},
|
||||
"predicateType": "https://slsa.dev/provenance/v1",
|
||||
"subject": [
|
||||
{
|
||||
"digest": {
|
||||
"sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
},
|
||||
"name": "sbom.cdx.json"
|
||||
},
|
||||
{
|
||||
"digest": {
|
||||
"sha256": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
},
|
||||
"name": "sbom.spdx.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
{
|
||||
"bomFormat": "CycloneDX",
|
||||
"components": [
|
||||
{
|
||||
"bom-ref": "moloapp@1.2.3-test",
|
||||
"hashes": [
|
||||
{
|
||||
"alg": "SHA-256",
|
||||
"content": "9892f106e6f5ca6500d39edb73913cd9cb0d9ea6b25686bb10e270dec3c9532e"
|
||||
}
|
||||
],
|
||||
"licenses": [
|
||||
{
|
||||
"license": {
|
||||
"name": "proprietary"
|
||||
}
|
||||
}
|
||||
],
|
||||
"name": "moloapp",
|
||||
"supplier": {
|
||||
"name": "moloapp"
|
||||
},
|
||||
"type": "library",
|
||||
"version": "1.2.3-test"
|
||||
},
|
||||
{
|
||||
"bom-ref": "wolfssl@5.7.0",
|
||||
"hashes": [
|
||||
{
|
||||
"alg": "SHA-256",
|
||||
"content": "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
}
|
||||
],
|
||||
"licenses": [
|
||||
{
|
||||
"license": {
|
||||
"name": "GPL-2.0"
|
||||
}
|
||||
}
|
||||
],
|
||||
"name": "wolfssl",
|
||||
"supplier": {
|
||||
"name": "wolfSSL Inc."
|
||||
},
|
||||
"type": "library",
|
||||
"version": "5.7.0"
|
||||
},
|
||||
{
|
||||
"bom-ref": "zlib@1.3.1",
|
||||
"hashes": [
|
||||
{
|
||||
"alg": "SHA-256",
|
||||
"content": "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
}
|
||||
],
|
||||
"licenses": [
|
||||
{
|
||||
"license": {
|
||||
"name": "Zlib"
|
||||
}
|
||||
}
|
||||
],
|
||||
"name": "zlib",
|
||||
"supplier": {
|
||||
"name": "https://zlib.net"
|
||||
},
|
||||
"type": "library",
|
||||
"version": "1.3.1"
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"component": {
|
||||
"bom-ref": "moloapp@1.2.3-test",
|
||||
"name": "moloapp",
|
||||
"type": "application",
|
||||
"version": "1.2.3-test"
|
||||
},
|
||||
"timestamp": "2026-01-01T00:00:00Z",
|
||||
"tools": {
|
||||
"components": [
|
||||
{
|
||||
"name": "gcc",
|
||||
"version": "15.2.0"
|
||||
},
|
||||
{
|
||||
"name": "cmake",
|
||||
"version": "3.30.0"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"serialNumber": "urn:uuid:00000000-0000-4000-8000-8682df12a0d3",
|
||||
"specVersion": "1.6",
|
||||
"version": 1
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
{
|
||||
"SPDXID": "SPDXRef-DOCUMENT",
|
||||
"creationInfo": {
|
||||
"created": "2026-01-01T00:00:00Z",
|
||||
"creators": [
|
||||
"Tool: moloapp-1.2.3-test"
|
||||
]
|
||||
},
|
||||
"dataLicense": "CC0-1.0",
|
||||
"documentDescribes": [
|
||||
"SPDXRef-DOCUMENT"
|
||||
],
|
||||
"documentNamespace": "https://moloapp.dev/sbom/9f3c1a2b4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8",
|
||||
"name": "moloapp-sbom",
|
||||
"packages": [
|
||||
{
|
||||
"SPDXID": "SPDXRef-Package-moloapp",
|
||||
"checksums": [
|
||||
{
|
||||
"algorithm": "SHA256",
|
||||
"checksumValue": "9892f106e6f5ca6500d39edb73913cd9cb0d9ea6b25686bb10e270dec3c9532e"
|
||||
}
|
||||
],
|
||||
"downloadLocation": "https://moloapp.dev",
|
||||
"externalRefs": [
|
||||
{
|
||||
"category": "PACKAGE-MANAGER",
|
||||
"locator": "https://moloapp.dev",
|
||||
"referenceType": "purl"
|
||||
}
|
||||
],
|
||||
"licenseDeclared": "proprietary",
|
||||
"name": "moloapp",
|
||||
"supplier": "Organization: moloapp",
|
||||
"versionInfo": "1.2.3-test"
|
||||
},
|
||||
{
|
||||
"SPDXID": "SPDXRef-Package-wolfssl",
|
||||
"checksums": [
|
||||
{
|
||||
"algorithm": "SHA256",
|
||||
"checksumValue": "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
}
|
||||
],
|
||||
"downloadLocation": "https://www.wolfssl.com/download",
|
||||
"externalRefs": [
|
||||
{
|
||||
"category": "PACKAGE-MANAGER",
|
||||
"locator": "https://www.wolfssl.com/download",
|
||||
"referenceType": "purl"
|
||||
}
|
||||
],
|
||||
"licenseDeclared": "GPL-2.0",
|
||||
"name": "wolfssl",
|
||||
"supplier": "Organization: wolfSSL Inc.",
|
||||
"versionInfo": "5.7.0"
|
||||
},
|
||||
{
|
||||
"SPDXID": "SPDXRef-Package-zlib",
|
||||
"checksums": [
|
||||
{
|
||||
"algorithm": "SHA256",
|
||||
"checksumValue": "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
}
|
||||
],
|
||||
"downloadLocation": "https://zlib.net",
|
||||
"externalRefs": [
|
||||
{
|
||||
"category": "PACKAGE-MANAGER",
|
||||
"locator": "https://zlib.net",
|
||||
"referenceType": "purl"
|
||||
}
|
||||
],
|
||||
"licenseDeclared": "Zlib",
|
||||
"name": "zlib",
|
||||
"supplier": "Organization: https://zlib.net",
|
||||
"versionInfo": "1.3.1"
|
||||
}
|
||||
],
|
||||
"relationships": [
|
||||
{
|
||||
"relatedSpdxElement": "SPDXRef-Package-moloapp",
|
||||
"relationshipType": "CONTAINS",
|
||||
"spdxElementId": "SPDXRef-DOCUMENT"
|
||||
},
|
||||
{
|
||||
"relatedSpdxElement": "SPDXRef-Package-wolfssl",
|
||||
"relationshipType": "CONTAINS",
|
||||
"spdxElementId": "SPDXRef-DOCUMENT"
|
||||
},
|
||||
{
|
||||
"relatedSpdxElement": "SPDXRef-Package-zlib",
|
||||
"relationshipType": "CONTAINS",
|
||||
"spdxElementId": "SPDXRef-DOCUMENT"
|
||||
}
|
||||
],
|
||||
"spdxVersion": "SPDX-2.3"
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
// test_cdx_equiv: CycloneDX emission carries the same triples as SPDX.
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include "src/supplychain/common/Json.hpp"
|
||||
#include "src/supplychain/policy/DependencyPolicy.hpp"
|
||||
#include "src/supplychain/policy/WolfPin.hpp"
|
||||
#include "src/supplychain/sbom/CycloneDxEmitter.hpp"
|
||||
#include "src/supplychain/sbom/SbomGenerator.hpp"
|
||||
#include "src/supplychain/sbom/SpdxEmitter.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
int g_fail = 0;
|
||||
#define CHECK(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
std::cerr << "FAIL line " << __LINE__ << ": " #cond "\n"; \
|
||||
++g_fail; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
const char* kPinSha = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
||||
const char* kCommit = "9f3c1a2b4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8";
|
||||
|
||||
bool read_all(const std::string& path, std::string& text) {
|
||||
std::FILE* f = std::fopen(path.c_str(), "rb");
|
||||
if (f == nullptr) return false;
|
||||
char chunk[4096];
|
||||
std::size_t n = 0;
|
||||
while ((n = std::fread(chunk, 1, sizeof(chunk), f)) > 0) text.append(chunk, n);
|
||||
std::fclose(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
using Triple = std::tuple<std::string, std::string, std::string>;
|
||||
|
||||
std::set<Triple> spdx_triples(const molo::supply::common::Json::Value& root) {
|
||||
using molo::supply::common::Json;
|
||||
std::set<Triple> out;
|
||||
const Json::Value* pkgs = Json::find(root, "packages");
|
||||
if (pkgs == nullptr || pkgs->type != Json::Value::Type::Array) return out;
|
||||
for (const auto& p : pkgs->items) {
|
||||
const Json::Value* name = Json::find(p, "name");
|
||||
const Json::Value* ver = Json::find(p, "versionInfo");
|
||||
const Json::Value* sums = Json::find(p, "checksums");
|
||||
std::string sha;
|
||||
if (sums != nullptr && sums->type == Json::Value::Type::Array && !sums->items.empty()) {
|
||||
const Json::Value* v = Json::find(sums->items[0], "checksumValue");
|
||||
if (v != nullptr) sha = Json::string_or(*v, "");
|
||||
}
|
||||
out.emplace(Json::string_or(*name, ""), Json::string_or(*ver, ""), sha);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::set<Triple> cdx_triples(const molo::supply::common::Json::Value& root) {
|
||||
using molo::supply::common::Json;
|
||||
std::set<Triple> out;
|
||||
const Json::Value* comps = Json::find(root, "components");
|
||||
if (comps == nullptr || comps->type != Json::Value::Type::Array) return out;
|
||||
for (const auto& p : comps->items) {
|
||||
const Json::Value* name = Json::find(p, "name");
|
||||
const Json::Value* ver = Json::find(p, "version");
|
||||
const Json::Value* hashes = Json::find(p, "hashes");
|
||||
std::string sha;
|
||||
if (hashes != nullptr && hashes->type == Json::Value::Type::Array && !hashes->items.empty()) {
|
||||
const Json::Value* v = Json::find(hashes->items[0], "content");
|
||||
if (v != nullptr) sha = Json::string_or(*v, "");
|
||||
}
|
||||
out.emplace(Json::string_or(*name, ""), Json::string_or(*ver, ""), sha);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
using namespace molo::supply;
|
||||
const std::string golden =
|
||||
(argc > 1 ? std::string(argv[1]) : std::string("tests/src/supplychain/golden")) +
|
||||
"/sbom.cdx.json";
|
||||
const bool regen = std::getenv("MOLO_REGEN_GOLDEN") != nullptr;
|
||||
|
||||
std::string error;
|
||||
policy::WolfPin::Pin pin{"5.7.0", kPinSha, "https://www.wolfssl.com/download", "GPL-2.0",
|
||||
"vendor/wolfssl-5.7.0.receipt"};
|
||||
policy::DependencyPolicy manifest;
|
||||
std::string allow = std::string("{\"dependencies\": [{\"name\": \"zlib\", \"version\": "
|
||||
"\"1.3.1\", \"sha256\": \"") +
|
||||
kPinSha + "\", \"source\": \"https://zlib.net\", \"license\": \"Zlib\"}]}";
|
||||
CHECK(manifest.parse(allow, error));
|
||||
std::vector<sbom::SbomToolchainEntry> toolchain = {{"gcc", "15.2.0"}, {"cmake", "3.30.0"}};
|
||||
sbom::SbomDocument doc;
|
||||
CHECK(sbom::SbomGenerator::generate(kCommit, toolchain, pin, manifest, "1.2.3-test",
|
||||
"2026-01-01T00:00:00Z", doc, error));
|
||||
|
||||
std::string cdx;
|
||||
CHECK(sbom::CycloneDxEmitter::emit_json(doc, cdx, error));
|
||||
CHECK(!cdx.empty() && cdx.back() == '\n');
|
||||
CHECK(cdx.find('\r') == std::string::npos);
|
||||
CHECK(cdx.find("\"bomFormat\": \"CycloneDX\"") != std::string::npos);
|
||||
CHECK(cdx.find("\"specVersion\": \"1.6\"") != std::string::npos);
|
||||
CHECK(cdx.find(" \"bomFormat\"") != std::string::npos); // 2-space indent
|
||||
common::Json::Value cdx_root;
|
||||
CHECK(common::Json::parse(cdx, cdx_root, error));
|
||||
|
||||
std::string spdx;
|
||||
CHECK(sbom::SpdxEmitter::emit_json(doc, spdx, error));
|
||||
common::Json::Value spdx_root;
|
||||
CHECK(common::Json::parse(spdx, spdx_root, error));
|
||||
|
||||
std::set<Triple> a = spdx_triples(spdx_root);
|
||||
std::set<Triple> b = cdx_triples(cdx_root);
|
||||
CHECK(a.size() == 3 && b.size() == 3);
|
||||
CHECK(a == b); // identical (name, version, sha256) triples
|
||||
|
||||
if (regen) {
|
||||
std::FILE* f = std::fopen(golden.c_str(), "wb");
|
||||
CHECK(f != nullptr);
|
||||
if (f != nullptr) {
|
||||
CHECK(std::fwrite(cdx.data(), 1, cdx.size(), f) == cdx.size());
|
||||
std::fclose(f);
|
||||
std::cout << "test_cdx_equiv: regenerated " << golden << "\n";
|
||||
}
|
||||
} else {
|
||||
std::string want;
|
||||
CHECK(read_all(golden, want));
|
||||
if (want != cdx) {
|
||||
std::size_t at = 0;
|
||||
while (at < want.size() && at < cdx.size() && want[at] == cdx[at]) ++at;
|
||||
std::cerr << "FAIL golden mismatch at byte " << at << " want=" << want.size()
|
||||
<< " got=" << cdx.size() << "\n";
|
||||
++g_fail;
|
||||
}
|
||||
}
|
||||
|
||||
if (g_fail == 0) std::cout << "test_cdx_equiv: all checks passed\n";
|
||||
return g_fail == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
// test_pin: wolfSSL pin plus allow-list parsing and checks.
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "src/supplychain/policy/DependencyPolicy.hpp"
|
||||
#include "src/supplychain/policy/WolfPin.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
int g_fail = 0;
|
||||
#define CHECK(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
std::cerr << "FAIL line " << __LINE__ << ": " #cond "\n"; \
|
||||
++g_fail; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
const char* kPinSha = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
||||
|
||||
std::string valid_allow() {
|
||||
return std::string(
|
||||
"{\"wolfssl\": {\"version\": \"5.7.0\", \"sha256\": \"") +
|
||||
kPinSha +
|
||||
"\", \"source\": \"https://www.wolfssl.com/download\", \"license\": \"GPL-2.0\", "
|
||||
"\"receipt\": \"vendor/wolfssl-5.7.0.receipt\"}, \"dependencies\": [{\"name\": "
|
||||
"\"zlib\", \"version\": \"1.3.1\", \"sha256\": \"" +
|
||||
kPinSha +
|
||||
"\", \"source\": \"https://zlib.net\", \"license\": \"Zlib\"}]}";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
using molo::supply::policy::DependencyPolicy;
|
||||
using molo::supply::policy::WolfPin;
|
||||
|
||||
std::string error;
|
||||
WolfPin::Pin pin;
|
||||
CHECK(WolfPin::parse_pin(valid_allow(), pin, error));
|
||||
CHECK(error.empty());
|
||||
CHECK(pin.version == "5.7.0");
|
||||
CHECK(pin.sha256 == kPinSha);
|
||||
CHECK(pin.source == "https://www.wolfssl.com/download");
|
||||
CHECK(pin.license == "GPL-2.0");
|
||||
|
||||
CHECK(WolfPin::check(pin, "5.7.0", kPinSha) == WolfPin::Check::Ok);
|
||||
CHECK(WolfPin::check(pin, "5.6.0", kPinSha) == WolfPin::Check::VersionMismatch);
|
||||
CHECK(WolfPin::check(pin, "5.7.0",
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") ==
|
||||
WolfPin::Check::HashMismatch);
|
||||
CHECK(std::string(WolfPin::to_string(WolfPin::Check::Ok)) == "ok");
|
||||
|
||||
DependencyPolicy manifest;
|
||||
CHECK(manifest.parse(valid_allow(), error));
|
||||
CHECK(manifest.size() == 1);
|
||||
const DependencyPolicy::Entry* zlib = manifest.find("zlib");
|
||||
CHECK(zlib != nullptr && zlib->version == "1.3.1");
|
||||
CHECK(manifest.find("nope") == nullptr);
|
||||
|
||||
// wolfssl-only file has zero generic entries but still parses.
|
||||
DependencyPolicy wolf_only;
|
||||
CHECK(wolf_only.parse("{\"wolfssl\": {\"version\": \"5.7.0\"}}", error));
|
||||
CHECK(wolf_only.size() == 0);
|
||||
|
||||
// Rejections.
|
||||
WolfPin::Pin bad;
|
||||
CHECK(!WolfPin::parse_pin("{\"wolfssl\": {\"version\": \"5.7.0\"}}", bad, error));
|
||||
CHECK(!WolfPin::parse_pin("{\"wolfssl\": {\"version\": \"5.7.0\", \"sha256\": \"xyz\", "
|
||||
"\"source\": \"s\", \"license\": \"l\", \"receipt\": \"r\"}}",
|
||||
bad, error));
|
||||
CHECK(!WolfPin::parse_pin("{\"dependencies\": []}", bad, error));
|
||||
CHECK(!WolfPin::parse_pin("{oops", bad, error));
|
||||
std::string huge(70000, 'x');
|
||||
CHECK(!WolfPin::parse_pin(huge, bad, error));
|
||||
|
||||
DependencyPolicy over;
|
||||
std::string many = "{\"dependencies\": [";
|
||||
for (int i = 0; i < 65; ++i) {
|
||||
if (i > 0) many += ",";
|
||||
many += "{\"name\": \"d" + std::to_string(i) + "\", \"version\": \"1\", \"sha256\": \"" +
|
||||
std::string(kPinSha) + "\", \"source\": \"s\", \"license\": \"l\"}";
|
||||
}
|
||||
many += "]}";
|
||||
CHECK(!over.parse(many, error));
|
||||
|
||||
// load_file roundtrip through a temp file.
|
||||
const std::string tmp = "/tmp/supply_test_pin_allow.json";
|
||||
{
|
||||
std::FILE* f = std::fopen(tmp.c_str(), "wb");
|
||||
CHECK(f != nullptr);
|
||||
std::string body = valid_allow();
|
||||
CHECK(std::fwrite(body.data(), 1, body.size(), f) == body.size());
|
||||
std::fclose(f);
|
||||
}
|
||||
WolfPin::Pin from_file;
|
||||
CHECK(WolfPin::load_file(tmp, from_file, error));
|
||||
CHECK(from_file.version == "5.7.0");
|
||||
CHECK(!WolfPin::load_file("/tmp/supply_test_pin_missing.json", from_file, error));
|
||||
std::remove(tmp.c_str());
|
||||
|
||||
if (g_fail == 0) std::cout << "test_pin: all checks passed\n";
|
||||
return g_fail == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
// test_provenance: SLSA predicate plus in-toto statement wrapping.
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "src/supplychain/attest/Attestor.hpp"
|
||||
#include "src/supplychain/attest/ProvenanceBuilder.hpp"
|
||||
#include "src/supplychain/caps.hpp"
|
||||
#include "src/supplychain/common/Json.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
int g_fail = 0;
|
||||
#define CHECK(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
std::cerr << "FAIL line " << __LINE__ << ": " #cond "\n"; \
|
||||
++g_fail; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
const char* kShaA = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
||||
const char* kShaB = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
|
||||
|
||||
bool read_all(const std::string& path, std::string& text) {
|
||||
std::FILE* f = std::fopen(path.c_str(), "rb");
|
||||
if (f == nullptr) return false;
|
||||
char chunk[4096];
|
||||
std::size_t n = 0;
|
||||
while ((n = std::fread(chunk, 1, sizeof(chunk), f)) > 0) text.append(chunk, n);
|
||||
std::fclose(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
using molo::supply::attest::Attestor;
|
||||
using molo::supply::attest::ProvenanceBuilder;
|
||||
using molo::supply::caps::kMoloBuildType;
|
||||
using molo::supply::caps::kSlsaPredicateType;
|
||||
using molo::supply::common::Json;
|
||||
|
||||
const std::string golden =
|
||||
(argc > 1 ? std::string(argv[1]) : std::string("tests/src/supplychain/golden")) +
|
||||
"/provenance.json";
|
||||
const bool regen = std::getenv("MOLO_REGEN_GOLDEN") != nullptr;
|
||||
|
||||
// Subjects intentionally unsorted: output must sort by path.
|
||||
std::vector<ProvenanceBuilder::Subject> subjects = {{"sbom.spdx.json", kShaB},
|
||||
{"sbom.cdx.json", kShaA}};
|
||||
std::vector<std::string> toolchain = {"gcc@15.2.0", "cmake@3.30.0"};
|
||||
std::string predicate =
|
||||
ProvenanceBuilder::build("https://moloapp.dev/builder/test",
|
||||
"9f3c1a2b4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8",
|
||||
toolchain, subjects, "molo release --test");
|
||||
CHECK(predicate.find(kMoloBuildType) != std::string::npos);
|
||||
Json::Value pred_root;
|
||||
std::string error;
|
||||
CHECK(Json::parse(predicate, pred_root, error));
|
||||
|
||||
std::string statement;
|
||||
CHECK(Attestor::statement(subjects, kSlsaPredicateType, predicate, statement, error));
|
||||
CHECK(!statement.empty() && statement.back() == '\n');
|
||||
CHECK(statement.find(kSlsaPredicateType) != std::string::npos);
|
||||
CHECK(statement.find("https://in-toto.io/Statement/v1") != std::string::npos);
|
||||
std::size_t cdx = statement.find("sbom.cdx.json");
|
||||
std::size_t spdx = statement.find("sbom.spdx.json");
|
||||
CHECK(cdx != std::string::npos && spdx != std::string::npos && cdx < spdx); // sorted
|
||||
Json::Value stmt_root;
|
||||
CHECK(Json::parse(statement, stmt_root, error));
|
||||
|
||||
// 33rd subject rejected.
|
||||
std::vector<ProvenanceBuilder::Subject> many;
|
||||
for (int i = 0; i < 33; ++i) many.push_back({"f" + std::to_string(i), kShaA});
|
||||
std::string rejected;
|
||||
CHECK(!Attestor::statement(many, kSlsaPredicateType, predicate, rejected, error));
|
||||
// Exactly 32 accepted.
|
||||
many.pop_back();
|
||||
CHECK(Attestor::statement(many, kSlsaPredicateType, predicate, rejected, error));
|
||||
|
||||
// Bad inputs rejected.
|
||||
CHECK(!Attestor::statement(subjects, "", predicate, rejected, error));
|
||||
CHECK(!Attestor::statement(subjects, kSlsaPredicateType, "{oops", rejected, error));
|
||||
std::vector<ProvenanceBuilder::Subject> bad_sub = {{"x", "short"}};
|
||||
CHECK(!Attestor::statement(bad_sub, kSlsaPredicateType, predicate, rejected, error));
|
||||
|
||||
if (regen) {
|
||||
std::FILE* f = std::fopen(golden.c_str(), "wb");
|
||||
CHECK(f != nullptr);
|
||||
if (f != nullptr) {
|
||||
CHECK(std::fwrite(statement.data(), 1, statement.size(), f) == statement.size());
|
||||
std::fclose(f);
|
||||
std::cout << "test_provenance: regenerated " << golden << "\n";
|
||||
}
|
||||
} else {
|
||||
std::string want;
|
||||
CHECK(read_all(golden, want));
|
||||
if (want != statement) {
|
||||
std::size_t at = 0;
|
||||
while (at < want.size() && at < statement.size() && want[at] == statement[at]) ++at;
|
||||
std::cerr << "FAIL golden mismatch at byte " << at << " want=" << want.size()
|
||||
<< " got=" << statement.size() << "\n";
|
||||
++g_fail;
|
||||
}
|
||||
}
|
||||
|
||||
if (g_fail == 0) std::cout << "test_provenance: all checks passed\n";
|
||||
return g_fail == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
// test_scan: OSV client (fake transport) plus vulnerability gate verdicts.
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "src/supplychain/scan/OsvClient.hpp"
|
||||
#include "src/supplychain/scan/VulnHook.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
int g_fail = 0;
|
||||
#define CHECK(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
std::cerr << "FAIL line " << __LINE__ << ": " #cond "\n"; \
|
||||
++g_fail; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
bool read_all(const std::string& path, std::string& text) {
|
||||
std::FILE* f = std::fopen(path.c_str(), "rb");
|
||||
if (f == nullptr) return false;
|
||||
char chunk[4096];
|
||||
std::size_t n = 0;
|
||||
while ((n = std::fread(chunk, 1, sizeof(chunk), f)) > 0) text.append(chunk, n);
|
||||
std::fclose(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
const char* kOsvBody =
|
||||
"{\"vulns\": ["
|
||||
"{\"id\": \"GHSA-crit-1\", \"summary\": \"critical\", \"severity\": [{\"type\": "
|
||||
"\"CVSS_V3\", \"score\": \"9.8\"}], \"affected\": [{\"ranges\": [{\"events\": [{"
|
||||
"\"fixed\": \"5.7.1\"}]}]}]},"
|
||||
"{\"id\": \"GHSA-low-2\", \"summary\": \"low\", \"severity\": [{\"type\": "
|
||||
"\"CVSS_V3\", \"score\": \"4.1\"}], \"affected\": []},"
|
||||
"{\"id\": \"GHSA-nosev-3\", \"details\": \"no score\"}]}";
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
using molo::supply::scan::OsvClient;
|
||||
using molo::supply::scan::VulnHook;
|
||||
|
||||
std::string error;
|
||||
std::vector<OsvClient::Vuln> findings;
|
||||
|
||||
// Fake transport: first call fails, retry succeeds (1 retry cap).
|
||||
int calls = 0;
|
||||
OsvClient::Transport flaky = [&](const std::string& url, const std::string& body,
|
||||
std::string& resp, std::string& terr) {
|
||||
(void)url;
|
||||
(void)body;
|
||||
++calls;
|
||||
if (calls == 1) {
|
||||
terr = "boom";
|
||||
return false;
|
||||
}
|
||||
resp = kOsvBody;
|
||||
return true;
|
||||
};
|
||||
{
|
||||
OsvClient client(flaky);
|
||||
CHECK(client.query("wolfssl", "5.7.0", findings, error));
|
||||
CHECK(findings.size() == 3);
|
||||
CHECK(findings[0].id == "GHSA-crit-1");
|
||||
CHECK(findings[0].cvss > 9.7 && findings[0].cvss < 9.9);
|
||||
CHECK(findings[0].fixed_version == "5.7.1");
|
||||
CHECK(findings[1].cvss > 4.0 && findings[1].cvss < 4.2);
|
||||
CHECK(findings[2].cvss == 0.0);
|
||||
CHECK(client.queries_made() == 2); // initial plus one retry
|
||||
}
|
||||
|
||||
// Offline default transport fails; clean empty response parses.
|
||||
{
|
||||
OsvClient offline;
|
||||
CHECK(!offline.query("wolfssl", "5.7.0", findings, error));
|
||||
std::vector<OsvClient::Vuln> none;
|
||||
CHECK(OsvClient::parse_response("wolfssl", "{}", none, error));
|
||||
CHECK(none.empty());
|
||||
CHECK(!OsvClient::parse_response("wolfssl", "{bad", none, error));
|
||||
CHECK(!offline.query("", "1", findings, error));
|
||||
}
|
||||
|
||||
// Request body shape.
|
||||
CHECK(OsvClient::request_body("wolfssl", "5.7.0").find("\"version\": \"5.7.0\"") !=
|
||||
std::string::npos);
|
||||
|
||||
// Query cap: 64 allowed, 65th rejected.
|
||||
{
|
||||
int n = 0;
|
||||
OsvClient::Transport ok = [&](const std::string&, const std::string&, std::string& resp,
|
||||
std::string&) {
|
||||
++n;
|
||||
resp = "{}";
|
||||
return true;
|
||||
};
|
||||
OsvClient client(ok);
|
||||
for (int i = 0; i < 64; ++i) CHECK(client.query("p", "v", findings, error));
|
||||
CHECK(!client.query("p", "v", findings, error));
|
||||
CHECK(n == 64);
|
||||
}
|
||||
|
||||
// Gate verdicts with fixed clock.
|
||||
const long long now = 1780000000LL;
|
||||
std::string reason;
|
||||
std::vector<OsvClient::Vuln> empty;
|
||||
std::vector<VulnHook::Exception> no_exc;
|
||||
CHECK(VulnHook::evaluate(empty, no_exc, now, false, reason) == VulnHook::Verdict::Clean);
|
||||
CHECK(VulnHook::evaluate(empty, no_exc, now, true, reason) == VulnHook::Verdict::Blocked);
|
||||
|
||||
std::vector<OsvClient::Vuln> crit = {{"GHSA-crit-1", "c", "wolfssl", "5.7.1", 9.8}};
|
||||
CHECK(VulnHook::is_blocking(crit[0]));
|
||||
CHECK(VulnHook::evaluate(crit, no_exc, now, false, reason) == VulnHook::Verdict::Blocked);
|
||||
|
||||
std::vector<OsvClient::Vuln> wolf_fix = {{"GHSA-w-1", "w", "wolfssl", "5.7.1", 7.0}};
|
||||
CHECK(VulnHook::is_blocking(wolf_fix[0])); // wolfssl fix always blocks
|
||||
CHECK(VulnHook::evaluate(wolf_fix, no_exc, now, false, reason) == VulnHook::Verdict::Blocked);
|
||||
|
||||
std::vector<OsvClient::Vuln> low = {{"GHSA-low-2", "l", "zlib", "", 4.1}};
|
||||
CHECK(!VulnHook::is_blocking(low[0]));
|
||||
CHECK(VulnHook::evaluate(low, no_exc, now, false, reason) == VulnHook::Verdict::Disclosed);
|
||||
|
||||
std::vector<VulnHook::Exception> valid_exc = {{"GHSA-crit-1", now + 3600, "accepted risk"}};
|
||||
CHECK(VulnHook::evaluate(crit, valid_exc, now, false, reason) == VulnHook::Verdict::Disclosed);
|
||||
std::vector<VulnHook::Exception> stale_exc = {{"GHSA-crit-1", now - 1, "too old"}};
|
||||
CHECK(VulnHook::evaluate(crit, stale_exc, now, false, reason) == VulnHook::Verdict::Blocked);
|
||||
CHECK(reason.find("exception-expired") != std::string::npos);
|
||||
CHECK(std::string(VulnHook::to_string(VulnHook::Verdict::Clean)) == "clean");
|
||||
|
||||
// Golden advisories file drives the same gate end to end.
|
||||
const std::string golden =
|
||||
(argc > 1 ? std::string(argv[1]) : std::string("tests/src/supplychain/golden")) +
|
||||
"/advisories.json";
|
||||
std::string adv_text;
|
||||
CHECK(read_all(golden, adv_text));
|
||||
std::vector<OsvClient::Vuln> from_file;
|
||||
CHECK(VulnHook::parse_advisories(adv_text, from_file, error));
|
||||
CHECK(from_file.size() == 2);
|
||||
CHECK(VulnHook::evaluate(from_file, no_exc, now, false, reason) == VulnHook::Verdict::Blocked);
|
||||
std::vector<VulnHook::Exception> cover =
|
||||
{{"GHSA-test-0001", now + 604800, "patch scheduled"}};
|
||||
CHECK(VulnHook::evaluate(from_file, cover, now, false, reason) ==
|
||||
VulnHook::Verdict::Disclosed);
|
||||
|
||||
std::vector<VulnHook::Exception> exc;
|
||||
CHECK(VulnHook::parse_exceptions("{\"exceptions\": [{\"id\": \"a\", \"expires\": 9, "
|
||||
"\"reason\": \"r\"}]}",
|
||||
exc, error));
|
||||
CHECK(exc.size() == 1 && exc[0].expires_unix == 9);
|
||||
CHECK(!VulnHook::parse_exceptions("{\"exceptions\": [{\"id\": \"a\"}]}", exc, error));
|
||||
|
||||
if (g_fail == 0) std::cout << "test_scan: all checks passed\n";
|
||||
return g_fail == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
// test_spdx: SBOM generation plus SPDX 2.3 canonical emission.
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "src/supplychain/common/Json.hpp"
|
||||
#include "src/supplychain/policy/DependencyPolicy.hpp"
|
||||
#include "src/supplychain/policy/WolfPin.hpp"
|
||||
#include "src/supplychain/sbom/SbomCheck.hpp"
|
||||
#include "src/supplychain/sbom/SbomGenerator.hpp"
|
||||
#include "src/supplychain/sbom/SpdxEmitter.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
int g_fail = 0;
|
||||
#define CHECK(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
std::cerr << "FAIL line " << __LINE__ << ": " #cond "\n"; \
|
||||
++g_fail; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
const char* kPinSha = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
||||
const char* kCommit = "9f3c1a2b4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8";
|
||||
|
||||
bool read_all(const std::string& path, std::string& text) {
|
||||
std::FILE* f = std::fopen(path.c_str(), "rb");
|
||||
if (f == nullptr) return false;
|
||||
char chunk[4096];
|
||||
std::size_t n = 0;
|
||||
while ((n = std::fread(chunk, 1, sizeof(chunk), f)) > 0) text.append(chunk, n);
|
||||
std::fclose(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool make_doc(molo::supply::sbom::SbomDocument& doc, std::string& error) {
|
||||
using namespace molo::supply;
|
||||
policy::WolfPin::Pin pin{"5.7.0", kPinSha, "https://www.wolfssl.com/download", "GPL-2.0",
|
||||
"vendor/wolfssl-5.7.0.receipt"};
|
||||
policy::DependencyPolicy manifest;
|
||||
std::string allow = std::string("{\"dependencies\": [{\"name\": \"zlib\", \"version\": "
|
||||
"\"1.3.1\", \"sha256\": \"") +
|
||||
kPinSha + "\", \"source\": \"https://zlib.net\", \"license\": \"Zlib\"}]}";
|
||||
if (!manifest.parse(allow, error)) return false;
|
||||
std::vector<sbom::SbomToolchainEntry> toolchain = {{"gcc", "15.2.0"}, {"cmake", "3.30.0"}};
|
||||
return sbom::SbomGenerator::generate(kCommit, toolchain, pin, manifest, "1.2.3-test",
|
||||
"2026-01-01T00:00:00Z", doc, error);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
using namespace molo::supply;
|
||||
const std::string golden =
|
||||
(argc > 1 ? std::string(argv[1]) : std::string("tests/src/supplychain/golden")) +
|
||||
"/sbom.spdx.json";
|
||||
const bool regen = std::getenv("MOLO_REGEN_GOLDEN") != nullptr;
|
||||
|
||||
std::string error;
|
||||
sbom::SbomDocument doc;
|
||||
CHECK(make_doc(doc, error));
|
||||
CHECK(doc.components.size() == 3);
|
||||
CHECK(doc.components[0].name == "moloapp");
|
||||
CHECK(doc.components[1].name == "wolfssl");
|
||||
CHECK(doc.components[2].name == "zlib");
|
||||
|
||||
std::vector<std::string> missing;
|
||||
CHECK(sbom::SbomCheck::check_ntia(doc, missing, error));
|
||||
CHECK(missing.empty());
|
||||
|
||||
// Negative NTIA case.
|
||||
sbom::SbomDocument bad_doc = doc;
|
||||
bad_doc.components[2].sha256 = "nope";
|
||||
CHECK(!sbom::SbomCheck::check_ntia(bad_doc, missing, error));
|
||||
CHECK(missing.size() == 1);
|
||||
|
||||
// Generator rejections.
|
||||
sbom::SbomDocument out;
|
||||
policy::WolfPin::Pin pin{"5.7.0", kPinSha, "s", "l", "r"};
|
||||
policy::DependencyPolicy manifest;
|
||||
std::vector<sbom::SbomToolchainEntry> toolchain = {{"gcc", "15.2.0"}};
|
||||
CHECK(!sbom::SbomGenerator::generate("", toolchain, pin, manifest, "1", "t", out, error));
|
||||
std::vector<sbom::SbomToolchainEntry> too_many(101, {"t", "v"});
|
||||
CHECK(!sbom::SbomGenerator::generate(kCommit, too_many, pin, manifest, "1", "t", out, error));
|
||||
|
||||
// Canonical emission checks.
|
||||
std::string spdx;
|
||||
CHECK(sbom::SpdxEmitter::emit_json(doc, spdx, error));
|
||||
CHECK(!spdx.empty() && spdx.back() == '\n');
|
||||
CHECK(spdx.find('\r') == std::string::npos);
|
||||
CHECK(spdx.find(" \"SPDXID\": \"SPDXRef-DOCUMENT\"") != std::string::npos); // 2-space indent
|
||||
std::size_t a = spdx.find("\"SPDXID\"");
|
||||
std::size_t b = spdx.find("\"creationInfo\"");
|
||||
std::size_t c = spdx.find("\"dataLicense\"");
|
||||
std::size_t d = spdx.find("\"spdxVersion\"");
|
||||
CHECK(a < b && b < c && c < d); // sorted keys
|
||||
CHECK(spdx.find("\"spdxVersion\": \"SPDX-2.3\"") != std::string::npos);
|
||||
// Parses back as an object.
|
||||
common::Json::Value back;
|
||||
CHECK(common::Json::parse(spdx, back, error));
|
||||
|
||||
// 1 MiB cap: 64 components with huge sources must be rejected, not cut.
|
||||
sbom::SbomDocument fat = doc;
|
||||
for (int i = 0; i < 61; ++i) {
|
||||
sbom::SbomComponent comp{"dep" + std::to_string(i), "1",
|
||||
std::string(kPinSha), std::string(70000, 's'), "MIT", "acme"};
|
||||
fat.components.push_back(comp);
|
||||
}
|
||||
CHECK(fat.components.size() == 64);
|
||||
std::string fat_out;
|
||||
CHECK(!sbom::SpdxEmitter::emit_json(fat, fat_out, error));
|
||||
|
||||
// Golden comparison.
|
||||
if (regen) {
|
||||
std::FILE* f = std::fopen(golden.c_str(), "wb");
|
||||
CHECK(f != nullptr);
|
||||
if (f != nullptr) {
|
||||
CHECK(std::fwrite(spdx.data(), 1, spdx.size(), f) == spdx.size());
|
||||
std::fclose(f);
|
||||
std::cout << "test_spdx: regenerated " << golden << "\n";
|
||||
}
|
||||
} else {
|
||||
std::string want;
|
||||
CHECK(read_all(golden, want));
|
||||
if (want != spdx) {
|
||||
std::size_t at = 0;
|
||||
while (at < want.size() && at < spdx.size() && want[at] == spdx[at]) ++at;
|
||||
std::cerr << "FAIL golden mismatch at byte " << at << " want=" << want.size()
|
||||
<< " got=" << spdx.size() << "\n";
|
||||
++g_fail;
|
||||
}
|
||||
}
|
||||
|
||||
if (g_fail == 0) std::cout << "test_spdx: all checks passed\n";
|
||||
return g_fail == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
// test_verify: hashing, offline verification, fake signing, CLI handlers.
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#if __has_include("cli/embed/sha256.hpp")
|
||||
#include "cli/embed/sha256.hpp"
|
||||
#elif __has_include("embed/sha256.hpp")
|
||||
#include "embed/sha256.hpp"
|
||||
#else
|
||||
#include "src/supplychain/adapters/cli/embed/sha256.hpp" // ADAPTER fallback for isolated builds
|
||||
#endif
|
||||
#include "src/supplychain/attest/Signer.hpp"
|
||||
#include "src/supplychain/attest/Verify.hpp"
|
||||
#include "src/supplychain/cli/SupplyCli.hpp"
|
||||
#include "src/supplychain/policy/WolfPin.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
int g_fail = 0;
|
||||
#define CHECK(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
std::cerr << "FAIL line " << __LINE__ << ": " #cond "\n"; \
|
||||
++g_fail; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
const char* kPinSha = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
||||
|
||||
std::string g_root;
|
||||
|
||||
bool write_all(const std::string& path, const std::string& text) {
|
||||
std::FILE* f = std::fopen(path.c_str(), "wb");
|
||||
if (f == nullptr) return false;
|
||||
bool ok = std::fwrite(text.data(), 1, text.size(), f) == text.size();
|
||||
ok = ok && std::fclose(f) == 0;
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool read_all(const std::string& path, std::string& text) {
|
||||
std::FILE* f = std::fopen(path.c_str(), "rb");
|
||||
if (f == nullptr) return false;
|
||||
char chunk[4096];
|
||||
std::size_t n = 0;
|
||||
while ((n = std::fread(chunk, 1, sizeof(chunk), f)) > 0) text.append(chunk, n);
|
||||
std::fclose(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
molo::cli::ParsedArgs make_args(std::vector<std::string> positionals,
|
||||
std::map<std::string, std::string> flags = {}) {
|
||||
molo::cli::ParsedArgs args;
|
||||
args.positionals = std::move(positionals);
|
||||
args.flags = std::move(flags);
|
||||
return args;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
using molo::cli::ExitCode;
|
||||
using molo::cli::Output;
|
||||
using molo::supply::attest::Signer;
|
||||
using molo::supply::attest::Verify;
|
||||
using molo::supply::cli::SupplyCli;
|
||||
using molo::supply::policy::WolfPin;
|
||||
|
||||
g_root = "/tmp/supply_test_verify_" + std::to_string(::getpid());
|
||||
std::filesystem::remove_all(g_root);
|
||||
CHECK(std::filesystem::create_directories(g_root));
|
||||
|
||||
// Independent SHA-256 check vector ("abc").
|
||||
CHECK(molo::embed::Sha256::hex_of_string("abc") ==
|
||||
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
|
||||
CHECK(molo::embed::Sha256::hex_of_string("").size() == 64);
|
||||
std::string missing_hex;
|
||||
CHECK(!molo::embed::Sha256::hex_of_file(g_root + "/nope", missing_hex));
|
||||
|
||||
// Fixture files.
|
||||
const std::string rel = g_root + "/release.bin";
|
||||
const std::string wolf = g_root + "/wolfssl-5.7.0.tar.gz";
|
||||
CHECK(write_all(rel, "release-bytes-v1"));
|
||||
CHECK(write_all(wolf, "wolfssl-bytes-v1"));
|
||||
std::string dig_rel, dig_wolf;
|
||||
CHECK(molo::embed::Sha256::hex_of_file(rel, dig_rel));
|
||||
CHECK(molo::embed::Sha256::hex_of_file(wolf, dig_wolf));
|
||||
CHECK(dig_rel != dig_wolf);
|
||||
|
||||
const std::string subjects_path = g_root + "/subjects.txt";
|
||||
CHECK(write_all(subjects_path, dig_rel + " release.bin\n" + dig_wolf + " wolfssl-5.7.0.tar.gz\n"));
|
||||
const std::string bundle_path = g_root + "/bundle.json";
|
||||
CHECK(write_all(bundle_path, "{\"subjects\": [\"" + dig_rel + "\", \"" + dig_wolf +
|
||||
"\"], \"signature\": \"FAKE\"}\n"));
|
||||
|
||||
std::vector<std::pair<std::string, std::string>> expected = {{rel, dig_rel}, {wolf, dig_wolf}};
|
||||
WolfPin::Pin pin{"5.7.0", dig_wolf, "https://www.wolfssl.com/download", "GPL-2.0", "receipt"};
|
||||
Verify::Result r = Verify::verify_files(expected, bundle_path, &pin);
|
||||
CHECK(r.ok);
|
||||
|
||||
// Tamper with one byte: recompute must fail.
|
||||
CHECK(write_all(rel, "release-bytes-v2"));
|
||||
r = Verify::verify_files(expected, bundle_path, &pin);
|
||||
CHECK(!r.ok);
|
||||
CHECK(write_all(rel, "release-bytes-v1"));
|
||||
CHECK(Verify::verify_files(expected, bundle_path, &pin).ok);
|
||||
|
||||
// Bundle that drops a digest fails the chain check.
|
||||
const std::string thin_bundle = g_root + "/thin.json";
|
||||
CHECK(write_all(thin_bundle, "{\"subjects\": [\"" + dig_rel + "\"]}\n"));
|
||||
r = Verify::verify_files(expected, thin_bundle, &pin);
|
||||
CHECK(!r.ok);
|
||||
|
||||
// Pin mismatch on the wolfssl subject fails.
|
||||
WolfPin::Pin wrong_pin{"5.7.0", std::string(kPinSha), "s", "l", "r"};
|
||||
r = Verify::verify_files(expected, bundle_path, &wrong_pin);
|
||||
CHECK(!r.ok);
|
||||
|
||||
// Empty subjects and missing bundle fail loudly.
|
||||
std::vector<std::pair<std::string, std::string>> none;
|
||||
CHECK(!Verify::verify_files(none, bundle_path, nullptr).ok);
|
||||
CHECK(!Verify::verify_files(expected, g_root + "/absent.json", nullptr).ok);
|
||||
std::vector<std::pair<std::string, std::string>> short_hex = {{rel, "abc"}};
|
||||
CHECK(!Verify::verify_files(short_hex, bundle_path, nullptr).ok);
|
||||
|
||||
// Fake cosign path: deterministic offline bundle.
|
||||
::setenv("MOLO_FAKE_COSIGN", "1", 1);
|
||||
const std::string fake_bundle = g_root + "/fake.bundle.json";
|
||||
std::string sign_error;
|
||||
CHECK(Signer::sign(rel, fake_bundle, sign_error));
|
||||
std::string fake_text;
|
||||
CHECK(read_all(fake_bundle, fake_text));
|
||||
CHECK(fake_text.find(dig_rel) != std::string::npos);
|
||||
std::vector<std::pair<std::string, std::string>> single = {{rel, dig_rel}};
|
||||
CHECK(Verify::verify_files(single, fake_bundle, nullptr).ok);
|
||||
CHECK(Signer::quote_path("a'b") == "'a'\\''b'");
|
||||
CHECK(!Signer::sign("", fake_bundle, sign_error));
|
||||
|
||||
// CLI: run_verify end to end through the adapter Output.
|
||||
{
|
||||
Output out(false, 0, true, false);
|
||||
CHECK(SupplyCli::run_verify(make_args({g_root}), out) == ExitCode::Ok);
|
||||
}
|
||||
{
|
||||
Output out(false, 0, true, false);
|
||||
CHECK(SupplyCli::run_verify(make_args({g_root}, {{"manifest", "subjects.txt"}}), out) ==
|
||||
ExitCode::Ok);
|
||||
}
|
||||
{
|
||||
Output out(false, 0, true, false);
|
||||
CHECK(SupplyCli::run_verify(make_args({g_root}, {{"bogus", "1"}}), out) == ExitCode::Usage);
|
||||
}
|
||||
{
|
||||
Output out(false, 0, true, false);
|
||||
molo::cli::ParsedArgs args = make_args({g_root + "/absent-dir"});
|
||||
CHECK(SupplyCli::run_verify(args, out) == ExitCode::Runtime);
|
||||
}
|
||||
{
|
||||
// JSON mode prints a status object.
|
||||
Output out(true, 0, true, false);
|
||||
molo::cli::ParsedArgs args = make_args({g_root});
|
||||
args.global.format = "json";
|
||||
CHECK(SupplyCli::run_verify(args, out) == ExitCode::Ok);
|
||||
}
|
||||
|
||||
// CLI: run_check with clean advisories passes, blocking ones fail.
|
||||
const std::string allow_path = g_root + "/supply.allow.json";
|
||||
CHECK(write_all(allow_path, "{\"wolfssl\": {\"version\": \"5.7.0\", \"sha256\": \"" +
|
||||
std::string(kPinSha) +
|
||||
"\", \"source\": \"https://www.wolfssl.com/download\", "
|
||||
"\"license\": \"GPL-2.0\", \"receipt\": \"r\"}}"));
|
||||
CHECK(write_all(g_root + "/advisories.json", "{\"advisories\": []}"));
|
||||
{
|
||||
Output out(false, 0, true, false);
|
||||
CHECK(SupplyCli::run_check(make_args({g_root}), out) == ExitCode::Ok);
|
||||
}
|
||||
CHECK(write_all(g_root + "/advisories.json",
|
||||
"{\"advisories\": [{\"id\": \"GHSA-x\", \"package\": \"wolfssl\", \"cvss\": "
|
||||
"9.9, \"fixed\": \"5.7.1\", \"summary\": \"s\"}]}"));
|
||||
{
|
||||
Output out(false, 0, true, false);
|
||||
CHECK(SupplyCli::run_check(make_args({g_root}), out) == ExitCode::Runtime);
|
||||
}
|
||||
// Unexpired exception downgrades to disclosed (still exit 0).
|
||||
CHECK(write_all(g_root + "/exceptions.json",
|
||||
"{\"exceptions\": [{\"id\": \"GHSA-x\", \"expires\": 4100000000, \"reason\": "
|
||||
"\"scheduled\"}]}"));
|
||||
{
|
||||
Output out(false, 0, true, false);
|
||||
CHECK(SupplyCli::run_check(make_args({g_root}, {{"exceptions", "exceptions.json"}}), out) ==
|
||||
ExitCode::Ok);
|
||||
}
|
||||
|
||||
// CLI: run_release full six steps with fake cosign.
|
||||
CHECK(write_all(g_root + "/advisories.json", "{\"advisories\": []}"));
|
||||
CHECK(write_all(g_root + "/suite.stamp", "PASS\n"));
|
||||
CHECK(write_all(g_root + "/toolchain.txt", "gcc@15.2.0\ncmake@3.30.0\n"));
|
||||
{
|
||||
Output out(false, 0, true, false);
|
||||
molo::cli::ParsedArgs args =
|
||||
make_args({}, {{"dir", g_root},
|
||||
{"commit", "9f3c1a2b4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8"},
|
||||
{"suite-stamp", "suite.stamp"}});
|
||||
CHECK(SupplyCli::run_release(args, out) == ExitCode::Ok);
|
||||
std::string tag;
|
||||
CHECK(read_all(g_root + "/TAG", tag));
|
||||
CHECK(tag.find("9f3c1a2b") != std::string::npos);
|
||||
std::string prov;
|
||||
CHECK(read_all(g_root + "/provenance.json", prov));
|
||||
CHECK(prov.find("https://slsa.dev/provenance/v1") != std::string::npos);
|
||||
std::string bundle;
|
||||
CHECK(read_all(g_root + "/bundle.json", bundle));
|
||||
CHECK(!bundle.empty());
|
||||
std::string spdx_text;
|
||||
CHECK(read_all(g_root + "/sbom.spdx.json", spdx_text));
|
||||
}
|
||||
{
|
||||
// Failing stamp stops at step 1.
|
||||
CHECK(write_all(g_root + "/suite.stamp", "FAIL\n"));
|
||||
Output out(false, 0, true, false);
|
||||
molo::cli::ParsedArgs args =
|
||||
make_args({}, {{"dir", g_root}, {"commit", "abc"}, {"suite-stamp", "suite.stamp"}});
|
||||
CHECK(SupplyCli::run_release(args, out) == ExitCode::Runtime);
|
||||
CHECK(write_all(g_root + "/suite.stamp", "PASS\n"));
|
||||
}
|
||||
{
|
||||
// Missing required flags is usage, not runtime.
|
||||
Output out(false, 0, true, false);
|
||||
CHECK(SupplyCli::run_release(make_args({}), out) == ExitCode::Usage);
|
||||
}
|
||||
::unsetenv("MOLO_FAKE_COSIGN");
|
||||
|
||||
std::filesystem::remove_all(g_root);
|
||||
|
||||
if (g_fail == 0) std::cout << "test_verify: all checks passed\n";
|
||||
return g_fail == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in New Issue
Block a user