feat(package): add registry binary-variant client API and vbs publish-binary
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 5s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 27s

This commit is contained in:
faumaray committed 2026-12-03 21:04:48 +03:00
1 parent 663215f7f3
commit 8245a209a5
10 files changed
+290 -2

No files matched your search

+147
View File
@@ -4,6 +4,7 @@
#include <Void/Build/Platform/FileOps.hpp>
#include <Void/Build/Platform/ProcessSupervisor.hpp>
#include <Void/Build/Provenance/Signing.hpp>
#include <Void/Build/Provenance/Verification.hpp>
#include <Void/Build/Util/Sha256.hpp>
#include <Void/Core/Scope.hpp>
#include <Void/Data/StrictJson.hpp>
@@ -268,6 +269,15 @@ bool get_bool(const JsonVal::Object &obj, const std::string &k, bool def = false
return b ? *b : def;
}
std::int64_t get_i64(const JsonVal::Object &obj, const std::string &k, std::int64_t def = 0) {
const auto it = obj.find(k);
if (it == obj.end()) {
return def;
}
const auto *i = std::get_if<std::int64_t>(&it->second.value);
return i ? *i : def;
}
} // namespace
Void::Result::Result<std::vector<ReleaseRecord>, Void::Result::Error>
@@ -379,6 +389,143 @@ download_release_archive(std::string_view registry_url, std::string_view package
return dest_file;
}
Void::Result::Result<std::string, Void::Result::Error>
publish_binary_variant(const BinaryPublishOptions &options) {
if (!Void::Semver::parse_strict(options.version)) {
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
"binary variant version must be strict SemVer")};
}
if (options.variant_id.empty()) {
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
"binary variant id must not be empty")};
}
auto parsed_url = parse_registry_url(options.registry_url);
if (!parsed_url) {
return std::unexpected{parsed_url.error()};
}
auto bytes = Void::FileSystem::read_file(options.artifact_path);
if (!bytes) {
return std::unexpected{bytes.error()};
}
const std::string sha = Util::sha256_hex(*bytes);
std::map<std::string, std::string, std::less<>> headers;
headers["X-Content-SHA256"] = sha;
if (!options.auth.bearer_token.empty()) {
headers["Authorization"] = "Bearer " + options.auth.bearer_token;
}
if (options.signing_seed_path) {
const std::string message = Provenance::canonical_binary_message(options.variant_id, sha);
auto signature = Provenance::sign_message(
std::as_bytes(std::span<const char>{message.data(), message.size()}),
*options.signing_seed_path);
if (!signature) {
return std::unexpected{signature.error()};
}
auto public_key = Provenance::public_key_from_seed(*options.signing_seed_path);
if (!public_key) {
return std::unexpected{public_key.error()};
}
headers["X-Binary-Signature"] = *signature;
headers["X-Binary-Public-Key"] = *public_key;
}
const std::string path = "/api/v1/packages/" +
Void::Encoding::Url::encode(options.package_name) + "/" +
Void::Encoding::Url::encode(options.version) + "/binaries/" +
Void::Encoding::Url::encode(options.variant_id);
auto reply = send_http_request(*parsed_url, "PUT", path, headers, *bytes);
if (!reply) {
return std::unexpected{reply.error()};
}
if (reply->status != 200 && reply->status != 201) {
return std::unexpected{make_error(
reply->status == 409 ? Void::System::SystemErrorCode::already_exists
: Void::System::SystemErrorCode::permission_denied,
std::format("registry binary publish {}@{}/{} failed with HTTP {}",
options.package_name, options.version, options.variant_id, reply->status))};
}
return sha;
}
Void::Result::Result<std::vector<BinaryReleaseInfo>, Void::Result::Error>
fetch_binary_variants(std::string_view registry_url, std::string_view package_name,
std::string_view version, const RegistryAuth &auth) {
auto parsed_url = parse_registry_url(registry_url);
if (!parsed_url) {
return std::unexpected{parsed_url.error()};
}
std::map<std::string, std::string, std::less<>> headers;
if (!auth.bearer_token.empty()) {
headers["Authorization"] = "Bearer " + auth.bearer_token;
}
const std::string path = "/api/v1/packages/" + Void::Encoding::Url::encode(package_name);
auto reply = send_http_request(*parsed_url, "GET", path, headers, {});
if (!reply) {
return std::unexpected{reply.error()};
}
if (reply->status != 200) {
return std::unexpected{
make_error(reply->status == 404 ? Void::System::SystemErrorCode::not_found
: Void::System::SystemErrorCode::io_error,
std::format("registry GET {} failed with HTTP {}", path, reply->status))};
}
auto json = Void::Json::Strict::parse(reply->body);
if (!json) {
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
"invalid JSON returned by registry package endpoint")};
}
const auto *root = std::get_if<JsonVal::Object>(&json->value);
if (!root) {
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
"registry response root must be a JSON object")};
}
std::vector<BinaryReleaseInfo> variants;
const auto rit = root->find("releases");
if (rit == root->end()) {
return variants;
}
const auto *releases = std::get_if<JsonVal::Array>(&rit->second.value);
if (!releases) {
return variants;
}
for (const auto &rv : *releases) {
const auto *ro = std::get_if<JsonVal::Object>(&rv.value);
if (!ro || get_str(*ro, "version") != version) {
continue;
}
const auto bit = ro->find("binaries");
if (bit == ro->end()) {
continue;
}
const auto *binaries = std::get_if<JsonVal::Array>(&bit->second.value);
if (!binaries) {
continue;
}
for (const auto &bv : *binaries) {
const auto *bo = std::get_if<JsonVal::Object>(&bv.value);
if (!bo) {
continue;
}
BinaryReleaseInfo info;
info.variant_id = get_str(*bo, "variant_id");
info.sha256 = get_str(*bo, "sha256");
info.size_bytes = static_cast<std::uint64_t>(get_i64(*bo, "size_bytes", 0));
if (const auto sig = get_str(*bo, "signature"); !sig.empty()) {
info.signature = sig;
}
if (const auto key = get_str(*bo, "public_key"); !key.empty()) {
info.public_key = key;
}
if (!info.variant_id.empty()) {
variants.push_back(std::move(info));
}
}
}
return variants;
}
Void::Result::Result<std::filesystem::path, Void::Result::Error>
download_binary_artifact(std::string_view url, std::string_view expected_sha256,
const std::filesystem::path &dest_file) {