feat(registry): store and serve signed binary variants
This commit is contained in:
1 parent
64116f6e0d
commit
663215f7f3
7 files changed
+378
-17
No files matched your search
@@ -299,6 +299,29 @@ Void::Result::Result<void, Void::Result::Error> RegistryService::load_locked() {
|
||||
rm.published_epoch = get_i64(*ro, "published_epoch", 0);
|
||||
rm.signature = get_str(*ro, "signature");
|
||||
rm.public_key = get_str(*ro, "public_key");
|
||||
if (const auto *bv = find_key(*ro, "binaries")) {
|
||||
if (const auto *barr =
|
||||
std::get_if<JsonVal::Array>(&bv->value)) {
|
||||
for (const auto &b : *barr) {
|
||||
if (const auto *bo =
|
||||
std::get_if<JsonVal::Object>(&b.value)) {
|
||||
BinaryReleaseMeta bm;
|
||||
bm.variant_id = get_str(*bo, "variant_id");
|
||||
bm.sha256 = get_str(*bo, "sha256");
|
||||
bm.size_bytes = static_cast<std::uint64_t>(
|
||||
get_i64(*bo, "size_bytes", 0));
|
||||
bm.signature = get_str(*bo, "signature");
|
||||
bm.public_key = get_str(*bo, "public_key");
|
||||
bm.publisher = get_str(*bo, "publisher");
|
||||
bm.published_epoch =
|
||||
get_i64(*bo, "published_epoch", 0);
|
||||
if (!bm.variant_id.empty()) {
|
||||
rm.binaries[bm.variant_id] = std::move(bm);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
p.releases[rm.version] = std::move(rm);
|
||||
}
|
||||
}
|
||||
@@ -382,6 +405,21 @@ Void::Result::Result<void, Void::Result::Error> RegistryService::save_locked() {
|
||||
(r->public_key.empty()
|
||||
? std::string{}
|
||||
: ",\"public_key\":" + json_quote(r->public_key)) +
|
||||
",\"binaries\":" +
|
||||
json_array(
|
||||
r->binaries,
|
||||
[](const auto &entry) {
|
||||
const auto &b = entry.second;
|
||||
return "{\"variant_id\":" + json_quote(b.variant_id) +
|
||||
",\"sha256\":" + json_quote(b.sha256) +
|
||||
",\"size_bytes\":" +
|
||||
std::to_string(b.size_bytes) +
|
||||
",\"signature\":" + json_quote(b.signature) +
|
||||
",\"public_key\":" + json_quote(b.public_key) +
|
||||
",\"publisher\":" + json_quote(b.publisher) +
|
||||
",\"published_epoch\":" +
|
||||
std::to_string(b.published_epoch) + '}';
|
||||
}) +
|
||||
'}';
|
||||
}) +
|
||||
'}';
|
||||
@@ -792,22 +830,35 @@ HttpResponse RegistryService::handle_request(const HttpRequest &req) {
|
||||
",\"description\":" + json_quote(pit->second.description) +
|
||||
",\"is_private\":" + std::string{pit->second.is_private ? "true" : "false"} +
|
||||
",\"owners\":" + json_array(pit->second.owners, json_quote) + ",\"releases\":" +
|
||||
json_array(rlist,
|
||||
[](const ReleaseMeta *r) {
|
||||
return "{\"version\":" + json_quote(r->version) +
|
||||
",\"sha256\":" + json_quote(r->sha256) +
|
||||
",\"size_bytes\":" + std::to_string(r->size_bytes) +
|
||||
",\"yanked\":" +
|
||||
std::string{r->yanked ? "true" : "false"} +
|
||||
",\"publisher\":" + json_quote(r->publisher) +
|
||||
(r->signature.empty()
|
||||
? std::string{}
|
||||
: ",\"signature\":" + json_quote(r->signature)) +
|
||||
(r->public_key.empty()
|
||||
? std::string{}
|
||||
: ",\"public_key\":" + json_quote(r->public_key)) +
|
||||
'}';
|
||||
}) +
|
||||
json_array(
|
||||
rlist,
|
||||
[](const ReleaseMeta *r) {
|
||||
return "{\"version\":" + json_quote(r->version) +
|
||||
",\"sha256\":" + json_quote(r->sha256) +
|
||||
",\"size_bytes\":" + std::to_string(r->size_bytes) +
|
||||
",\"yanked\":" + std::string{r->yanked ? "true" : "false"} +
|
||||
",\"publisher\":" + json_quote(r->publisher) +
|
||||
(r->signature.empty()
|
||||
? std::string{}
|
||||
: ",\"signature\":" + json_quote(r->signature)) +
|
||||
(r->public_key.empty()
|
||||
? std::string{}
|
||||
: ",\"public_key\":" + json_quote(r->public_key)) +
|
||||
",\"binaries\":" +
|
||||
json_array(
|
||||
r->binaries,
|
||||
[](const auto &entry) {
|
||||
const auto &b = entry.second;
|
||||
return "{\"variant_id\":" + json_quote(b.variant_id) +
|
||||
",\"sha256\":" + json_quote(b.sha256) +
|
||||
",\"size_bytes\":" +
|
||||
std::to_string(b.size_bytes) +
|
||||
",\"signature\":" + json_quote(b.signature) +
|
||||
",\"public_key\":" + json_quote(b.public_key) +
|
||||
'}';
|
||||
}) +
|
||||
'}';
|
||||
}) +
|
||||
"}\n");
|
||||
}
|
||||
|
||||
@@ -979,6 +1030,154 @@ HttpResponse RegistryService::handle_request(const HttpRequest &req) {
|
||||
return resp;
|
||||
}
|
||||
|
||||
// Publish a prebuilt binary variant:
|
||||
// PUT /api/v1/packages/<name>/<version>/binaries/<variant>
|
||||
if (segs.size() == 7 && segs[5] == "binaries" && req.method == "PUT") {
|
||||
if (!principal) {
|
||||
return HttpResponse::error_json(401, "unauthorized", "authentication required");
|
||||
}
|
||||
if (!check_csrf_locked(req, *principal)) {
|
||||
return HttpResponse::error_json(403, "csrf_failed",
|
||||
"missing or invalid CSRF token");
|
||||
}
|
||||
const std::string &ver_str = segs[4];
|
||||
const std::string &variant_id = segs[6];
|
||||
const bool valid_variant = !variant_id.empty() && variant_id.size() <= 128 &&
|
||||
std::ranges::all_of(variant_id, [](char c) {
|
||||
return std::isalnum(static_cast<unsigned char>(c)) ||
|
||||
c == '.' || c == '_' || c == '-';
|
||||
});
|
||||
if (!valid_variant) {
|
||||
return HttpResponse::error_json(400, "invalid_variant_id",
|
||||
"variant id must be 1-128 chars of "
|
||||
"[A-Za-z0-9._-]");
|
||||
}
|
||||
const auto pit = packages_.find(pkg_name);
|
||||
if (pit == packages_.end()) {
|
||||
return HttpResponse::error_json(404, "not_found", "package not found");
|
||||
}
|
||||
if (!can_publish_pkg(&pit->second)) {
|
||||
return HttpResponse::error_json(403, "forbidden",
|
||||
"insufficient publish authority or ownership");
|
||||
}
|
||||
const auto rit = pit->second.releases.find(ver_str);
|
||||
if (rit == pit->second.releases.end()) {
|
||||
return HttpResponse::error_json(404, "not_found", "release not found");
|
||||
}
|
||||
const std::string actual_sha = Util::sha256_hex(req.body);
|
||||
if (const auto claimed = req.header("x-content-sha256");
|
||||
claimed && *claimed != actual_sha) {
|
||||
return HttpResponse::error_json(400, "digest_mismatch",
|
||||
"X-Content-SHA256 does not match uploaded bytes");
|
||||
}
|
||||
if (req.body.size() > config_.max_upload_bytes) {
|
||||
return HttpResponse::error_json(413, "too_large", "binary artifact exceeds limit");
|
||||
}
|
||||
if (total_storage_bytes_locked() + req.body.size() > config_.max_storage_bytes) {
|
||||
return HttpResponse::error_json(413, "quota_exceeded",
|
||||
"registry storage quota exceeded");
|
||||
}
|
||||
std::string binary_signature;
|
||||
std::string binary_public_key;
|
||||
if (const auto sig = req.header("x-binary-signature"); sig) {
|
||||
binary_signature = std::string{*sig};
|
||||
}
|
||||
if (const auto key = req.header("x-binary-public-key"); key) {
|
||||
binary_public_key = std::string{*key};
|
||||
}
|
||||
if (binary_signature.empty() != binary_public_key.empty()) {
|
||||
return HttpResponse::error_json(
|
||||
400, "invalid_signature",
|
||||
"X-Binary-Signature and X-Binary-Public-Key must be provided together");
|
||||
}
|
||||
auto is_hex = [](std::string_view text) {
|
||||
return std::ranges::all_of(text, [](char c) {
|
||||
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
|
||||
(c >= 'A' && c <= 'F');
|
||||
});
|
||||
};
|
||||
if (!binary_signature.empty() &&
|
||||
(binary_signature.size() != 128 || !is_hex(binary_signature))) {
|
||||
return HttpResponse::error_json(400, "invalid_signature",
|
||||
"X-Binary-Signature must be 128 hex characters");
|
||||
}
|
||||
if (!binary_public_key.empty() &&
|
||||
(binary_public_key.size() != 64 || !is_hex(binary_public_key))) {
|
||||
return HttpResponse::error_json(400, "invalid_signature",
|
||||
"X-Binary-Public-Key must be 64 hex characters");
|
||||
}
|
||||
if (const auto bit = rit->second.binaries.find(variant_id);
|
||||
bit != rit->second.binaries.end()) {
|
||||
if (bit->second.sha256 == actual_sha) {
|
||||
return HttpResponse::json(200, "{\"status\":\"already_published\",\"sha256\":" +
|
||||
json_quote(actual_sha) + "}\n");
|
||||
}
|
||||
return HttpResponse::error_json(
|
||||
409, "immutable_conflict",
|
||||
"binary variant already exists with different content digest");
|
||||
}
|
||||
|
||||
Void::FileSystem::TempDir probe_tmp{"vbs_reg_binary_"};
|
||||
const auto staged = probe_tmp / "binary";
|
||||
if (auto w = Platform::atomic_write_file(staged, req.body, false); !w) {
|
||||
return HttpResponse::error_json(500, "stage_error", w.error().message());
|
||||
}
|
||||
const auto dst_blob = blob_path(actual_sha);
|
||||
std::error_code ec;
|
||||
if (!std::filesystem::exists(dst_blob, ec)) {
|
||||
auto copied = Platform::copy_isolated(staged, dst_blob, 0444u);
|
||||
if (!copied) {
|
||||
return HttpResponse::error_json(500, "blob_publish_failed",
|
||||
copied.error().message());
|
||||
}
|
||||
}
|
||||
rit->second.binaries[variant_id] = BinaryReleaseMeta{
|
||||
variant_id, actual_sha, req.body.size(), binary_signature,
|
||||
binary_public_key, principal->username, now_epoch()};
|
||||
append_audit_locked(principal->username, "binary.publish",
|
||||
pkg_name + "@" + ver_str + "/" + variant_id, "ok",
|
||||
"sha256=" + actual_sha);
|
||||
if (auto s = save_locked(); !s) {
|
||||
return HttpResponse::error_json(500, "metadata_commit_failed", s.error().message());
|
||||
}
|
||||
return HttpResponse::json(201, "{\"variant_id\":" + json_quote(variant_id) +
|
||||
",\"sha256\":" + json_quote(actual_sha) + "}\n");
|
||||
}
|
||||
|
||||
// Download a prebuilt binary variant:
|
||||
// GET /api/v1/packages/<name>/<version>/binaries/<variant>/download
|
||||
if (segs.size() == 8 && segs[5] == "binaries" && segs[7] == "download" &&
|
||||
req.method == "GET") {
|
||||
const auto pit = packages_.find(pkg_name);
|
||||
if (pit == packages_.end()) {
|
||||
return HttpResponse::error_json(404, "not_found", "package not found");
|
||||
}
|
||||
if (!can_read_pkg(pit->second)) {
|
||||
return HttpResponse::error_json(principal ? 403 : 401, "forbidden",
|
||||
"private package requires authorization");
|
||||
}
|
||||
const auto rit = pit->second.releases.find(segs[4]);
|
||||
if (rit == pit->second.releases.end()) {
|
||||
return HttpResponse::error_json(404, "not_found", "release not found");
|
||||
}
|
||||
const auto bit = rit->second.binaries.find(segs[6]);
|
||||
if (bit == rit->second.binaries.end()) {
|
||||
return HttpResponse::error_json(404, "not_found", "binary variant not found");
|
||||
}
|
||||
const auto bpath = blob_path(bit->second.sha256);
|
||||
auto bytes = Void::FileSystem::read_file_bounded(bpath, {256ULL * 1024ULL * 1024ULL});
|
||||
if (!bytes || Util::sha256_hex(*bytes) != bit->second.sha256) {
|
||||
return HttpResponse::error_json(500, "integrity_error",
|
||||
"stored binary blob missing or corrupted");
|
||||
}
|
||||
HttpResponse resp;
|
||||
resp.status = 200;
|
||||
resp.headers["Content-Type"] = "application/octet-stream";
|
||||
resp.headers["X-Content-SHA256"] = bit->second.sha256;
|
||||
resp.body = std::move(*bytes);
|
||||
return resp;
|
||||
}
|
||||
|
||||
// Yank/unyank release: POST /api/v1/packages/<name>/<version>/yank
|
||||
if (segs.size() == 6 && segs[5] == "yank" && req.method == "POST") {
|
||||
if (!principal) {
|
||||
|
||||
Reference in new issue
Block a user