feat(package): authenticate private binary-variant downloads
This commit is contained in:
1 parent
40fef50a6f
commit
4e5bdab8cd
10 files changed
+140
-8
No files matched your search
@@ -681,6 +681,116 @@ VOID_TEST(RegistryTests, registry_binary_variant_publish_download_and_materializ
|
||||
(*svc)->stop_http();
|
||||
}
|
||||
|
||||
VOID_TEST(RegistryTests, private_binary_variant_download_requires_auth) {
|
||||
if (!Provenance::signing_available()) {
|
||||
return;
|
||||
}
|
||||
Void::FileSystem::TempDir tmp{"vbs_priv_binary_"};
|
||||
RegistryConfig cfg;
|
||||
cfg.data_dir = tmp / "reg_data";
|
||||
cfg.web_root = std::filesystem::path{Bootstrap::source_dir} / "registry" / "web";
|
||||
cfg.bind_address = "127.0.0.1";
|
||||
cfg.port = 0;
|
||||
cfg.bootstrap_admin_user = "admin";
|
||||
cfg.bootstrap_admin_password = "admin_pw";
|
||||
|
||||
auto svc = RegistryService::open(cfg);
|
||||
Void::Testing::expect(svc.has_value());
|
||||
if (!svc) {
|
||||
return;
|
||||
}
|
||||
Void::Testing::expect(
|
||||
(*svc)->create_user("priv_pub", "priv_pass", Role::Publisher).has_value());
|
||||
auto tok = (*svc)->create_token("priv_pub", "publish", {"read", "publish"});
|
||||
Void::Testing::expect(tok.has_value());
|
||||
if (!tok) {
|
||||
return;
|
||||
}
|
||||
Void::Testing::expect((*svc)->start_http().has_value());
|
||||
const auto port = (*svc)->bound_port();
|
||||
const std::string reg_url = "http://127.0.0.1:" + std::to_string(port);
|
||||
|
||||
const auto pkg_src = tmp / "priv_pkg";
|
||||
(void)Platform::atomic_write_file(pkg_src / "payload.txt", "PRIVATE_SOURCE", false);
|
||||
const auto tar = tmp / "priv.tar";
|
||||
auto source_sha = Package::create_source_package(pkg_src, tar, 0);
|
||||
Void::Testing::require(source_sha.has_value());
|
||||
auto source_bytes = Void::FileSystem::read_file(tar);
|
||||
Void::Testing::require(source_bytes.has_value());
|
||||
auto src_pub = send_loopback_http(port, "PUT", "/api/v1/packages/priv-bin/1.0.0",
|
||||
{{"Authorization", "Bearer " + *tok},
|
||||
{"X-Content-SHA256", *source_sha},
|
||||
{"X-Package-Private", "true"}},
|
||||
*source_bytes);
|
||||
Void::Testing::require(src_pub.has_value() && src_pub->status == 201);
|
||||
|
||||
const std::string artifact = "PRIVATE_BINARY_BYTES";
|
||||
const std::string artifact_sha = Util::sha256_hex(artifact);
|
||||
const std::string variant_id = "private-variant";
|
||||
const std::string binary_path = "/api/v1/packages/priv-bin/1.0.0/binaries/" + variant_id;
|
||||
const auto seed_path = tmp / "private.seed";
|
||||
Void::Testing::require(Provenance::generate_signing_seed(seed_path).has_value());
|
||||
auto public_key = Provenance::public_key_from_seed(seed_path);
|
||||
Void::Testing::require(public_key.has_value());
|
||||
const std::string message = Provenance::canonical_binary_message(variant_id, artifact_sha);
|
||||
auto signature = Provenance::sign_message(
|
||||
std::as_bytes(std::span<const char>{message.data(), message.size()}), seed_path);
|
||||
Void::Testing::require(signature.has_value());
|
||||
auto bin_pub = send_loopback_http(port, "PUT", binary_path,
|
||||
{{"Authorization", "Bearer " + *tok},
|
||||
{"X-Content-SHA256", artifact_sha},
|
||||
{"X-Binary-Signature", *signature},
|
||||
{"X-Binary-Public-Key", *public_key}},
|
||||
artifact);
|
||||
Void::Testing::require(bin_pub.has_value() && bin_pub->status == 201);
|
||||
|
||||
const std::string download_url = reg_url + binary_path + "/download";
|
||||
auto anonymous = send_loopback_http(port, "GET", binary_path + "/download");
|
||||
Void::Testing::expect(anonymous.has_value() && anonymous->status == 401);
|
||||
|
||||
const auto dest = tmp / "private-binary.bin";
|
||||
auto downloaded = Package::download_binary_artifact(download_url, artifact_sha, dest,
|
||||
Package::RegistryAuth{*tok});
|
||||
Void::Testing::require(downloaded.has_value());
|
||||
auto bytes = Void::FileSystem::read_file(dest);
|
||||
Void::Testing::require(bytes.has_value());
|
||||
Void::Testing::expect(*bytes == artifact);
|
||||
|
||||
Package::BinaryVariantMeta variant;
|
||||
variant.variant_id = variant_id;
|
||||
variant.os = "Linux";
|
||||
variant.architecture = "x86_64";
|
||||
variant.compiler_family = "gcc";
|
||||
variant.compiler_abi = "gcc-16";
|
||||
variant.stdlib_abi = "libstdc++";
|
||||
variant.linkage = "static";
|
||||
variant.lto_mode = "off";
|
||||
variant.dependency_closure_hash = "closure";
|
||||
variant.sha256 = artifact_sha;
|
||||
variant.download_url = download_url;
|
||||
variant.signature = *signature;
|
||||
variant.public_key = *public_key;
|
||||
|
||||
Package::BinaryCompatibilityKey key;
|
||||
key.os = variant.os;
|
||||
key.architecture = variant.architecture;
|
||||
key.compiler_family = variant.compiler_family;
|
||||
key.compiler_abi = variant.compiler_abi;
|
||||
key.stdlib_abi = variant.stdlib_abi;
|
||||
key.linkage = variant.linkage;
|
||||
key.lto_mode = variant.lto_mode;
|
||||
key.dependency_closure_hash = variant.dependency_closure_hash;
|
||||
|
||||
Package::BinaryFetchOptions options{tmp / "private_cache", {*public_key}, *tok};
|
||||
auto materialized = Package::materialize_binary_variant(key, {variant}, options);
|
||||
Void::Testing::require(materialized.has_value());
|
||||
auto cached = Void::FileSystem::read_file(tmp / "private_cache" / "binaries" / artifact_sha);
|
||||
Void::Testing::require(cached.has_value());
|
||||
Void::Testing::expect(*cached == artifact);
|
||||
|
||||
(*svc)->stop_http();
|
||||
}
|
||||
|
||||
// End-to-end live client <-> server test: `vbs publish` -> `vbs deps resolve` -> `vbs build
|
||||
// --locked`!
|
||||
VOID_TEST(RegistryTests, cli_publish_deps_resolve_and_locked_build_against_live_registry) {
|
||||
|
||||
Reference in new issue
Block a user