Add M10.8b signed handoff tickets and ticket admission
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / checks (client) (push) Successful in 12m40s
Foundation / checks (postgres) (push) Canceled after 4s
Foundation / checks (network) (push) Successful in 12m53s
Foundation / checks (tsan) (push) Successful in 7m55s
Foundation / checks (checks) (push) Successful in 31m47s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / checks (client) (push) Successful in 12m40s
Foundation / checks (postgres) (push) Canceled after 4s
Foundation / checks (network) (push) Successful in 12m53s
Foundation / checks (tsan) (push) Successful in 7m55s
Foundation / checks (checks) (push) Successful in 31m47s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
ADR-0096. A source region's handoff key signs a 160-byte ticket for one probe transfer, account and destination; it rides the Hello grant slot and is verified against neighbour keys bound to their regions. SessionControl admits a ticket once per region, superseding like a grant, with the session ending at the client's original grant expiry. RegionSessions makes the destination's durable step first: PostgresHandoffLedger stages and commits the transfer and moves the account's control, idempotently. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "evo/network/session_control.hpp"
|
||||
#include "evo/network/handoff_ticket.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
namespace evo::network {
|
||||
@@ -154,6 +155,82 @@ std::expected<OpenedSession, ControlError> SessionControl::open(const VerifiedSe
|
||||
return opened;
|
||||
}
|
||||
|
||||
std::optional<ControlError> SessionControl::ticket_error(const VerifiedHandoffTicket& ticket,
|
||||
std::uint64_t now_seconds) const {
|
||||
const auto& c = ticket.claims();
|
||||
if (c.world != config_.world)
|
||||
return ControlError::wrong_world;
|
||||
if (c.destination != config_.region)
|
||||
return ControlError::wrong_region;
|
||||
if (now_seconds >= c.expires_at || now_seconds >= c.session_expires_at)
|
||||
return ControlError::expired;
|
||||
if (std::ranges::any_of(used_tickets_, [&](const UsedTicket& u) {
|
||||
return u.transfer == c.transfer && u.until > now_seconds;
|
||||
}))
|
||||
return ControlError::stale_grant; // A replayed ticket.
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::expected<void, ControlError> SessionControl::check_handoff(const VerifiedHandoffTicket& ticket,
|
||||
std::uint64_t now_seconds) const {
|
||||
if (const auto e = ticket_error(ticket, now_seconds))
|
||||
return std::unexpected(*e);
|
||||
const auto& c = ticket.claims();
|
||||
const auto found =
|
||||
std::ranges::find_if(accounts_, [&](const Account& a) { return a.id == c.account; });
|
||||
if (found == accounts_.end()) {
|
||||
const auto kept = std::ranges::count_if(
|
||||
accounts_, [&](const Account& a) { return !forgettable(a, now_seconds); });
|
||||
if (static_cast<std::size_t>(kept) >= config_.max_accounts)
|
||||
return std::unexpected(ControlError::accounts_full);
|
||||
} else if (!window_over(found->window_start, now_seconds, config_.attempt_window) &&
|
||||
found->attempts >= config_.max_account_attempts) {
|
||||
return std::unexpected(ControlError::rate_limited);
|
||||
}
|
||||
if ((found == accounts_.end() || found->session == 0) && sessions() >= config_.max_sessions)
|
||||
return std::unexpected(ControlError::sessions_full);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<OpenedSession, ControlError>
|
||||
SessionControl::open_handoff(const VerifiedHandoffTicket& ticket, std::uint64_t now_seconds) {
|
||||
std::erase_if(used_tickets_, [&](const UsedTicket& u) { return u.until <= now_seconds; });
|
||||
if (const auto e = ticket_error(ticket, now_seconds))
|
||||
return std::unexpected(*e);
|
||||
const auto& c = ticket.claims();
|
||||
if (used_tickets_.size() >= config_.max_accounts)
|
||||
return std::unexpected(ControlError::accounts_full); // Fail closed: never forget early.
|
||||
forget_idle(now_seconds);
|
||||
auto found =
|
||||
std::ranges::find_if(accounts_, [&](const Account& a) { return a.id == c.account; });
|
||||
if (found == accounts_.end()) {
|
||||
if (accounts_.size() >= config_.max_accounts)
|
||||
return std::unexpected(ControlError::accounts_full);
|
||||
accounts_.push_back({c.account, 0, 0, 0, 0, now_seconds, 0});
|
||||
found = std::prev(accounts_.end());
|
||||
}
|
||||
auto& a = *found;
|
||||
if (window_over(a.window_start, now_seconds, config_.attempt_window)) {
|
||||
a.window_start = now_seconds;
|
||||
a.attempts = 0;
|
||||
}
|
||||
if (a.attempts >= config_.max_account_attempts)
|
||||
return std::unexpected(ControlError::rate_limited);
|
||||
++a.attempts;
|
||||
if (a.session == 0 && sessions() >= config_.max_sessions)
|
||||
return std::unexpected(ControlError::sessions_full);
|
||||
used_tickets_.push_back({c.transfer, c.expires_at});
|
||||
OpenedSession opened;
|
||||
opened.superseded = a.session;
|
||||
a.session = next_session_++;
|
||||
a.session_expires_at =
|
||||
std::min<std::uint64_t>(c.session_expires_at, now_seconds + config_.max_grant_lifetime);
|
||||
opened.session = a.session;
|
||||
opened.epoch = config_.epoch;
|
||||
opened.expires_at = a.session_expires_at;
|
||||
return opened;
|
||||
}
|
||||
|
||||
std::expected<std::uint64_t, ControlError>
|
||||
SessionControl::refresh(std::uint64_t session, const VerifiedSessionGrant& grant,
|
||||
std::uint64_t now_seconds) {
|
||||
|
||||
@@ -0,0 +1,245 @@
|
||||
#include "evo/network/handoff_ticket.hpp"
|
||||
#include <algorithm>
|
||||
#include <psa/crypto.h>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::network {
|
||||
namespace {
|
||||
constexpr std::array<std::uint8_t, 16> marker{'E', 'V', 'O', '-', 'H', 'A', 'N', 'D',
|
||||
'O', 'F', 'F', 0, 0, 0, 0, 0};
|
||||
constexpr std::uint16_t ticket_version = 1;
|
||||
constexpr std::size_t signed_end = 96; // The signature covers bytes [0, 96).
|
||||
constexpr std::size_t signature_bytes = handoff_ticket_bytes - signed_end;
|
||||
constexpr psa_algorithm_t algorithm = PSA_ALG_ECDSA(PSA_ALG_SHA_256);
|
||||
static_assert(signature_bytes == PSA_ECDSA_SIGNATURE_SIZE(256));
|
||||
static_assert(handoff_ticket_bytes == session_grant_bytes, "tickets use the Hello grant slot");
|
||||
|
||||
std::uint8_t environment_byte(DeploymentEnvironment e) {
|
||||
return static_cast<std::uint8_t>(static_cast<std::uint8_t>(e) + 1);
|
||||
}
|
||||
void put(HandoffTicket& out, std::size_t at, std::uint64_t v, std::size_t count) {
|
||||
for (std::size_t i = 0; i < count; ++i)
|
||||
out.at(at + i) = static_cast<std::uint8_t>(v >> (8 * i));
|
||||
}
|
||||
std::uint64_t get(std::span<const std::uint8_t> in, std::size_t at, std::size_t count) {
|
||||
std::uint64_t v = 0;
|
||||
for (std::size_t i = 0; i < count; ++i)
|
||||
v |= static_cast<std::uint64_t>(in[at + i]) << (8 * i);
|
||||
return v;
|
||||
}
|
||||
bool structurally_valid(const TicketClaims& c) {
|
||||
return std::ranges::any_of(c.transfer, [](std::uint8_t b) { return b != 0; }) &&
|
||||
c.account.valid() && c.world.world != 0 && c.world.frame != 0 && c.source != 0 &&
|
||||
c.destination != 0 && c.source != c.destination && c.expires_at != 0 &&
|
||||
c.session_expires_at != 0;
|
||||
}
|
||||
std::expected<std::uint32_t, HandoffTicketError> import_key(const psa_key_attributes_t& a,
|
||||
std::span<const std::uint8_t> bytes) {
|
||||
mbedtls_svc_key_id_t key = PSA_KEY_ID_NULL;
|
||||
if (psa_import_key(&a, bytes.data(), bytes.size(), &key) != PSA_SUCCESS)
|
||||
return std::unexpected(HandoffTicketError::invalid_key);
|
||||
return key;
|
||||
}
|
||||
psa_key_attributes_t pair_attributes() {
|
||||
psa_key_attributes_t a = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_set_key_type(&a, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
|
||||
psa_set_key_bits(&a, 256);
|
||||
psa_set_key_usage_flags(&a, PSA_KEY_USAGE_SIGN_MESSAGE);
|
||||
psa_set_key_algorithm(&a, algorithm);
|
||||
return a;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool looks_like_handoff_ticket(std::span<const std::uint8_t> bytes) noexcept {
|
||||
return bytes.size() == handoff_ticket_bytes &&
|
||||
std::equal(marker.begin(), marker.end(), bytes.begin());
|
||||
}
|
||||
|
||||
std::expected<HandoffSigningKey, HandoffTicketError>
|
||||
HandoffSigningKey::generate(DeploymentEnvironment environment, std::uint32_t key_id) {
|
||||
if (key_id == 0)
|
||||
return std::unexpected(HandoffTicketError::invalid_key);
|
||||
if (psa_crypto_init() != PSA_SUCCESS)
|
||||
return std::unexpected(HandoffTicketError::crypto);
|
||||
auto a = pair_attributes();
|
||||
mbedtls_svc_key_id_t key = PSA_KEY_ID_NULL;
|
||||
const auto status = psa_generate_key(&a, &key);
|
||||
psa_reset_key_attributes(&a);
|
||||
if (status != PSA_SUCCESS)
|
||||
return std::unexpected(HandoffTicketError::crypto);
|
||||
return HandoffSigningKey{environment, key_id, key};
|
||||
}
|
||||
|
||||
std::expected<HandoffSigningKey, HandoffTicketError>
|
||||
HandoffSigningKey::import(DeploymentEnvironment environment, std::uint32_t key_id,
|
||||
std::span<const std::uint8_t> private_key) {
|
||||
if (key_id == 0 || private_key.size() != grant_private_key_bytes)
|
||||
return std::unexpected(HandoffTicketError::invalid_key);
|
||||
if (psa_crypto_init() != PSA_SUCCESS)
|
||||
return std::unexpected(HandoffTicketError::crypto);
|
||||
auto a = pair_attributes();
|
||||
auto key = import_key(a, private_key);
|
||||
psa_reset_key_attributes(&a);
|
||||
if (!key)
|
||||
return std::unexpected(key.error());
|
||||
return HandoffSigningKey{environment, key_id, *key};
|
||||
}
|
||||
|
||||
HandoffSigningKey::HandoffSigningKey(HandoffSigningKey&& other) noexcept
|
||||
: environment_{other.environment_}, key_id_{other.key_id_},
|
||||
key_{std::exchange(other.key_, PSA_KEY_ID_NULL)} {}
|
||||
|
||||
HandoffSigningKey& HandoffSigningKey::operator=(HandoffSigningKey&& other) noexcept {
|
||||
if (this != &other) {
|
||||
if (key_ != PSA_KEY_ID_NULL)
|
||||
(void)psa_destroy_key(key_);
|
||||
environment_ = other.environment_;
|
||||
key_id_ = other.key_id_;
|
||||
key_ = std::exchange(other.key_, PSA_KEY_ID_NULL);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
HandoffSigningKey::~HandoffSigningKey() {
|
||||
if (key_ != PSA_KEY_ID_NULL)
|
||||
(void)psa_destroy_key(key_);
|
||||
}
|
||||
|
||||
std::expected<HandoffTicket, HandoffTicketError>
|
||||
HandoffSigningKey::sign(const TicketClaims& claims) const {
|
||||
if (key_ == PSA_KEY_ID_NULL)
|
||||
return std::unexpected(HandoffTicketError::invalid_key);
|
||||
if (!structurally_valid(claims))
|
||||
return std::unexpected(HandoffTicketError::invalid_claims);
|
||||
HandoffTicket out{};
|
||||
std::ranges::copy(marker, out.begin());
|
||||
put(out, 16, ticket_version, 2);
|
||||
out.at(18) = environment_byte(environment_);
|
||||
out.at(19) = 0;
|
||||
put(out, 20, key_id_, 4);
|
||||
std::ranges::copy(claims.transfer, out.begin() + 24);
|
||||
std::ranges::copy(claims.account.bytes, out.begin() + 40);
|
||||
put(out, 56, claims.world.world, 8);
|
||||
put(out, 64, claims.world.frame, 8);
|
||||
put(out, 72, claims.source, 8);
|
||||
put(out, 80, claims.destination, 8);
|
||||
put(out, 88, claims.expires_at, 4);
|
||||
put(out, 92, claims.session_expires_at, 4);
|
||||
std::size_t length = 0;
|
||||
if (psa_sign_message(key_, algorithm, out.data(), signed_end, out.data() + signed_end,
|
||||
signature_bytes, &length) != PSA_SUCCESS ||
|
||||
length != signature_bytes)
|
||||
return std::unexpected(HandoffTicketError::crypto);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<GrantPublicKey, HandoffTicketError> HandoffSigningKey::public_key() const {
|
||||
if (key_ == PSA_KEY_ID_NULL)
|
||||
return std::unexpected(HandoffTicketError::invalid_key);
|
||||
GrantPublicKey out{};
|
||||
std::size_t length = 0;
|
||||
if (psa_export_public_key(key_, out.data(), out.size(), &length) != PSA_SUCCESS ||
|
||||
length != out.size())
|
||||
return std::unexpected(HandoffTicketError::crypto);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<HandoffVerifier, HandoffTicketError>
|
||||
HandoffVerifier::create(DeploymentEnvironment environment, world::WorldIdentity world,
|
||||
std::uint64_t region) {
|
||||
if (world.world == 0 || world.frame == 0 || region == 0)
|
||||
return std::unexpected(HandoffTicketError::invalid_claims);
|
||||
if (psa_crypto_init() != PSA_SUCCESS)
|
||||
return std::unexpected(HandoffTicketError::crypto);
|
||||
return HandoffVerifier{environment, world, region};
|
||||
}
|
||||
|
||||
HandoffVerifier::HandoffVerifier(HandoffVerifier&& other) noexcept
|
||||
: environment_{other.environment_}, world_{other.world_}, region_{other.region_},
|
||||
keys_{std::exchange(other.keys_, {})} {}
|
||||
|
||||
HandoffVerifier& HandoffVerifier::operator=(HandoffVerifier&& other) noexcept {
|
||||
if (this != &other) {
|
||||
destroy();
|
||||
environment_ = other.environment_;
|
||||
world_ = other.world_;
|
||||
region_ = other.region_;
|
||||
keys_ = std::exchange(other.keys_, {});
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
HandoffVerifier::~HandoffVerifier() {
|
||||
destroy();
|
||||
}
|
||||
|
||||
void HandoffVerifier::destroy() noexcept {
|
||||
for (const auto& k : keys_)
|
||||
(void)psa_destroy_key(k.key);
|
||||
keys_.clear();
|
||||
}
|
||||
|
||||
std::expected<void, HandoffTicketError>
|
||||
HandoffVerifier::add_key(std::uint64_t source_region, std::uint32_t key_id,
|
||||
std::span<const std::uint8_t> public_key) {
|
||||
if (source_region == 0 || source_region == region_ || key_id == 0 ||
|
||||
public_key.size() != grant_public_key_bytes)
|
||||
return std::unexpected(HandoffTicketError::invalid_key);
|
||||
if (std::ranges::any_of(keys_, [&](const Key& k) { return k.key_id == key_id; }))
|
||||
return std::unexpected(HandoffTicketError::duplicate_key);
|
||||
if (keys_.size() >= max_keys)
|
||||
return std::unexpected(HandoffTicketError::too_many_keys);
|
||||
psa_key_attributes_t a = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_set_key_type(&a, PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1));
|
||||
psa_set_key_bits(&a, 256);
|
||||
psa_set_key_usage_flags(&a, PSA_KEY_USAGE_VERIFY_MESSAGE);
|
||||
psa_set_key_algorithm(&a, algorithm);
|
||||
auto key = import_key(a, public_key);
|
||||
psa_reset_key_attributes(&a);
|
||||
if (!key)
|
||||
return std::unexpected(key.error());
|
||||
keys_.push_back({source_region, key_id, *key});
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<VerifiedHandoffTicket, HandoffTicketError>
|
||||
HandoffVerifier::verify(std::span<const std::uint8_t> ticket, std::uint64_t now_seconds) const {
|
||||
if (ticket.size() != handoff_ticket_bytes)
|
||||
return std::unexpected(HandoffTicketError::malformed);
|
||||
// The key id only selects which key must verify; it is itself signed.
|
||||
const auto key_id = static_cast<std::uint32_t>(get(ticket, 20, 4));
|
||||
const auto key = std::ranges::find_if(keys_, [&](const Key& k) { return k.key_id == key_id; });
|
||||
if (key == keys_.end())
|
||||
return std::unexpected(HandoffTicketError::unknown_key);
|
||||
const auto status = psa_verify_message(key->key, algorithm, ticket.data(), signed_end,
|
||||
ticket.data() + signed_end, signature_bytes);
|
||||
if (status == PSA_ERROR_INVALID_SIGNATURE)
|
||||
return std::unexpected(HandoffTicketError::bad_signature);
|
||||
if (status != PSA_SUCCESS)
|
||||
return std::unexpected(HandoffTicketError::crypto);
|
||||
if (!std::equal(marker.begin(), marker.end(), ticket.begin()))
|
||||
return std::unexpected(HandoffTicketError::wrong_marker);
|
||||
if (get(ticket, 16, 2) != ticket_version || ticket[19] != 0)
|
||||
return std::unexpected(HandoffTicketError::unsupported_version);
|
||||
if (ticket[18] != environment_byte(environment_))
|
||||
return std::unexpected(HandoffTicketError::wrong_environment);
|
||||
TicketClaims c;
|
||||
std::copy_n(ticket.begin() + 24, c.transfer.size(), c.transfer.begin());
|
||||
std::copy_n(ticket.begin() + 40, c.account.bytes.size(), c.account.bytes.begin());
|
||||
c.world = {get(ticket, 56, 8), get(ticket, 64, 8)};
|
||||
c.source = get(ticket, 72, 8);
|
||||
c.destination = get(ticket, 80, 8);
|
||||
c.expires_at = static_cast<std::uint32_t>(get(ticket, 88, 4));
|
||||
c.session_expires_at = static_cast<std::uint32_t>(get(ticket, 92, 4));
|
||||
if (!structurally_valid(c))
|
||||
return std::unexpected(HandoffTicketError::invalid_claims);
|
||||
if (c.source != key->region)
|
||||
return std::unexpected(HandoffTicketError::wrong_source);
|
||||
if (c.world.world != world_.world || c.world.frame != world_.frame || c.destination != region_)
|
||||
return std::unexpected(HandoffTicketError::wrong_scope);
|
||||
if (now_seconds >= c.expires_at || now_seconds >= c.session_expires_at ||
|
||||
c.expires_at - now_seconds > max_ticket_lifetime)
|
||||
return std::unexpected(HandoffTicketError::expired);
|
||||
return VerifiedHandoffTicket{c};
|
||||
}
|
||||
} // namespace evo::network
|
||||
@@ -0,0 +1,70 @@
|
||||
#include "evo/server/postgres_handoff_ledger.hpp"
|
||||
#include "evo/persistence/account_control_store.hpp"
|
||||
#include <string_view>
|
||||
|
||||
namespace evo::server {
|
||||
namespace {
|
||||
using network::HandoffLedgerError;
|
||||
using persistence::ProbeStoreError;
|
||||
|
||||
// The region lease itself fenced (EVL02/EVL06) versus this transfer not being ours (EVL13).
|
||||
bool lease_fenced(const persistence::PgFailure& f) {
|
||||
const std::string_view state(f.sqlstate.data(), 5);
|
||||
return state == "EVL02" || state == "EVL06";
|
||||
}
|
||||
HandoffLedgerError ledger_error(const persistence::ProbeStoreFailure& f) {
|
||||
switch (f.kind) {
|
||||
case ProbeStoreError::fenced:
|
||||
return lease_fenced(f.cause) ? HandoffLedgerError::fenced : HandoffLedgerError::refused;
|
||||
case ProbeStoreError::unknown_transfer:
|
||||
case ProbeStoreError::not_staged:
|
||||
case ProbeStoreError::invalid:
|
||||
case ProbeStoreError::exhausted:
|
||||
return HandoffLedgerError::refused;
|
||||
default:
|
||||
return HandoffLedgerError::unavailable;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<network::ControlLease, HandoffLedgerError>
|
||||
PostgresHandoffLedger::admit(const network::TicketClaims& ticket, std::uint64_t session) {
|
||||
const auto lease = lease_();
|
||||
if (!lease)
|
||||
return std::unexpected(HandoffLedgerError::fenced);
|
||||
auto* db = db_->get();
|
||||
if (!db)
|
||||
return std::unexpected(HandoffLedgerError::unavailable);
|
||||
const auto fail = [&](const persistence::ProbeStoreFailure& f) {
|
||||
if (f.kind == ProbeStoreError::store || f.kind == ProbeStoreError::unknown)
|
||||
db_->broken(); // Unknown connection state: reconnect next time.
|
||||
return std::unexpected(ledger_error(f));
|
||||
};
|
||||
const auto staged =
|
||||
persistence::stage_probe_transfer(*db, key_, *lease, ticket.transfer, timeout_);
|
||||
if (!staged)
|
||||
return fail(staged.error());
|
||||
if (staged->account != ticket.account.bytes || staged->source != ticket.source ||
|
||||
staged->destination != key_.region || staged->state == persistence::TransferState::aborted)
|
||||
return std::unexpected(HandoffLedgerError::refused);
|
||||
const auto committed =
|
||||
persistence::commit_probe_transfer(*db, key_, *lease, ticket.transfer, timeout_);
|
||||
if (!committed)
|
||||
return fail(committed.error());
|
||||
if (committed->state != persistence::TransferState::committed)
|
||||
return std::unexpected(HandoffLedgerError::refused); // Aborted meanwhile.
|
||||
const auto control = persistence::handoff_account_control(
|
||||
*db, key_, *lease, ticket.account.bytes, ticket.transfer, session, duration_, timeout_);
|
||||
if (!control) {
|
||||
const auto kind = control.error().kind;
|
||||
if (kind == persistence::ControlStoreError::store ||
|
||||
kind == persistence::ControlStoreError::unknown) {
|
||||
db_->broken();
|
||||
return std::unexpected(HandoffLedgerError::unavailable);
|
||||
}
|
||||
return std::unexpected(lease_fenced(control.error().cause) ? HandoffLedgerError::fenced
|
||||
: HandoffLedgerError::refused);
|
||||
}
|
||||
return network::ControlLease{control->generation, control->wait, control->granted};
|
||||
}
|
||||
} // namespace evo::server
|
||||
@@ -38,6 +38,27 @@ SessionCloseReason reason_for(network::ControlLedgerError e) {
|
||||
return SessionCloseReason::capacity; // Not decided: the client may retry later.
|
||||
}
|
||||
}
|
||||
SessionCloseReason reason_for(network::HandoffTicketError e) {
|
||||
switch (e) {
|
||||
case network::HandoffTicketError::wrong_environment:
|
||||
case network::HandoffTicketError::wrong_scope:
|
||||
return SessionCloseReason::wrong_scope;
|
||||
case network::HandoffTicketError::expired:
|
||||
return SessionCloseReason::expired;
|
||||
default:
|
||||
return SessionCloseReason::unauthenticated;
|
||||
}
|
||||
}
|
||||
SessionCloseReason reason_for(network::HandoffLedgerError e) {
|
||||
switch (e) {
|
||||
case network::HandoffLedgerError::refused:
|
||||
return SessionCloseReason::unauthenticated;
|
||||
case network::HandoffLedgerError::fenced:
|
||||
return SessionCloseReason::shutdown;
|
||||
default:
|
||||
return SessionCloseReason::capacity;
|
||||
}
|
||||
}
|
||||
SessionCloseReason reason_for(SessionGrantError e) {
|
||||
return e == SessionGrantError::wrong_environment ? SessionCloseReason::wrong_scope
|
||||
: SessionCloseReason::unauthenticated;
|
||||
@@ -62,15 +83,16 @@ struct RegionSessions::Impl {
|
||||
};
|
||||
|
||||
Impl(RegionSessionsConfig c, std::unique_ptr<network::quic::Endpoint> e,
|
||||
network::GrantVerifier v, network::SessionControl s, network::IntentAdmission i,
|
||||
network::ReplicationServer r)
|
||||
network::GrantVerifier v, std::optional<network::HandoffVerifier> h,
|
||||
network::SessionControl s, network::IntentAdmission i, network::ReplicationServer r)
|
||||
: config{std::move(c)}, endpoint{std::move(e)}, verifier{std::move(v)},
|
||||
control{std::move(s)}, intents{std::move(i)}, replication{std::move(r)},
|
||||
markers{config.control.max_sessions} {}
|
||||
tickets{std::move(h)}, control{std::move(s)}, intents{std::move(i)},
|
||||
replication{std::move(r)}, markers{config.control.max_sessions} {}
|
||||
|
||||
RegionSessionsConfig config;
|
||||
std::unique_ptr<network::quic::Endpoint> endpoint;
|
||||
network::GrantVerifier verifier;
|
||||
std::optional<network::HandoffVerifier> tickets; // Handoff keys configured.
|
||||
network::SessionControl control;
|
||||
network::IntentAdmission intents;
|
||||
network::ReplicationServer replication;
|
||||
@@ -137,6 +159,8 @@ struct RegionSessions::Impl {
|
||||
return close(c, SessionCloseReason::protocol, now);
|
||||
if (!control.attempt(now_seconds)) // Region-wide, before signature work.
|
||||
return close(c, SessionCloseReason::rate_limited, now);
|
||||
if (network::looks_like_handoff_ticket(m.grant))
|
||||
return ticket_hello(c, m, now_seconds, now);
|
||||
const auto grant = verifier.verify(m.grant);
|
||||
if (!grant)
|
||||
return close(c, reason_for(grant.error()), now);
|
||||
@@ -154,12 +178,47 @@ struct RegionSessions::Impl {
|
||||
return close(c, reason_for(claimed.error()), now);
|
||||
lease = *claimed;
|
||||
}
|
||||
const auto opened = control.open(*grant, now_seconds);
|
||||
admitted(c, control.open(*grant, now_seconds), lease, promised, claims.account, now_seconds,
|
||||
now);
|
||||
}
|
||||
|
||||
// M10.8b: a Hello carrying a handoff ticket (ADR-0096). Verified, checked locally, then
|
||||
// made durable (stage, commit, control) before the local admission.
|
||||
void ticket_hello(Connection& c, const network::SessionHello& m, std::uint64_t now_seconds,
|
||||
Clock::time_point now) {
|
||||
if (!tickets)
|
||||
return close(c, SessionCloseReason::unauthenticated, now);
|
||||
const auto ticket = tickets->verify(m.grant, now_seconds);
|
||||
if (!ticket)
|
||||
return close(c, reason_for(ticket.error()), now);
|
||||
const auto& claims = ticket->claims();
|
||||
c.account = network::log_label(claims.account);
|
||||
if (const auto checked = control.check_handoff(*ticket, now_seconds); !checked)
|
||||
return close(c, reason_for(checked.error()), now);
|
||||
std::optional<network::ControlLease> lease;
|
||||
const auto promised = control.next_session();
|
||||
if (config.handoff) {
|
||||
auto moved = config.handoff->admit(claims, promised);
|
||||
if (!moved)
|
||||
return close(c, reason_for(moved.error()), now);
|
||||
lease = *moved;
|
||||
} else if (config.ledger) {
|
||||
return close(c, SessionCloseReason::capacity, now); // Misconfigured: fail closed.
|
||||
}
|
||||
admitted(c, control.open_handoff(*ticket, now_seconds), lease, promised, claims.account,
|
||||
now_seconds, now);
|
||||
}
|
||||
|
||||
// Common to grants and tickets: the local admission's result after any durable claim.
|
||||
void admitted(Connection& c, const std::expected<network::OpenedSession, ControlError>& opened,
|
||||
const std::optional<network::ControlLease>& lease, std::uint64_t promised,
|
||||
const network::AccountId& account, std::uint64_t now_seconds,
|
||||
Clock::time_point now) {
|
||||
if (!opened || opened->session != promised) {
|
||||
if (opened)
|
||||
(void)control.close(opened->session).has_value();
|
||||
if (lease)
|
||||
config.ledger->release(claims.account, lease->generation);
|
||||
config.ledger->release(account, lease->generation);
|
||||
return close(c, opened ? SessionCloseReason::capacity : reason_for(opened.error()),
|
||||
now);
|
||||
}
|
||||
@@ -167,7 +226,7 @@ struct RegionSessions::Impl {
|
||||
if (auto* old = by_session(opened->superseded))
|
||||
close(*old, SessionCloseReason::superseded, now);
|
||||
c.session = opened->session;
|
||||
c.account_id = claims.account;
|
||||
c.account_id = account;
|
||||
c.welcome.session = opened->session;
|
||||
c.welcome.epoch = opened->epoch;
|
||||
c.welcome.world = config.control.world.world;
|
||||
@@ -334,7 +393,7 @@ RegionSessions::~RegionSessions() = default;
|
||||
std::expected<std::unique_ptr<RegionSessions>, RegionSessionsError>
|
||||
RegionSessions::create(RegionSessionsConfig config) {
|
||||
// Connections beyond sessions are headroom for logins and closes still flushing.
|
||||
if (config.issuer_keys.empty() ||
|
||||
if (config.issuer_keys.empty() || (config.handoff != nullptr && config.ledger == nullptr) ||
|
||||
config.transport.limits.max_connections < config.control.max_sessions ||
|
||||
config.hello_deadline.count() <= 0 || config.close_flush.count() <= 0 ||
|
||||
config.renew_interval.count() <= 0 || config.safety_margin.count() <= 0)
|
||||
@@ -348,6 +407,17 @@ RegionSessions::create(RegionSessionsConfig config) {
|
||||
for (const auto& k : config.issuer_keys)
|
||||
if (!verifier->add_key(k.key_id, k.public_key))
|
||||
return std::unexpected(RegionSessionsError::keys);
|
||||
std::optional<network::HandoffVerifier> tickets;
|
||||
if (!config.handoff_keys.empty()) {
|
||||
auto made = network::HandoffVerifier::create(config.control.environment,
|
||||
config.control.world, config.control.region);
|
||||
if (!made)
|
||||
return std::unexpected(RegionSessionsError::keys);
|
||||
for (const auto& k : config.handoff_keys)
|
||||
if (!made->add_key(k.source_region, k.key_id, k.public_key))
|
||||
return std::unexpected(RegionSessionsError::keys);
|
||||
tickets.emplace(std::move(*made));
|
||||
}
|
||||
config.intents.max_sessions = config.control.max_sessions;
|
||||
auto intents = network::IntentAdmission::create(config.intents);
|
||||
if (!intents)
|
||||
@@ -360,9 +430,9 @@ RegionSessions::create(RegionSessionsConfig config) {
|
||||
auto endpoint = network::quic::Endpoint::serve(config.transport);
|
||||
if (!endpoint)
|
||||
return std::unexpected(RegionSessionsError::transport);
|
||||
return std::unique_ptr<RegionSessions>{new RegionSessions{
|
||||
std::make_unique<Impl>(std::move(config), std::move(*endpoint), std::move(*verifier),
|
||||
std::move(*control), std::move(*intents), std::move(*replication))}};
|
||||
return std::unique_ptr<RegionSessions>{new RegionSessions{std::make_unique<Impl>(
|
||||
std::move(config), std::move(*endpoint), std::move(*verifier), std::move(tickets),
|
||||
std::move(*control), std::move(*intents), std::move(*replication))}};
|
||||
}
|
||||
|
||||
void RegionSessions::poll(const HostFacts& facts, std::uint64_t now_seconds,
|
||||
|
||||
Reference in New Issue
Block a user