Files
Library/include/Void/Update/Feed.hpp
T

304 lines
9.4 KiB
C++

#pragma once
#include <Void/Crypto/Crypto.hpp>
#include <Void/Functional/Error.hpp>
#include <Void/Functional/Result.hpp>
#include <Void/Text/Semver.hpp>
#include <Void/Update/Types.hpp>
#include <array>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
namespace Void::Update
{
struct Compatibility {
std::unordered_map<std::string, Semver::VersionRange>
versionContracts;
std::unordered_map<std::string, std::string> exactContracts;
};
struct Release {
std::string component;
std::string artifactId;
std::string channel;
Semver::Version version;
std::string cohort;
std::string platform;
std::string architecture;
std::string url;
std::uint64_t size{0};
std::string sha256;
std::string packageType;
Compatibility compatibility;
std::string releaseManifestSha256;
};
struct Feed {
std::uint32_t schemaVersion{1};
std::string feedId;
std::uint64_t sequence{0};
std::chrono::system_clock::time_point generatedAt;
std::chrono::system_clock::time_point expiresAt;
std::vector<Release> releases;
};
struct FeedEnvelope {
std::uint32_t schemaVersion{1};
std::string keyId;
std::string payload;
Crypto::Digest signature;
};
using Ed25519PublicKey =
std::array<std::byte, Crypto::Ed25519PublicKeySize>;
using Keyring = std::unordered_map<std::string, Ed25519PublicKey>;
struct VerificationPolicy {
std::size_t maxEnvelopeBytes{5 * 1024 * 1024};
std::size_t maxPayloadBytes{4 * 1024 * 1024};
std::size_t maxReleases{10'000};
std::optional<std::uint64_t> minimumSequence;
std::chrono::system_clock::time_point now{
std::chrono::system_clock::now()};
bool requireUnexpired{true};
SchemaParsePolicy parsePolicy{SchemaParsePolicy::LegacyGeneralJson};
std::size_t maxJsonDepth{64};
};
struct VerifiedFeed {
Feed feed;
std::string keyId;
std::string signedPayload;
};
/// A signature-verified payload whose schema-specific parsing is left to
/// the caller. This is useful for signed discovery documents that are not
/// legacy aggregate feeds.
struct VerifiedEnvelope {
std::uint32_t schemaVersion{0};
std::string keyId;
std::string signedPayload;
};
struct ComponentArtifact {
std::string name;
std::string url;
std::string sha256;
std::uint64_t size{0};
std::string manifestSha256;
std::string packageType;
};
struct ComponentCatalogEntry {
std::string componentId;
std::optional<std::string> pluginId;
std::string kind;
Semver::Version componentVersion;
std::string activation;
std::map<std::string, std::string> dependencyConstraints;
std::map<std::string, Semver::VersionRange> dependencies;
std::uint64_t sequence{0};
std::string latestUrl;
std::string artifactSha256;
};
struct ComponentCatalog {
std::uint32_t schemaVersion{2};
std::string channel;
std::uint64_t sequence{0};
std::chrono::system_clock::time_point generatedAt;
std::chrono::system_clock::time_point expiresAt;
Semver::Version productVersion;
std::vector<ComponentCatalogEntry> components;
};
struct ComponentRelease {
std::uint32_t schemaVersion{2};
std::string channel;
std::uint64_t sequence{0};
std::chrono::system_clock::time_point generatedAt;
std::chrono::system_clock::time_point expiresAt;
Semver::Version productVersion;
std::string componentId;
std::optional<std::string> pluginId;
std::string kind;
Semver::Version componentVersion;
std::string sourceSha;
std::string sourceTreeHash;
std::optional<std::string> abiHash;
std::string artifactIdentity;
std::string activation;
std::string platform;
std::string architecture;
std::string toolchainContract;
std::map<std::string, std::string> dependencyConstraints;
std::map<std::string, Semver::VersionRange> dependencies;
ComponentArtifact artifact;
};
struct Environment {
std::unordered_map<std::string, Semver::Version> versionContracts;
std::unordered_map<std::string, std::string> exactContracts;
std::string platform;
std::string architecture;
};
struct SelectionRequest {
std::string component;
std::string artifactId;
std::string channel;
Environment environment;
std::optional<Semver::Version> currentVersion;
std::optional<std::string> requiredCohort;
bool allowDowngrade{false};
bool includeCurrent{false};
};
/// Parse and validate an unsigned payload. Trust and activation policy is a
/// caller concern; all structural, size, expiry and replay checks still
/// run.
[[nodiscard]] Void::Result::Result<Feed>
parseFeedPayload(std::string_view payload,
const VerificationPolicy& policy = {});
/// Verify the envelope signature before parsing the signed payload.
[[nodiscard]] Void::Result::Result<VerifiedFeed>
verifyFeedEnvelope(std::string_view envelopeJson, const Keyring& keys,
const VerificationPolicy& policy = {});
/// Verify and decode a signed envelope without assuming a payload schema.
[[nodiscard]] Void::Result::Result<VerifiedEnvelope>
verifyEnvelope(std::string_view envelopeJson, const Keyring& keys,
const VerificationPolicy& policy = {});
[[nodiscard]] Void::Result::Result<ComponentCatalog>
parseComponentCatalogPayload(std::string_view payload,
const VerificationPolicy& policy = {});
[[nodiscard]] Void::Result::Result<ComponentRelease>
parseComponentReleasePayload(std::string_view payload,
const VerificationPolicy& policy = {});
/// Validate that a catalog and its fetched releases form one complete,
/// dependency-compatible transaction.
[[nodiscard]] Void::Result::Result<void> validateComponentSet(
const ComponentCatalog& catalog,
const std::map<std::string, ComponentRelease>& releases);
/// Select the greatest compatible version, independent of feed order.
[[nodiscard]] std::optional<Release>
selectRelease(const Feed& feed, const SelectionRequest& request);
/// Non-forgeable signature-verified release selection retaining the exact
/// verified envelope bytes, signer key ID, policy decision, and selected
/// release/component metadata. Can only be constructed by verified factory
/// functions.
class VerifiedSelection
{
public:
VerifiedSelection(const VerifiedSelection&) = default;
VerifiedSelection(VerifiedSelection&&) noexcept = default;
VerifiedSelection& operator=(const VerifiedSelection&) = default;
VerifiedSelection& operator=(VerifiedSelection&&) noexcept = default;
[[nodiscard]] const std::string&
signed_envelope_bytes() const noexcept
{
return m_signed_envelope;
}
[[nodiscard]] const std::string&
signed_payload_bytes() const noexcept
{
return m_signed_payload;
}
[[nodiscard]] const std::string& signer_key_id() const noexcept
{
return m_signer_key_id;
}
[[nodiscard]] const std::string& feed_id() const noexcept
{
return m_feed_id;
}
[[nodiscard]] std::uint64_t sequence() const noexcept
{
return m_sequence;
}
[[nodiscard]] std::chrono::system_clock::time_point
expires_at() const noexcept
{
return m_expires_at;
}
[[nodiscard]] SchemaParsePolicy parse_policy() const noexcept
{
return m_parse_policy;
}
[[nodiscard]] const ReleaseIdentity& release() const noexcept
{
return m_release;
}
[[nodiscard]] const std::map<std::string, ComponentRelease>&
component_releases() const noexcept
{
return m_components;
}
private:
VerifiedSelection() = default;
std::string m_signed_envelope{};
std::string m_signed_payload{};
std::string m_signer_key_id{};
std::string m_feed_id{};
std::uint64_t m_sequence{0};
std::chrono::system_clock::time_point m_expires_at{};
SchemaParsePolicy m_parse_policy{
SchemaParsePolicy::LegacyGeneralJson};
ReleaseIdentity m_release{};
std::map<std::string, ComponentRelease> m_components{};
friend Void::Result::Result<VerifiedSelection>
verify_and_select_release(std::string_view envelopeJson,
const Keyring& keys,
const SelectionRequest& request,
const VerificationPolicy& policy);
friend Void::Result::Result<VerifiedSelection>
verify_and_select_component_set(
std::string_view catalogEnvelopeJson,
const std::map<std::string, std::string>&
releaseEnvelopeJsons,
const Keyring& keys, std::string_view primaryComponentId,
const VerificationPolicy& policy);
};
/// Verify an aggregate feed envelope (verify-before-parse), validate
/// policy, and produce a non-forgeable VerifiedSelection for the best
/// compatible release.
[[nodiscard]] Void::Result::Result<VerifiedSelection>
verify_and_select_release(std::string_view envelopeJson,
const Keyring& keys,
const SelectionRequest& request,
const VerificationPolicy& policy = {});
/// Verify a signed component catalog envelope and signed per-component
/// release envelopes, validate dependency closure and transaction limits
/// (<= 32 components, <= 256 dependency edges), and return a non-forgeable
/// VerifiedSelection.
[[nodiscard]] Void::Result::Result<VerifiedSelection>
verify_and_select_component_set(
std::string_view catalogEnvelopeJson,
const std::map<std::string, std::string>& releaseEnvelopeJsons,
const Keyring& keys, std::string_view primaryComponentId,
const VerificationPolicy& policy = {});
} // namespace Void::Update