diff --git a/cmd/corkd/swagger.yaml b/cmd/corkd/swagger.yaml index ad0503e..f58d2a3 100644 --- a/cmd/corkd/swagger.yaml +++ b/cmd/corkd/swagger.yaml @@ -515,6 +515,15 @@ definitions: format: int64 network: type: string + worker: + type: string + description: "The private address of the worker the instance is on, for orchestration only. Absent on the local daemon." + worker_public: + type: string + description: "The address a player connects to: the worker's public address, else its private one. Resolved at read time, never stored. Absent on the local daemon." + hostname: + type: string + description: "worker_public again, under the key the platform resolves {{server}} and {{http_base}} from. Present wherever worker_public is." BuildChallengeRequest: type: object properties: diff --git a/cork/api.go b/cork/api.go index 902fc1e..b5f717d 100644 --- a/cork/api.go +++ b/cork/api.go @@ -912,6 +912,7 @@ func (m *Manager) GetSchemaState(name string) ([]*ChallengeMetadata, error) { if err != nil { return nil, err } + m.resolvePublicAddr(build.Instances...) if challenge != nil && challenge.Id != build.Challenge { challenges = append(challenges, challenge) @@ -949,15 +950,22 @@ func (m *Manager) GetInstanceMetadata(instance InstanceId) (*InstanceMetadata, e if err != nil { return nil, err } - // Resolve the player-facing address at read time so a worker's public - // address can be corrected by re-adding it, without touching instances. - if iMeta.Worker != "" { - iMeta.WorkerPublic = m.workerPublicAddr(iMeta.Worker) - iMeta.Hostname = iMeta.WorkerPublic - } + m.resolvePublicAddr(iMeta) return iMeta, nil } +// resolvePublicAddr fills in the player-facing address of instances on a +// worker. Resolved at read time, so re-adding a worker corrects the next read; +// every read that hands instances to a consumer must call this. +func (m *Manager) resolvePublicAddr(instances ...*InstanceMetadata) { + for _, iMeta := range instances { + if iMeta.Worker != "" { + iMeta.WorkerPublic = m.workerPublicAddr(iMeta.Worker) + iMeta.Hostname = iMeta.WorkerPublic + } + } +} + func (m *Manager) DumpState(challenges []ChallengeId) ([]*ChallengeMetadata, error) { allChallenges, err := m.dumpState() if len(challenges) == 0 { diff --git a/cork/database.go b/cork/database.go index f813ca5..d708d2f 100644 --- a/cork/database.go +++ b/cork/database.go @@ -855,6 +855,7 @@ func (m *Manager) dumpChallenge(challenge ChallengeId) (*ChallengeMetadata, erro m.log.errorf("failed to select instances for '%s/%d': %s", challenge, bMeta.Id, err) return nil, err } + m.resolvePublicAddr(bMeta.Instances...) meta.Builds[j] = bMeta } return meta, nil diff --git a/cork/select_worker_test.go b/cork/select_worker_test.go index a2c164d..0b2c87a 100644 --- a/cork/select_worker_test.go +++ b/cork/select_worker_test.go @@ -230,42 +230,109 @@ func TestWorkerPublicAddr(t *testing.T) { } } -// The picoCTF platform reads the player-facing address under "hostname": with -// dynamic_public_hostname set it stores the whole launch response and looks that -// key up to resolve {{server}} in a challenge description. It is the same value -// as worker_public, so the two are asserted to agree here rather than being left -// to drift apart in a later edit. -func TestHostnameMirrorsWorkerPublic(t *testing.T) { - withPublic := okWorker("10.0.0.1") - withPublic.public = "ctf.example.org" - m := managerWithWorkers(t, withPublic, okWorker("10.0.0.2")) +// Every read that hands out instances reports the player-facing address under +// "hostname" and "worker_public": the platform resolves {{server}} from +// whichever read it stored, the launch response or the schema state. +func TestInstanceReadsReportThePublicAddress(t *testing.T) { + m := setupTestManager(t) + t.Cleanup(func() { m.db.Close() }) + named, bare := okWorker("10.0.0.1"), okWorker("10.0.0.2") + named.public = "ctf.example.org" + m.workers = map[string]*workerConn{named.ip: named, bare.ip: bare} + m.workerOrder = []string{named.ip, bare.ip} - for _, ip := range []string{"10.0.0.1", "10.0.0.2"} { - meta := &InstanceMetadata{Id: 1, Worker: ip} - meta.WorkerPublic = m.workerPublicAddr(meta.Worker) - meta.Hostname = meta.WorkerPublic + challenge := testChallenge("test/public-address", 0) + if errs := m.addChallenges([]*ChallengeMetadata{challenge}); len(errs) > 0 { + t.Fatalf("addChallenges: %v", errs) + } + build := insertTestBuild(t, m, "event", string(challenge.Id), "flag{%s}", 1, 0x1111) - if meta.Hostname != meta.WorkerPublic { - t.Fatalf("worker %s: hostname %q and worker_public %q disagree", ip, meta.Hostname, meta.WorkerPublic) - } - body, err := json.Marshal(meta) + // The address each instance's player is handed: the worker's public name, + // else its ip, and none at all on the local daemon. + want := map[InstanceId]string{} + for worker, addr := range map[string]string{named.ip: named.public, bare.ip: bare.ip, "": ""} { + res, err := m.db.Exec("INSERT INTO instances(build, lastsolved, is_finalized, worker) VALUES (?, 0, 1, ?);", build, worker) if err != nil { - t.Fatalf("marshalling the instance: %s", err) + t.Fatal(err) } - var out map[string]any - if err := json.Unmarshal(body, &out); err != nil { - t.Fatalf("unmarshalling the instance: %s", err) - } - // Both keys, under the exact names each consumer reads. - if out["hostname"] != meta.WorkerPublic { - t.Errorf("worker %s: json 'hostname' is %v, want %q", ip, out["hostname"], meta.WorkerPublic) - } - if out["worker_public"] != meta.WorkerPublic { - t.Errorf("worker %s: json 'worker_public' is %v, want %q", ip, out["worker_public"], meta.WorkerPublic) + id, err := res.LastInsertId() + if err != nil { + t.Fatal(err) } - if out["worker"] != ip { - t.Errorf("worker %s: json 'worker' is %v, want the private address", ip, out["worker"]) + want[InstanceId(id)] = addr + } + + inState := func(state []*ChallengeMetadata) []*InstanceMetadata { + var instances []*InstanceMetadata + for _, c := range state { + for _, b := range c.Builds { + instances = append(instances, b.Instances...) + } } + return instances + } + reads := []struct { + name string + read func() ([]*InstanceMetadata, error) + }{ + // GET /instances/, and the launch response + {"instance", func() ([]*InstanceMetadata, error) { + var instances []*InstanceMetadata + for id := range want { + meta, err := m.GetInstanceMetadata(id) + if err != nil { + return nil, err + } + instances = append(instances, meta) + } + return instances, nil + }}, + // GET /schemas/ + {"schema-state", func() ([]*InstanceMetadata, error) { + state, err := m.GetSchemaState("event") + return inState(state), err + }}, + // GET /state, and through dumpChallenge the hand-over response + {"state-dump", func() ([]*InstanceMetadata, error) { + state, err := m.DumpState(nil) + return inState(state), err + }}, + } + for _, r := range reads { + t.Run(r.name, func(t *testing.T) { + instances, err := r.read() + if err != nil { + t.Fatal(err) + } + if len(instances) != len(want) { + t.Fatalf("read %d instances, want %d", len(instances), len(want)) + } + for _, meta := range instances { + body, err := json.Marshal(meta) + if err != nil { + t.Fatal(err) + } + var out map[string]any + if err := json.Unmarshal(body, &out); err != nil { + t.Fatal(err) + } + addr := want[meta.Id] + // "worker" stays the private address that placement and teardown dial. + if worker, _ := out["worker"].(string); worker != meta.Worker { + t.Errorf("instance %d: worker is %q, want the private address %q", meta.Id, worker, meta.Worker) + } + // Under the exact names each consumer reads. + for _, key := range []string{"hostname", "worker_public"} { + got, present := out[key] + if addr == "" && present { + t.Errorf("instance %d on the local daemon reported %s %v", meta.Id, key, got) + } + if addr != "" && got != addr { + t.Errorf("instance %d on worker %q: %s is %v, want %q", meta.Id, meta.Worker, key, got, addr) + } + } + } + }) } } diff --git a/cork/types.go b/cork/types.go index 5e45772..ab8c4f9 100644 --- a/cork/types.go +++ b/cork/types.go @@ -368,14 +368,12 @@ type InstanceMetadata struct { // WorkerPublic is the worker's player-facing address (IP or hostname) — // what consumers should hand to users, since Worker is the private // orchestration address. Resolved from the worker registry at read time - // (GetInstanceMetadata), never stored; falls back to Worker when the + // (resolvePublicAddr), never stored; falls back to Worker when the // worker has no public address configured. WorkerPublic string `json:"worker_public,omitempty" db:"-"` // Hostname is WorkerPublic again, under the key the picoCTF platform - // reads it by: with a challenge server's dynamic_public_hostname set, it - // stores the whole launch response and looks up "hostname" to resolve the - // {{server}} and {{http_base}} tokens in a challenge description. Same - // value, same resolution, so the two cannot disagree. + // resolves {{server}} and {{http_base}} from, in the launch response and + // the schema state alike. Same value, same resolution. Hostname string `json:"hostname,omitempty" db:"-"` } diff --git a/e2e/scenario.sh b/e2e/scenario.sh index e38e9f6..b47f772 100755 --- a/e2e/scenario.sh +++ b/e2e/scenario.sh @@ -1224,6 +1224,10 @@ pub=$(jq -r .worker_public <<<"$PERSIST_META") port=$(jq -r .ports.socat <<<"$PERSIST_META") [[ -n "${PUBLIC[$PERSIST_WORKER]:-}" ]] || fail "instance $PERSIST_INST is on unknown worker '$PERSIST_WORKER'" [[ "$pub" == "${PUBLIC[$PERSIST_WORKER]}" ]] || fail "worker_public is '$pub', expected '${PUBLIC[$PERSIST_WORKER]}'" +# The platform syncs a persistent instance from the schema state, not this +# read, and resolves {{server}} from its "hostname". +spub=$(challenge "$CH_PERSISTENT" | jq -r '.builds[0].instances[0].hostname // empty') +[[ "$spub" == "$pub" ]] || fail "the schema state's hostname for instance $PERSIST_INST is '$spub', expected '$pub'" (( port >= PORT_LOW && port <= PORT_HIGH )) || fail "port $port is outside CORK_PORTS $PORT_LOW-$PORT_HIGH" assert_on_worker "$PERSIST_WORKER" "$PERSIST_META" retry 30 "the socat service at $pub:$port" tcp_says "$pub" "$port" '' 'openssl aes-256-cbc'