feat(provenance): distribute trusted Ed25519 keys via files, vbs.keys, and environment
This commit is contained in:
1 parent
f5c0c9f09d
commit
ff59f6ff5c
13 files changed
+314
-7
No files matched your search
@@ -8,6 +8,7 @@
|
||||
#include <Void/Build/Package/Lockfile.hpp>
|
||||
#include <Void/Build/Package/Resolver.hpp>
|
||||
#include <Void/Build/Package/Toml.hpp>
|
||||
#include <Void/Build/Provenance/TrustedKeys.hpp>
|
||||
#include <Void/Build/Provider/Provider.hpp>
|
||||
#include <Void/Build/State/StateStore.hpp>
|
||||
#include <Void/Build/Toolchain/Driver.hpp>
|
||||
@@ -440,6 +441,18 @@ Void::Result::Result<std::size_t, std::string> materialize_project_dependencies(
|
||||
resolve_options.default_registry_url = Void::Configuration::get_env_or("VBS_REGISTRY_URL", "");
|
||||
resolve_options.registry_auth =
|
||||
Package::RegistryAuth{Void::Configuration::get_env_or("VBS_REGISTRY_TOKEN", "")};
|
||||
const std::string trusted_environment =
|
||||
Void::Configuration::get_env_or("VBS_TRUSTED_PUBLIC_KEYS", "");
|
||||
std::optional<std::string_view> trusted_environment_value;
|
||||
if (!trusted_environment.empty()) {
|
||||
trusted_environment_value = trusted_environment;
|
||||
}
|
||||
auto trusted_keys =
|
||||
Provenance::load_trusted_keys({}, options.project_root, trusted_environment_value);
|
||||
if (!trusted_keys) {
|
||||
return std::unexpected{std::format("build: {}", trusted_keys.error().format())};
|
||||
}
|
||||
resolve_options.trusted_public_keys = std::move(*trusted_keys);
|
||||
auto resolved =
|
||||
Package::resolve_and_materialize_dependencies(evaluated.context, *merged, resolve_options);
|
||||
if (!resolved) {
|
||||
|
||||
+42
-4
@@ -17,6 +17,7 @@
|
||||
#include <Void/Build/Platform/FileOps.hpp>
|
||||
#include <Void/Build/Platform/ProcessSupervisor.hpp>
|
||||
#include <Void/Build/Provenance/Sbom.hpp>
|
||||
#include <Void/Build/Provenance/TrustedKeys.hpp>
|
||||
#include <Void/Build/Provenance/Verification.hpp>
|
||||
#include <Void/Build/Provider/Provider.hpp>
|
||||
#include <Void/Build/Provider/Translator.hpp>
|
||||
@@ -282,6 +283,33 @@ Void::Result::Result<ResolvedDepMaterialization, Void::Result::Error> resolve_an
|
||||
return Package::resolve_and_materialize_dependencies(ctx, merged_deps, options);
|
||||
}
|
||||
|
||||
Void::Result::Result<std::vector<std::string>, Void::Result::Error>
|
||||
combine_trusted_keys(const std::vector<std::string> &inline_keys,
|
||||
const std::vector<std::string> &key_files,
|
||||
const std::filesystem::path &project_root) {
|
||||
std::vector<std::filesystem::path> files;
|
||||
files.reserve(key_files.size());
|
||||
for (const auto &file : key_files) {
|
||||
files.emplace_back(file);
|
||||
}
|
||||
const std::string environment = Void::Configuration::get_env_or("VBS_TRUSTED_PUBLIC_KEYS", "");
|
||||
std::optional<std::string_view> environment_value;
|
||||
if (!environment.empty()) {
|
||||
environment_value = environment;
|
||||
}
|
||||
auto loaded = Provenance::load_trusted_keys(files, project_root, environment_value);
|
||||
if (!loaded) {
|
||||
return std::unexpected{loaded.error()};
|
||||
}
|
||||
std::vector<std::string> combined = inline_keys;
|
||||
for (auto &key : *loaded) {
|
||||
if (std::ranges::find(combined, key) == combined.end()) {
|
||||
combined.push_back(std::move(key));
|
||||
}
|
||||
}
|
||||
return combined;
|
||||
}
|
||||
|
||||
struct BuildCmdOptions {
|
||||
std::string project_arg;
|
||||
std::optional<std::string> build_arg;
|
||||
@@ -300,6 +328,7 @@ struct BuildCmdOptions {
|
||||
std::optional<std::filesystem::path> lock_root;
|
||||
std::optional<std::filesystem::path> state_dir;
|
||||
std::vector<std::string> trusted_public_keys;
|
||||
std::vector<std::string> trusted_key_files;
|
||||
bool publish_editor_selection{true};
|
||||
bool no_cache{false};
|
||||
bool locked{false};
|
||||
@@ -333,6 +362,12 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
|
||||
const std::filesystem::path project_root{opts.project_arg};
|
||||
const std::filesystem::path build_root =
|
||||
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
||||
auto trusted_keys =
|
||||
combine_trusted_keys(opts.trusted_public_keys, opts.trusted_key_files, project_root);
|
||||
if (!trusted_keys) {
|
||||
std::cerr << cmd_name << ": " << trusted_keys.error().message() << '\n';
|
||||
return 1;
|
||||
}
|
||||
std::vector<std::filesystem::path> extra_build_sources;
|
||||
for (const auto &s : opts.extra_build_sources) {
|
||||
extra_build_sources.emplace_back(s);
|
||||
@@ -492,9 +527,9 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto resolved = resolve_and_materialize_deps(ctx, *merged_deps, project_root,
|
||||
build_root, *pinned_lock, opts.offline,
|
||||
false, opts.trusted_public_keys);
|
||||
auto resolved =
|
||||
resolve_and_materialize_deps(ctx, *merged_deps, project_root, build_root,
|
||||
*pinned_lock, opts.offline, false, *trusted_keys);
|
||||
if (!resolved) {
|
||||
std::cerr << "vbs: " << resolved.error().message() << '\n';
|
||||
return 1;
|
||||
@@ -2151,7 +2186,10 @@ int main(int argc, char **argv) {
|
||||
build_opts.native_foreign)
|
||||
.option_many("--trusted-public-key", "",
|
||||
"Trusted Ed25519 package public key (hex; repeatable)",
|
||||
build_opts.trusted_public_keys);
|
||||
build_opts.trusted_public_keys)
|
||||
.option_many("--trusted-key-file", "",
|
||||
"File with trusted Ed25519 public keys (repeatable)",
|
||||
build_opts.trusted_key_files);
|
||||
add_log_flags(p);
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
#include <Void/Build/Provenance/TrustedKeys.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Build::Provenance {
|
||||
namespace {
|
||||
|
||||
using Void::Result::Error;
|
||||
using Void::Result::ErrorKind;
|
||||
using Void::Result::Result;
|
||||
|
||||
constexpr std::size_t PublicKeyHexLength = 64;
|
||||
|
||||
Error make_error(ErrorKind kind, std::string message) { return Error{kind, std::move(message)}; }
|
||||
|
||||
std::string_view trim_ascii(std::string_view text) {
|
||||
while (!text.empty() && std::isspace(static_cast<unsigned char>(text.front())) != 0) {
|
||||
text.remove_prefix(1);
|
||||
}
|
||||
while (!text.empty() && std::isspace(static_cast<unsigned char>(text.back())) != 0) {
|
||||
text.remove_suffix(1);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
bool is_hex_digit(char c) noexcept {
|
||||
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
|
||||
}
|
||||
|
||||
Result<void, Error> add_key(std::string_view raw, std::string_view origin,
|
||||
std::vector<std::string> &keys, std::set<std::string> &seen) {
|
||||
const std::string_view trimmed = trim_ascii(raw);
|
||||
if (trimmed.empty()) {
|
||||
return {};
|
||||
}
|
||||
if (trimmed.size() != PublicKeyHexLength || !std::ranges::all_of(trimmed, is_hex_digit)) {
|
||||
return std::unexpected{make_error(
|
||||
ErrorKind::invalid_argument,
|
||||
std::format("{}: trusted public key must be exactly {} hex characters, got '{}'",
|
||||
origin, PublicKeyHexLength, trimmed))};
|
||||
}
|
||||
std::string normalized{trimmed};
|
||||
std::ranges::transform(normalized, normalized.begin(), [](char c) {
|
||||
return static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
||||
});
|
||||
if (seen.insert(normalized).second) {
|
||||
keys.push_back(std::move(normalized));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void, Error> add_key_file(const std::filesystem::path &path, std::vector<std::string> &keys,
|
||||
std::set<std::string> &seen) {
|
||||
std::ifstream stream{path};
|
||||
if (!stream) {
|
||||
return std::unexpected{make_error(
|
||||
ErrorKind::io, std::format("cannot open trusted key file '{}'", path.string()))};
|
||||
}
|
||||
std::string line;
|
||||
std::size_t line_number = 0;
|
||||
while (std::getline(stream, line)) {
|
||||
++line_number;
|
||||
const auto comment = line.find('#');
|
||||
if (comment != std::string::npos) {
|
||||
line.resize(comment);
|
||||
}
|
||||
if (auto added =
|
||||
add_key(line, std::format("{}:{}", path.string(), line_number), keys, seen);
|
||||
!added) {
|
||||
return added;
|
||||
}
|
||||
}
|
||||
if (!stream.eof()) {
|
||||
return std::unexpected{make_error(
|
||||
ErrorKind::io, std::format("cannot read trusted key file '{}'", path.string()))};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Result<std::string, Error> normalize_public_key(std::string_view hex) {
|
||||
const std::string_view trimmed = trim_ascii(hex);
|
||||
if (trimmed.size() != PublicKeyHexLength || !std::ranges::all_of(trimmed, is_hex_digit)) {
|
||||
return std::unexpected{
|
||||
make_error(ErrorKind::invalid_argument,
|
||||
std::format("trusted public key must be exactly {} hex characters",
|
||||
PublicKeyHexLength))};
|
||||
}
|
||||
std::string normalized{trimmed};
|
||||
std::ranges::transform(normalized, normalized.begin(), [](char c) {
|
||||
return static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
||||
});
|
||||
return normalized;
|
||||
}
|
||||
|
||||
Result<std::vector<std::string>, Error>
|
||||
load_trusted_keys(std::span<const std::filesystem::path> explicit_files,
|
||||
const std::filesystem::path &project_root,
|
||||
std::optional<std::string_view> environment_value) {
|
||||
std::vector<std::string> keys;
|
||||
std::set<std::string> seen;
|
||||
|
||||
for (const auto &file : explicit_files) {
|
||||
if (auto added = add_key_file(file, keys, seen); !added) {
|
||||
return std::unexpected{added.error()};
|
||||
}
|
||||
}
|
||||
|
||||
const auto project_key_file = project_root / "vbs.keys";
|
||||
std::error_code ec;
|
||||
if (std::filesystem::is_regular_file(project_key_file, ec)) {
|
||||
if (auto added = add_key_file(project_key_file, keys, seen); !added) {
|
||||
return std::unexpected{added.error()};
|
||||
}
|
||||
}
|
||||
|
||||
if (environment_value) {
|
||||
std::string_view text = *environment_value;
|
||||
while (!text.empty()) {
|
||||
const auto separator = text.find_first_of(", \t\r\n");
|
||||
const std::string_view token =
|
||||
separator == std::string_view::npos ? text : text.substr(0, separator);
|
||||
if (auto added = add_key(token, "VBS_TRUSTED_PUBLIC_KEYS", keys, seen); !added) {
|
||||
return std::unexpected{added.error()};
|
||||
}
|
||||
if (separator == std::string_view::npos) {
|
||||
break;
|
||||
}
|
||||
text.remove_prefix(separator + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
} // namespace Void::Build::Provenance
|
||||
Reference in new issue
Block a user