Files
CosmicClash/server/domain/assignment.go
T
Josh Creek 5765532409 fix(allocator): publish signed assignment rosters before servers start
The root blocker (issue #14). The worker bound the provider allocation
and stopped. Service.PublishRoster and store.SaveVerifiedAssignmentRoster
both existed, fully tested, with zero non-test callers, and the
production allocator configured neither a roster store nor a signing
key. Nothing ever wrote the assignments table.

The allocated supervisor fetches a non-empty roster before it launches
the game child, so every real allocation failed at that fetch: no match
could reach ASSIGNMENT_READY or accept a player. Existing tests seeded
assignments directly, which is exactly why the missing hand-off went
unnoticed.

The worker now builds one join authorisation per durable participant,
signs each with the active key, and publishes them. Participants are
read through the same query SaveVerifiedAssignmentRoster re-validates
against, so the allocator cannot construct a roster the persistence
boundary would reject. The manifest commits to a digest over the whole
roster, so a server cannot be handed a truncated roster whose surviving
entries are each individually valid.

Persist the provider endpoint on the allocation: it arrived on the
provider response and was never stored, so a worker crashing between
allocating and publishing had no endpoint to recover and would have
stranded the match permanently. Republishing is idempotent, so that
crash now simply retries.

cmd/allocator refuses to start without key material rather than running
an allocator that binds allocations and silently strands every match.
The k8s allocator Deployment mounts the same key set the Fleet does, and
both now take the JSON key map so a rotation can publish several.

New integration test drives the real worker through to the supervisor's
own roster read path without seeding the assignments table. Verified it
fails with "assignments = 0, want 2" when the publish step is removed.
2026-09-05 10:42:31 +01:00

62 lines
2.3 KiB
Go

package domain
import (
"crypto/sha256"
"fmt"
)
type AllocationManifest struct {
AllocationID string
MatchID string
ServerID string
Region string
Build string
Protocol int
Transport string
RosterDigest string
}
type Assignment struct {
Allocation Allocation
Manifest AllocationManifest
Endpoint string
}
var ErrManifestRejected = fmt.Errorf("allocation manifest rejected")
// VerifyAssignment is the assignment-ready gate. A Ready/Allocated process
// has no client-facing endpoint until its signed manifest, allocator binding,
// and hosted endpoint all pass this check.
func VerifyAssignment(allocation Allocation, manifest AllocationManifest, endpoint string, signature []byte, verify func([]byte, []byte) bool) (Assignment, error) {
if allocation.State != ServerAllocated || allocation.AllocationID == "" || allocation.MatchID == "" || allocation.ServerID == "" || endpoint == "" || len(signature) == 0 || verify == nil {
return Assignment{}, ErrManifestRejected
}
if manifest.AllocationID != allocation.AllocationID || manifest.MatchID != allocation.MatchID || manifest.ServerID != allocation.ServerID || manifest.Region != allocation.Region || manifest.Build != allocation.Build || manifest.Protocol != allocation.Protocol || manifest.Transport != allocation.Transport || manifest.RosterDigest == "" {
return Assignment{}, ErrManifestRejected
}
if !verify(manifestBytes(manifest), signature) {
return Assignment{}, ErrManifestRejected
}
return Assignment{Allocation: allocation, Manifest: manifest, Endpoint: endpoint}, nil
}
func manifestBytes(manifest AllocationManifest) []byte {
canonical := fmt.Sprintf("%s\x00%s\x00%s\x00%s\x00%s\x00%d\x00%s\x00%s", manifest.AllocationID, manifest.MatchID, manifest.ServerID, manifest.Region, manifest.Build, manifest.Protocol, manifest.Transport, manifest.RosterDigest)
return []byte(canonical)
}
func ManifestDigest(manifest AllocationManifest) [32]byte {
return sha256.Sum256(manifestBytes(manifest))
}
// AssignmentParticipant is the durable roster row the allocator turns into one
// signed join authorisation. It lives here rather than in the store so the
// allocator can consume it through an interface without depending on the
// persistence package.
type AssignmentParticipant struct {
PlayerID string
SteamID string
Slot int
Team int
}