feat(package): add registry binary-variant client API and vbs publish-binary
This commit is contained in:
1 parent
663215f7f3
commit
8245a209a5
10 files changed
+290
-2
No files matched your search
@@ -1109,6 +1109,48 @@ int execute_bridge_command(const BridgeCmdOptions &opts) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct PublishBinaryCmdOptions {
|
||||
std::optional<std::string> registry_arg;
|
||||
std::optional<std::string> name_arg;
|
||||
std::optional<std::string> version_arg;
|
||||
std::optional<std::string> variant_arg;
|
||||
std::optional<std::string> file_arg;
|
||||
std::optional<std::string> signing_key_arg;
|
||||
std::string token_env{"VBS_REGISTRY_TOKEN"};
|
||||
};
|
||||
|
||||
int execute_publish_binary_command(const PublishBinaryCmdOptions &opts) {
|
||||
const std::string registry_url = opts.registry_arg
|
||||
? *opts.registry_arg
|
||||
: Void::Configuration::get_env_or("VBS_REGISTRY_URL", "");
|
||||
if (registry_url.empty() || !opts.name_arg || !opts.version_arg || !opts.variant_arg ||
|
||||
!opts.file_arg) {
|
||||
std::cerr << "vbs publish-binary: --registry, --name, --version, --variant, and --file "
|
||||
"are required\n";
|
||||
return 2;
|
||||
}
|
||||
Package::BinaryPublishOptions popts;
|
||||
popts.registry_url = registry_url;
|
||||
popts.package_name = *opts.name_arg;
|
||||
popts.version = *opts.version_arg;
|
||||
popts.variant_id = *opts.variant_arg;
|
||||
popts.artifact_path = std::filesystem::path{*opts.file_arg};
|
||||
popts.auth.bearer_token = Void::Configuration::get_env_or(opts.token_env, "");
|
||||
if (opts.signing_key_arg) {
|
||||
popts.signing_seed_path = std::filesystem::path{*opts.signing_key_arg};
|
||||
}
|
||||
Void::Log::info("Publish", "Uploading binary variant {}@{}/{} to {}...", popts.package_name,
|
||||
popts.version, popts.variant_id, registry_url);
|
||||
auto sha = Package::publish_binary_variant(popts);
|
||||
if (!sha) {
|
||||
std::cerr << "vbs publish-binary: " << sha.error().message() << '\n';
|
||||
return 1;
|
||||
}
|
||||
std::cout << "published binary " << popts.package_name << '@' << popts.version << '/'
|
||||
<< popts.variant_id << " sha256=" << *sha << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct PackCmdOptions {
|
||||
std::string project_arg;
|
||||
std::string out_arg{"package.tar"};
|
||||
@@ -2179,6 +2221,7 @@ int main(int argc, char **argv) {
|
||||
PackCmdOptions pack_opts;
|
||||
pack_opts.project_arg = cwd;
|
||||
PublishCmdOptions publish_opts;
|
||||
PublishBinaryCmdOptions publish_binary_opts;
|
||||
publish_opts.project_arg = cwd;
|
||||
BridgeCmdOptions bridge_opts;
|
||||
GcCmdOptions gc_opts;
|
||||
@@ -2327,6 +2370,21 @@ int main(int argc, char **argv) {
|
||||
uninstall_opts.prefix_arg);
|
||||
add_log_flags(uninstall_cmd);
|
||||
|
||||
auto &publish_binary_cmd = root.subcommand(
|
||||
"publish-binary", "Publish a prebuilt binary variant to a VBS package registry");
|
||||
publish_binary_cmd
|
||||
.option("--registry", "-r", "Registry base URL (or VBS_REGISTRY_URL)",
|
||||
publish_binary_opts.registry_arg)
|
||||
.option("--name", "-n", "Package name", publish_binary_opts.name_arg)
|
||||
.option("--version", "-v", "Release version", publish_binary_opts.version_arg)
|
||||
.option("--variant", "", "Binary variant id", publish_binary_opts.variant_arg)
|
||||
.option("--file", "-f", "Prebuilt artifact file", publish_binary_opts.file_arg)
|
||||
.option("--signing-key", "", "Ed25519 signing seed file (64 hex characters)",
|
||||
publish_binary_opts.signing_key_arg)
|
||||
.option_default("--token-env", "", "Environment variable holding bearer token",
|
||||
publish_binary_opts.token_env, "VBS_REGISTRY_TOKEN");
|
||||
add_log_flags(publish_binary_cmd, false); // `-v` is `--version` on publish-binary.
|
||||
|
||||
auto &pack_cmd = root.subcommand("pack", "Create a deterministic USTAR source archive (.tar)");
|
||||
pack_cmd.option_default("--project", "-C", "Project directory", pack_opts.project_arg, cwd)
|
||||
.option_default("--output", "-o", "Output archive path", pack_opts.out_arg, "package.tar");
|
||||
@@ -2553,6 +2611,9 @@ int main(int argc, char **argv) {
|
||||
if (cmd == "publish") {
|
||||
return execute_publish_command(publish_opts);
|
||||
}
|
||||
if (cmd == "publish-binary") {
|
||||
return execute_publish_binary_command(publish_binary_opts);
|
||||
}
|
||||
if (cmd == "bridge") {
|
||||
return execute_bridge_command(bridge_opts);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user