mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 08:23:45 +00:00
feat(multiplayer): deliver signed workload tokens via Agones allocation annotation
Closes the remaining gap the previous two commits left open: WorkloadVerify itself worked, but nothing minted a real token at allocation time or handed it to a running pod, so it had no real caller yet. agones.Client gains WorkloadSecret/WorkloadTokenTTL. When set, Allocate mints a signed workload token for the allocation (allocation_id is known at request-construction time, before Agones has picked a server -- see the previous commit for why that's the only identifier the token can bind) and requests it as a third cosmic-clash.io/workload-token annotation, alongside the existing match-id/allocation-id ones. Left unset (the default), Allocate requests no such annotation, so a deployment not yet using this path is unaffected. cmd/allocator wires it from a new --workload-secret / COSMIC_CLASH_WORKLOAD_SECRET flag (must match cmd/control-plane's own), with a startup warning if left unset. supervisor.Supervisor.workloadToken() resolves the bearer credential for control-plane registration: an explicitly configured --workload-token-path always wins (kept for a future Kubernetes-projected-JWT WorkloadVerify path, not yet wired server-side), otherwise it falls back to the cosmic-clash.io/workload-token annotation on the allocated GameServer -- the same annotation-fallback pattern matchID already used for cosmic-clash.io/match-id. WorkloadTokenPath is accordingly no longer required at construction time when ControlPlaneURL is set. Verified: new agones test proves the annotation is requested (and parses/ verifies against the same secret, naming the right allocation) when WorkloadSecret is configured, and that it's absent when it isn't; new supervisor tests prove the annotation-sourced token is what's actually sent as the Authorization bearer, and that Start fails closed with neither a configured path nor an annotation present. Full `go build ./... && go vet ./... && gofmt -l . && go test ./... -race` and `go test -tags integration ./... -race` both clean.
This commit is contained in:
@@ -57,13 +57,20 @@ type Config struct {
|
||||
// matchmaking control plane (multiplayer-next.md task 8.28) once Agones
|
||||
// Ready succeeds. Leaving it empty preserves every existing behavior
|
||||
// exactly -- direct/Compose mode and allocated-without-control-plane mode
|
||||
// are both unaffected. WorkloadTokenPath is read fresh on every call
|
||||
// rather than cached, matching how a Kubernetes projected service account
|
||||
// token is rotated in place by kubelet before it expires; ServerID and
|
||||
// ImageDigest are expected to be populated from the pod spec (Downward
|
||||
// API / mounted build metadata). MatchID may be left empty here and is
|
||||
// then read from the allocated GameServer's own annotations (see
|
||||
// GameServer.ObjectMeta above) -- an explicit value here always wins.
|
||||
// are both unaffected. WorkloadTokenPath, if set, is read fresh on every
|
||||
// call rather than cached, matching how a Kubernetes projected service
|
||||
// account token is rotated in place by kubelet before it expires -- this
|
||||
// is for a future Kubernetes-JWT-based WorkloadVerify (server/workload/
|
||||
// jwt.go), not yet wired server-side. Today the control plane instead
|
||||
// verifies a self-issued signed token (server/workload/signed_token.go),
|
||||
// which reaches this process via the cosmic-clash.io/workload-token
|
||||
// annotation Agones applies to the allocated GameServer (see
|
||||
// server/agones.Client.Allocate) -- see workloadToken() for the
|
||||
// precedence between the two sources. ServerID and ImageDigest are
|
||||
// expected to be populated from the pod spec (Downward API / mounted
|
||||
// build metadata). MatchID may be left empty here and is then read from
|
||||
// the allocated GameServer's own annotations (see GameServer.ObjectMeta
|
||||
// above) -- an explicit value here always wins.
|
||||
ControlPlaneURL string
|
||||
WorkloadTokenPath string
|
||||
ServerID string
|
||||
@@ -126,13 +133,13 @@ func New(config Config) (*Supervisor, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if config.ControlPlaneURL != "" && (config.WorkloadTokenPath == "" || config.ServerID == "" || config.ProtocolVersion < 1 || config.ImageDigest == "") {
|
||||
return nil, fmt.Errorf("control-plane registration requires a workload token path, server ID, protocol version and image digest")
|
||||
if config.ControlPlaneURL != "" && (config.ServerID == "" || config.ProtocolVersion < 1 || config.ImageDigest == "") {
|
||||
return nil, fmt.Errorf("control-plane registration requires a server ID, protocol version and image digest")
|
||||
}
|
||||
// MatchID is deliberately not required here: it can also be resolved at
|
||||
// Start time from the allocated GameServer's own annotations (see
|
||||
// registerControlPlane). It is validated to actually be resolvable
|
||||
// there, not silently skipped.
|
||||
// Neither MatchID nor WorkloadTokenPath is required here: both can
|
||||
// instead be resolved at Start time from the allocated GameServer's own
|
||||
// annotations (see registerControlPlane/workloadToken/matchID). They are
|
||||
// validated to actually be resolvable there, not silently skipped.
|
||||
return &Supervisor{config: config, client: config.HTTPClient}, nil
|
||||
}
|
||||
|
||||
@@ -243,6 +250,34 @@ func (s *Supervisor) matchID() string {
|
||||
return s.lastGameServer.ObjectMeta.Annotations["cosmic-clash.io/match-id"]
|
||||
}
|
||||
|
||||
// workloadToken resolves the bearer credential for control-plane
|
||||
// registration. WorkloadTokenPath, when configured, always wins -- it is
|
||||
// for a future Kubernetes-projected-JWT WorkloadVerify path (see the Config
|
||||
// field's doc comment) and an operator who explicitly set it presumably
|
||||
// wants it used. Otherwise it falls back to the cosmic-clash.io/workload-
|
||||
// token annotation Agones applied to this GameServer at allocation time
|
||||
// (server/agones.Client.Allocate, verified by
|
||||
// api.WorkloadVerifierFromSignedToken today) -- the same annotation-fallback
|
||||
// pattern matchID already uses for cosmic-clash.io/match-id.
|
||||
func (s *Supervisor) workloadToken() (string, error) {
|
||||
if s.config.WorkloadTokenPath != "" {
|
||||
tokenBytes, err := os.ReadFile(s.config.WorkloadTokenPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read workload token: %w", err)
|
||||
}
|
||||
token := strings.TrimSpace(string(tokenBytes))
|
||||
if token == "" {
|
||||
return "", fmt.Errorf("workload token file %q is empty", s.config.WorkloadTokenPath)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
token := s.lastGameServer.ObjectMeta.Annotations["cosmic-clash.io/workload-token"]
|
||||
if token == "" {
|
||||
return "", fmt.Errorf("control-plane registration has no workload token: no --workload-token-path configured, and no cosmic-clash.io/workload-token annotation was present on the allocated GameServer")
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (s *Supervisor) registerControlPlane(ctx context.Context, assignmentReady bool) error {
|
||||
if s.config.ControlPlaneURL == "" {
|
||||
return nil
|
||||
@@ -251,13 +286,9 @@ func (s *Supervisor) registerControlPlane(ctx context.Context, assignmentReady b
|
||||
if matchID == "" {
|
||||
return fmt.Errorf("control-plane registration has no match ID: not configured, and no cosmic-clash.io/match-id annotation was present on the allocated GameServer")
|
||||
}
|
||||
tokenBytes, err := os.ReadFile(s.config.WorkloadTokenPath)
|
||||
token, err := s.workloadToken()
|
||||
if err != nil {
|
||||
return fmt.Errorf("read workload token: %w", err)
|
||||
}
|
||||
token := strings.TrimSpace(string(tokenBytes))
|
||||
if token == "" {
|
||||
return fmt.Errorf("workload token file %q is empty", s.config.WorkloadTokenPath)
|
||||
return err
|
||||
}
|
||||
body, err := json.Marshal(struct {
|
||||
MatchID string `json:"match_id"`
|
||||
|
||||
Reference in New Issue
Block a user